4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class WikitextContentTest
extends TextContentTest
{
10 static $sections = "Intro
22 public function newContent( $text ) {
23 return new WikitextContent( $text );
26 public static function dataGetParserOutput() {
29 "WikitextContentTest_testGetParserOutput",
30 CONTENT_MODEL_WIKITEXT
,
32 "<p>hello <i>world</i>\n</p>"
38 public static function dataGetSecondaryDataUpdates() {
40 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
41 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
43 'LinksUpdate' => array(
49 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
50 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
52 'LinksUpdate' => array(
55 array( 'World_test_21344' => 0 )
65 * @dataProvider dataGetSecondaryDataUpdates
68 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
69 $ns = $this->getDefaultWikitextNS();
70 $title = Title
::newFromText( $title, $ns );
72 $content = ContentHandler
::makeContent( $text, $title, $model );
74 $page = WikiPage
::factory( $title );
75 $page->doEditContent( $content, '' );
77 $updates = $content->getSecondaryDataUpdates( $title );
79 // make updates accessible by class name
80 foreach ( $updates as $update ) {
81 $class = get_class( $update );
82 $updates[$class] = $update;
85 foreach ( $expectedStuff as $class => $fieldValues ) {
86 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
88 $update = $updates[$class];
90 foreach ( $fieldValues as $field => $value ) {
91 $v = $update->$field; #if the field doesn't exist, just crash and burn
92 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
96 $page->doDeleteArticle( '' );
99 public static function dataGetSection() {
101 array( WikitextContentTest
::$sections,
105 array( WikitextContentTest
::$sections,
110 array( WikitextContentTest
::$sections,
118 * @dataProvider dataGetSection
120 public function testGetSection( $text, $sectionId, $expectedText ) {
121 $content = $this->newContent( $text );
123 $sectionContent = $content->getSection( $sectionId );
124 if ( is_object( $sectionContent ) ) {
125 $sectionText = $sectionContent->getNativeData();
127 $sectionText = $sectionContent;
130 $this->assertEquals( $expectedText, $sectionText );
133 public static function dataReplaceSection() {
135 array( WikitextContentTest
::$sections,
139 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
141 array( WikitextContentTest
::$sections,
147 array( WikitextContentTest
::$sections,
149 "== TEST ==\nmore fun",
151 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
153 array( WikitextContentTest
::$sections,
157 WikitextContentTest
::$sections
159 array( WikitextContentTest
::$sections,
163 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
169 * @dataProvider dataReplaceSection
171 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
172 $content = $this->newContent( $text );
173 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
175 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
178 public function testAddSectionHeader() {
179 $content = $this->newContent( 'hello world' );
180 $content = $content->addSectionHeader( 'test' );
182 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
185 public static function dataPreSaveTransform() {
187 array( 'hello this is ~~~',
188 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
190 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
191 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
200 public static function dataPreloadTransform() {
202 array( 'hello this is ~~~',
205 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
206 'hello \'\'this\'\' is bar',
211 public static function dataGetRedirectTarget() {
213 array( '#REDIRECT [[Test]]',
216 array( '#REDIRECT Test',
219 array( '* #REDIRECT [[Test]]',
225 public static function dataGetTextForSummary() {
227 array( "hello\nworld.",
231 array( 'hello world.',
235 array( '[[hello world]].',
243 * @todo Test needs database! Should be done by a test class in the Database group.
246 public function getRedirectChain() {
247 $text = $this->getNativeData();
248 return Title::newFromRedirectArray( $text );
253 * @todo Test needs database! Should be done by a test class in the Database group.
256 public function getUltimateRedirectTarget() {
257 $text = $this->getNativeData();
258 return Title::newFromRedirectRecurse( $text );
262 public static function dataIsCountable() {
289 array( 'Foo [[bar]]',
299 array( 'Foo [[bar]]',
304 array( '#REDIRECT [[bar]]',
309 array( '#REDIRECT [[bar]]',
314 array( '#REDIRECT [[bar]]',
322 public function testMatchMagicWord() {
323 $mw = MagicWord
::get( "staticredirect" );
325 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
326 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
328 $content = $this->newContent( "#REDIRECT [[FOO]]" );
329 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
332 public function testUpdateRedirect() {
333 $target = Title
::newFromText( "testUpdateRedirect_target" );
335 // test with non-redirect page
336 $content = $this->newContent( "hello world." );
337 $newContent = $content->updateRedirect( $target );
339 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
341 // test with actual redirect
342 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
343 $newContent = $content->updateRedirect( $target );
345 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
346 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
348 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
351 public function testGetModel() {
352 $content = $this->newContent( "hello world." );
354 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
357 public function testGetContentHandler() {
358 $content = $this->newContent( "hello world." );
360 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
363 public static function dataEquals() {
365 array( new WikitextContent( "hallo" ), null, false ),
366 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
367 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
368 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
369 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
373 public static function dataGetDeletionUpdates() {
375 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
376 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
377 array( 'LinksDeletionUpdate' => array() )
379 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
380 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
381 array( 'LinksDeletionUpdate' => array() )