4 * @group ContentHandler
7 * ^--- needed, because we do need the database to test link updates
9 class WikitextContentTest
extends TextContentTest
{
11 public function newContent( $text ) {
12 return new WikitextContent( $text );
15 public function dataGetParserOutput() {
17 array("WikitextContentTest_testGetParserOutput", CONTENT_MODEL_WIKITEXT
, "hello ''world''\n", "<p>hello <i>world</i>\n</p>"),
22 public function dataGetSecondaryDataUpdates() {
24 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
25 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
26 array( 'LinksUpdate' => array( 'mRecursive' => true,
27 'mLinks' => array() ) )
29 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
30 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
31 array( 'LinksUpdate' => array( 'mRecursive' => true,
32 'mLinks' => array( array( 'World_test_21344' => 0 ) ) ) )
39 * @dataProvider dataGetSecondaryDataUpdates
42 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) {
43 $title = Title
::newFromText( $title );
44 $title->resetArticleID( 2342 ); //dummy id. fine as long as we don't try to execute the updates!
46 $content = ContentHandler
::makeContent( $text, $title, $model );
48 $updates = $content->getSecondaryDataUpdates( $title );
50 // make updates accessible by class name
51 foreach ( $updates as $update ) {
52 $class = get_class( $update );
53 $updates[$class] = $update;
56 foreach ( $expectedStuff as $class => $fieldValues ) {
57 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" );
59 $update = $updates[$class];
61 foreach ( $fieldValues as $field => $value ) {
62 $v = $update->$field; #if the field doesn't exist, just crash and burn
63 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" );
83 public function dataGetSection() {
85 array( WikitextContentTest
::$sections,
89 array( WikitextContentTest
::$sections,
94 array( WikitextContentTest
::$sections,
102 * @dataProvider dataGetSection
104 public function testGetSection( $text, $sectionId, $expectedText ) {
105 $content = $this->newContent( $text );
107 $sectionContent = $content->getSection( $sectionId );
108 if ( is_object( $sectionContent ) ) {
109 $sectionText = $sectionContent->getNativeData();
111 $sectionText = $sectionContent;
114 $this->assertEquals( $expectedText, $sectionText );
117 public function dataReplaceSection() {
119 array( WikitextContentTest
::$sections,
123 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest
::$sections ) )
125 array( WikitextContentTest
::$sections,
131 array( WikitextContentTest
::$sections,
133 "== TEST ==\nmore fun",
135 trim( preg_replace( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest
::$sections ) )
137 array( WikitextContentTest
::$sections,
141 WikitextContentTest
::$sections
143 array( WikitextContentTest
::$sections,
147 trim( WikitextContentTest
::$sections ) . "\n\n\n== New ==\n\nNo more"
153 * @dataProvider dataReplaceSection
155 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) {
156 $content = $this->newContent( $text );
157 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle );
159 $this->assertEquals( $expected, is_null( $c ) ?
null : $c->getNativeData() );
162 public function testAddSectionHeader( ) {
163 $content = $this->newContent( 'hello world' );
164 $content = $content->addSectionHeader( 'test' );
166 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() );
169 public function dataPreSaveTransform() {
171 array( 'hello this is ~~~',
172 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]",
174 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
175 'hello \'\'this\'\' is <nowiki>~~~</nowiki>',
180 public function dataPreloadTransform() {
182 array( 'hello this is ~~~',
185 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>',
186 'hello \'\'this\'\' is bar',
191 public function dataGetRedirectTarget() {
193 array( '#REDIRECT [[Test]]',
196 array( '#REDIRECT Test',
199 array( '* #REDIRECT [[Test]]',
205 public function dataGetTextForSummary() {
207 array( "hello\nworld.",
211 array( 'hello world.',
215 array( '[[hello world]].',
223 * @todo: test needs database! Should be done by a test class in the Database group.
226 public function getRedirectChain() {
227 $text = $this->getNativeData();
228 return Title::newFromRedirectArray( $text );
233 * @todo: test needs database! Should be done by a test class in the Database group.
236 public function getUltimateRedirectTarget() {
237 $text = $this->getNativeData();
238 return Title::newFromRedirectRecurse( $text );
243 public function dataIsCountable() {
270 array( 'Foo [[bar]]',
280 array( 'Foo [[bar]]',
285 array( '#REDIRECT [[bar]]',
290 array( '#REDIRECT [[bar]]',
295 array( '#REDIRECT [[bar]]',
303 public function testMatchMagicWord( ) {
304 $mw = MagicWord
::get( "staticredirect" );
306 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" );
307 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" );
309 $content = $this->newContent( "#REDIRECT [[FOO]]" );
310 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" );
313 public function testUpdateRedirect( ) {
314 $target = Title
::newFromText( "testUpdateRedirect_target" );
316 // test with non-redirect page
317 $content = $this->newContent( "hello world." );
318 $newContent = $content->updateRedirect( $target );
320 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" );
322 // test with actual redirect
323 $content = $this->newContent( "#REDIRECT [[Someplace]]" );
324 $newContent = $content->updateRedirect( $target );
326 $this->assertFalse( $content->equals( $newContent ), "content should have changed" );
327 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" );
329 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() );
332 # =================================================================================================================
334 public function testGetModel() {
335 $content = $this->newContent( "hello world." );
337 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getModel() );
340 public function testGetContentHandler() {
341 $content = $this->newContent( "hello world." );
343 $this->assertEquals( CONTENT_MODEL_WIKITEXT
, $content->getContentHandler()->getModelID() );
346 public function dataEquals( ) {
348 array( new WikitextContent( "hallo" ), null, false ),
349 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ),
350 array( new WikitextContent( "hallo" ), new JavascriptContent( "hallo" ), false ),
351 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ),
352 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ),
356 public function dataGetDeletionUpdates() {
358 array("WikitextContentTest_testGetSecondaryDataUpdates_1",
359 CONTENT_MODEL_WIKITEXT
, "hello ''world''\n",
360 array( 'LinksDeletionUpdate' => array( ) )
362 array("WikitextContentTest_testGetSecondaryDataUpdates_2",
363 CONTENT_MODEL_WIKITEXT
, "hello [[world test 21344]]\n",
364 array( 'LinksDeletionUpdate' => array( ) )