|
5 | 5 | namespace phpweb\Test\Unit\Downloads; |
6 | 6 |
|
7 | 7 | use phpweb\Downloads\OptionResolver; |
| 8 | +use phpweb\Downloads\Resolution; |
8 | 9 | use PHPUnit\Framework; |
9 | 10 |
|
10 | 11 | #[Framework\Attributes\CoversClass(OptionResolver::class)] |
| 12 | +#[Framework\Attributes\CoversClass(Resolution::class)] |
11 | 13 | class OptionResolverTest extends Framework\TestCase |
12 | 14 | { |
13 | 15 | /** |
@@ -41,97 +43,131 @@ class OptionResolverTest extends Framework\TestCase |
41 | 43 | * Reproduces the production crash: a bot requesting downloads.php?os=<garbage> |
42 | 44 | * previously made $os[$options['os']] null and threw |
43 | 45 | * "array_key_exists(): Argument #2 ($array) must be of type array, null given". |
| 46 | + * It now redirects to the auto-detected results instead. |
44 | 47 | */ |
45 | | - public function testInvalidOsParameterFallsBackToDefault(): void |
| 48 | + public function testInvalidOsParameterRedirectsToDefault(): void |
46 | 49 | { |
47 | | - $options = $this->resolver()->resolve(['os' => 'not-a-real-os'], '', ''); |
| 50 | + $resolution = $this->resolver()->resolve(['os' => 'not-a-real-os'], '', ''); |
48 | 51 |
|
49 | | - self::assertSame('linux', $options['os']); |
50 | | - self::assertSame('linux-debian', $options['osvariant']); |
| 52 | + self::assertSame('linux', $resolution->options['os']); |
| 53 | + self::assertSame('linux-debian', $resolution->options['osvariant']); |
| 54 | + self::assertSame('os=linux&osvariant=linux-debian&version=default', $resolution->redirectQuery); |
51 | 55 | } |
52 | 56 |
|
53 | | - public function testArrayOsParameterFallsBackToDefault(): void |
| 57 | + public function testArrayOsParameterRedirectsToDefault(): void |
54 | 58 | { |
55 | | - $options = $this->resolver()->resolve(['os' => ['linux']], '', ''); |
| 59 | + $resolution = $this->resolver()->resolve(['os' => ['linux']], '', ''); |
56 | 60 |
|
57 | | - self::assertSame('linux', $options['os']); |
| 61 | + self::assertSame('linux', $resolution->options['os']); |
| 62 | + self::assertSame('os=linux&osvariant=linux-debian&version=default', $resolution->redirectQuery); |
58 | 63 | } |
59 | 64 |
|
60 | | - public function testValidOsParameterSelectsFirstVariant(): void |
| 65 | + public function testInvalidOsRedirectsToAutoDetectedResults(): void |
61 | 66 | { |
62 | | - $options = $this->resolver()->resolve(['os' => 'windows'], '', ''); |
| 67 | + $resolution = $this->resolver()->resolve( |
| 68 | + ['os' => 'error'], |
| 69 | + '', |
| 70 | + 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', |
| 71 | + ); |
| 72 | + |
| 73 | + self::assertSame('windows', $resolution->options['os']); |
| 74 | + self::assertSame('os=windows&osvariant=windows-downloads&version=default', $resolution->redirectQuery); |
| 75 | + } |
| 76 | + |
| 77 | + public function testInvalidOsRedirectPreservesRequestedVersion(): void |
| 78 | + { |
| 79 | + $resolution = $this->resolver()->resolve(['os' => 'error', 'version' => '8.4'], '', ''); |
| 80 | + |
| 81 | + self::assertSame('os=linux&osvariant=linux-debian&version=8.4', $resolution->redirectQuery); |
| 82 | + } |
| 83 | + |
| 84 | + public function testValidOsParameterDoesNotRedirect(): void |
| 85 | + { |
| 86 | + $resolution = $this->resolver()->resolve(['os' => 'windows'], '', ''); |
| 87 | + |
| 88 | + self::assertNull($resolution->redirectQuery); |
| 89 | + self::assertSame('windows', $resolution->options['os']); |
| 90 | + self::assertSame('windows-downloads', $resolution->options['osvariant']); |
| 91 | + } |
| 92 | + |
| 93 | + public function testAbsentOsParameterDoesNotRedirect(): void |
| 94 | + { |
| 95 | + $resolution = $this->resolver()->resolve([], '', ''); |
63 | 96 |
|
64 | | - self::assertSame('windows', $options['os']); |
65 | | - self::assertSame('windows-downloads', $options['osvariant']); |
| 97 | + self::assertNull($resolution->redirectQuery); |
| 98 | + self::assertSame('linux', $resolution->options['os']); |
66 | 99 | } |
67 | 100 |
|
68 | 101 | public function testValidOsAndVariantAreHonored(): void |
69 | 102 | { |
70 | | - $options = $this->resolver()->resolve( |
| 103 | + $resolution = $this->resolver()->resolve( |
71 | 104 | ['os' => 'osx', 'osvariant' => 'osx-macports'], |
72 | 105 | '', |
73 | 106 | '', |
74 | 107 | ); |
75 | 108 |
|
76 | | - self::assertSame('osx', $options['os']); |
77 | | - self::assertSame('osx-macports', $options['osvariant']); |
| 109 | + self::assertNull($resolution->redirectQuery); |
| 110 | + self::assertSame('osx', $resolution->options['os']); |
| 111 | + self::assertSame('osx-macports', $resolution->options['osvariant']); |
78 | 112 | } |
79 | 113 |
|
80 | 114 | public function testInvalidVariantFallsBackToFirstForThatOs(): void |
81 | 115 | { |
82 | | - $options = $this->resolver()->resolve( |
| 116 | + $resolution = $this->resolver()->resolve( |
83 | 117 | ['os' => 'windows', 'osvariant' => 'linux-debian'], |
84 | 118 | '', |
85 | 119 | '', |
86 | 120 | ); |
87 | 121 |
|
88 | | - self::assertSame('windows', $options['os']); |
89 | | - self::assertSame('windows-downloads', $options['osvariant']); |
| 122 | + self::assertNull($resolution->redirectQuery); |
| 123 | + self::assertSame('windows', $resolution->options['os']); |
| 124 | + self::assertSame('windows-downloads', $resolution->options['osvariant']); |
90 | 125 | } |
91 | 126 |
|
92 | 127 | public function testVersionDefaultsWhenNotSupplied(): void |
93 | 128 | { |
94 | | - $options = $this->resolver()->resolve([], '', ''); |
| 129 | + $resolution = $this->resolver()->resolve([], '', ''); |
95 | 130 |
|
96 | | - self::assertSame('default', $options['version']); |
| 131 | + self::assertSame('default', $resolution->options['version']); |
97 | 132 | } |
98 | 133 |
|
99 | 134 | public function testGetParametersArePreserved(): void |
100 | 135 | { |
101 | | - $options = $this->resolver()->resolve( |
| 136 | + $resolution = $this->resolver()->resolve( |
102 | 137 | ['os' => 'linux', 'version' => '8.4', 'source' => 'Y'], |
103 | 138 | '', |
104 | 139 | '', |
105 | 140 | ); |
106 | 141 |
|
107 | | - self::assertSame('8.4', $options['version']); |
108 | | - self::assertSame('Y', $options['source']); |
| 142 | + self::assertSame('8.4', $resolution->options['version']); |
| 143 | + self::assertSame('Y', $resolution->options['source']); |
109 | 144 | } |
110 | 145 |
|
111 | 146 | public function testDetectsWindowsFromUserAgent(): void |
112 | 147 | { |
113 | | - $options = $this->resolver()->resolve([], '', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'); |
| 148 | + $resolution = $this->resolver()->resolve([], '', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)'); |
114 | 149 |
|
115 | | - self::assertSame('windows', $options['os']); |
| 150 | + self::assertSame('windows', $resolution->options['os']); |
116 | 151 | } |
117 | 152 |
|
118 | 153 | public function testDetectsUbuntuVariantFromUserAgent(): void |
119 | 154 | { |
120 | | - $options = $this->resolver()->resolve([], '', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64)'); |
| 155 | + $resolution = $this->resolver()->resolve([], '', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64)'); |
121 | 156 |
|
122 | | - self::assertSame('linux', $options['os']); |
123 | | - self::assertSame('linux-ubuntu', $options['osvariant']); |
| 157 | + self::assertSame('linux', $resolution->options['os']); |
| 158 | + self::assertSame('linux-ubuntu', $resolution->options['osvariant']); |
124 | 159 | } |
125 | 160 |
|
126 | 161 | public function testExplicitOsParameterOverridesAutoDetection(): void |
127 | 162 | { |
128 | | - $options = $this->resolver()->resolve( |
| 163 | + $resolution = $this->resolver()->resolve( |
129 | 164 | ['os' => 'osx'], |
130 | 165 | '', |
131 | 166 | 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)', |
132 | 167 | ); |
133 | 168 |
|
134 | | - self::assertSame('osx', $options['os']); |
| 169 | + self::assertNull($resolution->redirectQuery); |
| 170 | + self::assertSame('osx', $resolution->options['os']); |
135 | 171 | } |
136 | 172 |
|
137 | 173 | private function resolver(): OptionResolver |
|
0 commit comments