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