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