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() {
56 'class name' => array( 'SpecialAllPages', false ),
57 'closure' => array( function () {
58 return new SpecialAllPages();
60 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
65 * @covers SpecialPageFactory::getPage
66 * @dataProvider specialPageProvider
68 public function testGetPage( $spec, $shouldReuseInstance ) {
69 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
70 SpecialPageFactory
::resetList();
72 $page = SpecialPageFactory
::getPage( 'testdummy' );
73 $this->assertInstanceOf( 'SpecialPage', $page );
75 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
76 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
80 * @covers SpecialPageFactory::getNames
82 public function testGetNames() {
83 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => 'SpecialAllPages' ) );
84 SpecialPageFactory
::resetList();
86 $names = SpecialPageFactory
::getNames();
87 $this->assertInternalType( 'array', $names );
88 $this->assertContains( 'testdummy', $names );
92 * @covers SpecialPageFactory::resolveAlias
94 public function testResolveAlias() {
95 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
96 SpecialPageFactory
::resetList();
98 list( $name, $param ) = SpecialPageFactory
::resolveAlias( 'Spezialseiten/Foo' );
99 $this->assertEquals( 'Specialpages', $name );
100 $this->assertEquals( 'Foo', $param );
104 * @covers SpecialPageFactory::getLocalNameFor
106 public function testGetLocalNameFor() {
107 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
108 SpecialPageFactory
::resetList();
110 $name = SpecialPageFactory
::getLocalNameFor( 'Specialpages', 'Foo' );
111 $this->assertEquals( 'Spezialseiten/Foo', $name );
115 * @covers SpecialPageFactory::getTitleForAlias
117 public function testGetTitleForAlias() {
118 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
119 SpecialPageFactory
::resetList();
121 $title = SpecialPageFactory
::getTitleForAlias( 'Specialpages/Foo' );
122 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
123 $this->assertEquals( NS_SPECIAL
, $title->getNamespace() );
127 * @dataProvider provideTestConflictResolution
129 public function testConflictResolution(
130 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
133 $lang = clone $wgContLang;
134 $lang->mExtendedSpecialPageAliases
= $aliasesList;
135 $this->setMwGlobals( 'wgContLang', $lang );
136 $this->setMwGlobals( 'wgSpecialPages',
137 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
139 SpecialPageFactory
::resetList();
141 // Catch the warnings we expect to be raised
143 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
144 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
145 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
146 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
148 $warnings[] = $errstr;
153 $reset = new ScopedCallback( 'restore_error_handler' );
155 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
156 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
157 $result = SpecialPageFactory
::getLocalNameFor( $name );
158 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
160 $gotWarnings = count( $warnings );
161 if ( $gotWarnings !== $expectWarnings ) {
162 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
163 join( "\n", $warnings )
169 * @dataProvider provideTestConflictResolution
171 public function testConflictResolutionReversed(
172 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
174 // Make sure order doesn't matter by reversing the list
175 $aliasesList = array_reverse( $aliasesList );
176 return $this->testConflictResolution(
177 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
181 public function provideTestConflictResolution() {
184 'Canonical name wins',
185 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
193 'Doesn\'t redirect to a different special page\'s canonical name',
194 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
202 'Canonical name wins even if not aliased',
203 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
211 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
212 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
220 'First local name beats non-first',
221 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
229 'Doesn\'t redirect to a different special page\'s first alias',
231 'Foo' => array( 'Foo' ),
232 'First' => array( 'Bar' ),
233 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
242 'Doesn\'t redirect wrong even if all aliases conflict',
244 'Foo' => array( 'Foo' ),
245 'First' => array( 'Bar' ),
246 'Baz' => array( 'Foo', 'Bar' )
257 public function testGetAliasListRecursion() {
259 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
260 'SpecialPage_initList' => array(
261 function () use ( &$called ) {
262 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
267 SpecialPageFactory
::resetList();
268 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
269 $this->assertTrue( $called, 'Recursive call succeeded' );