Merge ""spellcheck" attribute for HTMLForm "text" and "textarea""
[lhc/web/wiklou.git] / tests / phpunit / includes / content / ContentHandlerTest.php
1 <?php
2
3 /**
4 * @group ContentHandler
5 */
6 class ContentHandlerTest extends MediaWikiTestCase {
7
8 protected function setUp() {
9 global $wgContLang;
10 parent::setUp();
11
12 $this->setMwGlobals( array(
13 'wgExtraNamespaces' => array(
14 12312 => 'Dummy',
15 12313 => 'Dummy_talk',
16 ),
17 // The below tests assume that namespaces not mentioned here (Help, User, MediaWiki, ..)
18 // default to CONTENT_MODEL_WIKITEXT.
19 'wgNamespaceContentModels' => array(
20 12312 => 'testing',
21 ),
22 'wgContentHandlers' => array(
23 CONTENT_MODEL_WIKITEXT => 'WikitextContentHandler',
24 CONTENT_MODEL_JAVASCRIPT => 'JavaScriptContentHandler',
25 CONTENT_MODEL_JSON => 'JsonContentHandler',
26 CONTENT_MODEL_CSS => 'CssContentHandler',
27 CONTENT_MODEL_TEXT => 'TextContentHandler',
28 'testing' => 'DummyContentHandlerForTesting',
29 ),
30 ) );
31
32 // Reset namespace cache
33 MWNamespace::getCanonicalNamespaces( true );
34 $wgContLang->resetNamespaces();
35 // And LinkCache
36 LinkCache::destroySingleton();
37 }
38
39 protected function tearDown() {
40 global $wgContLang;
41
42 // Reset namespace cache
43 MWNamespace::getCanonicalNamespaces( true );
44 $wgContLang->resetNamespaces();
45 // And LinkCache
46 LinkCache::destroySingleton();
47
48 parent::tearDown();
49 }
50
51 public static function dataGetDefaultModelFor() {
52 return array(
53 array( 'Help:Foo', CONTENT_MODEL_WIKITEXT ),
54 array( 'Help:Foo.js', CONTENT_MODEL_WIKITEXT ),
55 array( 'Help:Foo.css', CONTENT_MODEL_WIKITEXT ),
56 array( 'Help:Foo.json', CONTENT_MODEL_WIKITEXT ),
57 array( 'Help:Foo/bar.js', CONTENT_MODEL_WIKITEXT ),
58 array( 'User:Foo', CONTENT_MODEL_WIKITEXT ),
59 array( 'User:Foo.js', CONTENT_MODEL_WIKITEXT ),
60 array( 'User:Foo.css', CONTENT_MODEL_WIKITEXT ),
61 array( 'User:Foo.json', CONTENT_MODEL_WIKITEXT ),
62 array( 'User:Foo/bar.js', CONTENT_MODEL_JAVASCRIPT ),
63 array( 'User:Foo/bar.css', CONTENT_MODEL_CSS ),
64 array( 'User:Foo/bar.json', CONTENT_MODEL_JSON ),
65 array( 'User:Foo/bar.json.nope', CONTENT_MODEL_WIKITEXT ),
66 array( 'User talk:Foo/bar.css', CONTENT_MODEL_WIKITEXT ),
67 array( 'User:Foo/bar.js.xxx', CONTENT_MODEL_WIKITEXT ),
68 array( 'User:Foo/bar.xxx', CONTENT_MODEL_WIKITEXT ),
69 array( 'MediaWiki:Foo.js', CONTENT_MODEL_JAVASCRIPT ),
70 array( 'MediaWiki:Foo.JS', CONTENT_MODEL_WIKITEXT ),
71 array( 'MediaWiki:Foo.css', CONTENT_MODEL_CSS ),
72 array( 'MediaWiki:Foo.css.xxx', CONTENT_MODEL_WIKITEXT ),
73 array( 'MediaWiki:Foo.CSS', CONTENT_MODEL_WIKITEXT ),
74 array( 'MediaWiki:Foo.json', CONTENT_MODEL_JSON ),
75 array( 'MediaWiki:Foo.JSON', CONTENT_MODEL_WIKITEXT ),
76 );
77 }
78
79 /**
80 * @dataProvider dataGetDefaultModelFor
81 * @covers ContentHandler::getDefaultModelFor
82 */
83 public function testGetDefaultModelFor( $title, $expectedModelId ) {
84 $title = Title::newFromText( $title );
85 $this->assertEquals( $expectedModelId, ContentHandler::getDefaultModelFor( $title ) );
86 }
87
88 /**
89 * @dataProvider dataGetDefaultModelFor
90 * @covers ContentHandler::getForTitle
91 */
92 public function testGetForTitle( $title, $expectedContentModel ) {
93 $title = Title::newFromText( $title );
94 LinkCache::singleton()->addBadLinkObj( $title );
95 $handler = ContentHandler::getForTitle( $title );
96 $this->assertEquals( $expectedContentModel, $handler->getModelID() );
97 }
98
99 public static function dataGetLocalizedName() {
100 return array(
101 array( null, null ),
102 array( "xyzzy", null ),
103
104 // XXX: depends on content language
105 array( CONTENT_MODEL_JAVASCRIPT, '/javascript/i' ),
106 );
107 }
108
109 /**
110 * @dataProvider dataGetLocalizedName
111 * @covers ContentHandler::getLocalizedName
112 */
113 public function testGetLocalizedName( $id, $expected ) {
114 $name = ContentHandler::getLocalizedName( $id );
115
116 if ( $expected ) {
117 $this->assertNotNull( $name, "no name found for content model $id" );
118 $this->assertTrue( preg_match( $expected, $name ) > 0,
119 "content model name for #$id did not match pattern $expected"
120 );
121 } else {
122 $this->assertEquals( $id, $name, "localization of unknown model $id should have "
123 . "fallen back to use the model id directly."
124 );
125 }
126 }
127
128 public static function dataGetPageLanguage() {
129 global $wgLanguageCode;
130
131 return array(
132 array( "Main", $wgLanguageCode ),
133 array( "Dummy:Foo", $wgLanguageCode ),
134 array( "MediaWiki:common.js", 'en' ),
135 array( "User:Foo/common.js", 'en' ),
136 array( "MediaWiki:common.css", 'en' ),
137 array( "User:Foo/common.css", 'en' ),
138 array( "User:Foo", $wgLanguageCode ),
139
140 array( CONTENT_MODEL_JAVASCRIPT, 'javascript' ),
141 );
142 }
143
144 /**
145 * @dataProvider dataGetPageLanguage
146 * @covers ContentHandler::getPageLanguage
147 */
148 public function testGetPageLanguage( $title, $expected ) {
149 if ( is_string( $title ) ) {
150 $title = Title::newFromText( $title );
151 LinkCache::singleton()->addBadLinkObj( $title );
152 }
153
154 $expected = wfGetLangObj( $expected );
155
156 $handler = ContentHandler::getForTitle( $title );
157 $lang = $handler->getPageLanguage( $title );
158
159 $this->assertEquals( $expected->getCode(), $lang->getCode() );
160 }
161
162 public static function dataGetContentText_Null() {
163 return array(
164 array( 'fail' ),
165 array( 'serialize' ),
166 array( 'ignore' ),
167 );
168 }
169
170 /**
171 * @dataProvider dataGetContentText_Null
172 * @covers ContentHandler::getContentText
173 */
174 public function testGetContentText_Null( $contentHandlerTextFallback ) {
175 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
176
177 $content = null;
178
179 $text = ContentHandler::getContentText( $content );
180 $this->assertEquals( '', $text );
181 }
182
183 public static function dataGetContentText_TextContent() {
184 return array(
185 array( 'fail' ),
186 array( 'serialize' ),
187 array( 'ignore' ),
188 );
189 }
190
191 /**
192 * @dataProvider dataGetContentText_TextContent
193 * @covers ContentHandler::getContentText
194 */
195 public function testGetContentText_TextContent( $contentHandlerTextFallback ) {
196 $this->setMwGlobals( 'wgContentHandlerTextFallback', $contentHandlerTextFallback );
197
198 $content = new WikitextContent( "hello world" );
199
200 $text = ContentHandler::getContentText( $content );
201 $this->assertEquals( $content->getNativeData(), $text );
202 }
203
204 /**
205 * ContentHandler::getContentText should have thrown an exception for non-text Content object
206 * @expectedException MWException
207 * @covers ContentHandler::getContentText
208 */
209 public function testGetContentText_NonTextContent_fail() {
210 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'fail' );
211
212 $content = new DummyContentForTesting( "hello world" );
213
214 ContentHandler::getContentText( $content );
215 }
216
217 /**
218 * @covers ContentHandler::getContentText
219 */
220 public function testGetContentText_NonTextContent_serialize() {
221 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'serialize' );
222
223 $content = new DummyContentForTesting( "hello world" );
224
225 $text = ContentHandler::getContentText( $content );
226 $this->assertEquals( $content->serialize(), $text );
227 }
228
229 /**
230 * @covers ContentHandler::getContentText
231 */
232 public function testGetContentText_NonTextContent_ignore() {
233 $this->setMwGlobals( 'wgContentHandlerTextFallback', 'ignore' );
234
235 $content = new DummyContentForTesting( "hello world" );
236
237 $text = ContentHandler::getContentText( $content );
238 $this->assertNull( $text );
239 }
240
241 /*
242 public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {}
243 */
244
245 public static function dataMakeContent() {
246 return array(
247 array( 'hallo', 'Help:Test', null, null, CONTENT_MODEL_WIKITEXT, 'hallo', false ),
248 array( 'hallo', 'MediaWiki:Test.js', null, null, CONTENT_MODEL_JAVASCRIPT, 'hallo', false ),
249 array( serialize( 'hallo' ), 'Dummy:Test', null, null, "testing", 'hallo', false ),
250
251 array(
252 'hallo',
253 'Help:Test',
254 null,
255 CONTENT_FORMAT_WIKITEXT,
256 CONTENT_MODEL_WIKITEXT,
257 'hallo',
258 false
259 ),
260 array(
261 'hallo',
262 'MediaWiki:Test.js',
263 null,
264 CONTENT_FORMAT_JAVASCRIPT,
265 CONTENT_MODEL_JAVASCRIPT,
266 'hallo',
267 false
268 ),
269 array( serialize( 'hallo' ), 'Dummy:Test', null, "testing", "testing", 'hallo', false ),
270
271 array( 'hallo', 'Help:Test', CONTENT_MODEL_CSS, null, CONTENT_MODEL_CSS, 'hallo', false ),
272 array(
273 'hallo',
274 'MediaWiki:Test.js',
275 CONTENT_MODEL_CSS,
276 null,
277 CONTENT_MODEL_CSS,
278 'hallo',
279 false
280 ),
281 array(
282 serialize( 'hallo' ),
283 'Dummy:Test',
284 CONTENT_MODEL_CSS,
285 null,
286 CONTENT_MODEL_CSS,
287 serialize( 'hallo' ),
288 false
289 ),
290
291 array( 'hallo', 'Help:Test', CONTENT_MODEL_WIKITEXT, "testing", null, null, true ),
292 array( 'hallo', 'MediaWiki:Test.js', CONTENT_MODEL_CSS, "testing", null, null, true ),
293 array( 'hallo', 'Dummy:Test', CONTENT_MODEL_JAVASCRIPT, "testing", null, null, true ),
294 );
295 }
296
297 /**
298 * @dataProvider dataMakeContent
299 * @covers ContentHandler::makeContent
300 */
301 public function testMakeContent( $data, $title, $modelId, $format,
302 $expectedModelId, $expectedNativeData, $shouldFail
303 ) {
304 $title = Title::newFromText( $title );
305 LinkCache::singleton()->addBadLinkObj( $title );
306 try {
307 $content = ContentHandler::makeContent( $data, $title, $modelId, $format );
308
309 if ( $shouldFail ) {
310 $this->fail( "ContentHandler::makeContent should have failed!" );
311 }
312
313 $this->assertEquals( $expectedModelId, $content->getModel(), 'bad model id' );
314 $this->assertEquals( $expectedNativeData, $content->getNativeData(), 'bads native data' );
315 } catch ( MWException $ex ) {
316 if ( !$shouldFail ) {
317 $this->fail( "ContentHandler::makeContent failed unexpectedly: " . $ex->getMessage() );
318 } else {
319 // dummy, so we don't get the "test did not perform any assertions" message.
320 $this->assertTrue( true );
321 }
322 }
323 }
324
325 /*
326 * Test if we become a "Created blank page" summary from getAutoSummary if no Content added to
327 * page.
328 */
329 public function testGetAutosummary() {
330 $this->setMwGlobals( 'wgContLang', Language::factory( 'en' ) );
331
332 $content = new DummyContentHandlerForTesting( CONTENT_MODEL_WIKITEXT );
333 $title = Title::newFromText( 'Help:Test' );
334 // Create a new content object with no content
335 $newContent = ContentHandler::makeContent( '', $title, null, null, CONTENT_MODEL_WIKITEXT );
336 // first check, if we become a blank page created summary with the right bitmask
337 $autoSummary = $content->getAutosummary( null, $newContent, 97 );
338 $this->assertEquals( $autoSummary, 'Created blank page' );
339 // now check, what we become with another bitmask
340 $autoSummary = $content->getAutosummary( null, $newContent, 92 );
341 $this->assertEquals( $autoSummary, '' );
342 }
343
344 /*
345 public function testSupportsSections() {
346 $this->markTestIncomplete( "not yet implemented" );
347 }
348 */
349
350 public function testSupportsDirectEditing() {
351 $handler = new DummyContentHandlerForTesting( CONTENT_MODEL_JSON );
352 $this->assertFalse( $handler->supportsDirectEditing(), 'direct editing is not supported' );
353 }
354
355 /**
356 * @covers ContentHandler::runLegacyHooks
357 */
358 public function testRunLegacyHooks() {
359 Hooks::register( 'testRunLegacyHooks', __CLASS__ . '::dummyHookHandler' );
360
361 $content = new WikitextContent( 'test text' );
362 $ok = ContentHandler::runLegacyHooks(
363 'testRunLegacyHooks',
364 array( 'foo', &$content, 'bar' ),
365 false
366 );
367
368 $this->assertTrue( $ok, "runLegacyHooks should have returned true" );
369 $this->assertEquals( "TEST TEXT", $content->getNativeData() );
370 }
371
372 public static function dummyHookHandler( $foo, &$text, $bar ) {
373 if ( $text === null || $text === false ) {
374 return false;
375 }
376
377 $text = strtoupper( $text );
378
379 return true;
380 }
381 }