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