Merge "mediawiki.action.history.diff: Remove white background from table.diff"
[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 newSpecialAllPages() {
32 return new SpecialAllPages();
33 }
34
35 public function specialPageProvider() {
36 return array(
37 'class name' => array( 'SpecialAllPages', false ),
38 'closure' => array( function() {
39 return new SpecialAllPages();
40 }, false ),
41 'function' => array( array( $this, 'newSpecialAllPages' ), false ),
42 );
43 }
44
45 /**
46 * @covers SpecialPageFactory::getPage
47 * @dataProvider specialPageProvider
48 */
49 public function testGetPage( $spec, $shouldReuseInstance ) {
50 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => $spec ) );
51 SpecialPageFactory::resetList();
52
53 $page = SpecialPageFactory::getPage( 'testdummy' );
54 $this->assertInstanceOf( 'SpecialPage', $page );
55
56 $page2 = SpecialPageFactory::getPage( 'testdummy' );
57 $this->assertEquals( $shouldReuseInstance, $page2 === $page, "Should re-use instance:" );
58 }
59
60 /**
61 * @covers SpecialPageFactory::getNames
62 */
63 public function testGetNames() {
64 $this->mergeMwGlobalArrayValue( 'wgSpecialPages', array( 'testdummy' => 'SpecialAllPages' ) );
65 SpecialPageFactory::resetList();
66
67 $names = SpecialPageFactory::getNames();
68 $this->assertInternalType( 'array', $names );
69 $this->assertContains( 'testdummy', $names );
70 }
71
72 /**
73 * @covers SpecialPageFactory::resolveAlias
74 */
75 public function testResolveAlias() {
76 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
77 SpecialPageFactory::resetList();
78
79 list( $name, $param ) = SpecialPageFactory::resolveAlias( 'Spezialseiten/Foo' );
80 $this->assertEquals( 'Specialpages', $name );
81 $this->assertEquals( 'Foo', $param );
82 }
83
84 /**
85 * @covers SpecialPageFactory::getLocalNameFor
86 */
87 public function testGetLocalNameFor() {
88 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
89 SpecialPageFactory::resetList();
90
91 $name = SpecialPageFactory::getLocalNameFor( 'Specialpages', 'Foo' );
92 $this->assertEquals( 'Spezialseiten/Foo', $name );
93 }
94
95 /**
96 * @covers SpecialPageFactory::getTitleForAlias
97 */
98 public function testGetTitleForAlias() {
99 $this->setMwGlobals( 'wgContLang', Language::factory( 'de' ) );
100 SpecialPageFactory::resetList();
101
102 $title = SpecialPageFactory::getTitleForAlias( 'Specialpages/Foo' );
103 $this->assertEquals( 'Spezialseiten/Foo', $title->getText() );
104 $this->assertEquals( NS_SPECIAL, $title->getNamespace() );
105 }
106
107 /**
108 * @dataProvider provideTestConflictResolution
109 */
110 public function testConflictResolution(
111 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
112 ) {
113 global $wgContLang;
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 ) )
119 );
120 SpecialPageFactory::resetList();
121
122 // Catch the warnings we expect to be raised
123 $warnings = array();
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 )
128 ) {
129 $warnings[] = $errstr;
130 return true;
131 }
132 return false;
133 } );
134 $reset = new ScopedCallback( 'restore_error_handler' );
135
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" );
140
141 $gotWarnings = count( $warnings );
142 if ( $gotWarnings !== $expectWarnings ) {
143 $this->fail( "Expected $expectWarnings warning(s), but got $gotWarnings:\n" .
144 join( "\n", $warnings )
145 );
146 }
147 }
148
149 /**
150 * @dataProvider provideTestConflictResolution
151 */
152 public function testConflictResolutionReversed(
153 $test, $aliasesList, $alias, $expectedName, $expectedAlias, $expectWarnings
154 ) {
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
159 );
160 }
161
162 public function provideTestConflictResolution() {
163 return array(
164 array(
165 'Canonical name wins',
166 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
167 'Foo',
168 'Foo',
169 'Foo',
170 1,
171 ),
172
173 array(
174 'Doesn\'t redirect to a different special page\'s canonical name',
175 array( 'Foo' => array( 'Foo', 'Bar' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
176 'Baz',
177 'Baz',
178 'BazPage',
179 1,
180 ),
181
182 array(
183 'Canonical name wins even if not aliased',
184 array( 'Foo' => array( 'FooPage' ), 'Baz' => array( 'Foo', 'BazPage', 'Baz2' ) ),
185 'Foo',
186 'Foo',
187 'FooPage',
188 1,
189 ),
190
191 array(
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' ) ),
194 'Baz',
195 'Baz',
196 'BazPage',
197 1,
198 ),
199
200 array(
201 'First local name beats non-first',
202 array( 'First' => array( 'Foo' ), 'NonFirst' => array( 'Bar', 'Foo' ) ),
203 'Foo',
204 'First',
205 'Foo',
206 0,
207 ),
208
209 array(
210 'Doesn\'t redirect to a different special page\'s first alias',
211 array(
212 'Foo' => array( 'Foo' ),
213 'First' => array( 'Bar' ),
214 'Baz' => array( 'Foo', 'Bar', 'BazPage', 'Baz2' )
215 ),
216 'Baz',
217 'Baz',
218 'BazPage',
219 1,
220 ),
221
222 array(
223 'Doesn\'t redirect wrong even if all aliases conflict',
224 array(
225 'Foo' => array( 'Foo' ),
226 'First' => array( 'Bar' ),
227 'Baz' => array( 'Foo', 'Bar' )
228 ),
229 'Baz',
230 'Baz',
231 'Baz',
232 2,
233 ),
234
235 );
236 }
237
238 }