3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\ScopedCallback
;
7 * Factory for handling the special page list and generating SpecialPage objects.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @covers SpecialPageFactory
27 class SpecialPageFactoryTest
extends MediaWikiTestCase
{
29 protected function tearDown() {
32 SpecialPageFactory
::resetList();
35 public function testResetList() {
36 SpecialPageFactory
::resetList();
37 $this->assertContains( 'Specialpages', SpecialPageFactory
::getNames() );
40 public function testHookNotCalledTwice() {
42 $this->mergeMwGlobalArrayValue( 'wgHooks', [
43 'SpecialPage_initList' => [
44 function () use ( &$count ) {
48 SpecialPageFactory
::resetList();
49 SpecialPageFactory
::getNames();
50 SpecialPageFactory
::getNames();
51 $this->assertEquals( 1, $count );
54 public function newSpecialAllPages() {
55 return new SpecialAllPages();
58 public function specialPageProvider() {
59 $specialPageTestHelper = new SpecialPageTestHelper();
62 'class name' => [ 'SpecialAllPages', false ],
63 'closure' => [ function () {
64 return new SpecialAllPages();
66 'function' => [ [ $this, 'newSpecialAllPages' ], false ],
67 'callback string' => [ 'SpecialPageTestHelper::newSpecialAllPages', false ],
68 'callback with object' => [
69 [ $specialPageTestHelper, 'newSpecialAllPages' ],
73 [ 'SpecialPageTestHelper', 'newSpecialAllPages' ],
80 * @covers SpecialPageFactory::getPage
81 * @dataProvider specialPageProvider
83 public function testGetPage( $spec, $shouldReuseInstance ) {
84 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => $spec ] );
85 SpecialPageFactory
::resetList();
87 $page = SpecialPageFactory
::getPage( 'testdummy' );
88 $this->assertInstanceOf( SpecialPage
::class, $page );
90 $page2 = SpecialPageFactory
::getPage( 'testdummy' );
91 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
95 * @covers SpecialPageFactory::getNames
97 public function testGetNames() {
98 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', [ 'testdummy' => SpecialAllPages
::class ] );
99 SpecialPageFactory
::resetList();
101 $names = SpecialPageFactory
::getNames();
102 $this->assertInternalType( 'array', $names );
103 $this->assertContains( 'testdummy', $names );
107 * @covers SpecialPageFactory::resolveAlias
109 public function testResolveAlias() {
110 $this->setContentLang( 'de' );
111 SpecialPageFactory
::resetList();
113 list( $name, $param ) = SpecialPageFactory
::resolveAlias( 'Spezialseiten/Foo' );
114 $this->assertEquals( 'Specialpages', $name );
115 $this->assertEquals( 'Foo', $param );
119 * @covers SpecialPageFactory::getLocalNameFor
121 public function testGetLocalNameFor() {
122 $this->setContentLang( 'de' );
123 SpecialPageFactory
::resetList();
125 $name = SpecialPageFactory
::getLocalNameFor( 'Specialpages', 'Foo' );
126 $this->assertEquals( 'Spezialseiten/Foo', $name );
130 * @covers SpecialPageFactory::getTitleForAlias
132 public function testGetTitleForAlias() {
133 $this->setContentLang( 'de' );
134 SpecialPageFactory
::resetList();
136 $title = SpecialPageFactory
::getTitleForAlias( 'Specialpages/Foo' );
137 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
138 $this->assertEquals( NS_SPECIAL
, $title->getNamespace() );
142 * @dataProvider provideTestConflictResolution
144 public function testConflictResolution(
145 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
147 $lang = clone MediaWikiServices
::getInstance()->getContentLanguage();
148 $lang->mExtendedSpecialPageAliases
= $aliasesList;
149 $this->setContentLang( $lang );
150 $this->setMwGlobals( 'wgSpecialPages',
151 array_combine( array_keys( $aliasesList ), array_keys( $aliasesList ) )
153 SpecialPageFactory
::resetList();
155 // Catch the warnings we expect to be raised
157 $this->setMwGlobals( 'wgDevelopmentWarnings', true );
158 set_error_handler( function ( $errno, $errstr ) use ( &$warnings ) {
159 if ( preg_match( '/First alias \'[^\']*\' for .*/', $errstr ) ||
160 preg_match( '/Did not find a usable alias for special page .*/', $errstr )
162 $warnings[] = $errstr;
167 $reset = new ScopedCallback( 'restore_error_handler' );
169 list( $name, /*...*/ ) = SpecialPageFactory
::resolveAlias( $alias );
170 $this->assertEquals( $expectedName, $name, "$test: Alias to name" );
171 $result = SpecialPageFactory
::getLocalNameFor( $name );
172 $this->assertEquals( $expectedAlias, $result, "$test: Alias to name to alias" );
174 $gotWarnings = count( $warnings );
175 if ( $gotWarnings !== $expectWarnings ) {
176 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
177 implode( "\n", $warnings )
183 * @dataProvider provideTestConflictResolution
185 public function testConflictResolutionReversed(
186 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
188 // Make sure order doesn't matter by reversing the list
189 $aliasesList = array_reverse( $aliasesList );
190 return $this->testConflictResolution(
191 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
195 public function provideTestConflictResolution() {
198 'Canonical name wins',
199 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
207 'Doesn\'t redirect to a different special page\'s canonical name',
208 [ 'Foo' => [ 'Foo', 'Bar' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
216 'Canonical name wins even if not aliased',
217 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
225 'Doesn\'t redirect to a different special page\'s canonical name even if not aliased',
226 [ 'Foo' => [ 'FooPage' ], 'Baz' => [ 'Foo', 'BazPage', 'Baz2' ] ],
234 'First local name beats non-first',
235 [ 'First' => [ 'Foo' ], 'NonFirst' => [ 'Bar', 'Foo' ] ],
243 'Doesn\'t redirect to a different special page\'s first alias',
246 'First' => [ 'Bar' ],
247 'Baz' => [ 'Foo', 'Bar', 'BazPage', 'Baz2' ]
256 'Doesn\'t redirect wrong even if all aliases conflict',
259 'First' => [ 'Bar' ],
260 'Baz' => [ 'Foo', 'Bar' ]
271 public function testGetAliasListRecursion() {
273 $this->mergeMwGlobalArrayValue( 'wgHooks', [
274 'SpecialPage_initList' => [
275 function () use ( &$called ) {
276 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
281 SpecialPageFactory
::resetList();
282 SpecialPageFactory
::getLocalNameFor( 'Specialpages' );
283 $this->assertTrue( $called, 'Recursive call succeeded' );