4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class WikitextContentTest
extends MediaWikiTestCase
{
11 public function setup() {
16 $wgUser->setName( '127.0.0.1' );
18 $this->context
= new RequestContext( new FauxRequest() );
19 $this->context
->setTitle( Title
::newFromText( "Test" ) );
20 $this->context
->setUser( $wgUser );
23 public function newContent( $text ) {
24 return new WikitextContent( $text );
28 public function dataGetParserOutput() {
30 array("WikitextContentTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
36 * @dataProvider dataGetParserOutput
38 public function testGetParserOutput( $title, $text, $expectedHtml ) {
39 $title = Title
::newFromText( $title );
40 $content = ContentHandler
::makeContent( $text, $title );
42 $po = $content->getParserOutput( $title );
44 $this->assertEquals( $expectedHtml, $po->getText() );
45 // @todo: assert more properties
48 public function dataGetSecondaryDataUpdates() {
50 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
51 array( 'LinksUpdate' => array( 'mRecursive' => true,
52 'mLinks' => array() ) )
54 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
55 array( 'LinksUpdate' => array( 'mRecursive' => true,
56 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
63 * @dataProvider dataGetSecondaryDataUpdates
66 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
67 $title = Title
::newFromText( $title );
68 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
70 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
71 $content = ContentHandler
::makeContent( $text, $title );
73 $updates = $content->getSecondaryDataUpdates( $title );
75 // make updates accessible by class name
76 foreach ( $updates as $update ) {
77 $class = get_class( $update );
78 $updates[ $class ] = $update;
81 foreach ( $expectedStuff as $class => $fieldValues ) {
82 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
84 $update = $updates[ $class ];
86 foreach ( $fieldValues as $field => $value ) {
87 $v = $update->$field; #if the field doesn't exist, just crash and burn
88 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
108 public function dataGetSection() {
110 array( WikitextContentTest
::$sections,
114 array( WikitextContentTest
::$sections,
119 array( WikitextContentTest
::$sections,
127 * @dataProvider dataGetSection
129 public function testGetSection( $text, $sectionId, $expectedText ) {
130 $content = $this->newContent( $text );
132 $sectionContent = $content->getSection( $sectionId );
134 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
137 public function dataReplaceSection() {
139 array( WikitextContentTest
::$sections,
143 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
145 array( WikitextContentTest
::$sections,
151 array( WikitextContentTest
::$sections,
153 "== TEST ==\nmore fun",
155 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
157 array( WikitextContentTest
::$sections,
161 WikitextContentTest
::$sections
163 array( WikitextContentTest
::$sections,
167 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
173 * @dataProvider dataReplaceSection
175 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
176 $content = $this->newContent( $text );
177 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
179 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
182 public function testAddSectionHeader( ) {
183 $content = $this->newContent( 'hello world' );
184 $content = $content->addSectionHeader( 'test' );
186 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
189 public function dataPreSaveTransform() {
191 array( 'hello this is ~~~',
192 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
194 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
195 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
201 * @dataProvider dataPreSaveTransform
203 public function testPreSaveTransform( $text, $expected ) {
206 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
208 $content = $this->newContent( $text );
209 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
211 $this->assertEquals( $expected, $content->getNativeData() );
214 public function dataPreloadTransform() {
216 array( 'hello this is ~~~',
219 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
220 'hello \'\'this\'\' is bar',
226 * @dataProvider dataPreloadTransform
228 public function testPreloadTransform( $text, $expected ) {
230 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
232 $content = $this->newContent( $text );
233 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
235 $this->assertEquals( $expected, $content->getNativeData() );
238 public function dataGetRedirectTarget() {
240 array( '#REDIRECT [[Test]]',
243 array( '#REDIRECT Test',
246 array( '* #REDIRECT [[Test]]',
253 * @dataProvider dataGetRedirectTarget
255 public function testGetRedirectTarget( $text, $expected ) {
256 $content = $this->newContent( $text );
257 $t = $content->getRedirectTarget( );
259 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
260 else $this->assertEquals( $expected, $t->getPrefixedText() );
264 * @dataProvider dataGetRedirectTarget
266 public function isRedirect( $text, $expected ) {
267 $content = $this->newContent( $text );
269 $this->assertEquals( !is_null($expected), $content->isRedirect() );
274 * @todo: test needs database!
277 public function getRedirectChain() {
278 $text = $this->getNativeData();
279 return Title::newFromRedirectArray( $text );
284 * @todo: test needs database!
287 public function getUltimateRedirectTarget() {
288 $text = $this->getNativeData();
289 return Title::newFromRedirectRecurse( $text );
294 public function dataIsCountable() {
321 array( 'Foo [[bar]]',
331 array( 'Foo [[bar]]',
336 array( '#REDIRECT [[bar]]',
341 array( '#REDIRECT [[bar]]',
346 array( '#REDIRECT [[bar]]',
356 * @dataProvider dataIsCountable
359 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
360 global $wgArticleCountMethod;
362 $old = $wgArticleCountMethod;
363 $wgArticleCountMethod = $mode;
365 $content = $this->newContent( $text );
367 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
368 $wgArticleCountMethod = $old;
370 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
371 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
374 public function dataGetTextForSummary() {
376 array( "hello\nworld.",
380 array( 'hello world.',
384 array( '[[hello world]].',
392 * @dataProvider dataGetTextForSummary
394 public function testGetTextForSummary( $text, $maxlength, $expected ) {
395 $content = $this->newContent( $text );
397 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
401 public function testGetTextForSearchIndex( ) {
402 $content = $this->newContent( "hello world." );
404 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
407 public function testCopy() {
408 $content = $this->newContent( "hello world." );
409 $copy = $content->copy();
411 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
412 $this->assertEquals( "hello world.", $copy->getNativeData() );
415 public function testGetSize( ) {
416 $content = $this->newContent( "hello world." );
418 $this->assertEquals( 12, $content->getSize() );
421 public function testGetNativeData( ) {
422 $content = $this->newContent( "hello world." );
424 $this->assertEquals( "hello world.", $content->getNativeData() );
427 public function testGetWikitextForTransclusion( ) {
428 $content = $this->newContent( "hello world." );
430 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
433 public function testMatchMagicWord( ) {
434 $mw = MagicWord
::get( "staticredirect" );
436 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
437 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
439 $content = $this->newContent( "#REDIRECT [[FOO]]" );
440 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
443 public function testUpdateRedirect( ) {
444 $target = Title
::newFromText( "testUpdateRedirect_target" );
446 // test with non-redirect page
447 $content = $this->newContent( "hello world." );
448 $newContent = $content->updateRedirect( $target );
450 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
452 // test with actual redirect
453 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
454 $newContent = $content->updateRedirect( $target );
456 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
457 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
459 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
462 # =================================================================================================================
464 public function testGetModel() {
465 $content = $this->newContent( "hello world." );
467 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
470 public function testGetContentHandler() {
471 $content = $this->newContent( "hello world." );
473 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
476 public function dataIsEmpty( ) {
481 array( 'hallo welt.', false ),
486 * @dataProvider dataIsEmpty
488 public function testIsEmpty( $text, $empty ) {
489 $content = $this->newContent( $text );
491 $this->assertEquals( $empty, $content->isEmpty() );
494 public function dataEquals( ) {
496 array( new WikitextContent( "hallo" ), null, false ),
497 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
498 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
499 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
504 * @dataProvider dataEquals
506 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
507 $this->assertEquals( $equal, $a->equals( $b ) );
510 public function dataGetDeletionUpdates() {
512 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
513 array( 'LinksDeletionUpdate' => array( ) )
515 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
516 array( 'LinksDeletionUpdate' => array( ) )
523 * @dataProvider dataGetDeletionUpdates
525 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
526 $title = Title
::newFromText( $title );
527 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
529 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
530 $content = ContentHandler
::makeContent( $text, $title );
532 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
534 // make updates accessible by class name
535 foreach ( $updates as $update ) {
536 $class = get_class( $update );
537 $updates[ $class ] = $update;
540 foreach ( $expectedStuff as $class => $fieldValues ) {
541 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
543 $update = $updates[ $class ];
545 foreach ( $fieldValues as $field => $value ) {
546 $v = $update->$field; #if the field doesn't exist, just crash and burn
547 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );