MediaWiki
REL1_24
|
00001 <?php 00002 00009 class WikitextContentTest extends TextContentTest { 00010 public static $sections = "Intro 00011 00012 == stuff == 00013 hello world 00014 00015 == test == 00016 just a test 00017 00018 == foo == 00019 more stuff 00020 "; 00021 00022 public function newContent( $text ) { 00023 return new WikitextContent( $text ); 00024 } 00025 00026 public static function dataGetParserOutput() { 00027 return array( 00028 array( 00029 "WikitextContentTest_testGetParserOutput", 00030 CONTENT_MODEL_WIKITEXT, 00031 "hello ''world''\n", 00032 "<p>hello <i>world</i>\n</p>" 00033 ), 00034 // TODO: more...? 00035 ); 00036 } 00037 00038 public static function dataGetSecondaryDataUpdates() { 00039 return array( 00040 array( "WikitextContentTest_testGetSecondaryDataUpdates_1", 00041 CONTENT_MODEL_WIKITEXT, "hello ''world''\n", 00042 array( 00043 'LinksUpdate' => array( 00044 'mRecursive' => true, 00045 'mLinks' => array() 00046 ) 00047 ) 00048 ), 00049 array( "WikitextContentTest_testGetSecondaryDataUpdates_2", 00050 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n", 00051 array( 00052 'LinksUpdate' => array( 00053 'mRecursive' => true, 00054 'mLinks' => array( 00055 array( 'World_test_21344' => 0 ) 00056 ) 00057 ) 00058 ) 00059 ), 00060 // TODO: more...? 00061 ); 00062 } 00063 00069 public function testGetSecondaryDataUpdates( $title, $model, $text, $expectedStuff ) { 00070 $ns = $this->getDefaultWikitextNS(); 00071 $title = Title::newFromText( $title, $ns ); 00072 00073 $content = ContentHandler::makeContent( $text, $title, $model ); 00074 00075 $page = WikiPage::factory( $title ); 00076 $page->doEditContent( $content, '' ); 00077 00078 $updates = $content->getSecondaryDataUpdates( $title ); 00079 00080 // make updates accessible by class name 00081 foreach ( $updates as $update ) { 00082 $class = get_class( $update ); 00083 $updates[$class] = $update; 00084 } 00085 00086 foreach ( $expectedStuff as $class => $fieldValues ) { 00087 $this->assertArrayHasKey( $class, $updates, "missing an update of type $class" ); 00088 00089 $update = $updates[$class]; 00090 00091 foreach ( $fieldValues as $field => $value ) { 00092 $v = $update->$field; #if the field doesn't exist, just crash and burn 00093 $this->assertEquals( $value, $v, "unexpected value for field $field in instance of $class" ); 00094 } 00095 } 00096 00097 $page->doDeleteArticle( '' ); 00098 } 00099 00100 public static function dataGetSection() { 00101 return array( 00102 array( WikitextContentTest::$sections, 00103 "0", 00104 "Intro" 00105 ), 00106 array( WikitextContentTest::$sections, 00107 "2", 00108 "== test == 00109 just a test" 00110 ), 00111 array( WikitextContentTest::$sections, 00112 "8", 00113 false 00114 ), 00115 ); 00116 } 00117 00122 public function testGetSection( $text, $sectionId, $expectedText ) { 00123 $content = $this->newContent( $text ); 00124 00125 $sectionContent = $content->getSection( $sectionId ); 00126 if ( is_object( $sectionContent ) ) { 00127 $sectionText = $sectionContent->getNativeData(); 00128 } else { 00129 $sectionText = $sectionContent; 00130 } 00131 00132 $this->assertEquals( $expectedText, $sectionText ); 00133 } 00134 00135 public static function dataReplaceSection() { 00136 return array( 00137 array( WikitextContentTest::$sections, 00138 "0", 00139 "No more", 00140 null, 00141 trim( preg_replace( '/^Intro/sm', 'No more', WikitextContentTest::$sections ) ) 00142 ), 00143 array( WikitextContentTest::$sections, 00144 "", 00145 "No more", 00146 null, 00147 "No more" 00148 ), 00149 array( WikitextContentTest::$sections, 00150 "2", 00151 "== TEST ==\nmore fun", 00152 null, 00153 trim( preg_replace( 00154 '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", 00155 WikitextContentTest::$sections 00156 ) ) 00157 ), 00158 array( WikitextContentTest::$sections, 00159 "8", 00160 "No more", 00161 null, 00162 WikitextContentTest::$sections 00163 ), 00164 array( WikitextContentTest::$sections, 00165 "new", 00166 "No more", 00167 "New", 00168 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more" 00169 ), 00170 ); 00171 } 00172 00177 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) { 00178 $content = $this->newContent( $text ); 00179 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle ); 00180 00181 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() ); 00182 } 00183 00187 public function testAddSectionHeader() { 00188 $content = $this->newContent( 'hello world' ); 00189 $content = $content->addSectionHeader( 'test' ); 00190 00191 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() ); 00192 } 00193 00194 public static function dataPreSaveTransform() { 00195 return array( 00196 array( 'hello this is ~~~', 00197 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]", 00198 ), 00199 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 00200 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 00201 ), 00202 array( // rtrim 00203 " Foo \n ", 00204 " Foo", 00205 ), 00206 ); 00207 } 00208 00209 public static function dataPreloadTransform() { 00210 return array( 00211 array( 'hello this is ~~~', 00212 "hello this is ~~~", 00213 ), 00214 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>', 00215 'hello \'\'this\'\' is bar', 00216 ), 00217 ); 00218 } 00219 00220 public static function dataGetRedirectTarget() { 00221 return array( 00222 array( '#REDIRECT [[Test]]', 00223 'Test', 00224 ), 00225 array( '#REDIRECT Test', 00226 null, 00227 ), 00228 array( '* #REDIRECT [[Test]]', 00229 null, 00230 ), 00231 ); 00232 } 00233 00234 public static function dataGetTextForSummary() { 00235 return array( 00236 array( "hello\nworld.", 00237 16, 00238 'hello world.', 00239 ), 00240 array( 'hello world.', 00241 8, 00242 'hello...', 00243 ), 00244 array( '[[hello world]].', 00245 8, 00246 'hel...', 00247 ), 00248 ); 00249 } 00250 00251 public static function dataIsCountable() { 00252 return array( 00253 array( '', 00254 null, 00255 'any', 00256 true 00257 ), 00258 array( 'Foo', 00259 null, 00260 'any', 00261 true 00262 ), 00263 array( 'Foo', 00264 null, 00265 'comma', 00266 false 00267 ), 00268 array( 'Foo, bar', 00269 null, 00270 'comma', 00271 true 00272 ), 00273 array( 'Foo', 00274 null, 00275 'link', 00276 false 00277 ), 00278 array( 'Foo [[bar]]', 00279 null, 00280 'link', 00281 true 00282 ), 00283 array( 'Foo', 00284 true, 00285 'link', 00286 true 00287 ), 00288 array( 'Foo [[bar]]', 00289 false, 00290 'link', 00291 false 00292 ), 00293 array( '#REDIRECT [[bar]]', 00294 true, 00295 'any', 00296 false 00297 ), 00298 array( '#REDIRECT [[bar]]', 00299 true, 00300 'comma', 00301 false 00302 ), 00303 array( '#REDIRECT [[bar]]', 00304 true, 00305 'link', 00306 false 00307 ), 00308 ); 00309 } 00310 00314 public function testMatchMagicWord() { 00315 $mw = MagicWord::get( "staticredirect" ); 00316 00317 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" ); 00318 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" ); 00319 00320 $content = $this->newContent( "#REDIRECT [[FOO]]" ); 00321 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" ); 00322 } 00323 00327 public function testUpdateRedirect() { 00328 $target = Title::newFromText( "testUpdateRedirect_target" ); 00329 00330 // test with non-redirect page 00331 $content = $this->newContent( "hello world." ); 00332 $newContent = $content->updateRedirect( $target ); 00333 00334 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" ); 00335 00336 // test with actual redirect 00337 $content = $this->newContent( "#REDIRECT [[Someplace]]" ); 00338 $newContent = $content->updateRedirect( $target ); 00339 00340 $this->assertFalse( $content->equals( $newContent ), "content should have changed" ); 00341 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" ); 00342 00343 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() ); 00344 } 00345 00349 public function testGetModel() { 00350 $content = $this->newContent( "hello world." ); 00351 00352 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() ); 00353 } 00354 00358 public function testGetContentHandler() { 00359 $content = $this->newContent( "hello world." ); 00360 00361 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() ); 00362 } 00363 00364 public function testRedirectParserOption() { 00365 $title = Title::newFromText( 'testRedirectParserOption' ); 00366 00367 // Set up hook and its reporting variables 00368 $wikitext = null; 00369 $redirectTarget = null; 00370 $this->mergeMwGlobalArrayValue( 'wgHooks', array( 00371 'InternalParseBeforeLinks' => array( 00372 function ( &$parser, &$text, &$stripState ) use ( &$wikitext, &$redirectTarget ) { 00373 $wikitext = $text; 00374 $redirectTarget = $parser->getOptions()->getRedirectTarget(); 00375 } 00376 ) 00377 ) ); 00378 00379 // Test with non-redirect page 00380 $wikitext = false; 00381 $redirectTarget = false; 00382 $content = $this->newContent( 'hello world.' ); 00383 $options = $content->getContentHandler()->makeParserOptions( 'canonical' ); 00384 $options->setRedirectTarget( $title ); 00385 $content->getParserOutput( $title, null, $options ); 00386 $this->assertEquals( 'hello world.', $wikitext, 00387 'Wikitext passed to hook was not as expected' 00388 ); 00389 $this->assertEquals( null, $redirectTarget, 'Redirect seen in hook was not null' ); 00390 $this->assertEquals( $title, $options->getRedirectTarget(), 00391 'ParserOptions\' redirectTarget was changed' 00392 ); 00393 00394 // Test with a redirect page 00395 $wikitext = false; 00396 $redirectTarget = false; 00397 $content = $this->newContent( "#REDIRECT [[TestRedirectParserOption/redir]]\nhello redirect." ); 00398 $options = $content->getContentHandler()->makeParserOptions( 'canonical' ); 00399 $content->getParserOutput( $title, null, $options ); 00400 $this->assertEquals( 'hello redirect.', $wikitext, 'Wikitext passed to hook was not as expected' ); 00401 $this->assertNotEquals( null, $redirectTarget, 'Redirect seen in hook was null' ); 00402 $this->assertEquals( 'TestRedirectParserOption/redir', $redirectTarget->getFullText(), 00403 'Redirect seen in hook was not the expected title' 00404 ); 00405 $this->assertEquals( null, $options->getRedirectTarget(), 00406 'ParserOptions\' redirectTarget was changed' 00407 ); 00408 } 00409 00410 public static function dataEquals() { 00411 return array( 00412 array( new WikitextContent( "hallo" ), null, false ), 00413 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ), 00414 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ), 00415 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ), 00416 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ), 00417 ); 00418 } 00419 00420 public static function dataGetDeletionUpdates() { 00421 return array( 00422 array( "WikitextContentTest_testGetSecondaryDataUpdates_1", 00423 CONTENT_MODEL_WIKITEXT, "hello ''world''\n", 00424 array( 'LinksDeletionUpdate' => array() ) 00425 ), 00426 array( "WikitextContentTest_testGetSecondaryDataUpdates_2", 00427 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n", 00428 array( 'LinksDeletionUpdate' => array() ) 00429 ), 00430 // @todo more...? 00431 ); 00432 } 00433 }