MediaWiki
REL1_24
|
00001 <?php 00002 00006 class RevisionTest extends MediaWikiTestCase { 00007 protected function setUp() { 00008 global $wgContLang; 00009 00010 parent::setUp(); 00011 00012 $this->setMwGlobals( array( 00013 'wgContLang' => Language::factory( 'en' ), 00014 'wgLanguageCode' => 'en', 00015 'wgLegacyEncoding' => false, 00016 'wgCompressRevisions' => false, 00017 00018 'wgContentHandlerTextFallback' => 'ignore', 00019 ) ); 00020 00021 $this->mergeMwGlobalArrayValue( 00022 'wgExtraNamespaces', 00023 array( 00024 12312 => 'Dummy', 00025 12313 => 'Dummy_talk', 00026 ) 00027 ); 00028 00029 $this->mergeMwGlobalArrayValue( 00030 'wgNamespaceContentModels', 00031 array( 00032 12312 => 'testing', 00033 ) 00034 ); 00035 00036 $this->mergeMwGlobalArrayValue( 00037 'wgContentHandlers', 00038 array( 00039 'testing' => 'DummyContentHandlerForTesting', 00040 'RevisionTestModifyableContent' => 'RevisionTestModifyableContentHandler', 00041 ) 00042 ); 00043 00044 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache 00045 $wgContLang->resetNamespaces(); # reset namespace cache 00046 } 00047 00048 function tearDown() { 00049 global $wgContLang; 00050 00051 MWNamespace::getCanonicalNamespaces( true ); # reset namespace cache 00052 $wgContLang->resetNamespaces(); # reset namespace cache 00053 00054 parent::tearDown(); 00055 } 00056 00060 public function testGetRevisionText() { 00061 $row = new stdClass; 00062 $row->old_flags = ''; 00063 $row->old_text = 'This is a bunch of revision text.'; 00064 $this->assertEquals( 00065 'This is a bunch of revision text.', 00066 Revision::getRevisionText( $row ) ); 00067 } 00068 00072 public function testGetRevisionTextGzip() { 00073 $this->checkPHPExtension( 'zlib' ); 00074 00075 $row = new stdClass; 00076 $row->old_flags = 'gzip'; 00077 $row->old_text = gzdeflate( 'This is a bunch of revision text.' ); 00078 $this->assertEquals( 00079 'This is a bunch of revision text.', 00080 Revision::getRevisionText( $row ) ); 00081 } 00082 00086 public function testGetRevisionTextUtf8Native() { 00087 $row = new stdClass; 00088 $row->old_flags = 'utf-8'; 00089 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00090 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00091 $this->assertEquals( 00092 "Wiki est l'\xc3\xa9cole superieur !", 00093 Revision::getRevisionText( $row ) ); 00094 } 00095 00099 public function testGetRevisionTextUtf8Legacy() { 00100 $row = new stdClass; 00101 $row->old_flags = ''; 00102 $row->old_text = "Wiki est l'\xe9cole superieur !"; 00103 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00104 $this->assertEquals( 00105 "Wiki est l'\xc3\xa9cole superieur !", 00106 Revision::getRevisionText( $row ) ); 00107 } 00108 00112 public function testGetRevisionTextUtf8NativeGzip() { 00113 $this->checkPHPExtension( 'zlib' ); 00114 00115 $row = new stdClass; 00116 $row->old_flags = 'gzip,utf-8'; 00117 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ); 00118 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00119 $this->assertEquals( 00120 "Wiki est l'\xc3\xa9cole superieur !", 00121 Revision::getRevisionText( $row ) ); 00122 } 00123 00127 public function testGetRevisionTextUtf8LegacyGzip() { 00128 $this->checkPHPExtension( 'zlib' ); 00129 00130 $row = new stdClass; 00131 $row->old_flags = 'gzip'; 00132 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" ); 00133 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00134 $this->assertEquals( 00135 "Wiki est l'\xc3\xa9cole superieur !", 00136 Revision::getRevisionText( $row ) ); 00137 } 00138 00142 public function testCompressRevisionTextUtf8() { 00143 $row = new stdClass; 00144 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00145 $row->old_flags = Revision::compressRevisionText( $row->old_text ); 00146 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), 00147 "Flags should contain 'utf-8'" ); 00148 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ), 00149 "Flags should not contain 'gzip'" ); 00150 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00151 $row->old_text, "Direct check" ); 00152 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00153 Revision::getRevisionText( $row ), "getRevisionText" ); 00154 } 00155 00159 public function testCompressRevisionTextUtf8Gzip() { 00160 $this->checkPHPExtension( 'zlib' ); 00161 $this->setMwGlobals( 'wgCompressRevisions', true ); 00162 00163 $row = new stdClass; 00164 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00165 $row->old_flags = Revision::compressRevisionText( $row->old_text ); 00166 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), 00167 "Flags should contain 'utf-8'" ); 00168 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ), 00169 "Flags should contain 'gzip'" ); 00170 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00171 gzinflate( $row->old_text ), "Direct check" ); 00172 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00173 Revision::getRevisionText( $row ), "getRevisionText" ); 00174 } 00175 00176 # ========================================================================= 00177 00186 function newTestRevision( $text, $title = "Test", 00187 $model = CONTENT_MODEL_WIKITEXT, $format = null 00188 ) { 00189 if ( is_string( $title ) ) { 00190 $title = Title::newFromText( $title ); 00191 } 00192 00193 $content = ContentHandler::makeContent( $text, $title, $model, $format ); 00194 00195 $rev = new Revision( 00196 array( 00197 'id' => 42, 00198 'page' => 23, 00199 'title' => $title, 00200 00201 'content' => $content, 00202 'length' => $content->getSize(), 00203 'comment' => "testing", 00204 'minor_edit' => false, 00205 00206 'content_format' => $format, 00207 ) 00208 ); 00209 00210 return $rev; 00211 } 00212 00213 function dataGetContentModel() { 00214 //NOTE: we expect the help namespace to always contain wikitext 00215 return array( 00216 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ), 00217 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ), 00218 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00219 ); 00220 } 00221 00227 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) { 00228 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00229 00230 $this->assertEquals( $expectedModel, $rev->getContentModel() ); 00231 } 00232 00233 function dataGetContentFormat() { 00234 //NOTE: we expect the help namespace to always contain wikitext 00235 return array( 00236 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ), 00237 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ), 00238 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ), 00239 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00240 ); 00241 } 00242 00248 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) { 00249 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00250 00251 $this->assertEquals( $expectedFormat, $rev->getContentFormat() ); 00252 } 00253 00254 function dataGetContentHandler() { 00255 //NOTE: we expect the help namespace to always contain wikitext 00256 return array( 00257 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ), 00258 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ), 00259 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ), 00260 ); 00261 } 00262 00268 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) { 00269 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00270 00271 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) ); 00272 } 00273 00274 function dataGetContent() { 00275 //NOTE: we expect the help namespace to always contain wikitext 00276 return array( 00277 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00278 array( 00279 serialize( 'hello world' ), 00280 'Hello', 00281 "testing", 00282 null, 00283 Revision::FOR_PUBLIC, 00284 serialize( 'hello world' ) 00285 ), 00286 array( 00287 serialize( 'hello world' ), 00288 'Dummy:Hello', 00289 null, 00290 null, 00291 Revision::FOR_PUBLIC, 00292 serialize( 'hello world' ) 00293 ), 00294 ); 00295 } 00296 00302 public function testGetContent( $text, $title, $model, $format, 00303 $audience, $expectedSerialization 00304 ) { 00305 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00306 $content = $rev->getContent( $audience ); 00307 00308 $this->assertEquals( 00309 $expectedSerialization, 00310 is_null( $content ) ? null : $content->serialize( $format ) 00311 ); 00312 } 00313 00314 function dataGetText() { 00315 //NOTE: we expect the help namespace to always contain wikitext 00316 return array( 00317 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00318 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ), 00319 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ), 00320 ); 00321 } 00322 00328 public function testGetText( $text, $title, $model, $format, $audience, $expectedText ) { 00329 $this->hideDeprecated( 'Revision::getText' ); 00330 00331 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00332 00333 $this->assertEquals( $expectedText, $rev->getText( $audience ) ); 00334 } 00335 00341 public function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) { 00342 $this->hideDeprecated( 'Revision::getRawText' ); 00343 00344 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00345 00346 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) ); 00347 } 00348 00349 public function dataGetSize() { 00350 return array( 00351 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ), 00352 array( serialize( "hello world." ), "testing", 12 ), 00353 ); 00354 } 00355 00361 public function testGetSize( $text, $model, $expected_size ) { 00362 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model ); 00363 $this->assertEquals( $expected_size, $rev->getSize() ); 00364 } 00365 00366 public function dataGetSha1() { 00367 return array( 00368 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ), 00369 array( 00370 serialize( "hello world." ), 00371 "testing", 00372 Revision::base36Sha1( serialize( "hello world." ) ) 00373 ), 00374 ); 00375 } 00376 00382 public function testGetSha1( $text, $model, $expected_hash ) { 00383 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model ); 00384 $this->assertEquals( $expected_hash, $rev->getSha1() ); 00385 } 00386 00390 public function testConstructWithText() { 00391 $this->hideDeprecated( "Revision::getText" ); 00392 00393 $rev = new Revision( array( 00394 'text' => 'hello world.', 00395 'content_model' => CONTENT_MODEL_JAVASCRIPT 00396 ) ); 00397 00398 $this->assertNotNull( $rev->getText(), 'no content text' ); 00399 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00400 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00401 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00402 } 00403 00407 public function testConstructWithContent() { 00408 $this->hideDeprecated( "Revision::getText" ); 00409 00410 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' ); 00411 00412 $rev = new Revision( array( 00413 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ), 00414 ) ); 00415 00416 $this->assertNotNull( $rev->getText(), 'no content text' ); 00417 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00418 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00419 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00420 } 00421 00428 public function testGetContentClone() { 00429 $content = new RevisionTestModifyableContent( "foo" ); 00430 00431 $rev = new Revision( 00432 array( 00433 'id' => 42, 00434 'page' => 23, 00435 'title' => Title::newFromText( "testGetContentClone_dummy" ), 00436 00437 'content' => $content, 00438 'length' => $content->getSize(), 00439 'comment' => "testing", 00440 'minor_edit' => false, 00441 ) 00442 ); 00443 00444 $content = $rev->getContent( Revision::RAW ); 00445 $content->setText( "bar" ); 00446 00447 $content2 = $rev->getContent( Revision::RAW ); 00448 // content is mutable, expect clone 00449 $this->assertNotSame( $content, $content2, "expected a clone" ); 00450 // clone should contain the original text 00451 $this->assertEquals( "foo", $content2->getText() ); 00452 00453 $content2->setText( "bla bla" ); 00454 $this->assertEquals( "bar", $content->getText() ); // clones should be independent 00455 } 00456 00463 public function testGetContentUncloned() { 00464 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT ); 00465 $content = $rev->getContent( Revision::RAW ); 00466 $content2 = $rev->getContent( Revision::RAW ); 00467 00468 // for immutable content like wikitext, this should be the same object 00469 $this->assertSame( $content, $content2 ); 00470 } 00471 } 00472 00473 class RevisionTestModifyableContent extends TextContent { 00474 public function __construct( $text ) { 00475 parent::__construct( $text, "RevisionTestModifyableContent" ); 00476 } 00477 00478 public function copy() { 00479 return new RevisionTestModifyableContent( $this->mText ); 00480 } 00481 00482 public function getText() { 00483 return $this->mText; 00484 } 00485 00486 public function setText( $text ) { 00487 $this->mText = $text; 00488 } 00489 } 00490 00491 class RevisionTestModifyableContentHandler extends TextContentHandler { 00492 00493 public function __construct() { 00494 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) ); 00495 } 00496 00497 public function unserializeContent( $text, $format = null ) { 00498 $this->checkFormat( $format ); 00499 00500 return new RevisionTestModifyableContent( $text ); 00501 } 00502 00503 public function makeEmptyContent() { 00504 return new RevisionTestModifyableContent( '' ); 00505 } 00506 }