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 newSpecialAllPages() {
32 return new SpecialAllPages();
35 public function specialPageProvider() {
37 'class name' => array( 'SpecialAllPages', false ),
38 'closure' => array( function() {
39 return new SpecialAllPages();
41 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
46 * @covers SpecialPageFactory::getPage
47 * @dataProvider specialPageProvider
49 public function testGetPage( $spec, $shouldReuseInstance ) {
50 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
51 SpecialPageFactory
::resetList();
53 $page = SpecialPageFactory
::getPage( 'testdummy' );
54 $this->assertInstanceOf( 'SpecialPage', $page );
56 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
57 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
61 * @covers SpecialPageFactory::getNames
63 public function testGetNames() {
64 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => 'SpecialAllPages' ) );
65 SpecialPageFactory
::resetList();
67 $names = SpecialPageFactory
::getNames();
68 $this->assertInternalType( 'array', $names );
69 $this->assertContains( 'testdummy', $names );
73 * @covers SpecialPageFactory::resolveAlias
75 public function testResolveAlias() {
76 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
77 SpecialPageFactory
::resetList();
79 list( $name, $param ) = SpecialPageFactory
::resolveAlias( 'Spezialseiten/Foo' );
80 $this->assertEquals( 'Specialpages', $name );
81 $this->assertEquals( 'Foo', $param );
85 * @covers SpecialPageFactory::getLocalNameFor
87 public function testGetLocalNameFor() {
88 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
89 SpecialPageFactory
::resetList();
91 $name = SpecialPageFactory
::getLocalNameFor( 'Specialpages', 'Foo' );
92 $this->assertEquals( 'Spezialseiten/Foo', $name );
96 * @covers SpecialPageFactory::getTitleForAlias
98 public function testGetTitleForAlias() {
99 $this->setMwGlobals( 'wgContLang', Language
::factory( 'de' ) );
100 SpecialPageFactory
::resetList();
102 $title = SpecialPageFactory
::getTitleForAlias( 'Specialpages/Foo' );
103 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
104 $this->assertEquals( NS_SPECIAL
, $title->getNamespace() );
108 * @dataProvider provideTestConflictResolution
110 public function testConflictResolution(
111 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
114 $lang = clone $wgContLang;
115 $lang->mExtendedSpecialPageAliases
= $aliasesList;
116 $this->setMwGlobals( 'wgContLang', $lang );
117 $this->setMwGlobals( 'wgSpecialPages',
118 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
120 SpecialPageFactory
::resetList();
122 // Catch the warnings we expect to be raised
124 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
125 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
126 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
127 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
129 $warnings[] = $errstr;
134 $reset = new ScopedCallback( 'restore_error_handler' );
136 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
137 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
138 $result = SpecialPageFactory
::getLocalNameFor( $name );
139 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
141 $gotWarnings = count( $warnings );
142 if ( $gotWarnings !== $expectWarnings ) {
143 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
144 join( "\n", $warnings )
150 * @dataProvider provideTestConflictResolution
152 public function testConflictResolutionReversed(
153 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
155 // Make sure order doesn't matter by reversing the list
156 $aliasesList = array_reverse( $aliasesList );
157 return $this->testConflictResolution(
158 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
162 public function provideTestConflictResolution() {
165 'Canonical name wins',
166 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
174 'Doesn\'t redirect to a different special page\'s canonical name',
175 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
183 'Canonical name wins even if not aliased',
184 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
192 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
193 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
201 'First local name beats non-first',
202 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
210 'Doesn\'t redirect to a different special page\'s first alias',
212 'Foo' => array( 'Foo' ),
213 'First' => array( 'Bar' ),
214 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
223 'Doesn\'t redirect wrong even if all aliases conflict',
225 'Foo' => array( 'Foo' ),
226 'First' => array( 'Bar' ),
227 'Baz' => array( 'Foo', 'Bar' )