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() {
17 $this->mergeMwGlobalArrayValue(
21 12303 => 'TEST-JS_TALK',
25 $this->mergeMwGlobalArrayValue(
26 'wgNamespaceContentModels',
28 12302 => CONTENT_MODEL_JAVASCRIPT
,
33 public static function provideEquals() {
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 ],
46 * @dataProvider provideEquals
47 * @covers Title::equals
49 public function testEquals( $titleA, $titleB, $expectedBool ) {
50 $titleA = Title
::newFromText( $titleA );
51 $titleB = Title
::newFromText( $titleB );
53 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
54 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
57 public static function provideInNamespace() {
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 ],
71 * @dataProvider provideInNamespace
72 * @covers Title::inNamespace
74 public function testInNamespace( $title, $ns, $expectedBool ) {
75 $title = Title
::newFromText( $title );
76 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
80 * @covers Title::inNamespaces
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
] ) );
90 public static function provideHasSubjectNamespace() {
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 ],
104 * @dataProvider provideHasSubjectNamespace
105 * @covers Title::hasSubjectNamespace
107 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
108 $title = Title
::newFromText( $title );
109 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
112 public function dataGetContentModel() {
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
],
138 * @dataProvider dataGetContentModel
139 * @covers Title::getContentModel
141 public function testGetContentModel( $title, $expectedModelId ) {
142 $title = Title
::newFromText( $title );
143 $this->assertEquals( $expectedModelId, $title->getContentModel() );
147 * @dataProvider dataGetContentModel
148 * @covers Title::hasContentModel
150 public function testHasContentModel( $title, $expectedModelId ) {
151 $title = Title
::newFromText( $title );
152 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
155 public static function provideIsSiteConfigPage() {
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 ],
185 * @dataProvider provideIsSiteConfigPage
186 * @covers Title::isSiteConfigPage
188 public function testSiteConfigPage( $title, $expectedBool ) {
189 $title = Title
::newFromText( $title );
190 $this->assertEquals( $expectedBool, $title->isSiteConfigPage() );
193 public static function provideIsUserConfigPage() {
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 ],
222 * @dataProvider provideIsUserConfigPage
223 * @covers Title::isUserConfigPage
225 public function testIsUserConfigPage( $title, $expectedBool ) {
226 $title = Title
::newFromText( $title );
227 $this->assertEquals( $expectedBool, $title->isUserConfigPage() );
230 public static function provideIsUserCssConfigPage() {
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 ],
245 * @dataProvider provideIsUserCssConfigPage
246 * @covers Title::isUserCssConfigPage
248 public function testIsUserCssConfigPage( $title, $expectedBool ) {
249 $title = Title
::newFromText( $title );
250 $this->assertEquals( $expectedBool, $title->isUserCssConfigPage() );
253 public static function provideIsUserJsConfigPage() {
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 ],
268 * @dataProvider provideIsUserJsConfigPage
269 * @covers Title::isUserJsConfigPage
271 public function testIsUserJsConfigPage( $title, $expectedBool ) {
272 $title = Title
::newFromText( $title );
273 $this->assertEquals( $expectedBool, $title->isUserJsConfigPage() );
276 public static function provideIsWikitextPage() {
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 ],
305 * @dataProvider provideIsWikitextPage
306 * @covers Title::isWikitextPage
308 public function testIsWikitextPage( $title, $expectedBool ) {
309 $title = Title
::newFromText( $title );
310 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
313 public static function provideGetOtherPage() {
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 ],
325 * @dataProvider provideGetOtherpage
326 * @covers Title::getOtherPage
328 * @param string $text
329 * @param string|null $expected
331 public function testGetOtherPage( $text, $expected ) {
332 if ( $expected === null ) {
333 $this->setExpectedException( MWException
::class );
336 $title = Title
::newFromText( $text );
337 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
341 * @covers Title::clearCaches
343 public function testClearCaches() {
344 $linkCache = MediaWikiServices
::getInstance()->getLinkCache();
346 $title1 = Title
::newFromText( 'Foo' );
347 $linkCache->addGoodLinkObj( 23, $title1 );
349 Title
::clearCaches();
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' );
356 function tearDown() {
357 Title
::clearCaches();