2 use Wikimedia\ScopedCallback
;
5 * Factory for handling the special page list and generating SpecialPage objects.
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.
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.
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
22 * @covers SpecialPageFactory
25 class SpecialPageFactoryTest
extends MediaWikiTestCase
{
27 protected function tearDown() {
30 SpecialPageFactory
::resetList();
33 public function testResetList() {
34 SpecialPageFactory
::resetList();
35 $this->assertContains( 'Specialpages', SpecialPageFactory
::getNames() );
38 public function testHookNotCalledTwice() {
40 $this->mergeMwGlobalArrayValue( 'wgHooks', [
41 'SpecialPage_initList' => [
42 function () use ( &$count ) {
46 SpecialPageFactory
::resetList();
47 SpecialPageFactory
::getNames();
48 SpecialPageFactory
::getNames();
49 $this->assertEquals( 1, $count );
52 public function newSpecialAllPages() {
53 return new SpecialAllPages();
56 public function specialPageProvider() {
57 $specialPageTestHelper = new SpecialPageTestHelper();
60 'class name' => [ 'SpecialAllPages' ],
61 'closure' => [ function () {
62 return new SpecialAllPages();
64 'function' => [ [ $this, 'newSpecialAllPages' ] ],
65 'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages' ],
66 'callback with object' => [
67 [ $specialPageTestHelper, 'newSpecialAllPages' ]
70 [ 'SpecialPageTestHelper', 'newSpecialAllPages' ]
76 * @covers SpecialPageFactory::getPage
77 * @dataProvider specialPageProvider
79 public function testGetPage( $spec ) {
80 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
81 SpecialPageFactory
::resetList();
83 $page = SpecialPageFactory
::getPage( 'testdummy' );
84 $this->assertInstanceOf( 'SpecialPage', $page );
86 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
87 $this->assertEquals( true, $page2 === $page, "Should re-use instance:" );
91 * @covers SpecialPageFactory::getNames
93 public function testGetNames() {
94 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => 'SpecialAllPages' ] );
95 SpecialPageFactory
::resetList();
97 $names = SpecialPageFactory
::getNames();
98 $this->assertInternalType( 'array', $names );
99 $this->assertContains( 'testdummy', $names );
103 * @covers SpecialPageFactory::resolveAlias
105 public function testResolveAlias() {
106 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
107 SpecialPageFactory
::resetList();
109 list( $name, $param ) = SpecialPageFactory
::resolveAlias( 'Spezialseiten/Foo' );
110 $this->assertEquals( 'Specialpages', $name );
111 $this->assertEquals( 'Foo', $param );
115 * @covers SpecialPageFactory::getLocalNameFor
117 public function testGetLocalNameFor() {
118 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
119 SpecialPageFactory
::resetList();
121 $name = SpecialPageFactory
::getLocalNameFor( 'Specialpages', 'Foo' );
122 $this->assertEquals( 'Spezialseiten/Foo', $name );
126 * @covers SpecialPageFactory::getTitleForAlias
128 public function testGetTitleForAlias() {
129 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
130 SpecialPageFactory
::resetList();
132 $title = SpecialPageFactory
::getTitleForAlias( 'Specialpages/Foo' );
133 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
134 $this->assertEquals( NS_SPECIAL
, $title->getNamespace() );
138 * @dataProvider provideTestConflictResolution
140 public function testConflictResolution(
141 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
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 ) )
150 SpecialPageFactory
::resetList();
152 // Catch the warnings we expect to be raised
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 )
159 $warnings[] = $errstr;
164 $reset = new ScopedCallback( 'restore_error_handler' );
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" );
171 $gotWarnings = count( $warnings );
172 if ( $gotWarnings !== $expectWarnings ) {
173 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
174 implode( "\n", $warnings )
180 * @dataProvider provideTestConflictResolution
182 public function testConflictResolutionReversed(
183 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
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
192 public function provideTestConflictResolution() {
195 'Canonical name wins',
196 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
204 'Doesn\'t redirect to a different special page\'s canonical name',
205 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
213 'Canonical name wins even if not aliased',
214 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
222 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
223 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
231 'First local name beats non-first',
232 [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
240 'Doesn\'t redirect to a different special page\'s first alias',
243 'First' => [ 'Bar' ],
244 'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
253 'Doesn\'t redirect wrong even if all aliases conflict',
256 'First' => [ 'Bar' ],
257 'Baz' => [ 'Foo', 'Bar' ]
268 public function testGetAliasListRecursion() {
270 $this->mergeMwGlobalArrayValue( 'wgHooks', [
271 'SpecialPage_initList' => [
272 function () use ( &$called ) {
273 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
278 SpecialPageFactory
::resetList();
279 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
280 $this->assertTrue( $called, 'Recursive call succeeded' );