Merge "resourceloader: Mark clearCache() as private (only for tests)"
[lhc/web/wiklou.git] / tests / phpunit / includes / registration / VersionCheckerTest.php
1 <?php
2
3 /**
4 * @covers VersionChecker
5 */
6 class VersionCheckerTest extends PHPUnit\Framework\TestCase {
7
8 use MediaWikiCoversValidator;
9 use PHPUnit4And6Compat;
10
11 /**
12 * @dataProvider provideMediaWikiCheck
13 */
14 public function testMediaWikiCheck( $coreVersion, $constraint, $expected ) {
15 $checker = new VersionChecker( $coreVersion, '7.0.0' );
16 $this->assertEquals( $expected, !(bool)$checker->checkArray( [
17 'FakeExtension' => [
18 'MediaWiki' => $constraint,
19 ],
20 ] ) );
21 }
22
23 public static function provideMediaWikiCheck() {
24 return [
25 // [ $wgVersion, constraint, expected ]
26 [ '1.25alpha', '>= 1.26', false ],
27 [ '1.25.0', '>= 1.26', false ],
28 [ '1.26alpha', '>= 1.26', true ],
29 [ '1.26alpha', '>= 1.26.0', true ],
30 [ '1.26alpha', '>= 1.26.0-stable', false ],
31 [ '1.26.0', '>= 1.26.0-stable', true ],
32 [ '1.26.1', '>= 1.26.0-stable', true ],
33 [ '1.27.1', '>= 1.26.0-stable', true ],
34 [ '1.26alpha', '>= 1.26.1', false ],
35 [ '1.26alpha', '>= 1.26alpha', true ],
36 [ '1.26alpha', '>= 1.25', true ],
37 [ '1.26.0-alpha.14', '>= 1.26.0-alpha.15', false ],
38 [ '1.26.0-alpha.14', '>= 1.26.0-alpha.10', true ],
39 [ '1.26.1', '>= 1.26.2, <=1.26.0', false ],
40 [ '1.26.1', '^1.26.2', false ],
41 // Accept anything for un-parsable version strings
42 [ '1.26mwf14', '== 1.25alpha', true ],
43 [ 'totallyinvalid', '== 1.0', true ],
44 ];
45 }
46
47 /**
48 * @dataProvider providePhpValidCheck
49 */
50 public function testPhpValidCheck( $phpVersion, $constraint, $expected ) {
51 $checker = new VersionChecker( '1.0.0', $phpVersion );
52 $this->assertEquals( $expected, !(bool)$checker->checkArray( [
53 'FakeExtension' => [
54 'platform' => [
55 'php' => $constraint,
56 ],
57 ],
58 ] ) );
59 }
60
61 public static function providePhpValidCheck() {
62 return [
63 // [ phpVersion, constraint, expected ]
64 [ '7.0.23', '>= 7.0.0', true ],
65 [ '7.0.23', '^7.1.0', false ],
66 [ '7.0.23', '7.0.23', true ],
67 ];
68 }
69
70 /**
71 * @expectedException UnexpectedValueException
72 */
73 public function testPhpInvalidConstraint() {
74 $checker = new VersionChecker( '1.0.0', '7.0.0' );
75 $checker->checkArray( [
76 'FakeExtension' => [
77 'platform' => [
78 'php' => 'totallyinvalid',
79 ],
80 ],
81 ] );
82 }
83
84 /**
85 * @dataProvider providePhpInvalidVersion
86 * @expectedException UnexpectedValueException
87 */
88 public function testPhpInvalidVersion( $phpVersion ) {
89 $checker = new VersionChecker( '1.0.0', $phpVersion );
90 }
91
92 public static function providePhpInvalidVersion() {
93 return [
94 // [ phpVersion ]
95 [ '7.abc' ],
96 [ '5.a.x' ],
97 ];
98 }
99
100 /**
101 * @dataProvider provideType
102 */
103 public function testType( $given, $expected ) {
104 $checker = new VersionChecker( '1.0.0', '7.0.0' );
105 $checker->setLoadedExtensionsAndSkins( [
106 'FakeDependency' => [
107 'version' => '1.0.0',
108 ],
109 'NoVersionGiven' => [],
110 ] );
111 $this->assertEquals( $expected, $checker->checkArray( [
112 'FakeExtension' => $given,
113 ] ) );
114 }
115
116 public static function provideType() {
117 return [
118 // valid type
119 [
120 [
121 'extensions' => [
122 'FakeDependency' => '1.0.0',
123 ],
124 ],
125 [],
126 ],
127 [
128 [
129 'MediaWiki' => '1.0.0',
130 ],
131 [],
132 ],
133 [
134 [
135 'extensions' => [
136 'NoVersionGiven' => '*',
137 ],
138 ],
139 [],
140 ],
141 [
142 [
143 'extensions' => [
144 'NoVersionGiven' => '1.0',
145 ],
146 ],
147 [
148 [
149 'incompatible' => 'FakeExtension',
150 'type' => 'incompatible-extensions',
151 'msg' => 'NoVersionGiven does not expose its version, but FakeExtension requires: 1.0.',
152 ],
153 ],
154 ],
155 [
156 [
157 'extensions' => [
158 'Missing' => '*',
159 ],
160 ],
161 [
162 [
163 'missing' => 'Missing',
164 'type' => 'missing-extensions',
165 'msg' => 'FakeExtension requires Missing to be installed.',
166 ],
167 ],
168 ],
169 [
170 [
171 'extensions' => [
172 'FakeDependency' => '2.0.0',
173 ],
174 ],
175 [
176 [
177 'incompatible' => 'FakeExtension',
178 'type' => 'incompatible-extensions',
179 // phpcs:ignore Generic.Files.LineLength.TooLong
180 'msg' => 'FakeExtension is not compatible with the current installed version of FakeDependency (1.0.0), it requires: 2.0.0.',
181 ],
182 ],
183 ],
184 [
185 [
186 'skins' => [
187 'FakeSkin' => '*',
188 ],
189 ],
190 [
191 [
192 'missing' => 'FakeSkin',
193 'type' => 'missing-skins',
194 'msg' => 'FakeExtension requires FakeSkin to be installed.',
195 ],
196 ],
197 ],
198 ];
199 }
200
201 /**
202 * Check, if a non-parsable version constraint does not throw an exception or
203 * returns any error message.
204 */
205 public function testInvalidConstraint() {
206 $checker = new VersionChecker( '1.0.0', '7.0.0' );
207 $checker->setLoadedExtensionsAndSkins( [
208 'FakeDependency' => [
209 'version' => 'not really valid',
210 ],
211 ] );
212 $this->assertEquals( [
213 [
214 'type' => 'invalid-version',
215 'msg' => "FakeDependency does not have a valid version string.",
216 ],
217 ], $checker->checkArray( [
218 'FakeExtension' => [
219 'extensions' => [
220 'FakeDependency' => '1.24.3',
221 ],
222 ],
223 ] ) );
224
225 $checker = new VersionChecker( '1.0.0', '7.0.0' );
226 $checker->setLoadedExtensionsAndSkins( [
227 'FakeDependency' => [
228 'version' => '1.24.3',
229 ],
230 ] );
231
232 $this->setExpectedException( UnexpectedValueException::class );
233 $checker->checkArray( [
234 'FakeExtension' => [
235 'FakeDependency' => 'not really valid',
236 ],
237 ] );
238 }
239
240 public function provideInvalidDependency() {
241 return [
242 [
243 [
244 'FakeExtension' => [
245 'platform' => [
246 'undefinedPlatformDependency' => '*',
247 ],
248 ],
249 ],
250 'undefinedPlatformDependency',
251 ],
252 [
253 [
254 'FakeExtension' => [
255 'undefinedDependencyType' => '*',
256 ],
257 ],
258 'undefinedDependencyType',
259 ],
260 // T197478
261 [
262 [
263 'FakeExtension' => [
264 'skin' => [
265 'FakeSkin' => '*',
266 ],
267 ],
268 ],
269 'skin',
270 ],
271 ];
272 }
273
274 /**
275 * @dataProvider provideInvalidDependency
276 */
277 public function testInvalidDependency( $depencency, $type ) {
278 $checker = new VersionChecker( '1.0.0', '7.0.0' );
279 $this->setExpectedException(
280 UnexpectedValueException::class,
281 "Dependency type $type unknown in FakeExtension"
282 );
283 $checker->checkArray( $depencency );
284 }
285 }