Don't assume main namespace contains wikitext
[lhc/web/wiklou.git] / tests / phpunit / includes / TitleMethodsTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 *
6 * @note: We don't make assumptions about the main namespace.
7 * But we do expect the Help namespace to contain Wikitext.
8 *
9 */
10 class TitleMethodsTest extends MediaWikiTestCase {
11
12 public function setup() {
13 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContLang;
14
15 $wgExtraNamespaces[ 12302 ] = 'TEST-JS';
16 $wgExtraNamespaces[ 12303 ] = 'TEST-JS_TALK';
17
18 $wgNamespaceContentModels[ 12302 ] = CONTENT_MODEL_JAVASCRIPT;
19
20 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
21 $wgContLang->resetNamespaces(); # reset namespace cache
22 }
23
24 public function teardown() {
25 global $wgExtraNamespaces, $wgNamespaceContentModels, $wgContLang;
26
27 unset( $wgExtraNamespaces[ 12302 ] );
28 unset( $wgExtraNamespaces[ 12303 ] );
29
30 unset( $wgNamespaceContentModels[ 12302 ] );
31
32 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache
33 $wgContLang->resetNamespaces(); # reset namespace cache
34 }
35
36 public function dataEquals() {
37 return array(
38 array( 'Main Page', 'Main Page', true ),
39 array( 'Main Page', 'Not The Main Page', false ),
40 array( 'Main Page', 'Project:Main Page', false ),
41 array( 'File:Example.png', 'Image:Example.png', true ),
42 array( 'Special:Version', 'Special:Version', true ),
43 array( 'Special:Version', 'Special:Recentchanges', false ),
44 array( 'Special:Version', 'Main Page', false ),
45 );
46 }
47
48 /**
49 * @dataProvider dataEquals
50 */
51 public function testEquals( $titleA, $titleB, $expectedBool ) {
52 $titleA = Title::newFromText( $titleA );
53 $titleB = Title::newFromText( $titleB );
54
55 $this->assertEquals( $expectedBool, $titleA->equals( $titleB ) );
56 $this->assertEquals( $expectedBool, $titleB->equals( $titleA ) );
57 }
58
59 public function dataInNamespace() {
60 return array(
61 array( 'Main Page', NS_MAIN, true ),
62 array( 'Main Page', NS_TALK, false ),
63 array( 'Main Page', NS_USER, false ),
64 array( 'User:Foo', NS_USER, true ),
65 array( 'User:Foo', NS_USER_TALK, false ),
66 array( 'User:Foo', NS_TEMPLATE, false ),
67 array( 'User_talk:Foo', NS_USER_TALK, true ),
68 array( 'User_talk:Foo', NS_USER, false ),
69 );
70 }
71
72 /**
73 * @dataProvider dataInNamespace
74 */
75 public function testInNamespace( $title, $ns, $expectedBool ) {
76 $title = Title::newFromText( $title );
77 $this->assertEquals( $expectedBool, $title->inNamespace( $ns ) );
78 }
79
80 public function testInNamespaces() {
81 $mainpage = Title::newFromText( 'Main Page' );
82 $this->assertTrue( $mainpage->inNamespaces( NS_MAIN, NS_USER ) );
83 $this->assertTrue( $mainpage->inNamespaces( array( NS_MAIN, NS_USER ) ) );
84 $this->assertTrue( $mainpage->inNamespaces( array( NS_USER, NS_MAIN ) ) );
85 $this->assertFalse( $mainpage->inNamespaces( array( NS_PROJECT, NS_TEMPLATE ) ) );
86 }
87
88 public function dataHasSubjectNamespace() {
89 return array(
90 array( 'Main Page', NS_MAIN, true ),
91 array( 'Main Page', NS_TALK, true ),
92 array( 'Main Page', NS_USER, false ),
93 array( 'User:Foo', NS_USER, true ),
94 array( 'User:Foo', NS_USER_TALK, true ),
95 array( 'User:Foo', NS_TEMPLATE, false ),
96 array( 'User_talk:Foo', NS_USER_TALK, true ),
97 array( 'User_talk:Foo', NS_USER, true ),
98 );
99 }
100
101 /**
102 * @dataProvider dataHasSubjectNamespace
103 */
104 public function testHasSubjectNamespace( $title, $ns, $expectedBool ) {
105 $title = Title::newFromText( $title );
106 $this->assertEquals( $expectedBool, $title->hasSubjectNamespace( $ns ) );
107 }
108
109 public function dataGetContentModel() {
110 return array(
111 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT ),
112 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ),
113 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
114 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
115 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
116 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
117 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
118 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
119 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
120 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
121 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
122 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
123 array( 'MediaWiki:Foo/bar.css', CONTENT_MODEL_CSS ),
124 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
125 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
126 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
127 array( 'TEST-JS:Foo', CONTENT_MODEL_JAVASCRIPT ),
128 array( 'TEST-JS:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
129 array( 'TEST-JS:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
130 array( 'TEST-JS_TALK:Foo.js', CONTENT_MODEL_WIKITEXT ),
131 );
132 }
133
134 /**
135 * @dataProvider dataGetContentModel
136 */
137 public function testGetContentModel( $title, $expectedModelId ) {
138 $title = Title::newFromText( $title );
139 $this->assertEquals( $expectedModelId, $title->getContentModel() );
140 }
141
142 /**
143 * @dataProvider dataGetContentModel
144 */
145 public function testHasContentModel( $title, $expectedModelId ) {
146 $title = Title::newFromText( $title );
147 $this->assertTrue( $title->hasContentModel( $expectedModelId ) );
148 }
149
150 public function dataIsCssOrJsPage() {
151 return array(
152 array( 'Help:Foo', false ),
153 array( 'Help:Foo.js', false ),
154 array( 'Help:Foo/bar.js', false ),
155 array( 'User:Foo', false ),
156 array( 'User:Foo.js', false ),
157 array( 'User:Foo/bar.js', false ),
158 array( 'User:Foo/bar.css', false ),
159 array( 'User talk:Foo/bar.css', false ),
160 array( 'User:Foo/bar.js.xxx', false ),
161 array( 'User:Foo/bar.xxx', false ),
162 array( 'MediaWiki:Foo.js', true ),
163 array( 'MediaWiki:Foo.css', true ),
164 array( 'MediaWiki:Foo.JS', false ),
165 array( 'MediaWiki:Foo.CSS', false ),
166 array( 'MediaWiki:Foo.css.xxx', false ),
167 array( 'TEST-JS:Foo', false ),
168 array( 'TEST-JS:Foo.js', false ),
169 );
170 }
171
172 /**
173 * @dataProvider dataIsCssOrJsPage
174 */
175 public function testIsCssOrJsPage( $title, $expectedBool ) {
176 $title = Title::newFromText( $title );
177 $this->assertEquals( $expectedBool, $title->isCssOrJsPage() );
178 }
179
180
181 public function dataIsCssJsSubpage() {
182 return array(
183 array( 'Help:Foo', false ),
184 array( 'Help:Foo.js', false ),
185 array( 'Help:Foo/bar.js', false ),
186 array( 'User:Foo', false ),
187 array( 'User:Foo.js', false ),
188 array( 'User:Foo/bar.js', true ),
189 array( 'User:Foo/bar.css', true ),
190 array( 'User talk:Foo/bar.css', false ),
191 array( 'User:Foo/bar.js.xxx', false ),
192 array( 'User:Foo/bar.xxx', false ),
193 array( 'MediaWiki:Foo.js', false ),
194 array( 'User:Foo/bar.JS', false ),
195 array( 'User:Foo/bar.CSS', false ),
196 array( 'TEST-JS:Foo', false ),
197 array( 'TEST-JS:Foo.js', false ),
198 );
199 }
200
201 /**
202 * @dataProvider dataIsCssJsSubpage
203 */
204 public function testIsCssJsSubpage( $title, $expectedBool ) {
205 $title = Title::newFromText( $title );
206 $this->assertEquals( $expectedBool, $title->isCssJsSubpage() );
207 }
208
209 public function dataIsCssSubpage() {
210 return array(
211 array( 'Help:Foo', false ),
212 array( 'Help:Foo.css', false ),
213 array( 'User:Foo', false ),
214 array( 'User:Foo.js', false ),
215 array( 'User:Foo.css', false ),
216 array( 'User:Foo/bar.js', false ),
217 array( 'User:Foo/bar.css', true ),
218 );
219 }
220
221 /**
222 * @dataProvider dataIsCssSubpage
223 */
224 public function testIsCssSubpage( $title, $expectedBool ) {
225 $title = Title::newFromText( $title );
226 $this->assertEquals( $expectedBool, $title->isCssSubpage() );
227 }
228
229 public function dataIsJsSubpage() {
230 return array(
231 array( 'Help:Foo', false ),
232 array( 'Help:Foo.css', false ),
233 array( 'User:Foo', false ),
234 array( 'User:Foo.js', false ),
235 array( 'User:Foo.css', false ),
236 array( 'User:Foo/bar.js', true ),
237 array( 'User:Foo/bar.css', false ),
238 );
239 }
240
241 /**
242 * @dataProvider dataIsJsSubpage
243 */
244 public function testIsJsSubpage( $title, $expectedBool ) {
245 $title = Title::newFromText( $title );
246 $this->assertEquals( $expectedBool, $title->isJsSubpage() );
247 }
248
249 public function dataIsWikitextPage() {
250 return array(
251 array( 'Help:Foo', true ),
252 array( 'Help:Foo.js', true ),
253 array( 'Help:Foo/bar.js', true ),
254 array( 'User:Foo', true ),
255 array( 'User:Foo.js', true ),
256 array( 'User:Foo/bar.js', false ),
257 array( 'User:Foo/bar.css', false ),
258 array( 'User talk:Foo/bar.css', true ),
259 array( 'User:Foo/bar.js.xxx', true ),
260 array( 'User:Foo/bar.xxx', true ),
261 array( 'MediaWiki:Foo.js', false ),
262 array( 'MediaWiki:Foo.css', false ),
263 array( 'MediaWiki:Foo/bar.css', false ),
264 array( 'User:Foo/bar.JS', true ),
265 array( 'User:Foo/bar.CSS', true ),
266 array( 'TEST-JS:Foo', false ),
267 array( 'TEST-JS:Foo.js', false ),
268 array( 'TEST-JS_TALK:Foo.js', true ),
269 );
270 }
271
272 /**
273 * @dataProvider dataIsWikitextPage
274 */
275 public function testIsWikitextPage( $title, $expectedBool ) {
276 $title = Title::newFromText( $title );
277 $this->assertEquals( $expectedBool, $title->isWikitextPage() );
278 }
279
280 }