fd1f886ff742299a8ad11d0595be5f4a647512dc
4 * @group ContentHandler
6 class WikitextContentTest
extends MediaWikiTestCase
{
8 public function setup() {
13 $wgUser->setName( '127.0.0.1' );
15 $this->context
= new RequestContext( new FauxRequest() );
16 $this->context
->setTitle( Title
::newFromText( "Test" ) );
17 $this->context
->setUser( $wgUser );
20 public function newContent( $text ) {
21 return new WikitextContent( $text );
25 public function dataGetParserOutput() {
27 array("WikitextContentTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
33 * @dataProvider dataGetParserOutput
35 public function testGetParserOutput( $title, $text, $expectedHtml ) {
36 $title = Title
::newFromText( $title );
37 $content = ContentHandler
::makeContent( $text, $title );
39 $po = $content->getParserOutput( $title );
41 $this->assertEquals( $expectedHtml, $po->getText() );
42 // @todo: assert more properties
45 public function dataGetSecondaryDataUpdates() {
47 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
48 array( 'LinksUpdate' => array( 'mRecursive' => true,
49 'mLinks' => array() ) )
51 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
52 array( 'LinksUpdate' => array( 'mRecursive' => true,
53 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
60 * @dataProvider dataGetSecondaryDataUpdates
63 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
64 $title = Title
::newFromText( $title );
65 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
67 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
68 $content = ContentHandler
::makeContent( $text, $title );
70 $updates = $content->getSecondaryDataUpdates( $title );
72 // make updates accessible by class name
73 foreach ( $updates as $update ) {
74 $class = get_class( $update );
75 $updates[ $class ] = $update;
78 foreach ( $expectedStuff as $class => $fieldValues ) {
79 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
81 $update = $updates[ $class ];
83 foreach ( $fieldValues as $field => $value ) {
84 $v = $update->$field; #if the field doesn't exist, just crash and burn
85 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
105 public function dataGetSection() {
107 array( WikitextContentTest
::$sections,
111 array( WikitextContentTest
::$sections,
116 array( WikitextContentTest
::$sections,
124 * @dataProvider dataGetSection
126 public function testGetSection( $text, $sectionId, $expectedText ) {
127 $content = $this->newContent( $text );
129 $sectionContent = $content->getSection( $sectionId );
131 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
134 public function dataReplaceSection() {
136 array( WikitextContentTest
::$sections,
140 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
142 array( WikitextContentTest
::$sections,
148 array( WikitextContentTest
::$sections,
150 "== TEST ==\nmore fun",
152 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
154 array( WikitextContentTest
::$sections,
158 WikitextContentTest
::$sections
160 array( WikitextContentTest
::$sections,
164 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
170 * @dataProvider dataReplaceSection
172 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
173 $content = $this->newContent( $text );
174 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
176 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
179 public function testAddSectionHeader( ) {
180 $content = $this->newContent( 'hello world' );
181 $content = $content->addSectionHeader( 'test' );
183 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
186 public function dataPreSaveTransform() {
188 array( 'hello this is ~~~',
189 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
191 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
192 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
198 * @dataProvider dataPreSaveTransform
200 public function testPreSaveTransform( $text, $expected ) {
203 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
205 $content = $this->newContent( $text );
206 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
208 $this->assertEquals( $expected, $content->getNativeData() );
211 public function dataPreloadTransform() {
213 array( 'hello this is ~~~',
216 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
217 'hello \'\'this\'\' is bar',
223 * @dataProvider dataPreloadTransform
225 public function testPreloadTransform( $text, $expected ) {
227 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
229 $content = $this->newContent( $text );
230 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
232 $this->assertEquals( $expected, $content->getNativeData() );
235 public function dataGetRedirectTarget() {
237 array( '#REDIRECT [[Test]]',
240 array( '#REDIRECT Test',
243 array( '* #REDIRECT [[Test]]',
250 * @dataProvider dataGetRedirectTarget
252 public function testGetRedirectTarget( $text, $expected ) {
253 $content = $this->newContent( $text );
254 $t = $content->getRedirectTarget( );
256 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
257 else $this->assertEquals( $expected, $t->getPrefixedText() );
261 * @dataProvider dataGetRedirectTarget
263 public function isRedirect( $text, $expected ) {
264 $content = $this->newContent( $text );
266 $this->assertEquals( !is_null($expected), $content->isRedirect() );
271 * @todo: test needs database!
274 public function getRedirectChain() {
275 $text = $this->getNativeData();
276 return Title::newFromRedirectArray( $text );
281 * @todo: test needs database!
284 public function getUltimateRedirectTarget() {
285 $text = $this->getNativeData();
286 return Title::newFromRedirectRecurse( $text );
291 public function dataIsCountable() {
318 array( 'Foo [[bar]]',
328 array( 'Foo [[bar]]',
333 array( '#REDIRECT [[bar]]',
338 array( '#REDIRECT [[bar]]',
343 array( '#REDIRECT [[bar]]',
353 * @dataProvider dataIsCountable
356 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
357 global $wgArticleCountMethod;
359 $old = $wgArticleCountMethod;
360 $wgArticleCountMethod = $mode;
362 $content = $this->newContent( $text );
364 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
365 $wgArticleCountMethod = $old;
367 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
368 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
371 public function dataGetTextForSummary() {
373 array( "hello\nworld.",
377 array( 'hello world.',
381 array( '[[hello world]].',
389 * @dataProvider dataGetTextForSummary
391 public function testGetTextForSummary( $text, $maxlength, $expected ) {
392 $content = $this->newContent( $text );
394 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
398 public function testGetTextForSearchIndex( ) {
399 $content = $this->newContent( "hello world." );
401 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
404 public function testCopy() {
405 $content = $this->newContent( "hello world." );
406 $copy = $content->copy();
408 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
409 $this->assertEquals( "hello world.", $copy->getNativeData() );
412 public function testGetSize( ) {
413 $content = $this->newContent( "hello world." );
415 $this->assertEquals( 12, $content->getSize() );
418 public function testGetNativeData( ) {
419 $content = $this->newContent( "hello world." );
421 $this->assertEquals( "hello world.", $content->getNativeData() );
424 public function testGetWikitextForTransclusion( ) {
425 $content = $this->newContent( "hello world." );
427 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
430 public function testMatchMagicWord( ) {
431 $mw = MagicWord
::get( "staticredirect" );
433 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
434 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
436 $content = $this->newContent( "#REDIRECT [[FOO]]" );
437 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
440 public function testUpdateRedirect( ) {
441 $target = Title
::newFromText( "testUpdateRedirect_target" );
443 // test with non-redirect page
444 $content = $this->newContent( "hello world." );
445 $newContent = $content->updateRedirect( $target );
447 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
449 // test with actual redirect
450 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
451 $newContent = $content->updateRedirect( $target );
453 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
454 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
456 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
459 # =================================================================================================================
461 public function testGetModel() {
462 $content = $this->newContent( "hello world." );
464 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
467 public function testGetContentHandler() {
468 $content = $this->newContent( "hello world." );
470 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
473 public function dataIsEmpty( ) {
478 array( 'hallo welt.', false ),
483 * @dataProvider dataIsEmpty
485 public function testIsEmpty( $text, $empty ) {
486 $content = $this->newContent( $text );
488 $this->assertEquals( $empty, $content->isEmpty() );
491 public function dataEquals( ) {
493 array( new WikitextContent( "hallo" ), null, false ),
494 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
495 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
496 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
501 * @dataProvider dataEquals
503 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
504 $this->assertEquals( $equal, $a->equals( $b ) );
507 public function dataGetDeletionUpdates() {
509 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
510 array( 'LinksDeletionUpdate' => array( ) )
512 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
513 array( 'LinksDeletionUpdate' => array( ) )
520 * @dataProvider dataGetDeletionUpdates
522 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
523 $title = Title
::newFromText( $title );
524 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
526 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
527 $content = ContentHandler
::makeContent( $text, $title );
529 $updates = $content->getDeletionUpdates( $title );
531 // make updates accessible by class name
532 foreach ( $updates as $update ) {
533 $class = get_class( $update );
534 $updates[ $class ] = $update;
537 foreach ( $expectedStuff as $class => $fieldValues ) {
538 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
540 $update = $updates[ $class ];
542 foreach ( $fieldValues as $field => $value ) {
543 $v = $update->$field; #if the field doesn't exist, just crash and burn
544 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );