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", CONTENT_MODEL_WIKITEXT
, "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
36 * @dataProvider dataGetParserOutput
38 public function testGetParserOutput( $title, $model, $text, $expectedHtml ) {
39 $title = Title
::newFromText( $title );
40 $content = ContentHandler
::makeContent( $text, $title, $model );
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",
51 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
52 array( 'LinksUpdate' => array( 'mRecursive' => true,
53 'mLinks' => array() ) )
55 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
56 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
57 array( 'LinksUpdate' => array( 'mRecursive' => true,
58 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
65 * @dataProvider dataGetSecondaryDataUpdates
68 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
69 $title = Title
::newFromText( $title );
70 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
72 $content = ContentHandler
::makeContent( $text, $title, $model );
74 $updates = $content->getSecondaryDataUpdates( $title );
76 // make updates accessible by class name
77 foreach ( $updates as $update ) {
78 $class = get_class( $update );
79 $updates[$class] = $update;
82 foreach ( $expectedStuff as $class => $fieldValues ) {
83 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
85 $update = $updates[$class];
87 foreach ( $fieldValues as $field => $value ) {
88 $v = $update->$field; #if the field doesn't exist, just crash and burn
89 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
109 public function dataGetSection() {
111 array( WikitextContentTest
::$sections,
115 array( WikitextContentTest
::$sections,
120 array( WikitextContentTest
::$sections,
128 * @dataProvider dataGetSection
130 public function testGetSection( $text, $sectionId, $expectedText ) {
131 $content = $this->newContent( $text );
133 $sectionContent = $content->getSection( $sectionId );
135 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
138 public function dataReplaceSection() {
140 array( WikitextContentTest
::$sections,
144 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
146 array( WikitextContentTest
::$sections,
152 array( WikitextContentTest
::$sections,
154 "== TEST ==\nmore fun",
156 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
158 array( WikitextContentTest
::$sections,
162 WikitextContentTest
::$sections
164 array( WikitextContentTest
::$sections,
168 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
174 * @dataProvider dataReplaceSection
176 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
177 $content = $this->newContent( $text );
178 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
180 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
183 public function testAddSectionHeader( ) {
184 $content = $this->newContent( 'hello world' );
185 $content = $content->addSectionHeader( 'test' );
187 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
190 public function dataPreSaveTransform() {
192 array( 'hello this is ~~~',
193 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
195 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
196 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
202 * @dataProvider dataPreSaveTransform
204 public function testPreSaveTransform( $text, $expected ) {
207 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
209 $content = $this->newContent( $text );
210 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
212 $this->assertEquals( $expected, $content->getNativeData() );
215 public function dataPreloadTransform() {
217 array( 'hello this is ~~~',
220 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
221 'hello \'\'this\'\' is bar',
227 * @dataProvider dataPreloadTransform
229 public function testPreloadTransform( $text, $expected ) {
231 $options = ParserOptions
::newFromUserAndLang( $this->context
->getUser(), $wgContLang );
233 $content = $this->newContent( $text );
234 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
236 $this->assertEquals( $expected, $content->getNativeData() );
239 public function dataGetRedirectTarget() {
241 array( '#REDIRECT [[Test]]',
244 array( '#REDIRECT Test',
247 array( '* #REDIRECT [[Test]]',
254 * @dataProvider dataGetRedirectTarget
256 public function testGetRedirectTarget( $text, $expected ) {
257 $content = $this->newContent( $text );
258 $t = $content->getRedirectTarget( );
260 if ( is_null( $expected ) ) {
261 $this->assertNull( $t, "text should not have generated a redirect target: $text" );
263 $this->assertEquals( $expected, $t->getPrefixedText() );
268 * @dataProvider dataGetRedirectTarget
270 public function isRedirect( $text, $expected ) {
271 $content = $this->newContent( $text );
273 $this->assertEquals( !is_null($expected), $content->isRedirect() );
278 * @todo: test needs database! Should be done by a test class in the Database group.
281 public function getRedirectChain() {
282 $text = $this->getNativeData();
283 return Title::newFromRedirectArray( $text );
288 * @todo: test needs database! Should be done by a test class in the Database group.
291 public function getUltimateRedirectTarget() {
292 $text = $this->getNativeData();
293 return Title::newFromRedirectRecurse( $text );
298 public function dataIsCountable() {
325 array( 'Foo [[bar]]',
335 array( 'Foo [[bar]]',
340 array( '#REDIRECT [[bar]]',
345 array( '#REDIRECT [[bar]]',
350 array( '#REDIRECT [[bar]]',
360 * @dataProvider dataIsCountable
363 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
364 global $wgArticleCountMethod;
366 $old = $wgArticleCountMethod;
367 $wgArticleCountMethod = $mode;
369 $content = $this->newContent( $text );
371 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
372 $wgArticleCountMethod = $old;
374 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
375 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
378 public function dataGetTextForSummary() {
380 array( "hello\nworld.",
384 array( 'hello world.',
388 array( '[[hello world]].',
396 * @dataProvider dataGetTextForSummary
398 public function testGetTextForSummary( $text, $maxlength, $expected ) {
399 $content = $this->newContent( $text );
401 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
405 public function testGetTextForSearchIndex( ) {
406 $content = $this->newContent( "hello world." );
408 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
411 public function testCopy() {
412 $content = $this->newContent( "hello world." );
413 $copy = $content->copy();
415 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
416 $this->assertEquals( "hello world.", $copy->getNativeData() );
419 public function testGetSize( ) {
420 $content = $this->newContent( "hello world." );
422 $this->assertEquals( 12, $content->getSize() );
425 public function testGetNativeData( ) {
426 $content = $this->newContent( "hello world." );
428 $this->assertEquals( "hello world.", $content->getNativeData() );
431 public function testGetWikitextForTransclusion( ) {
432 $content = $this->newContent( "hello world." );
434 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
437 public function testMatchMagicWord( ) {
438 $mw = MagicWord
::get( "staticredirect" );
440 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
441 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
443 $content = $this->newContent( "#REDIRECT [[FOO]]" );
444 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
447 public function testUpdateRedirect( ) {
448 $target = Title
::newFromText( "testUpdateRedirect_target" );
450 // test with non-redirect page
451 $content = $this->newContent( "hello world." );
452 $newContent = $content->updateRedirect( $target );
454 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
456 // test with actual redirect
457 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
458 $newContent = $content->updateRedirect( $target );
460 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
461 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
463 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
466 # =================================================================================================================
468 public function testGetModel() {
469 $content = $this->newContent( "hello world." );
471 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
474 public function testGetContentHandler() {
475 $content = $this->newContent( "hello world." );
477 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
480 public function dataIsEmpty( ) {
485 array( 'hallo welt.', false ),
490 * @dataProvider dataIsEmpty
492 public function testIsEmpty( $text, $empty ) {
493 $content = $this->newContent( $text );
495 $this->assertEquals( $empty, $content->isEmpty() );
498 public function dataEquals( ) {
500 array( new WikitextContent( "hallo" ), null, false ),
501 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
502 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
503 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
508 * @dataProvider dataEquals
510 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
511 $this->assertEquals( $equal, $a->equals( $b ) );
514 public function dataGetDeletionUpdates() {
516 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
517 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
518 array( 'LinksDeletionUpdate' => array( ) )
520 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
521 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
522 array( 'LinksDeletionUpdate' => array( ) )
529 * @dataProvider dataGetDeletionUpdates
531 public function testDeletionUpdates( $title, $model, $text, $expectedStuff ) {
532 $title = Title
::newFromText( $title );
533 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
535 $content = ContentHandler
::makeContent( $text, $title, $model );
537 $updates = $content->getDeletionUpdates( WikiPage
::factory( $title ) );
539 // make updates accessible by class name
540 foreach ( $updates as $update ) {
541 $class = get_class( $update );
542 $updates[ $class ] = $update;
545 foreach ( $expectedStuff as $class => $fieldValues ) {
546 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
548 $update = $updates[ $class ];
550 foreach ( $fieldValues as $field => $value ) {
551 $v = $update->$field; #if the field doesn't exist, just crash and burn
552 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );