3 use MediaWiki\MediaWikiServices
;
6 * @group ContentHandler
9 * @note We don't make assumptions about the main namespace.
10 * But we do expect the Help namespace to contain Wikitext.
12 class TitleMethodsTest
extends MediaWikiLangTestCase
{
14 protected function setUp() {
19 $this->mergeMwGlobalArrayValue(
23 12303 => 'TEST-JS_TALK',
27 $this->mergeMwGlobalArrayValue(
28 'wgNamespaceContentModels',
30 12302 => CONTENT_MODEL_JAVASCRIPT
,
34 MWNamespace
::clearCaches();
35 $wgContLang->resetNamespaces(); # reset namespace cache
38 protected function tearDown() {
43 MWNamespace
::clearCaches();
44 $wgContLang->resetNamespaces(); # reset namespace cache
47 public static function provideEquals() {
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 ],
60 * @dataProvider provideEquals
61 * @covers Title::equals
63 public function testEquals( $titleA, $titleB, $expectedBool ) {
64 $titleA = Title
::newFromText( $titleA );
65 $titleB = Title
::newFromText( $titleB );
67 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
68 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
71 public static function provideInNamespace() {
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 ],
85 * @dataProvider provideInNamespace
86 * @covers Title::inNamespace
88 public function testInNamespace( $title, $ns, $expectedBool ) {
89 $title = Title
::newFromText( $title );
90 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
94 * @covers Title::inNamespaces
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
] ) );
104 public static function provideHasSubjectNamespace() {
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 ],
118 * @dataProvider provideHasSubjectNamespace
119 * @covers Title::hasSubjectNamespace
121 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
122 $title = Title
::newFromText( $title );
123 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
126 public function dataGetContentModel() {
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
],
152 * @dataProvider dataGetContentModel
153 * @covers Title::getContentModel
155 public function testGetContentModel( $title, $expectedModelId ) {
156 $title = Title
::newFromText( $title );
157 $this->assertEquals( $expectedModelId, $title->getContentModel() );
161 * @dataProvider dataGetContentModel
162 * @covers Title::hasContentModel
164 public function testHasContentModel( $title, $expectedModelId ) {
165 $title = Title
::newFromText( $title );
166 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
169 public static function provideIsSiteConfigPage() {
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 ],
199 * @dataProvider provideIsSiteConfigPage
200 * @covers Title::isSiteConfigPage
202 public function testSiteConfigPage( $title, $expectedBool ) {
203 $title = Title
::newFromText( $title );
204 $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
207 public static function provideIsUserConfigPage() {
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 ],
236 * @dataProvider provideIsUserConfigPage
237 * @covers Title::isUserConfigPage
239 public function testIsUserConfigPage( $title, $expectedBool ) {
240 $title = Title
::newFromText( $title );
241 $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
244 public static function provideIsUserCssConfigPage() {
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 ],
259 * @dataProvider provideIsUserCssConfigPage
260 * @covers Title::isUserCssConfigPage
262 public function testIsUserCssConfigPage( $title, $expectedBool ) {
263 $title = Title
::newFromText( $title );
264 $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
267 public static function provideIsUserJsConfigPage() {
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 ],
282 * @dataProvider provideIsUserJsConfigPage
283 * @covers Title::isUserJsConfigPage
285 public function testIsUserJsConfigPage( $title, $expectedBool ) {
286 $title = Title
::newFromText( $title );
287 $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
290 public static function provideIsWikitextPage() {
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 ],
319 * @dataProvider provideIsWikitextPage
320 * @covers Title::isWikitextPage
322 public function testIsWikitextPage( $title, $expectedBool ) {
323 $title = Title
::newFromText( $title );
324 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
327 public static function provideGetOtherPage() {
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 ],
339 * @dataProvider provideGetOtherpage
340 * @covers Title::getOtherPage
342 * @param string $text
343 * @param string|null $expected
345 public function testGetOtherPage( $text, $expected ) {
346 if ( $expected === null ) {
347 $this->setExpectedException( MWException
::class );
350 $title = Title
::newFromText( $text );
351 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
355 * @covers Title::clearCaches
357 public function testClearCaches() {
358 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
360 $title1 = Title
::newFromText( 'Foo' );
361 $linkCache->addGoodLinkObj( 23, $title1 );
363 Title
::clearCaches();
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' );