]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - tests/phpunit/includes/TitleMethodsTest.php
MediaWiki 1.30.2-scripts
[autoinstallsdev/mediawiki.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::getCanonicalNamespaces( true ); # reset namespace cache
33                 $wgContLang->resetNamespaces(); # reset namespace cache
34         }
35
36         protected function tearDown() {
37                 global $wgContLang;
38
39                 parent::tearDown();
40
41                 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
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 provideIsCssOrJsPage() {
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 talk:Foo/bar.css', false ],
177                         [ 'User:Foo/bar.js.xxx', false ],
178                         [ 'User:Foo/bar.xxx', false ],
179                         [ 'MediaWiki:Foo.js', true ],
180                         [ 'MediaWiki:Foo.css', true ],
181                         [ 'MediaWiki:Foo.JS', false ],
182                         [ 'MediaWiki:Foo.CSS', false ],
183                         [ 'MediaWiki:Foo.css.xxx', false ],
184                         [ 'TEST-JS:Foo', false ],
185                         [ 'TEST-JS:Foo.js', false ],
186                 ];
187         }
188
189         /**
190          * @dataProvider provideIsCssOrJsPage
191          * @covers Title::isCssOrJsPage
192          */
193         public function testIsCssOrJsPage( $title, $expectedBool ) {
194                 $title = Title::newFromText( $title );
195                 $this->assertEquals( $expectedBool, $title->isCssOrJsPage() );
196         }
197
198         public static function provideIsCssJsSubpage() {
199                 return [
200                         [ 'Help:Foo', false ],
201                         [ 'Help:Foo.js', false ],
202                         [ 'Help:Foo/bar.js', false ],
203                         [ 'User:Foo', false ],
204                         [ 'User:Foo.js', false ],
205                         [ 'User:Foo/bar.js', true ],
206                         [ 'User:Foo/bar.css', true ],
207                         [ 'User talk:Foo/bar.css', false ],
208                         [ 'User:Foo/bar.js.xxx', false ],
209                         [ 'User:Foo/bar.xxx', false ],
210                         [ 'MediaWiki:Foo.js', false ],
211                         [ 'User:Foo/bar.JS', false ],
212                         [ 'User:Foo/bar.CSS', false ],
213                         [ 'TEST-JS:Foo', false ],
214                         [ 'TEST-JS:Foo.js', false ],
215                 ];
216         }
217
218         /**
219          * @dataProvider provideIsCssJsSubpage
220          * @covers Title::isCssJsSubpage
221          */
222         public function testIsCssJsSubpage( $title, $expectedBool ) {
223                 $title = Title::newFromText( $title );
224                 $this->assertEquals( $expectedBool, $title->isCssJsSubpage() );
225         }
226
227         public static function provideIsCssSubpage() {
228                 return [
229                         [ 'Help:Foo', false ],
230                         [ 'Help:Foo.css', false ],
231                         [ 'User:Foo', false ],
232                         [ 'User:Foo.js', false ],
233                         [ 'User:Foo.css', false ],
234                         [ 'User:Foo/bar.js', false ],
235                         [ 'User:Foo/bar.css', true ],
236                 ];
237         }
238
239         /**
240          * @dataProvider provideIsCssSubpage
241          * @covers Title::isCssSubpage
242          */
243         public function testIsCssSubpage( $title, $expectedBool ) {
244                 $title = Title::newFromText( $title );
245                 $this->assertEquals( $expectedBool, $title->isCssSubpage() );
246         }
247
248         public static function provideIsJsSubpage() {
249                 return [
250                         [ 'Help:Foo', false ],
251                         [ 'Help:Foo.css', false ],
252                         [ 'User:Foo', false ],
253                         [ 'User:Foo.js', false ],
254                         [ 'User:Foo.css', false ],
255                         [ 'User:Foo/bar.js', true ],
256                         [ 'User:Foo/bar.css', false ],
257                 ];
258         }
259
260         /**
261          * @dataProvider provideIsJsSubpage
262          * @covers Title::isJsSubpage
263          */
264         public function testIsJsSubpage( $title, $expectedBool ) {
265                 $title = Title::newFromText( $title );
266                 $this->assertEquals( $expectedBool, $title->isJsSubpage() );
267         }
268
269         public static function provideIsWikitextPage() {
270                 return [
271                         [ 'Help:Foo', true ],
272                         [ 'Help:Foo.js', true ],
273                         [ 'Help:Foo/bar.js', true ],
274                         [ 'User:Foo', true ],
275                         [ 'User:Foo.js', true ],
276                         [ 'User:Foo/bar.js', false ],
277                         [ 'User:Foo/bar.css', false ],
278                         [ 'User talk:Foo/bar.css', true ],
279                         [ 'User:Foo/bar.js.xxx', true ],
280                         [ 'User:Foo/bar.xxx', true ],
281                         [ 'MediaWiki:Foo.js', false ],
282                         [ 'MediaWiki:Foo.css', false ],
283                         [ 'MediaWiki:Foo/bar.css', false ],
284                         [ 'User:Foo/bar.JS', true ],
285                         [ 'User:Foo/bar.CSS', true ],
286                         [ 'TEST-JS:Foo', false ],
287                         [ 'TEST-JS:Foo.js', false ],
288                         [ 'TEST-JS_TALK:Foo.js', true ],
289                 ];
290         }
291
292         /**
293          * @dataProvider provideIsWikitextPage
294          * @covers Title::isWikitextPage
295          */
296         public function testIsWikitextPage( $title, $expectedBool ) {
297                 $title = Title::newFromText( $title );
298                 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
299         }
300
301         public static function provideGetOtherPage() {
302                 return [
303                         [ 'Main Page', 'Talk:Main Page' ],
304                         [ 'Talk:Main Page', 'Main Page' ],
305                         [ 'Help:Main Page', 'Help talk:Main Page' ],
306                         [ 'Help talk:Main Page', 'Help:Main Page' ],
307                         [ 'Special:FooBar', null ],
308                         [ 'Media:File.jpg', null ],
309                 ];
310         }
311
312         /**
313          * @dataProvider provideGetOtherpage
314          * @covers Title::getOtherPage
315          *
316          * @param string $text
317          * @param string|null $expected
318          */
319         public function testGetOtherPage( $text, $expected ) {
320                 if ( $expected === null ) {
321                         $this->setExpectedException( 'MWException' );
322                 }
323
324                 $title = Title::newFromText( $text );
325                 $this->assertEquals( $expected, $title->getOtherPage()->getPrefixedText() );
326         }
327
328         public function testClearCaches() {
329                 $linkCache = LinkCache::singleton();
330
331                 $title1 = Title::newFromText( 'Foo' );
332                 $linkCache->addGoodLinkObj( 23, $title1 );
333
334                 Title::clearCaches();
335
336                 $title2 = Title::newFromText( 'Foo' );
337                 $this->assertNotSame( $title1, $title2, 'title cache should be empty' );
338                 $this->assertEquals( 0, $linkCache->getGoodLinkID( 'Foo' ), 'link cache should be empty' );
339         }
340 }