MediaWiki
REL1_22
|
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", $model = CONTENT_MODEL_WIKITEXT, $format = null ) { 00187 if ( is_string( $title ) ) { 00188 $title = Title::newFromText( $title ); 00189 } 00190 00191 $content = ContentHandler::makeContent( $text, $title, $model, $format ); 00192 00193 $rev = new Revision( 00194 array( 00195 'id' => 42, 00196 'page' => 23, 00197 'title' => $title, 00198 00199 'content' => $content, 00200 'length' => $content->getSize(), 00201 'comment' => "testing", 00202 'minor_edit' => false, 00203 00204 'content_format' => $format, 00205 ) 00206 ); 00207 00208 return $rev; 00209 } 00210 00211 function dataGetContentModel() { 00212 //NOTE: we expect the help namespace to always contain wikitext 00213 return array( 00214 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ), 00215 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ), 00216 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00217 ); 00218 } 00219 00225 public function testGetContentModel( $text, $title, $model, $format, $expectedModel ) { 00226 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00227 00228 $this->assertEquals( $expectedModel, $rev->getContentModel() ); 00229 } 00230 00231 function dataGetContentFormat() { 00232 //NOTE: we expect the help namespace to always contain wikitext 00233 return array( 00234 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ), 00235 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ), 00236 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ), 00237 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00238 ); 00239 } 00240 00246 public function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) { 00247 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00248 00249 $this->assertEquals( $expectedFormat, $rev->getContentFormat() ); 00250 } 00251 00252 function dataGetContentHandler() { 00253 //NOTE: we expect the help namespace to always contain wikitext 00254 return array( 00255 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ), 00256 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ), 00257 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ), 00258 ); 00259 } 00260 00266 public function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) { 00267 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00268 00269 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) ); 00270 } 00271 00272 function dataGetContent() { 00273 //NOTE: we expect the help namespace to always contain wikitext 00274 return array( 00275 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00276 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ), 00277 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ), 00278 ); 00279 } 00280 00286 public function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) { 00287 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00288 $content = $rev->getContent( $audience ); 00289 00290 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) ); 00291 } 00292 00293 function dataGetText() { 00294 //NOTE: we expect the help namespace to always contain wikitext 00295 return array( 00296 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00297 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ), 00298 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ), 00299 ); 00300 } 00301 00307 public function testGetText( $text, $title, $model, $format, $audience, $expectedText ) { 00308 $this->hideDeprecated( 'Revision::getText' ); 00309 00310 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00311 00312 $this->assertEquals( $expectedText, $rev->getText( $audience ) ); 00313 } 00314 00320 public function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) { 00321 $this->hideDeprecated( 'Revision::getRawText' ); 00322 00323 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00324 00325 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) ); 00326 } 00327 00328 00329 public function dataGetSize() { 00330 return array( 00331 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ), 00332 array( serialize( "hello world." ), "testing", 12 ), 00333 ); 00334 } 00335 00341 public function testGetSize( $text, $model, $expected_size ) { 00342 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model ); 00343 $this->assertEquals( $expected_size, $rev->getSize() ); 00344 } 00345 00346 public function dataGetSha1() { 00347 return array( 00348 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ), 00349 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ), 00350 ); 00351 } 00352 00358 public function testGetSha1( $text, $model, $expected_hash ) { 00359 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model ); 00360 $this->assertEquals( $expected_hash, $rev->getSha1() ); 00361 } 00362 00366 public function testConstructWithText() { 00367 $this->hideDeprecated( "Revision::getText" ); 00368 00369 $rev = new Revision( array( 00370 'text' => 'hello world.', 00371 'content_model' => CONTENT_MODEL_JAVASCRIPT 00372 ) ); 00373 00374 $this->assertNotNull( $rev->getText(), 'no content text' ); 00375 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00376 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00377 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00378 } 00379 00383 public function testConstructWithContent() { 00384 $this->hideDeprecated( "Revision::getText" ); 00385 00386 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' ); 00387 00388 $rev = new Revision( array( 00389 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ), 00390 ) ); 00391 00392 $this->assertNotNull( $rev->getText(), 'no content text' ); 00393 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00394 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00395 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00396 } 00397 00404 public function testGetContentClone() { 00405 $content = new RevisionTestModifyableContent( "foo" ); 00406 00407 $rev = new Revision( 00408 array( 00409 'id' => 42, 00410 'page' => 23, 00411 'title' => Title::newFromText( "testGetContentClone_dummy" ), 00412 00413 'content' => $content, 00414 'length' => $content->getSize(), 00415 'comment' => "testing", 00416 'minor_edit' => false, 00417 ) 00418 ); 00419 00420 $content = $rev->getContent( Revision::RAW ); 00421 $content->setText( "bar" ); 00422 00423 $content2 = $rev->getContent( Revision::RAW ); 00424 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone 00425 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text 00426 00427 $content2->setText( "bla bla" ); 00428 $this->assertEquals( "bar", $content->getText() ); // clones should be independent 00429 } 00430 00431 00438 public function testGetContentUncloned() { 00439 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT ); 00440 $content = $rev->getContent( Revision::RAW ); 00441 $content2 = $rev->getContent( Revision::RAW ); 00442 00443 // for immutable content like wikitext, this should be the same object 00444 $this->assertSame( $content, $content2 ); 00445 } 00446 } 00447 00448 class RevisionTestModifyableContent extends TextContent { 00449 public function __construct( $text ) { 00450 parent::__construct( $text, "RevisionTestModifyableContent" ); 00451 } 00452 00453 public function copy() { 00454 return new RevisionTestModifyableContent( $this->mText ); 00455 } 00456 00457 public function getText() { 00458 return $this->mText; 00459 } 00460 00461 public function setText( $text ) { 00462 $this->mText = $text; 00463 } 00464 } 00465 00466 class RevisionTestModifyableContentHandler extends TextContentHandler { 00467 00468 public function __construct() { 00469 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) ); 00470 } 00471 00472 public function unserializeContent( $text, $format = null ) { 00473 $this->checkFormat( $format ); 00474 00475 return new RevisionTestModifyableContent( $text ); 00476 } 00477 00478 public function makeEmptyContent() { 00479 return new RevisionTestModifyableContent( '' ); 00480 } 00481 }