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
67 * @covers WikitextContent::getSecondaryDataUpdates
69 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
70 $ns = $this->getDefaultWikitextNS();
71 $title = Title
::newFromText( $title, $ns );
73 $content = ContentHandler
::makeContent( $text, $title, $model );
75 $page = WikiPage
::factory( $title );
76 $page->doEditContent( $content, '' );
78 $updates = $content->getSecondaryDataUpdates( $title );
80 // make updates accessible by class name
81 foreach ( $updates as $update ) {
82 $class = get_class( $update );
83 $updates[$class] = $update;
86 foreach ( $expectedStuff as $class => $fieldValues ) {
87 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
89 $update = $updates[$class];
91 foreach ( $fieldValues as $field => $value ) {
92 $v = $update->$field; #if the field doesn't exist, just crash and burn
93 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
97 $page->doDeleteArticle( '' );
100 public static function dataGetSection() {
102 array( WikitextContentTest
::$sections,
106 array( WikitextContentTest
::$sections,
111 array( WikitextContentTest
::$sections,
119 * @dataProvider dataGetSection
120 * @covers WikitextContent::getSection
122 public function testGetSection( $text, $sectionId, $expectedText ) {
123 $content = $this->newContent( $text );
125 $sectionContent = $content->getSection( $sectionId );
126 if ( is_object( $sectionContent ) ) {
127 $sectionText = $sectionContent->getNativeData();
129 $sectionText = $sectionContent;
132 $this->assertEquals( $expectedText, $sectionText );
135 public static function dataReplaceSection() {
137 array( WikitextContentTest
::$sections,
141 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
143 array( WikitextContentTest
::$sections,
149 array( WikitextContentTest
::$sections,
151 "== TEST ==\nmore fun",
153 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
155 array( WikitextContentTest
::$sections,
159 WikitextContentTest
::$sections
161 array( WikitextContentTest
::$sections,
165 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
171 * @dataProvider dataReplaceSection
172 * @covers WikitextContent::replaceSection
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() );
182 * @covers WikitextContent::addSectionHeader
184 public function testAddSectionHeader() {
185 $content = $this->newContent( 'hello world' );
186 $content = $content->addSectionHeader( 'test' );
188 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
191 public static function dataPreSaveTransform() {
193 array( 'hello this is ~~~',
194 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
196 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
197 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
206 public static function dataPreloadTransform() {
208 array( 'hello this is ~~~',
211 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
212 'hello \'\'this\'\' is bar',
217 public static function dataGetRedirectTarget() {
219 array( '#REDIRECT [[Test]]',
222 array( '#REDIRECT Test',
225 array( '* #REDIRECT [[Test]]',
231 public static function dataGetTextForSummary() {
233 array( "hello\nworld.",
237 array( 'hello world.',
241 array( '[[hello world]].',
248 public static function dataIsCountable() {
275 array( 'Foo [[bar]]',
285 array( 'Foo [[bar]]',
290 array( '#REDIRECT [[bar]]',
295 array( '#REDIRECT [[bar]]',
300 array( '#REDIRECT [[bar]]',
309 * @covers WikitextContent::matchMagicWord
311 public function testMatchMagicWord() {
312 $mw = MagicWord
::get( "staticredirect" );
314 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
315 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
317 $content = $this->newContent( "#REDIRECT [[FOO]]" );
318 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
322 * @covers WikitextContent::updateRedirect
324 public function testUpdateRedirect() {
325 $target = Title
::newFromText( "testUpdateRedirect_target" );
327 // test with non-redirect page
328 $content = $this->newContent( "hello world." );
329 $newContent = $content->updateRedirect( $target );
331 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
333 // test with actual redirect
334 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
335 $newContent = $content->updateRedirect( $target );
337 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
338 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
340 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
344 * @covers WikitextContent::getModel
346 public function testGetModel() {
347 $content = $this->newContent( "hello world." );
349 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
353 * @covers WikitextContent::getContentHandler
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() )