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