Support the creation of special pages with services injected
[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 \MediaWiki\Special\SpecialPageFactory
25 * @group SpecialPage
26 */
27 class SpecialPageFactoryTest extends MediaWikiTestCase {
28 public function testHookNotCalledTwice() {
29 $count = 0;
30 $this->mergeMwGlobalArrayValue( 'wgHooks', [
31 'SpecialPage_initList' => [
32 function () use ( &$count ) {
33 $count++;
34 }
35 ] ] );
36 $spf = MediaWikiServices::getInstance()->getSpecialPageFactory();
37 $spf->getNames();
38 $spf->getNames();
39 $this->assertEquals( 1, $count );
40 }
41
42 public function newSpecialAllPages() {
43 return new SpecialAllPages();
44 }
45
46 public function specialPageProvider() {
47 $specialPageTestHelper = new SpecialPageTestHelper();
48
49 return [
50 'class name' => [ 'SpecialAllPages', false ],
51 'closure' => [ function () {
52 return new SpecialAllPages();
53 }, false ],
54 'function' => [ [ $this, 'newSpecialAllPages' ], false ],
55 'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages', false ],
56 'callback with object' => [
57 [ $specialPageTestHelper, 'newSpecialAllPages' ],
58 false
59 ],
60 'callback array' => [
61 [ 'SpecialPageTestHelper', 'newSpecialAllPages' ],
62 false
63 ],
64 'object factory spec' => [
65 [ 'class' => SpecialAllPages::class ],
66 false
67 ]
68 ];
69 }
70
71 /**
72 * @covers SpecialPageFactory::getPage
73 * @dataProvider specialPageProvider
74 */
75 public function testGetPage( $spec, $shouldReuseInstance ) {
76 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
77
78 $page = SpecialPageFactory::getPage( 'testdummy' );
79 $this->assertInstanceOf( SpecialPage::class, $page );
80
81 $page2 = SpecialPageFactory::getPage( 'testdummy' );
82 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
83 }
84
85 /**
86 * @covers SpecialPageFactory::getNames
87 */
88 public function testGetNames() {
89 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => SpecialAllPages::class ] );
90
91 $names = SpecialPageFactory::getNames();
92 $this->assertInternalType( 'array', $names );
93 $this->assertContains( 'testdummy', $names );
94 }
95
96 /**
97 * @covers SpecialPageFactory::resolveAlias
98 */
99 public function testResolveAlias() {
100 $this->setContentLang( 'de' );
101
102 list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
103 $this->assertEquals( 'Specialpages', $name );
104 $this->assertEquals( 'Foo', $param );
105 }
106
107 /**
108 * @covers SpecialPageFactory::getLocalNameFor
109 */
110 public function testGetLocalNameFor() {
111 $this->setContentLang( 'de' );
112
113 $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
114 $this->assertEquals( 'Spezialseiten/Foo', $name );
115 }
116
117 /**
118 * @covers SpecialPageFactory::getTitleForAlias
119 */
120 public function testGetTitleForAlias() {
121 $this->setContentLang( 'de' );
122
123 $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
124 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
125 $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
126 }
127
128 /**
129 * @dataProvider provideTestConflictResolution
130 */
131 public function testConflictResolution(
132 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
133 ) {
134 $lang = clone MediaWikiServices::getInstance()->getContentLanguage();
135 $lang->mExtendedSpecialPageAliases = $aliasesList;
136 $this->setMwGlobals( 'wgSpecialPages',
137 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
138 );
139 $this->setContentLang( $lang );
140
141 // Catch the warnings we expect to be raised
142 $warnings = [];
143 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
144 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
145 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
146 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
147 ) {
148 $warnings[] = $errstr;
149 return true;
150 }
151 return false;
152 } );
153 $reset = new ScopedCallback( 'restore_error_handler' );
154
155 list( $name, /*...*/ ) = SpecialPageFactory::resolveAlias( $alias );
156 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
157 $result = SpecialPageFactory::getLocalNameFor( $name );
158 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
159
160 $gotWarnings = count( $warnings );
161 if ( $gotWarnings !== $expectWarnings ) {
162 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
163 implode( "\n", $warnings )
164 );
165 }
166 }
167
168 /**
169 * @dataProvider provideTestConflictResolution
170 */
171 public function testConflictResolutionReversed(
172 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
173 ) {
174 // Make sure order doesn't matter by reversing the list
175 $aliasesList = array_reverse( $aliasesList );
176 return $this->testConflictResolution(
177 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
178 );
179 }
180
181 public function provideTestConflictResolution() {
182 return [
183 [
184 'Canonical name wins',
185 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
186 'Foo',
187 'Foo',
188 'Foo',
189 1,
190 ],
191
192 [
193 'Doesn\'t redirect to a different special page\'s canonical name',
194 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
195 'Baz',
196 'Baz',
197 'BazPage',
198 1,
199 ],
200
201 [
202 'Canonical name wins even if not aliased',
203 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
204 'Foo',
205 'Foo',
206 'FooPage',
207 1,
208 ],
209
210 [
211 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
212 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
213 'Baz',
214 'Baz',
215 'BazPage',
216 1,
217 ],
218
219 [
220 'First local name beats non-first',
221 [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
222 'Foo',
223 'First',
224 'Foo',
225 0,
226 ],
227
228 [
229 'Doesn\'t redirect to a different special page\'s first alias',
230 [
231 'Foo' => [ 'Foo' ],
232 'First' => [ 'Bar' ],
233 'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
234 ],
235 'Baz',
236 'Baz',
237 'BazPage',
238 1,
239 ],
240
241 [
242 'Doesn\'t redirect wrong even if all aliases conflict',
243 [
244 'Foo' => [ 'Foo' ],
245 'First' => [ 'Bar' ],
246 'Baz' => [ 'Foo', 'Bar' ]
247 ],
248 'Baz',
249 'Baz',
250 'Baz',
251 2,
252 ],
253
254 ];
255 }
256
257 public function testGetAliasListRecursion() {
258 $called = false;
259 $this->mergeMwGlobalArrayValue( 'wgHooks', [
260 'SpecialPage_initList' => [
261 function () use ( &$called ) {
262 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
263 $called = true;
264 }
265 ],
266 ] );
267 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
268 $this->assertTrue( $called, 'Recursive call succeeded' );
269 }
270
271 /**
272 * @covers \MediaWiki\Special\SpecialPageFactory::getPage
273 */
274 public function testSpecialPageCreationThatRequiresService() {
275 $type = null;
276
277 $this->setMwGlobals( 'wgSpecialPages',
278 [ 'TestPage' => [
279 'factory' => function ( $spf ) use ( &$type ) {
280 $type = get_class( $spf );
281
282 return new class() extends SpecialPage {
283
284 };
285 },
286 'services' => [
287 'SpecialPageFactory'
288 ]
289 ] ]
290 );
291
292 SpecialPageFactory::getPage( 'TestPage' );
293
294 $this->assertEquals( \MediaWiki\Special\SpecialPageFactory::class, $type );
295 }
296 }