Merge "PrefixSearch: Add unit tests for StringPrefixSearch"
[lhc/web/wiklou.git] / tests / phpunit / includes / specialpage / SpecialPageFactoryTest.php
1 <?php
2 /**
3 * Factory for handling the special page list and generating SpecialPage objects.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @covers SpecialPageFactory
21 * @group SpecialPage
22 */
23 class SpecialPageFactoryTest extends MediaWikiTestCase {
24
25 protected function tearDown() {
26 parent::tearDown();
27
28 SpecialPageFactory::resetList();
29 }
30
31 public function testResetList() {
32 SpecialPageFactory::resetList();
33 $this->assertContains( 'Specialpages', SpecialPageFactory::getNames() );
34 }
35
36 public function testHookNotCalledTwice() {
37 $count = 0;
38 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
39 'SpecialPage_initList' => array(
40 function () use ( &$count ) {
41 $count++;
42 }
43 ) ) );
44 SpecialPageFactory::resetList();
45 SpecialPageFactory::getNames();
46 SpecialPageFactory::getNames();
47 $this->assertEquals( 1, $count );
48 }
49
50 public function newSpecialAllPages() {
51 return new SpecialAllPages();
52 }
53
54 public function specialPageProvider() {
55 return array(
56 'class name' => array( 'SpecialAllPages', false ),
57 'closure' => array( function () {
58 return new SpecialAllPages();
59 }, false ),
60 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
61 );
62 }
63
64 /**
65 * @covers SpecialPageFactory::getPage
66 * @dataProvider specialPageProvider
67 */
68 public function testGetPage( $spec, $shouldReuseInstance ) {
69 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
70 SpecialPageFactory::resetList();
71
72 $page = SpecialPageFactory::getPage( 'testdummy' );
73 $this->assertInstanceOf( 'SpecialPage', $page );
74
75 $page2 = SpecialPageFactory::getPage( 'testdummy' );
76 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
77 }
78
79 /**
80 * @covers SpecialPageFactory::getNames
81 */
82 public function testGetNames() {
83 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => 'SpecialAllPages' ) );
84 SpecialPageFactory::resetList();
85
86 $names = SpecialPageFactory::getNames();
87 $this->assertInternalType( 'array', $names );
88 $this->assertContains( 'testdummy', $names );
89 }
90
91 /**
92 * @covers SpecialPageFactory::resolveAlias
93 */
94 public function testResolveAlias() {
95 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
96 SpecialPageFactory::resetList();
97
98 list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
99 $this->assertEquals( 'Specialpages', $name );
100 $this->assertEquals( 'Foo', $param );
101 }
102
103 /**
104 * @covers SpecialPageFactory::getLocalNameFor
105 */
106 public function testGetLocalNameFor() {
107 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
108 SpecialPageFactory::resetList();
109
110 $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
111 $this->assertEquals( 'Spezialseiten/Foo', $name );
112 }
113
114 /**
115 * @covers SpecialPageFactory::getTitleForAlias
116 */
117 public function testGetTitleForAlias() {
118 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
119 SpecialPageFactory::resetList();
120
121 $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
122 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
123 $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
124 }
125
126 /**
127 * @dataProvider provideTestConflictResolution
128 */
129 public function testConflictResolution(
130 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
131 ) {
132 global $wgContLang;
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 ) )
138 );
139 SpecialPageFactory::resetList();
140
141 // Catch the warnings we expect to be raised
142 $warnings = array();
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 )
147 ) {
148 $warnings[] = $errstr;
149 return true;
150 }
151 return false;
152 } );
153 $reset = new ScopedCallback( 'restore_error_handler' );
154
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" );
159
160 $gotWarnings = count( $warnings );
161 if ( $gotWarnings !== $expectWarnings ) {
162 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
163 join( "\n", $warnings )
164 );
165 }
166 }
167
168 /**
169 * @dataProvider provideTestConflictResolution
170 */
171 public function testConflictResolutionReversed(
172 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
173 ) {
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
178 );
179 }
180
181 public function provideTestConflictResolution() {
182 return array(
183 array(
184 'Canonical name wins',
185 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
186 'Foo',
187 'Foo',
188 'Foo',
189 1,
190 ),
191
192 array(
193 'Doesn\'t redirect to a different special page\'s canonical name',
194 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
195 'Baz',
196 'Baz',
197 'BazPage',
198 1,
199 ),
200
201 array(
202 'Canonical name wins even if not aliased',
203 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
204 'Foo',
205 'Foo',
206 'FooPage',
207 1,
208 ),
209
210 array(
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' ) ),
213 'Baz',
214 'Baz',
215 'BazPage',
216 1,
217 ),
218
219 array(
220 'First local name beats non-first',
221 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
222 'Foo',
223 'First',
224 'Foo',
225 0,
226 ),
227
228 array(
229 'Doesn\'t redirect to a different special page\'s first alias',
230 array(
231 'Foo' => array( 'Foo' ),
232 'First' => array( 'Bar' ),
233 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
234 ),
235 'Baz',
236 'Baz',
237 'BazPage',
238 1,
239 ),
240
241 array(
242 'Doesn\'t redirect wrong even if all aliases conflict',
243 array(
244 'Foo' => array( 'Foo' ),
245 'First' => array( 'Bar' ),
246 'Baz' => array( 'Foo', 'Bar' )
247 ),
248 'Baz',
249 'Baz',
250 'Baz',
251 2,
252 ),
253
254 );
255 }
256
257 public function testGetAliasListRecursion() {
258 $called = false;
259 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
260 'SpecialPage_initList' => array(
261 function () use ( &$called ) {
262 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
263 $called = true;
264 }
265 ),
266 ) );
267 SpecialPageFactory::resetList();
268 SpecialPageFactory::getLocalNameFor( 'Specialpages' );
269 $this->assertTrue( $called, 'Recursive call succeeded' );
270 }
271
272 }