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