Merge "Warn against certain jQuery methods"
[lhc/web/wiklou.git] / tests / phpunit / includes / specialpage / SpecialPageFactoryTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use Wikimedia\ScopedCallback;
5
6 /**
7 * Factory for handling the special page list and generating SpecialPage objects.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @covers SpecialPageFactory
25 * @group SpecialPage
26 */
27 class SpecialPageFactoryTest extends MediaWikiTestCase {
28
29 protected function tearDown() {
30 parent::tearDown();
31
32 SpecialPageFactory::resetList();
33 }
34
35 public function testResetList() {
36 SpecialPageFactory::resetList();
37 $this->assertContains( 'Specialpages', SpecialPageFactory::getNames() );
38 }
39
40 public function testHookNotCalledTwice() {
41 $count = 0;
42 $this->mergeMwGlobalArrayValue( 'wgHooks', [
43 'SpecialPage_initList' => [
44 function () use ( &$count ) {
45 $count++;
46 }
47 ] ] );
48 SpecialPageFactory::resetList();
49 SpecialPageFactory::getNames();
50 SpecialPageFactory::getNames();
51 $this->assertEquals( 1, $count );
52 }
53
54 public function newSpecialAllPages() {
55 return new SpecialAllPages();
56 }
57
58 public function specialPageProvider() {
59 $specialPageTestHelper = new SpecialPageTestHelper();
60
61 return [
62 'class name' => [ 'SpecialAllPages', false ],
63 'closure' => [ function () {
64 return new SpecialAllPages();
65 }, false ],
66 'function' => [ [ $this, 'newSpecialAllPages' ], false ],
67 'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages', false ],
68 'callback with object' => [
69 [ $specialPageTestHelper, 'newSpecialAllPages' ],
70 false
71 ],
72 'callback array' => [
73 [ 'SpecialPageTestHelper', 'newSpecialAllPages' ],
74 false
75 ]
76 ];
77 }
78
79 /**
80 * @covers SpecialPageFactory::getPage
81 * @dataProvider specialPageProvider
82 */
83 public function testGetPage( $spec, $shouldReuseInstance ) {
84 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
85 SpecialPageFactory::resetList();
86
87 $page = SpecialPageFactory::getPage( 'testdummy' );
88 $this->assertInstanceOf( SpecialPage::class, $page );
89
90 $page2 = SpecialPageFactory::getPage( 'testdummy' );
91 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
92 }
93
94 /**
95 * @covers SpecialPageFactory::getNames
96 */
97 public function testGetNames() {
98 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => SpecialAllPages::class ] );
99 SpecialPageFactory::resetList();
100
101 $names = SpecialPageFactory::getNames();
102 $this->assertInternalType( 'array', $names );
103 $this->assertContains( 'testdummy', $names );
104 }
105
106 /**
107 * @covers SpecialPageFactory::resolveAlias
108 */
109 public function testResolveAlias() {
110 $this->setContentLang( 'de' );
111 SpecialPageFactory::resetList();
112
113 list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
114 $this->assertEquals( 'Specialpages', $name );
115 $this->assertEquals( 'Foo', $param );
116 }
117
118 /**
119 * @covers SpecialPageFactory::getLocalNameFor
120 */
121 public function testGetLocalNameFor() {
122 $this->setContentLang( 'de' );
123 SpecialPageFactory::resetList();
124
125 $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
126 $this->assertEquals( 'Spezialseiten/Foo', $name );
127 }
128
129 /**
130 * @covers SpecialPageFactory::getTitleForAlias
131 */
132 public function testGetTitleForAlias() {
133 $this->setContentLang( 'de' );
134 SpecialPageFactory::resetList();
135
136 $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
137 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
138 $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
139 }
140
141 /**
142 * @dataProvider provideTestConflictResolution
143 */
144 public function testConflictResolution(
145 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
146 ) {
147 $lang = clone MediaWikiServices::getInstance()->getContentLanguage();
148 $lang->mExtendedSpecialPageAliases = $aliasesList;
149 $this->setContentLang( $lang );
150 $this->setMwGlobals( 'wgSpecialPages',
151 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
152 );
153 SpecialPageFactory::resetList();
154
155 // Catch the warnings we expect to be raised
156 $warnings = [];
157 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
158 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
159 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
160 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
161 ) {
162 $warnings[] = $errstr;
163 return true;
164 }
165 return false;
166 } );
167 $reset = new ScopedCallback( 'restore_error_handler' );
168
169 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
170 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
171 $result = SpecialPageFactory::getLocalNameFor( $name );
172 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
173
174 $gotWarnings = count( $warnings );
175 if ( $gotWarnings !== $expectWarnings ) {
176 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
177 implode( "\n", $warnings )
178 );
179 }
180 }
181
182 /**
183 * @dataProvider provideTestConflictResolution
184 */
185 public function testConflictResolutionReversed(
186 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
187 ) {
188 // Make sure order doesn't matter by reversing the list
189 $aliasesList = array_reverse( $aliasesList );
190 return $this->testConflictResolution(
191 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
192 );
193 }
194
195 public function provideTestConflictResolution() {
196 return [
197 [
198 'Canonical name wins',
199 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
200 'Foo',
201 'Foo',
202 'Foo',
203 1,
204 ],
205
206 [
207 'Doesn\'t redirect to a different special page\'s canonical name',
208 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
209 'Baz',
210 'Baz',
211 'BazPage',
212 1,
213 ],
214
215 [
216 'Canonical name wins even if not aliased',
217 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
218 'Foo',
219 'Foo',
220 'FooPage',
221 1,
222 ],
223
224 [
225 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
226 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
227 'Baz',
228 'Baz',
229 'BazPage',
230 1,
231 ],
232
233 [
234 'First local name beats non-first',
235 [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
236 'Foo',
237 'First',
238 'Foo',
239 0,
240 ],
241
242 [
243 'Doesn\'t redirect to a different special page\'s first alias',
244 [
245 'Foo' => [ 'Foo' ],
246 'First' => [ 'Bar' ],
247 'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
248 ],
249 'Baz',
250 'Baz',
251 'BazPage',
252 1,
253 ],
254
255 [
256 'Doesn\'t redirect wrong even if all aliases conflict',
257 [
258 'Foo' => [ 'Foo' ],
259 'First' => [ 'Bar' ],
260 'Baz' => [ 'Foo', 'Bar' ]
261 ],
262 'Baz',
263 'Baz',
264 'Baz',
265 2,
266 ],
267
268 ];
269 }
270
271 public function testGetAliasListRecursion() {
272 $called = false;
273 $this->mergeMwGlobalArrayValue( 'wgHooks', [
274 'SpecialPage_initList' => [
275 function () use ( &$called ) {
276 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
277 $called = true;
278 }
279 ],
280 ] );
281 SpecialPageFactory::resetList();
282 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
283 $this->assertTrue( $called, 'Recursive call succeeded' );
284 }
285
286 }