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