4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class WikitextContentTest
extends TextContentTest
{
25 public function newContent( $text ) {
26 return new WikitextContent( $text );
29 public static function dataGetParserOutput() {
32 "WikitextContentTest_testGetParserOutput",
33 CONTENT_MODEL_WIKITEXT
,
35 "<p>hello <i>world</i>\n</p>"
41 public static function dataGetSecondaryDataUpdates() {
43 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
44 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
46 'LinksUpdate' => array(
52 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
53 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
55 'LinksUpdate' => array(
58 array( 'World_test_21344' => 0 )
68 * @dataProvider dataGetSecondaryDataUpdates
71 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
72 $ns = $this->getDefaultWikitextNS();
73 $title = Title
::newFromText( $title, $ns );
75 $content = ContentHandler
::makeContent( $text, $title, $model );
77 $page = WikiPage
::factory( $title );
78 $page->doEditContent( $content, '' );
80 $updates = $content->getSecondaryDataUpdates( $title );
82 // make updates accessible by class name
83 foreach ( $updates as $update ) {
84 $class = get_class( $update );
85 $updates[$class] = $update;
88 foreach ( $expectedStuff as $class => $fieldValues ) {
89 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
91 $update = $updates[$class];
93 foreach ( $fieldValues as $field => $value ) {
94 $v = $update->$field; #if the field doesn't exist, just crash and burn
95 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
99 $page->doDeleteArticle( '' );
102 public static function dataGetSection() {
104 array( WikitextContentTest
::$sections,
108 array( WikitextContentTest
::$sections,
113 array( WikitextContentTest
::$sections,
121 * @dataProvider dataGetSection
123 public function testGetSection( $text, $sectionId, $expectedText ) {
124 $content = $this->newContent( $text );
126 $sectionContent = $content->getSection( $sectionId );
127 if ( is_object( $sectionContent ) ) {
128 $sectionText = $sectionContent->getNativeData();
130 $sectionText = $sectionContent;
133 $this->assertEquals( $expectedText, $sectionText );
136 public static function dataReplaceSection() {
138 array( WikitextContentTest
::$sections,
142 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
144 array( WikitextContentTest
::$sections,
150 array( WikitextContentTest
::$sections,
152 "== TEST ==\nmore fun",
154 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
156 array( WikitextContentTest
::$sections,
160 WikitextContentTest
::$sections
162 array( WikitextContentTest
::$sections,
166 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
172 * @dataProvider dataReplaceSection
174 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
175 $content = $this->newContent( $text );
176 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
178 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
181 public function testAddSectionHeader( ) {
182 $content = $this->newContent( 'hello world' );
183 $content = $content->addSectionHeader( 'test' );
185 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
188 public static function dataPreSaveTransform() {
190 array( 'hello this is ~~~',
191 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
193 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
194 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
203 public static function dataPreloadTransform() {
205 array( 'hello this is ~~~',
208 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
209 'hello \'\'this\'\' is bar',
214 public static function dataGetRedirectTarget() {
216 array( '#REDIRECT [[Test]]',
219 array( '#REDIRECT Test',
222 array( '* #REDIRECT [[Test]]',
228 public static function dataGetTextForSummary() {
230 array( "hello\nworld.",
234 array( 'hello world.',
238 array( '[[hello world]].',
246 * @todo: test needs database! Should be done by a test class in the Database group.
249 public function getRedirectChain() {
250 $text = $this->getNativeData();
251 return Title::newFromRedirectArray( $text );
256 * @todo: test needs database! Should be done by a test class in the Database group.
259 public function getUltimateRedirectTarget() {
260 $text = $this->getNativeData();
261 return Title::newFromRedirectRecurse( $text );
265 public static function dataIsCountable() {
292 array( 'Foo [[bar]]',
302 array( 'Foo [[bar]]',
307 array( '#REDIRECT [[bar]]',
312 array( '#REDIRECT [[bar]]',
317 array( '#REDIRECT [[bar]]',
325 public function testMatchMagicWord( ) {
326 $mw = MagicWord
::get( "staticredirect" );
328 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
329 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
331 $content = $this->newContent( "#REDIRECT [[FOO]]" );
332 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
335 public function testUpdateRedirect( ) {
336 $target = Title
::newFromText( "testUpdateRedirect_target" );
338 // test with non-redirect page
339 $content = $this->newContent( "hello world." );
340 $newContent = $content->updateRedirect( $target );
342 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
344 // test with actual redirect
345 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
346 $newContent = $content->updateRedirect( $target );
348 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
349 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
351 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
354 public function testGetModel() {
355 $content = $this->newContent( "hello world." );
357 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
360 public function testGetContentHandler() {
361 $content = $this->newContent( "hello world." );
363 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
366 public static function dataEquals( ) {
368 array( new WikitextContent( "hallo" ), null, false ),
369 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
370 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
371 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
372 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
376 public static function dataGetDeletionUpdates() {
378 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
379 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
380 array( 'LinksDeletionUpdate' => array( ) )
382 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
383 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
384 array( 'LinksDeletionUpdate' => array( ) )