Handle extension dependencies in the installer
[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
10 /**
11 * @dataProvider provideCheck
12 */
13 public function testCheck( $coreVersion, $constraint, $expected ) {
14 $checker = new VersionChecker( $coreVersion );
15 $this->assertEquals( $expected, !(bool)$checker->checkArray( [
16 'FakeExtension' => [
17 'MediaWiki' => $constraint,
18 ],
19 ] )
20 );
21 }
22
23 public static function provideCheck() {
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 provideType
49 */
50 public function testType( $given, $expected ) {
51 $checker = new VersionChecker( '1.0.0' );
52 $checker
53 ->setLoadedExtensionsAndSkins( [
54 'FakeDependency' => [
55 'version' => '1.0.0',
56 ],
57 'NoVersionGiven' => [],
58 ] );
59 $this->assertEquals( $expected, $checker->checkArray( [
60 'FakeExtension' => $given,
61 ] )
62 );
63 }
64
65 public static function provideType() {
66 return [
67 // valid type
68 [
69 [
70 'extensions' => [
71 'FakeDependency' => '1.0.0'
72 ]
73 ],
74 []
75 ],
76 [
77 [
78 'MediaWiki' => '1.0.0'
79 ],
80 []
81 ],
82 [
83 [
84 'extensions' => [
85 'NoVersionGiven' => '*'
86 ]
87 ],
88 [],
89 ],
90 [
91 [
92 'extensions' => [
93 'NoVersionGiven' => '1.0',
94 ]
95 ],
96 [ [
97 'incompatible' => 'FakeExtension',
98 'type' => 'incompatible-extensions',
99 'msg' => 'NoVersionGiven does not expose its version, but FakeExtension requires: 1.0.'
100 ] ],
101 ],
102 [
103 [
104 'extensions' => [
105 'Missing' => '*',
106 ]
107 ],
108 [ [
109 'missing' => 'Missing',
110 'type' => 'missing-extensions',
111 'msg' => 'FakeExtension requires Missing to be installed.',
112 ] ],
113 ],
114 [
115 [
116 'extensions' => [
117 'FakeDependency' => '2.0.0',
118 ]
119 ],
120 [ [
121 'incompatible' => 'FakeExtension',
122 'type' => 'incompatible-extensions',
123 // phpcs:ignore Generic.Files.LineLength.TooLong
124 'msg' => 'FakeExtension is not compatible with the current installed version of FakeDependency (1.0.0), it requires: 2.0.0.'
125 ] ],
126 ]
127 ];
128 }
129
130 /**
131 * Check, if a non-parsable version constraint does not throw an exception or
132 * returns any error message.
133 */
134 public function testInvalidConstraint() {
135 $checker = new VersionChecker( '1.0.0' );
136 $checker
137 ->setLoadedExtensionsAndSkins( [
138 'FakeDependency' => [
139 'version' => 'not really valid',
140 ],
141 ] );
142 $this->assertEquals(
143 [ [
144 'type' => 'invalid-version',
145 'msg' => "FakeDependency does not have a valid version string."
146 ] ],
147 $checker->checkArray( [
148 'FakeExtension' => [
149 'extensions' => [
150 'FakeDependency' => '1.24.3',
151 ],
152 ],
153 ] )
154 );
155
156 $checker = new VersionChecker( '1.0.0' );
157 $checker
158 ->setLoadedExtensionsAndSkins( [
159 'FakeDependency' => [
160 'version' => '1.24.3',
161 ],
162 ] );
163
164 $this->setExpectedException( UnexpectedValueException::class );
165 $checker->checkArray( [
166 'FakeExtension' => [
167 'FakeDependency' => 'not really valid',
168 ]
169 ] );
170 }
171 }