Merge "DatabaseMssql: Don't duplicate body of makeList()"
[mediawiki.git] / tests / phpunit / includes / specialpage / SpecialPageFactoryTest.php
blobfd6911f67a996921cdedb133cdec647dc419973d
1 <?php
2 /**
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
21 * @group SpecialPage
23 class SpecialPageFactoryTest extends MediaWikiTestCase {
25 protected function tearDown() {
26 parent::tearDown();
28 SpecialPageFactory::resetList();
31 public function testResetList() {
32 SpecialPageFactory::resetList();
33 $this->assertContains( 'Specialpages', SpecialPageFactory::getNames() );
36 public function testHookNotCalledTwice() {
37 $count = 0;
38 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
39 'SpecialPage_initList' => array(
40 function () use ( &$count ) {
41 $count++;
43 ) ) );
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();
57 return array(
58 'class name' => array( 'SpecialAllPages', false ),
59 'closure' => array( function () {
60 return new SpecialAllPages();
61 }, false ),
62 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
63 'callback string' => array( 'SpecialPageTestHelper::newSpecialAllPages', false ),
64 'callback with object' => array(
65 array( $specialPageTestHelper, 'newSpecialAllPages' ),
66 false
68 'callback array' => array(
69 array( 'SpecialPageTestHelper', 'newSpecialAllPages' ),
70 false
75 /**
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:" );
90 /**
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
143 global $wgContLang;
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
153 $warnings = array();
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;
160 return true;
162 return false;
163 } );
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() {
193 return array(
194 array(
195 'Canonical name wins',
196 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
197 'Foo',
198 'Foo',
199 'Foo',
203 array(
204 'Doesn\'t redirect to a different special page\'s canonical name',
205 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
206 'Baz',
207 'Baz',
208 'BazPage',
212 array(
213 'Canonical name wins even if not aliased',
214 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
215 'Foo',
216 'Foo',
217 'FooPage',
221 array(
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' ) ),
224 'Baz',
225 'Baz',
226 'BazPage',
230 array(
231 'First local name beats non-first',
232 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
233 'Foo',
234 'First',
235 'Foo',
239 array(
240 'Doesn\'t redirect to a different special page\'s first alias',
241 array(
242 'Foo' => array( 'Foo' ),
243 'First' => array( 'Bar' ),
244 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
246 'Baz',
247 'Baz',
248 'BazPage',
252 array(
253 'Doesn\'t redirect wrong even if all aliases conflict',
254 array(
255 'Foo' => array( 'Foo' ),
256 'First' => array( 'Bar' ),
257 'Baz' => array( 'Foo', 'Bar' )
259 'Baz',
260 'Baz',
261 'Baz',
268 public function testGetAliasListRecursion() {
269 $called = false;
270 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
271 'SpecialPage_initList' => array(
272 function () use ( &$called ) {
273 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
274 $called = true;
277 ) );
278 SpecialPageFactory::resetList();
279 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
280 $this->assertTrue( $called, 'Recursive call succeeded' );