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', array(
39 'SpecialPage_initList' => array(
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' => array( 'SpecialAllPages', false ),
59 'closure' => array( function () {
60 return new SpecialAllPages();
62 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
63 'callback string' => array( 'SpecialPageTestHelper::newSpecialAllPages', false ),
64 'callback with object' => array(
65 array( $specialPageTestHelper, 'newSpecialAllPages' ),
68 'callback array' => array(
69 array( 'SpecialPageTestHelper', 'newSpecialAllPages' ),
76 * @covers SpecialPageFactory::getPage
77 * @dataProvider specialPageProvider
79 public function testGetPage( $spec, $shouldReuseInstance ) {
80 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
81 SpecialPageFactory
::resetList();
83 $page = SpecialPageFactory
::getPage( 'testdummy' );
84 $this->assertInstanceOf( 'SpecialPage', $page );
86 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
87 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
91 * @covers SpecialPageFactory::getNames
93 public function testGetNames() {
94 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( '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 join( "\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 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
204 'Doesn\'t redirect to a different special page\'s canonical name',
205 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
213 'Canonical name wins even if not aliased',
214 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
222 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
223 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
231 'First local name beats non-first',
232 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
240 'Doesn\'t redirect to a different special page\'s first alias',
242 'Foo' => array( 'Foo' ),
243 'First' => array( 'Bar' ),
244 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
253 'Doesn\'t redirect wrong even if all aliases conflict',
255 'Foo' => array( 'Foo' ),
256 'First' => array( 'Bar' ),
257 'Baz' => array( 'Foo', 'Bar' )
268 public function testGetAliasListRecursion() {
270 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
271 'SpecialPage_initList' => array(
272 function () use ( &$called ) {
273 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
278 SpecialPageFactory
::resetList();
279 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
280 $this->assertTrue( $called, 'Recursive call succeeded' );