MediaWiki
REL1_23
|
00001 <?php 00002 00009 class WikitextContentTest extends TextContentTest { 00010 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( '/^== test ==.*== foo ==/sm', "== TEST ==\nmore fun\n\n== foo ==", WikitextContentTest::$sections ) ) 00154 ), 00155 array( WikitextContentTest::$sections, 00156 "8", 00157 "No more", 00158 null, 00159 WikitextContentTest::$sections 00160 ), 00161 array( WikitextContentTest::$sections, 00162 "new", 00163 "No more", 00164 "New", 00165 trim( WikitextContentTest::$sections ) . "\n\n\n== New ==\n\nNo more" 00166 ), 00167 ); 00168 } 00169 00174 public function testReplaceSection( $text, $section, $with, $sectionTitle, $expected ) { 00175 $content = $this->newContent( $text ); 00176 $c = $content->replaceSection( $section, $this->newContent( $with ), $sectionTitle ); 00177 00178 $this->assertEquals( $expected, is_null( $c ) ? null : $c->getNativeData() ); 00179 } 00180 00184 public function testAddSectionHeader() { 00185 $content = $this->newContent( 'hello world' ); 00186 $content = $content->addSectionHeader( 'test' ); 00187 00188 $this->assertEquals( "== test ==\n\nhello world", $content->getNativeData() ); 00189 } 00190 00191 public static function dataPreSaveTransform() { 00192 return array( 00193 array( 'hello this is ~~~', 00194 "hello this is [[Special:Contributions/127.0.0.1|127.0.0.1]]", 00195 ), 00196 array( 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 00197 'hello \'\'this\'\' is <nowiki>~~~</nowiki>', 00198 ), 00199 array( // rtrim 00200 " Foo \n ", 00201 " Foo", 00202 ), 00203 ); 00204 } 00205 00206 public static function dataPreloadTransform() { 00207 return array( 00208 array( 'hello this is ~~~', 00209 "hello this is ~~~", 00210 ), 00211 array( 'hello \'\'this\'\' is <noinclude>foo</noinclude><includeonly>bar</includeonly>', 00212 'hello \'\'this\'\' is bar', 00213 ), 00214 ); 00215 } 00216 00217 public static function dataGetRedirectTarget() { 00218 return array( 00219 array( '#REDIRECT [[Test]]', 00220 'Test', 00221 ), 00222 array( '#REDIRECT Test', 00223 null, 00224 ), 00225 array( '* #REDIRECT [[Test]]', 00226 null, 00227 ), 00228 ); 00229 } 00230 00231 public static function dataGetTextForSummary() { 00232 return array( 00233 array( "hello\nworld.", 00234 16, 00235 'hello world.', 00236 ), 00237 array( 'hello world.', 00238 8, 00239 'hello...', 00240 ), 00241 array( '[[hello world]].', 00242 8, 00243 'hel...', 00244 ), 00245 ); 00246 } 00247 00248 public static function dataIsCountable() { 00249 return array( 00250 array( '', 00251 null, 00252 'any', 00253 true 00254 ), 00255 array( 'Foo', 00256 null, 00257 'any', 00258 true 00259 ), 00260 array( 'Foo', 00261 null, 00262 'comma', 00263 false 00264 ), 00265 array( 'Foo, bar', 00266 null, 00267 'comma', 00268 true 00269 ), 00270 array( 'Foo', 00271 null, 00272 'link', 00273 false 00274 ), 00275 array( 'Foo [[bar]]', 00276 null, 00277 'link', 00278 true 00279 ), 00280 array( 'Foo', 00281 true, 00282 'link', 00283 true 00284 ), 00285 array( 'Foo [[bar]]', 00286 false, 00287 'link', 00288 false 00289 ), 00290 array( '#REDIRECT [[bar]]', 00291 true, 00292 'any', 00293 false 00294 ), 00295 array( '#REDIRECT [[bar]]', 00296 true, 00297 'comma', 00298 false 00299 ), 00300 array( '#REDIRECT [[bar]]', 00301 true, 00302 'link', 00303 false 00304 ), 00305 ); 00306 } 00307 00311 public function testMatchMagicWord() { 00312 $mw = MagicWord::get( "staticredirect" ); 00313 00314 $content = $this->newContent( "#REDIRECT [[FOO]]\n__STATICREDIRECT__" ); 00315 $this->assertTrue( $content->matchMagicWord( $mw ), "should have matched magic word" ); 00316 00317 $content = $this->newContent( "#REDIRECT [[FOO]]" ); 00318 $this->assertFalse( $content->matchMagicWord( $mw ), "should not have matched magic word" ); 00319 } 00320 00324 public function testUpdateRedirect() { 00325 $target = Title::newFromText( "testUpdateRedirect_target" ); 00326 00327 // test with non-redirect page 00328 $content = $this->newContent( "hello world." ); 00329 $newContent = $content->updateRedirect( $target ); 00330 00331 $this->assertTrue( $content->equals( $newContent ), "content should be unchanged" ); 00332 00333 // test with actual redirect 00334 $content = $this->newContent( "#REDIRECT [[Someplace]]" ); 00335 $newContent = $content->updateRedirect( $target ); 00336 00337 $this->assertFalse( $content->equals( $newContent ), "content should have changed" ); 00338 $this->assertTrue( $newContent->isRedirect(), "new content should be a redirect" ); 00339 00340 $this->assertEquals( $target->getFullText(), $newContent->getRedirectTarget()->getFullText() ); 00341 } 00342 00346 public function testGetModel() { 00347 $content = $this->newContent( "hello world." ); 00348 00349 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getModel() ); 00350 } 00351 00355 public function testGetContentHandler() { 00356 $content = $this->newContent( "hello world." ); 00357 00358 $this->assertEquals( CONTENT_MODEL_WIKITEXT, $content->getContentHandler()->getModelID() ); 00359 } 00360 00361 public static function dataEquals() { 00362 return array( 00363 array( new WikitextContent( "hallo" ), null, false ), 00364 array( new WikitextContent( "hallo" ), new WikitextContent( "hallo" ), true ), 00365 array( new WikitextContent( "hallo" ), new JavaScriptContent( "hallo" ), false ), 00366 array( new WikitextContent( "hallo" ), new TextContent( "hallo" ), false ), 00367 array( new WikitextContent( "hallo" ), new WikitextContent( "HALLO" ), false ), 00368 ); 00369 } 00370 00371 public static function dataGetDeletionUpdates() { 00372 return array( 00373 array( "WikitextContentTest_testGetSecondaryDataUpdates_1", 00374 CONTENT_MODEL_WIKITEXT, "hello ''world''\n", 00375 array( 'LinksDeletionUpdate' => array() ) 00376 ), 00377 array( "WikitextContentTest_testGetSecondaryDataUpdates_2", 00378 CONTENT_MODEL_WIKITEXT, "hello [[world test 21344]]\n", 00379 array( 'LinksDeletionUpdate' => array() ) 00380 ), 00381 // @todo more...? 00382 ); 00383 } 00384 }