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