3 * Factory for handling the special page list and generating SpecialPage objects.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
20 * @covers SpecialPageFactory
23 class SpecialPageFactoryTest
extends MediaWikiTestCase
{
25 protected function tearDown() {
28 SpecialPageFactory
::resetList();
31 public function testResetList() {
32 SpecialPageFactory
::resetList();
33 $this->assertContains( 'Specialpages', SpecialPageFactory
::getNames() );
36 public function testHookNotCalledTwice() {
38 $this->mergeMwGlobalArrayValue( 'wgHooks', [
39 'SpecialPage_initList' => [
40 function () use ( &$count ) {
44 SpecialPageFactory
::resetList();
45 SpecialPageFactory
::getNames();
46 SpecialPageFactory
::getNames();
47 $this->assertEquals( 1, $count );
50 public function newSpecialAllPages() {
51 return new SpecialAllPages();
54 public function specialPageProvider() {
55 $specialPageTestHelper = new SpecialPageTestHelper();
58 'class name' => [ 'SpecialAllPages' ],
59 'closure' => [ function () {
60 return new SpecialAllPages();
62 'function' => [ [ $this, 'newSpecialAllPages' ] ],
63 'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages' ],
64 'callback with object' => [
65 [ $specialPageTestHelper, 'newSpecialAllPages' ]
68 [ 'SpecialPageTestHelper', 'newSpecialAllPages' ]
74 * @covers SpecialPageFactory::getPage
75 * @dataProvider specialPageProvider
77 public function testGetPage( $spec ) {
78 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
79 SpecialPageFactory
::resetList();
81 $page = SpecialPageFactory
::getPage( 'testdummy' );
82 $this->assertInstanceOf( 'SpecialPage', $page );
84 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
85 $this->assertEquals( true, $page2 === $page, "Should re-use instance:" );
89 * @covers SpecialPageFactory::getNames
91 public function testGetNames() {
92 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => 'SpecialAllPages' ] );
93 SpecialPageFactory
::resetList();
95 $names = SpecialPageFactory
::getNames();
96 $this->assertInternalType( 'array', $names );
97 $this->assertContains( 'testdummy', $names );
101 * @covers SpecialPageFactory::resolveAlias
103 public function testResolveAlias() {
104 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
105 SpecialPageFactory
::resetList();
107 list( $name, $param ) = SpecialPageFactory
::resolveAlias( 'Spezialseiten/Foo' );
108 $this->assertEquals( 'Specialpages', $name );
109 $this->assertEquals( 'Foo', $param );
113 * @covers SpecialPageFactory::getLocalNameFor
115 public function testGetLocalNameFor() {
116 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
117 SpecialPageFactory
::resetList();
119 $name = SpecialPageFactory
::getLocalNameFor( 'Specialpages', 'Foo' );
120 $this->assertEquals( 'Spezialseiten/Foo', $name );
124 * @covers SpecialPageFactory::getTitleForAlias
126 public function testGetTitleForAlias() {
127 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
128 SpecialPageFactory
::resetList();
130 $title = SpecialPageFactory
::getTitleForAlias( 'Specialpages/Foo' );
131 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
132 $this->assertEquals( NS_SPECIAL
, $title->getNamespace() );
136 * @dataProvider provideTestConflictResolution
138 public function testConflictResolution(
139 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
142 $lang = clone $wgContLang;
143 $lang->mExtendedSpecialPageAliases
= $aliasesList;
144 $this->setMwGlobals( 'wgContLang', $lang );
145 $this->setMwGlobals( 'wgSpecialPages',
146 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
148 SpecialPageFactory
::resetList();
150 // Catch the warnings we expect to be raised
152 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
153 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
154 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
155 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
157 $warnings[] = $errstr;
162 $reset = new ScopedCallback( 'restore_error_handler' );
164 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
165 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
166 $result = SpecialPageFactory
::getLocalNameFor( $name );
167 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
169 $gotWarnings = count( $warnings );
170 if ( $gotWarnings !== $expectWarnings ) {
171 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
172 implode( "\n", $warnings )
178 * @dataProvider provideTestConflictResolution
180 public function testConflictResolutionReversed(
181 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
183 // Make sure order doesn't matter by reversing the list
184 $aliasesList = array_reverse( $aliasesList );
185 return $this->testConflictResolution(
186 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
190 public function provideTestConflictResolution() {
193 'Canonical name wins',
194 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
202 'Doesn\'t redirect to a different special page\'s canonical name',
203 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
211 'Canonical name wins even if not aliased',
212 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
220 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
221 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
229 'First local name beats non-first',
230 [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
238 'Doesn\'t redirect to a different special page\'s first alias',
241 'First' => [ 'Bar' ],
242 'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
251 'Doesn\'t redirect wrong even if all aliases conflict',
254 'First' => [ 'Bar' ],
255 'Baz' => [ 'Foo', 'Bar' ]
266 public function testGetAliasListRecursion() {
268 $this->mergeMwGlobalArrayValue( 'wgHooks', [
269 'SpecialPage_initList' => [
270 function () use ( &$called ) {
271 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
276 SpecialPageFactory
::resetList();
277 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
278 $this->assertTrue( $called, 'Recursive call succeeded' );