Merge "Use MediaWiki\SuppressWarnings around trigger_error('') instead @"
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleMethodsTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4
5 /**
6 * @group ContentHandler
7 * @group Database
8 *
9 * @note We don't make assumptions about the main namespace.
10 * But we do expect the Help namespace to contain Wikitext.
11 */
12 class TitleMethodsTest extends MediaWikiLangTestCase {
13
14 protected function setUp() {
15 parent::setUp();
16
17 $this->mergeMwGlobalArrayValue(
18 'wgExtraNamespaces',
19 [
20 12302 => 'TEST-JS',
21 12303 => 'TEST-JS_TALK',
22 ]
23 );
24
25 $this->mergeMwGlobalArrayValue(
26 'wgNamespaceContentModels',
27 [
28 12302 => CONTENT_MODEL_JAVASCRIPT,
29 ]
30 );
31 }
32
33 public static function provideEquals() {
34 return [
35 [ 'Main Page', 'Main Page', true ],
36 [ 'Main Page', 'Not The Main Page', false ],
37 [ 'Main Page', 'Project:Main Page', false ],
38 [ 'File:Example.png', 'Image:Example.png', true ],
39 [ 'Special:Version', 'Special:Version', true ],
40 [ 'Special:Version', 'Special:Recentchanges', false ],
41 [ 'Special:Version', 'Main Page', false ],
42 ];
43 }
44
45 /**
46 * @dataProvider provideEquals
47 * @covers Title::equals
48 */
49 public function testEquals( $titleA, $titleB, $expectedBool ) {
50 $titleA = Title::newFromText( $titleA );
51 $titleB = Title::newFromText( $titleB );
52
53 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
54 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
55 }
56
57 public static function provideInNamespace() {
58 return [
59 [ 'Main Page', NS_MAIN, true ],
60 [ 'Main Page', NS_TALK, false ],
61 [ 'Main Page', NS_USER, false ],
62 [ 'User:Foo', NS_USER, true ],
63 [ 'User:Foo', NS_USER_TALK, false ],
64 [ 'User:Foo', NS_TEMPLATE, false ],
65 [ 'User_talk:Foo', NS_USER_TALK, true ],
66 [ 'User_talk:Foo', NS_USER, false ],
67 ];
68 }
69
70 /**
71 * @dataProvider provideInNamespace
72 * @covers Title::inNamespace
73 */
74 public function testInNamespace( $title, $ns, $expectedBool ) {
75 $title = Title::newFromText( $title );
76 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
77 }
78
79 /**
80 * @covers Title::inNamespaces
81 */
82 public function testInNamespaces() {
83 $mainpage = Title::newFromText( 'Main Page' );
84 $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
85 $this->assertTrue( $mainpage->inNamespaces( [ NS_MAIN, NS_USER ] ) );
86 $this->assertTrue( $mainpage->inNamespaces( [ NS_USER, NS_MAIN ] ) );
87 $this->assertFalse( $mainpage->inNamespaces( [ NS_PROJECT, NS_TEMPLATE ] ) );
88 }
89
90 public static function provideHasSubjectNamespace() {
91 return [
92 [ 'Main Page', NS_MAIN, true ],
93 [ 'Main Page', NS_TALK, true ],
94 [ 'Main Page', NS_USER, false ],
95 [ 'User:Foo', NS_USER, true ],
96 [ 'User:Foo', NS_USER_TALK, true ],
97 [ 'User:Foo', NS_TEMPLATE, false ],
98 [ 'User_talk:Foo', NS_USER_TALK, true ],
99 [ 'User_talk:Foo', NS_USER, true ],
100 ];
101 }
102
103 /**
104 * @dataProvider provideHasSubjectNamespace
105 * @covers Title::hasSubjectNamespace
106 */
107 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
108 $title = Title::newFromText( $title );
109 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
110 }
111
112 public function dataGetContentModel() {
113 return [
114 [ 'Help:Foo', CONTENT_MODEL_WIKITEXT ],
115 [ 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ],
116 [ 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ],
117 [ 'User:Foo', CONTENT_MODEL_WIKITEXT ],
118 [ 'User:Foo.js', CONTENT_MODEL_WIKITEXT ],
119 [ 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
120 [ 'User:Foo/bar.css', CONTENT_MODEL_CSS ],
121 [ 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ],
122 [ 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ],
123 [ 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ],
124 [ 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
125 [ 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ],
126 [ 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ],
127 [ 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ],
128 [ 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ],
129 [ 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ],
130 [ 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ],
131 [ 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ],
132 [ 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ],
133 [ 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ],
134 ];
135 }
136
137 /**
138 * @dataProvider dataGetContentModel
139 * @covers Title::getContentModel
140 */
141 public function testGetContentModel( $title, $expectedModelId ) {
142 $title = Title::newFromText( $title );
143 $this->assertEquals( $expectedModelId, $title->getContentModel() );
144 }
145
146 /**
147 * @dataProvider dataGetContentModel
148 * @covers Title::hasContentModel
149 */
150 public function testHasContentModel( $title, $expectedModelId ) {
151 $title = Title::newFromText( $title );
152 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
153 }
154
155 public static function provideIsSiteConfigPage() {
156 return [
157 [ 'Help:Foo', false ],
158 [ 'Help:Foo.js', false ],
159 [ 'Help:Foo/bar.js', false ],
160 [ 'User:Foo', false ],
161 [ 'User:Foo.js', false ],
162 [ 'User:Foo/bar.js', false ],
163 [ 'User:Foo/bar.json', false ],
164 [ 'User:Foo/bar.css', false ],
165 [ 'User:Foo/bar.JS', false ],
166 [ 'User:Foo/bar.JSON', false ],
167 [ 'User:Foo/bar.CSS', false ],
168 [ 'User talk:Foo/bar.css', false ],
169 [ 'User:Foo/bar.js.xxx', false ],
170 [ 'User:Foo/bar.xxx', false ],
171 [ 'MediaWiki:Foo.js', true ],
172 [ 'MediaWiki:Foo.json', true ],
173 [ 'MediaWiki:Foo.css', true ],
174 [ 'MediaWiki:Foo.JS', false ],
175 [ 'MediaWiki:Foo.JSON', false ],
176 [ 'MediaWiki:Foo.CSS', false ],
177 [ 'MediaWiki:Foo/bar.css', true ],
178 [ 'MediaWiki:Foo.css.xxx', false ],
179 [ 'TEST-JS:Foo', false ],
180 [ 'TEST-JS:Foo.js', false ],
181 ];
182 }
183
184 /**
185 * @dataProvider provideIsSiteConfigPage
186 * @covers Title::isSiteConfigPage
187 */
188 public function testSiteConfigPage( $title, $expectedBool ) {
189 $title = Title::newFromText( $title );
190 $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
191 }
192
193 public static function provideIsUserConfigPage() {
194 return [
195 [ 'Help:Foo', false ],
196 [ 'Help:Foo.js', false ],
197 [ 'Help:Foo/bar.js', false ],
198 [ 'User:Foo', false ],
199 [ 'User:Foo.js', false ],
200 [ 'User:Foo/bar.js', true ],
201 [ 'User:Foo/bar.JS', false ],
202 [ 'User:Foo/bar.json', true ],
203 [ 'User:Foo/bar.JSON', false ],
204 [ 'User:Foo/bar.css', true ],
205 [ 'User:Foo/bar.CSS', false ],
206 [ 'User talk:Foo/bar.css', false ],
207 [ 'User:Foo/bar.js.xxx', false ],
208 [ 'User:Foo/bar.xxx', false ],
209 [ 'MediaWiki:Foo.js', false ],
210 [ 'MediaWiki:Foo.json', false ],
211 [ 'MediaWiki:Foo.css', false ],
212 [ 'MediaWiki:Foo.JS', false ],
213 [ 'MediaWiki:Foo.JSON', false ],
214 [ 'MediaWiki:Foo.CSS', false ],
215 [ 'MediaWiki:Foo.css.xxx', false ],
216 [ 'TEST-JS:Foo', false ],
217 [ 'TEST-JS:Foo.js', false ],
218 ];
219 }
220
221 /**
222 * @dataProvider provideIsUserConfigPage
223 * @covers Title::isUserConfigPage
224 */
225 public function testIsUserConfigPage( $title, $expectedBool ) {
226 $title = Title::newFromText( $title );
227 $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
228 }
229
230 public static function provideIsUserCssConfigPage() {
231 return [
232 [ 'Help:Foo', false ],
233 [ 'Help:Foo.css', false ],
234 [ 'User:Foo', false ],
235 [ 'User:Foo.js', false ],
236 [ 'User:Foo.json', false ],
237 [ 'User:Foo.css', false ],
238 [ 'User:Foo/bar.js', false ],
239 [ 'User:Foo/bar.json', false ],
240 [ 'User:Foo/bar.css', true ],
241 ];
242 }
243
244 /**
245 * @dataProvider provideIsUserCssConfigPage
246 * @covers Title::isUserCssConfigPage
247 */
248 public function testIsUserCssConfigPage( $title, $expectedBool ) {
249 $title = Title::newFromText( $title );
250 $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
251 }
252
253 public static function provideIsUserJsConfigPage() {
254 return [
255 [ 'Help:Foo', false ],
256 [ 'Help:Foo.css', false ],
257 [ 'User:Foo', false ],
258 [ 'User:Foo.js', false ],
259 [ 'User:Foo.json', false ],
260 [ 'User:Foo.css', false ],
261 [ 'User:Foo/bar.js', true ],
262 [ 'User:Foo/bar.json', false ],
263 [ 'User:Foo/bar.css', false ],
264 ];
265 }
266
267 /**
268 * @dataProvider provideIsUserJsConfigPage
269 * @covers Title::isUserJsConfigPage
270 */
271 public function testIsUserJsConfigPage( $title, $expectedBool ) {
272 $title = Title::newFromText( $title );
273 $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
274 }
275
276 public static function provideIsWikitextPage() {
277 return [
278 [ 'Help:Foo', true ],
279 [ 'Help:Foo.js', true ],
280 [ 'Help:Foo/bar.js', true ],
281 [ 'User:Foo', true ],
282 [ 'User:Foo.js', true ],
283 [ 'User:Foo/bar.js', false ],
284 [ 'User:Foo/bar.json', false ],
285 [ 'User:Foo/bar.css', false ],
286 [ 'User talk:Foo/bar.css', true ],
287 [ 'User:Foo/bar.js.xxx', true ],
288 [ 'User:Foo/bar.xxx', true ],
289 [ 'MediaWiki:Foo.js', false ],
290 [ 'User:Foo/bar.JS', true ],
291 [ 'User:Foo/bar.JSON', true ],
292 [ 'User:Foo/bar.CSS', true ],
293 [ 'MediaWiki:Foo.json', false ],
294 [ 'MediaWiki:Foo.css', false ],
295 [ 'MediaWiki:Foo.JS', true ],
296 [ 'MediaWiki:Foo.JSON', true ],
297 [ 'MediaWiki:Foo.CSS', true ],
298 [ 'MediaWiki:Foo.css.xxx', true ],
299 [ 'TEST-JS:Foo', false ],
300 [ 'TEST-JS:Foo.js', false ],
301 ];
302 }
303
304 /**
305 * @dataProvider provideIsWikitextPage
306 * @covers Title::isWikitextPage
307 */
308 public function testIsWikitextPage( $title, $expectedBool ) {
309 $title = Title::newFromText( $title );
310 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
311 }
312
313 public static function provideGetOtherPage() {
314 return [
315 [ 'Main Page', 'Talk:Main Page' ],
316 [ 'Talk:Main Page', 'Main Page' ],
317 [ 'Help:Main Page', 'Help talk:Main Page' ],
318 [ 'Help talk:Main Page', 'Help:Main Page' ],
319 [ 'Special:FooBar', null ],
320 [ 'Media:File.jpg', null ],
321 ];
322 }
323
324 /**
325 * @dataProvider provideGetOtherpage
326 * @covers Title::getOtherPage
327 *
328 * @param string $text
329 * @param string|null $expected
330 */
331 public function testGetOtherPage( $text, $expected ) {
332 if ( $expected === null ) {
333 $this->setExpectedException( MWException::class );
334 }
335
336 $title = Title::newFromText( $text );
337 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
338 }
339
340 /**
341 * @covers Title::clearCaches
342 */
343 public function testClearCaches() {
344 $linkCache = MediaWikiServices::getInstance()->getLinkCache();
345
346 $title1 = Title::newFromText( 'Foo' );
347 $linkCache->addGoodLinkObj( 23, $title1 );
348
349 Title::clearCaches();
350
351 $title2 = Title::newFromText( 'Foo' );
352 $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
353 $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
354 }
355
356 function tearDown() {
357 Title::clearCaches();
358 parent::tearDown();
359 }
360 }