4 * @group ContentHandler
7 * @note We don't make assumptions about the main namespace.
8 * But we do expect the Help namespace to contain Wikitext.
10 class TitleMethodsTest
extends MediaWikiLangTestCase
{
12 protected function setUp() {
17 $this->mergeMwGlobalArrayValue(
21 12303 => 'TEST-JS_TALK',
25 $this->mergeMwGlobalArrayValue(
26 'wgNamespaceContentModels',
28 12302 => CONTENT_MODEL_JAVASCRIPT
,
32 MWNamespace
::clearCaches();
33 $wgContLang->resetNamespaces(); # reset namespace cache
36 protected function tearDown() {
41 MWNamespace
::clearCaches();
42 $wgContLang->resetNamespaces(); # reset namespace cache
45 public static function provideEquals() {
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 ],
58 * @dataProvider provideEquals
59 * @covers Title::equals
61 public function testEquals( $titleA, $titleB, $expectedBool ) {
62 $titleA = Title
::newFromText( $titleA );
63 $titleB = Title
::newFromText( $titleB );
65 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
66 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
69 public static function provideInNamespace() {
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 ],
83 * @dataProvider provideInNamespace
84 * @covers Title::inNamespace
86 public function testInNamespace( $title, $ns, $expectedBool ) {
87 $title = Title
::newFromText( $title );
88 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
92 * @covers Title::inNamespaces
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
] ) );
102 public static function provideHasSubjectNamespace() {
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 ],
116 * @dataProvider provideHasSubjectNamespace
117 * @covers Title::hasSubjectNamespace
119 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
120 $title = Title
::newFromText( $title );
121 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
124 public function dataGetContentModel() {
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
],
150 * @dataProvider dataGetContentModel
151 * @covers Title::getContentModel
153 public function testGetContentModel( $title, $expectedModelId ) {
154 $title = Title
::newFromText( $title );
155 $this->assertEquals( $expectedModelId, $title->getContentModel() );
159 * @dataProvider dataGetContentModel
160 * @covers Title::hasContentModel
162 public function testHasContentModel( $title, $expectedModelId ) {
163 $title = Title
::newFromText( $title );
164 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
167 public static function provideIsSiteConfigPage() {
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.json', false ],
176 [ 'User:Foo/bar.css', false ],
177 [ 'User:Foo/bar.JS', false ],
178 [ 'User:Foo/bar.JSON', false ],
179 [ 'User:Foo/bar.CSS', false ],
180 [ 'User talk:Foo/bar.css', false ],
181 [ 'User:Foo/bar.js.xxx', false ],
182 [ 'User:Foo/bar.xxx', false ],
183 [ 'MediaWiki:Foo.js', true ],
184 [ 'MediaWiki:Foo.json', true ],
185 [ 'MediaWiki:Foo.css', true ],
186 [ 'MediaWiki:Foo.JS', false ],
187 [ 'MediaWiki:Foo.JSON', false ],
188 [ 'MediaWiki:Foo.CSS', false ],
189 [ 'MediaWiki:Foo/bar.css', true ],
190 [ 'MediaWiki:Foo.css.xxx', false ],
191 [ 'TEST-JS:Foo', false ],
192 [ 'TEST-JS:Foo.js', false ],
197 * @dataProvider provideIsSiteConfigPage
198 * @covers Title::isSiteConfigPage
200 public function testSiteConfigPage( $title, $expectedBool ) {
201 $title = Title
::newFromText( $title );
202 $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
205 public static function provideIsUserConfigPage() {
207 [ 'Help:Foo', false ],
208 [ 'Help:Foo.js', false ],
209 [ 'Help:Foo/bar.js', false ],
210 [ 'User:Foo', false ],
211 [ 'User:Foo.js', false ],
212 [ 'User:Foo/bar.js', true ],
213 [ 'User:Foo/bar.JS', false ],
214 [ 'User:Foo/bar.json', true ],
215 [ 'User:Foo/bar.JSON', false ],
216 [ 'User:Foo/bar.css', true ],
217 [ 'User:Foo/bar.CSS', false ],
218 [ 'User talk:Foo/bar.css', false ],
219 [ 'User:Foo/bar.js.xxx', false ],
220 [ 'User:Foo/bar.xxx', false ],
221 [ 'MediaWiki:Foo.js', false ],
222 [ 'MediaWiki:Foo.json', false ],
223 [ 'MediaWiki:Foo.css', false ],
224 [ 'MediaWiki:Foo.JS', false ],
225 [ 'MediaWiki:Foo.JSON', false ],
226 [ 'MediaWiki:Foo.CSS', false ],
227 [ 'MediaWiki:Foo.css.xxx', false ],
228 [ 'TEST-JS:Foo', false ],
229 [ 'TEST-JS:Foo.js', false ],
234 * @dataProvider provideIsUserConfigPage
235 * @covers Title::isUserConfigPage
237 public function testIsUserConfigPage( $title, $expectedBool ) {
238 $title = Title
::newFromText( $title );
239 $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
242 public static function provideIsUserCssConfigPage() {
244 [ 'Help:Foo', false ],
245 [ 'Help:Foo.css', false ],
246 [ 'User:Foo', false ],
247 [ 'User:Foo.js', false ],
248 [ 'User:Foo.json', false ],
249 [ 'User:Foo.css', false ],
250 [ 'User:Foo/bar.js', false ],
251 [ 'User:Foo/bar.json', false ],
252 [ 'User:Foo/bar.css', true ],
257 * @dataProvider provideIsUserCssConfigPage
258 * @covers Title::isUserCssConfigPage
260 public function testIsUserCssConfigPage( $title, $expectedBool ) {
261 $title = Title
::newFromText( $title );
262 $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
265 public static function provideIsUserJsConfigPage() {
267 [ 'Help:Foo', false ],
268 [ 'Help:Foo.css', false ],
269 [ 'User:Foo', false ],
270 [ 'User:Foo.js', false ],
271 [ 'User:Foo.json', false ],
272 [ 'User:Foo.css', false ],
273 [ 'User:Foo/bar.js', true ],
274 [ 'User:Foo/bar.json', false ],
275 [ 'User:Foo/bar.css', false ],
280 * @dataProvider provideIsUserJsConfigPage
281 * @covers Title::isUserJsConfigPage
283 public function testIsUserJsConfigPage( $title, $expectedBool ) {
284 $title = Title
::newFromText( $title );
285 $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
288 public static function provideIsWikitextPage() {
290 [ 'Help:Foo', true ],
291 [ 'Help:Foo.js', true ],
292 [ 'Help:Foo/bar.js', true ],
293 [ 'User:Foo', true ],
294 [ 'User:Foo.js', true ],
295 [ 'User:Foo/bar.js', false ],
296 [ 'User:Foo/bar.json', false ],
297 [ 'User:Foo/bar.css', false ],
298 [ 'User talk:Foo/bar.css', true ],
299 [ 'User:Foo/bar.js.xxx', true ],
300 [ 'User:Foo/bar.xxx', true ],
301 [ 'MediaWiki:Foo.js', false ],
302 [ 'User:Foo/bar.JS', true ],
303 [ 'User:Foo/bar.JSON', true ],
304 [ 'User:Foo/bar.CSS', true ],
305 [ 'MediaWiki:Foo.json', false ],
306 [ 'MediaWiki:Foo.css', false ],
307 [ 'MediaWiki:Foo.JS', true ],
308 [ 'MediaWiki:Foo.JSON', true ],
309 [ 'MediaWiki:Foo.CSS', true ],
310 [ 'MediaWiki:Foo.css.xxx', true ],
311 [ 'TEST-JS:Foo', false ],
312 [ 'TEST-JS:Foo.js', false ],
317 * @dataProvider provideIsWikitextPage
318 * @covers Title::isWikitextPage
320 public function testIsWikitextPage( $title, $expectedBool ) {
321 $title = Title
::newFromText( $title );
322 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
325 public static function provideGetOtherPage() {
327 [ 'Main Page', 'Talk:Main Page' ],
328 [ 'Talk:Main Page', 'Main Page' ],
329 [ 'Help:Main Page', 'Help talk:Main Page' ],
330 [ 'Help talk:Main Page', 'Help:Main Page' ],
331 [ 'Special:FooBar', null ],
332 [ 'Media:File.jpg', null ],
337 * @dataProvider provideGetOtherpage
338 * @covers Title::getOtherPage
340 * @param string $text
341 * @param string|null $expected
343 public function testGetOtherPage( $text, $expected ) {
344 if ( $expected === null ) {
345 $this->setExpectedException( MWException
::class );
348 $title = Title
::newFromText( $text );
349 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
353 * @covers Title::clearCaches
355 public function testClearCaches() {
356 $linkCache = LinkCache
::singleton();
358 $title1 = Title
::newFromText( 'Foo' );
359 $linkCache->addGoodLinkObj( 23, $title1 );
361 Title
::clearCaches();
363 $title2 = Title
::newFromText( 'Foo' );
364 $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
365 $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );