4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class WikitextContentTest
extends TextContentTest
{
10 public 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",
154 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==",
155 WikitextContentTest
::$sections
158 array( WikitextContentTest
::$sections,
162 WikitextContentTest
::$sections
164 array( WikitextContentTest
::$sections,
168 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
174 * @dataProvider dataReplaceSection
175 * @covers WikitextContent::replaceSection
177 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
178 $content = $this->newContent( $text );
179 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
181 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
185 * @covers WikitextContent::addSectionHeader
187 public function testAddSectionHeader() {
188 $content = $this->newContent( 'hello world' );
189 $content = $content->addSectionHeader( 'test' );
191 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
194 public static function dataPreSaveTransform() {
196 array( 'hello this is ~~~',
197 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
199 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
200 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
209 public static function dataPreloadTransform() {
211 array( 'hello this is ~~~',
214 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
215 'hello \'\'this\'\' is bar',
220 public static function dataGetRedirectTarget() {
222 array( '#REDIRECT [[Test]]',
225 array( '#REDIRECT Test',
228 array( '* #REDIRECT [[Test]]',
234 public static function dataGetTextForSummary() {
236 array( "hello\nworld.",
240 array( 'hello world.',
244 array( '[[hello world]].',
251 public static function dataIsCountable() {
278 array( 'Foo [[bar]]',
288 array( 'Foo [[bar]]',
293 array( '#REDIRECT [[bar]]',
298 array( '#REDIRECT [[bar]]',
303 array( '#REDIRECT [[bar]]',
312 * @covers WikitextContent::matchMagicWord
314 public function testMatchMagicWord() {
315 $mw = MagicWord
::get( "staticredirect" );
317 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
318 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
320 $content = $this->newContent( "#REDIRECT [[FOO]]" );
321 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
325 * @covers WikitextContent::updateRedirect
327 public function testUpdateRedirect() {
328 $target = Title
::newFromText( "testUpdateRedirect_target" );
330 // test with non-redirect page
331 $content = $this->newContent( "hello world." );
332 $newContent = $content->updateRedirect( $target );
334 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
336 // test with actual redirect
337 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
338 $newContent = $content->updateRedirect( $target );
340 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
341 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
343 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
347 * @covers WikitextContent::getModel
349 public function testGetModel() {
350 $content = $this->newContent( "hello world." );
352 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
356 * @covers WikitextContent::getContentHandler
358 public function testGetContentHandler() {
359 $content = $this->newContent( "hello world." );
361 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
364 public function testRedirectParserOption() {
365 $title = Title
::newFromText( 'testRedirectParserOption' );
367 // Set up hook and its reporting variables
369 $redirectTarget = null;
370 $this->mergeMwGlobalArrayValue( 'wgHooks', array(
371 'InternalParseBeforeLinks' => array(
372 function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) {
374 $redirectTarget = $parser->getOptions()->getRedirectTarget();
379 // Test with non-redirect page
381 $redirectTarget = false;
382 $content = $this->newContent( 'hello world.' );
383 $options = $content->getContentHandler()->makeParserOptions( 'canonical' );
384 $options->setRedirectTarget( $title );
385 $content->getParserOutput( $title, null, $options );
386 $this->assertEquals( 'hello world.', $wikitext,
387 'Wikitext passed to hook was not as expected'
389 $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' );
390 $this->assertEquals( $title, $options->getRedirectTarget(),
391 'ParserOptions\' redirectTarget was changed'
394 // Test with a redirect page
396 $redirectTarget = false;
397 $content = $this->newContent( "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect." );
398 $options = $content->getContentHandler()->makeParserOptions( 'canonical' );
399 $content->getParserOutput( $title, null, $options );
400 $this->assertEquals( 'hello redirect.', $wikitext, 'Wikitext passed to hook was not as expected' );
401 $this->assertNotEquals( null, $redirectTarget, 'Redirect seen in hook was null' );
402 $this->assertEquals( 'TestRedirectParserOption/redir', $redirectTarget->getFullText(),
403 'Redirect seen in hook was not the expected title'
405 $this->assertEquals( null, $options->getRedirectTarget(),
406 'ParserOptions\' redirectTarget was changed'
410 public static function dataEquals() {
412 array( new WikitextContent( "hallo" ), null, false ),
413 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
414 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ),
415 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
416 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
420 public static function dataGetDeletionUpdates() {
422 array( "WikitextContentTest_testGetSecondaryDataUpdates_1",
423 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
424 array( 'LinksDeletionUpdate' => array() )
426 array( "WikitextContentTest_testGetSecondaryDataUpdates_2",
427 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
428 array( 'LinksDeletionUpdate' => array() )