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 $title = Title
::newFromText( $title );
73 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
75 $content = ContentHandler
::makeContent( $text, $title, $model );
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" );
97 public static function dataGetSection() {
99 array( WikitextContentTest
::$sections,
103 array( WikitextContentTest
::$sections,
108 array( WikitextContentTest
::$sections,
116 * @dataProvider dataGetSection
118 public function testGetSection( $text, $sectionId, $expectedText ) {
119 $content = $this->newContent( $text );
121 $sectionContent = $content->getSection( $sectionId );
122 if ( is_object( $sectionContent ) ) {
123 $sectionText = $sectionContent->getNativeData();
125 $sectionText = $sectionContent;
128 $this->assertEquals( $expectedText, $sectionText );
131 public static function dataReplaceSection() {
133 array( WikitextContentTest
::$sections,
137 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
139 array( WikitextContentTest
::$sections,
145 array( WikitextContentTest
::$sections,
147 "== TEST ==\nmore fun",
149 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
151 array( WikitextContentTest
::$sections,
155 WikitextContentTest
::$sections
157 array( WikitextContentTest
::$sections,
161 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
167 * @dataProvider dataReplaceSection
169 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
170 $content = $this->newContent( $text );
171 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
173 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
176 public function testAddSectionHeader( ) {
177 $content = $this->newContent( 'hello world' );
178 $content = $content->addSectionHeader( 'test' );
180 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
183 public static function dataPreSaveTransform() {
185 array( 'hello this is ~~~',
186 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
188 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
189 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
198 public static function dataPreloadTransform() {
200 array( 'hello this is ~~~',
203 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
204 'hello \'\'this\'\' is bar',
209 public static function dataGetRedirectTarget() {
211 array( '#REDIRECT [[Test]]',
214 array( '#REDIRECT Test',
217 array( '* #REDIRECT [[Test]]',
223 public static function dataGetTextForSummary() {
225 array( "hello\nworld.",
229 array( 'hello world.',
233 array( '[[hello world]].',
241 * @todo: test needs database! Should be done by a test class in the Database group.
244 public function getRedirectChain() {
245 $text = $this->getNativeData();
246 return Title::newFromRedirectArray( $text );
251 * @todo: test needs database! Should be done by a test class in the Database group.
254 public function getUltimateRedirectTarget() {
255 $text = $this->getNativeData();
256 return Title::newFromRedirectRecurse( $text );
260 public static function dataIsCountable() {
287 array( 'Foo [[bar]]',
297 array( 'Foo [[bar]]',
302 array( '#REDIRECT [[bar]]',
307 array( '#REDIRECT [[bar]]',
312 array( '#REDIRECT [[bar]]',
320 public function testMatchMagicWord( ) {
321 $mw = MagicWord
::get( "staticredirect" );
323 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
324 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
326 $content = $this->newContent( "#REDIRECT [[FOO]]" );
327 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
330 public function testUpdateRedirect( ) {
331 $target = Title
::newFromText( "testUpdateRedirect_target" );
333 // test with non-redirect page
334 $content = $this->newContent( "hello world." );
335 $newContent = $content->updateRedirect( $target );
337 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
339 // test with actual redirect
340 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
341 $newContent = $content->updateRedirect( $target );
343 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
344 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
346 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
349 public function testGetModel() {
350 $content = $this->newContent( "hello world." );
352 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
355 public function testGetContentHandler() {
356 $content = $this->newContent( "hello world." );
358 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
361 public static function dataEquals( ) {
363 array( new WikitextContent( "hallo" ), null, false ),
364 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
365 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
366 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
367 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
371 public static function dataGetDeletionUpdates() {
373 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
374 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
375 array( 'LinksDeletionUpdate' => array( ) )
377 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
378 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
379 array( 'LinksDeletionUpdate' => array( ) )