4 * @group ContentHandler
6 class WikitextContentTest
extends MediaWikiTestCase
{
8 public function setup() {
9 $this->context
= new RequestContext( new FauxRequest() );
10 $this->context
->setTitle( Title
::newFromText( "Test" ) );
13 public function newContent( $text ) {
14 return new WikitextContent( $text );
18 public function dataGetParserOutput() {
20 array("WikitextContentTest_testGetParserOutput", "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
26 * @dataProvider dataGetParserOutput
28 public function testGetParserOutput( $title, $text, $expectedHtml ) {
29 $title = Title
::newFromText( $title );
30 $content = ContentHandler
::makeContent( $text, $title );
32 $po = $content->getParserOutput( $title );
34 $this->assertEquals( $expectedHtml, $po->getText() );
35 // @todo: assert more properties
38 public function dataGetSecondaryDataUpdates() {
40 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
41 array( 'LinksUpdate' => array( 'mRecursive' => true,
42 'mLinks' => array() ) )
44 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
45 array( 'LinksUpdate' => array( 'mRecursive' => true,
46 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
53 * @dataProvider dataGetSecondaryDataUpdates
56 public function testGetSecondaryDataUpdates( $title, $text, $expectedStuff ) {
57 $title = Title
::newFromText( $title );
58 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
60 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
61 $content = ContentHandler
::makeContent( $text, $title );
63 $updates = $content->getSecondaryDataUpdates( $title );
65 // make updates accessible by class name
66 foreach ( $updates as $update ) {
67 $class = get_class( $update );
68 $updates[ $class ] = $update;
71 foreach ( $expectedStuff as $class => $fieldValues ) {
72 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
74 $update = $updates[ $class ];
76 foreach ( $fieldValues as $field => $value ) {
77 $v = $update->$field; #if the field doesn't exist, just crash and burn
78 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
98 public function dataGetSection() {
100 array( WikitextContentTest
::$sections,
104 array( WikitextContentTest
::$sections,
109 array( WikitextContentTest
::$sections,
117 * @dataProvider dataGetSection
119 public function testGetSection( $text, $sectionId, $expectedText ) {
120 $content = $this->newContent( $text );
122 $sectionContent = $content->getSection( $sectionId );
124 $this->assertEquals( $expectedText, is_null( $sectionContent ) ?
null : $sectionContent->getNativeData() );
127 public function dataReplaceSection() {
129 array( WikitextContentTest
::$sections,
133 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
135 array( WikitextContentTest
::$sections,
141 array( WikitextContentTest
::$sections,
143 "== TEST ==\nmore fun",
145 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
147 array( WikitextContentTest
::$sections,
151 WikitextContentTest
::$sections
153 array( WikitextContentTest
::$sections,
157 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
163 * @dataProvider dataReplaceSection
165 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
166 $content = $this->newContent( $text );
167 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
169 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
172 public function testAddSectionHeader( ) {
173 $content = $this->newContent( 'hello world' );
174 $content = $content->addSectionHeader( 'test' );
176 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
179 public function dataPreSaveTransform() {
181 array( 'hello this is ~~~',
182 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
184 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
185 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
191 * @dataProvider dataPreSaveTransform
193 public function testPreSaveTransform( $text, $expected ) {
194 global $wgUser, $wgContLang;
195 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
197 $content = $this->newContent( $text );
198 $content = $content->preSaveTransform( $this->context
->getTitle(), $this->context
->getUser(), $options );
200 $this->assertEquals( $expected, $content->getNativeData() );
203 public function dataPreloadTransform() {
205 array( 'hello this is ~~~',
208 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
209 'hello \'\'this\'\' is bar',
215 * @dataProvider dataPreloadTransform
217 public function testPreloadTransform( $text, $expected ) {
218 global $wgUser, $wgContLang;
219 $options = ParserOptions
::newFromUserAndLang( $wgUser, $wgContLang );
221 $content = $this->newContent( $text );
222 $content = $content->preloadTransform( $this->context
->getTitle(), $options );
224 $this->assertEquals( $expected, $content->getNativeData() );
227 public function dataGetRedirectTarget() {
229 array( '#REDIRECT [[Test]]',
232 array( '#REDIRECT Test',
235 array( '* #REDIRECT [[Test]]',
242 * @dataProvider dataGetRedirectTarget
244 public function testGetRedirectTarget( $text, $expected ) {
245 $content = $this->newContent( $text );
246 $t = $content->getRedirectTarget( );
248 if ( is_null( $expected ) ) $this->assertNull( $t, "text should not have generated a redirect target: $text" );
249 else $this->assertEquals( $expected, $t->getPrefixedText() );
253 * @dataProvider dataGetRedirectTarget
255 public function isRedirect( $text, $expected ) {
256 $content = $this->newContent( $text );
258 $this->assertEquals( !is_null($expected), $content->isRedirect() );
263 * @todo: test needs database!
266 public function getRedirectChain() {
267 $text = $this->getNativeData();
268 return Title::newFromRedirectArray( $text );
273 * @todo: test needs database!
276 public function getUltimateRedirectTarget() {
277 $text = $this->getNativeData();
278 return Title::newFromRedirectRecurse( $text );
283 public function dataIsCountable() {
310 array( 'Foo [[bar]]',
320 array( 'Foo [[bar]]',
325 array( '#REDIRECT [[bar]]',
330 array( '#REDIRECT [[bar]]',
335 array( '#REDIRECT [[bar]]',
345 * @dataProvider dataIsCountable
348 public function testIsCountable( $text, $hasLinks, $mode, $expected ) {
349 global $wgArticleCountMethod;
351 $old = $wgArticleCountMethod;
352 $wgArticleCountMethod = $mode;
354 $content = $this->newContent( $text );
356 $v = $content->isCountable( $hasLinks, $this->context
->getTitle() );
357 $wgArticleCountMethod = $old;
359 $this->assertEquals( $expected, $v, "isCountable() returned unexpected value " . var_export( $v, true )
360 . " instead of " . var_export( $expected, true ) . " in mode `$mode` for text \"$text\"" );
363 public function dataGetTextForSummary() {
365 array( "hello\nworld.",
369 array( 'hello world.',
373 array( '[[hello world]].',
381 * @dataProvider dataGetTextForSummary
383 public function testGetTextForSummary( $text, $maxlength, $expected ) {
384 $content = $this->newContent( $text );
386 $this->assertEquals( $expected, $content->getTextForSummary( $maxlength ) );
390 public function testGetTextForSearchIndex( ) {
391 $content = $this->newContent( "hello world." );
393 $this->assertEquals( "hello world.", $content->getTextForSearchIndex() );
396 public function testCopy() {
397 $content = $this->newContent( "hello world." );
398 $copy = $content->copy();
400 $this->assertTrue( $content->equals( $copy ), "copy must be equal to original" );
401 $this->assertEquals( "hello world.", $copy->getNativeData() );
404 public function testGetSize( ) {
405 $content = $this->newContent( "hello world." );
407 $this->assertEquals( 12, $content->getSize() );
410 public function testGetNativeData( ) {
411 $content = $this->newContent( "hello world." );
413 $this->assertEquals( "hello world.", $content->getNativeData() );
416 public function testGetWikitextForTransclusion( ) {
417 $content = $this->newContent( "hello world." );
419 $this->assertEquals( "hello world.", $content->getWikitextForTransclusion() );
422 public function testMatchMagicWord( ) {
423 $mw = MagicWord
::get( "staticredirect" );
425 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
426 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
428 $content = $this->newContent( "#REDIRECT [[FOO]]" );
429 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
432 # =================================================================================================================
434 public function testGetModel() {
435 $content = $this->newContent( "hello world." );
437 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
440 public function testGetContentHandler() {
441 $content = $this->newContent( "hello world." );
443 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
446 public function dataIsEmpty( ) {
451 array( 'hallo welt.', false ),
456 * @dataProvider dataIsEmpty
458 public function testIsEmpty( $text, $empty ) {
459 $content = $this->newContent( $text );
461 $this->assertEquals( $empty, $content->isEmpty() );
464 public function dataEquals( ) {
466 array( new WikitextContent( "hallo" ), null, false ),
467 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
468 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
469 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
474 * @dataProvider dataEquals
476 public function testEquals( Content
$a, Content
$b = null, $equal = false ) {
477 $this->assertEquals( $equal, $a->equals( $b ) );
480 public function dataGetDeletionUpdates() {
482 array("WikitextContentTest_testGetSecondaryDataUpdates_1", "hello ''world''\n",
483 array( 'LinksDeletionUpdate' => array( ) )
485 array("WikitextContentTest_testGetSecondaryDataUpdates_2", "hello [[world test 21344]]\n",
486 array( 'LinksDeletionUpdate' => array( ) )
493 * @dataProvider dataGetDeletionUpdates
495 public function testDeletionUpdates( $title, $text, $expectedStuff ) {
496 $title = Title
::newFromText( $title );
497 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
499 $handler = ContentHandler
::getForModelID( $title->getContentModel() );
500 $content = ContentHandler
::makeContent( $text, $title );
502 $updates = $content->getDeletionUpdates( $title );
504 // make updates accessible by class name
505 foreach ( $updates as $update ) {
506 $class = get_class( $update );
507 $updates[ $class ] = $update;
510 foreach ( $expectedStuff as $class => $fieldValues ) {
511 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
513 $update = $updates[ $class ];
515 foreach ( $fieldValues as $field => $value ) {
516 $v = $update->$field; #if the field doesn't exist, just crash and burn
517 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );