MediaWiki
REL1_21
|
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 00057 function testGetRevisionText() { 00058 $row = new stdClass; 00059 $row->old_flags = ''; 00060 $row->old_text = 'This is a bunch of revision text.'; 00061 $this->assertEquals( 00062 'This is a bunch of revision text.', 00063 Revision::getRevisionText( $row ) ); 00064 } 00065 00066 function testGetRevisionTextGzip() { 00067 $this->checkPHPExtension( 'zlib' ); 00068 00069 $row = new stdClass; 00070 $row->old_flags = 'gzip'; 00071 $row->old_text = gzdeflate( 'This is a bunch of revision text.' ); 00072 $this->assertEquals( 00073 'This is a bunch of revision text.', 00074 Revision::getRevisionText( $row ) ); 00075 } 00076 00077 function testGetRevisionTextUtf8Native() { 00078 $row = new stdClass; 00079 $row->old_flags = 'utf-8'; 00080 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00081 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00082 $this->assertEquals( 00083 "Wiki est l'\xc3\xa9cole superieur !", 00084 Revision::getRevisionText( $row ) ); 00085 } 00086 00087 function testGetRevisionTextUtf8Legacy() { 00088 $row = new stdClass; 00089 $row->old_flags = ''; 00090 $row->old_text = "Wiki est l'\xe9cole superieur !"; 00091 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00092 $this->assertEquals( 00093 "Wiki est l'\xc3\xa9cole superieur !", 00094 Revision::getRevisionText( $row ) ); 00095 } 00096 00097 function testGetRevisionTextUtf8NativeGzip() { 00098 $this->checkPHPExtension( 'zlib' ); 00099 00100 $row = new stdClass; 00101 $row->old_flags = 'gzip,utf-8'; 00102 $row->old_text = gzdeflate( "Wiki est l'\xc3\xa9cole superieur !" ); 00103 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00104 $this->assertEquals( 00105 "Wiki est l'\xc3\xa9cole superieur !", 00106 Revision::getRevisionText( $row ) ); 00107 } 00108 00109 function testGetRevisionTextUtf8LegacyGzip() { 00110 $this->checkPHPExtension( 'zlib' ); 00111 00112 $row = new stdClass; 00113 $row->old_flags = 'gzip'; 00114 $row->old_text = gzdeflate( "Wiki est l'\xe9cole superieur !" ); 00115 $GLOBALS['wgLegacyEncoding'] = 'iso-8859-1'; 00116 $this->assertEquals( 00117 "Wiki est l'\xc3\xa9cole superieur !", 00118 Revision::getRevisionText( $row ) ); 00119 } 00120 00121 function testCompressRevisionTextUtf8() { 00122 $row = new stdClass; 00123 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00124 $row->old_flags = Revision::compressRevisionText( $row->old_text ); 00125 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), 00126 "Flags should contain 'utf-8'" ); 00127 $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ), 00128 "Flags should not contain 'gzip'" ); 00129 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00130 $row->old_text, "Direct check" ); 00131 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00132 Revision::getRevisionText( $row ), "getRevisionText" ); 00133 } 00134 00135 function testCompressRevisionTextUtf8Gzip() { 00136 $this->checkPHPExtension( 'zlib' ); 00137 00138 global $wgCompressRevisions; 00139 $wgCompressRevisions = true; 00140 00141 $row = new stdClass; 00142 $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; 00143 $row->old_flags = Revision::compressRevisionText( $row->old_text ); 00144 $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), 00145 "Flags should contain 'utf-8'" ); 00146 $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ), 00147 "Flags should contain 'gzip'" ); 00148 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00149 gzinflate( $row->old_text ), "Direct check" ); 00150 $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", 00151 Revision::getRevisionText( $row ), "getRevisionText" ); 00152 } 00153 00154 # ================================================================================================================= 00155 00162 function newTestRevision( $text, $title = "Test", $model = CONTENT_MODEL_WIKITEXT, $format = null ) { 00163 if ( is_string( $title ) ) { 00164 $title = Title::newFromText( $title ); 00165 } 00166 00167 $content = ContentHandler::makeContent( $text, $title, $model, $format ); 00168 00169 $rev = new Revision( 00170 array( 00171 'id' => 42, 00172 'page' => 23, 00173 'title' => $title, 00174 00175 'content' => $content, 00176 'length' => $content->getSize(), 00177 'comment' => "testing", 00178 'minor_edit' => false, 00179 00180 'content_format' => $format, 00181 ) 00182 ); 00183 00184 return $rev; 00185 } 00186 00187 function dataGetContentModel() { 00188 //NOTE: we expect the help namespace to always contain wikitext 00189 return array( 00190 array( 'hello world', 'Help:Hello', null, null, CONTENT_MODEL_WIKITEXT ), 00191 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_MODEL_CSS ), 00192 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00193 ); 00194 } 00195 00200 function testGetContentModel( $text, $title, $model, $format, $expectedModel ) { 00201 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00202 00203 $this->assertEquals( $expectedModel, $rev->getContentModel() ); 00204 } 00205 00206 function dataGetContentFormat() { 00207 //NOTE: we expect the help namespace to always contain wikitext 00208 return array( 00209 array( 'hello world', 'Help:Hello', null, null, CONTENT_FORMAT_WIKITEXT ), 00210 array( 'hello world', 'Help:Hello', CONTENT_MODEL_CSS, null, CONTENT_FORMAT_CSS ), 00211 array( 'hello world', 'User:hello/there.css', null, null, CONTENT_FORMAT_CSS ), 00212 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, "testing" ), 00213 ); 00214 } 00215 00220 function testGetContentFormat( $text, $title, $model, $format, $expectedFormat ) { 00221 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00222 00223 $this->assertEquals( $expectedFormat, $rev->getContentFormat() ); 00224 } 00225 00226 function dataGetContentHandler() { 00227 //NOTE: we expect the help namespace to always contain wikitext 00228 return array( 00229 array( 'hello world', 'Help:Hello', null, null, 'WikitextContentHandler' ), 00230 array( 'hello world', 'User:hello/there.css', null, null, 'CssContentHandler' ), 00231 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, 'DummyContentHandlerForTesting' ), 00232 ); 00233 } 00234 00239 function testGetContentHandler( $text, $title, $model, $format, $expectedClass ) { 00240 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00241 00242 $this->assertEquals( $expectedClass, get_class( $rev->getContentHandler() ) ); 00243 } 00244 00245 function dataGetContent() { 00246 //NOTE: we expect the help namespace to always contain wikitext 00247 return array( 00248 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00249 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ), 00250 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, serialize( 'hello world' ) ), 00251 ); 00252 } 00253 00258 function testGetContent( $text, $title, $model, $format, $audience, $expectedSerialization ) { 00259 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00260 $content = $rev->getContent( $audience ); 00261 00262 $this->assertEquals( $expectedSerialization, is_null( $content ) ? null : $content->serialize( $format ) ); 00263 } 00264 00265 function dataGetText() { 00266 //NOTE: we expect the help namespace to always contain wikitext 00267 return array( 00268 array( 'hello world', 'Help:Hello', null, null, Revision::FOR_PUBLIC, 'hello world' ), 00269 array( serialize( 'hello world' ), 'Hello', "testing", null, Revision::FOR_PUBLIC, null ), 00270 array( serialize( 'hello world' ), 'Dummy:Hello', null, null, Revision::FOR_PUBLIC, null ), 00271 ); 00272 } 00273 00278 function testGetText( $text, $title, $model, $format, $audience, $expectedText ) { 00279 $this->hideDeprecated( 'Revision::getText' ); 00280 00281 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00282 00283 $this->assertEquals( $expectedText, $rev->getText( $audience ) ); 00284 } 00285 00290 function testGetRawText( $text, $title, $model, $format, $audience, $expectedText ) { 00291 $this->hideDeprecated( 'Revision::getRawText' ); 00292 00293 $rev = $this->newTestRevision( $text, $title, $model, $format ); 00294 00295 $this->assertEquals( $expectedText, $rev->getRawText( $audience ) ); 00296 } 00297 00298 00299 public function dataGetSize() { 00300 return array( 00301 array( "hello world.", CONTENT_MODEL_WIKITEXT, 12 ), 00302 array( serialize( "hello world." ), "testing", 12 ), 00303 ); 00304 } 00305 00311 public function testGetSize( $text, $model, $expected_size ) { 00312 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSize', $model ); 00313 $this->assertEquals( $expected_size, $rev->getSize() ); 00314 } 00315 00316 public function dataGetSha1() { 00317 return array( 00318 array( "hello world.", CONTENT_MODEL_WIKITEXT, Revision::base36Sha1( "hello world." ) ), 00319 array( serialize( "hello world." ), "testing", Revision::base36Sha1( serialize( "hello world." ) ) ), 00320 ); 00321 } 00322 00328 public function testGetSha1( $text, $model, $expected_hash ) { 00329 $rev = $this->newTestRevision( $text, 'RevisionTest_testGetSha1', $model ); 00330 $this->assertEquals( $expected_hash, $rev->getSha1() ); 00331 } 00332 00333 public function testConstructWithText() { 00334 $this->hideDeprecated( "Revision::getText" ); 00335 00336 $rev = new Revision( array( 00337 'text' => 'hello world.', 00338 'content_model' => CONTENT_MODEL_JAVASCRIPT 00339 ) ); 00340 00341 $this->assertNotNull( $rev->getText(), 'no content text' ); 00342 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00343 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00344 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00345 } 00346 00347 public function testConstructWithContent() { 00348 $this->hideDeprecated( "Revision::getText" ); 00349 00350 $title = Title::newFromText( 'RevisionTest_testConstructWithContent' ); 00351 00352 $rev = new Revision( array( 00353 'content' => ContentHandler::makeContent( 'hello world.', $title, CONTENT_MODEL_JAVASCRIPT ), 00354 ) ); 00355 00356 $this->assertNotNull( $rev->getText(), 'no content text' ); 00357 $this->assertNotNull( $rev->getContent(), 'no content object available' ); 00358 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() ); 00359 $this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContentModel() ); 00360 } 00361 00367 function testGetContentClone() { 00368 $content = new RevisionTestModifyableContent( "foo" ); 00369 00370 $rev = new Revision( 00371 array( 00372 'id' => 42, 00373 'page' => 23, 00374 'title' => Title::newFromText( "testGetContentClone_dummy" ), 00375 00376 'content' => $content, 00377 'length' => $content->getSize(), 00378 'comment' => "testing", 00379 'minor_edit' => false, 00380 ) 00381 ); 00382 00383 $content = $rev->getContent( Revision::RAW ); 00384 $content->setText( "bar" ); 00385 00386 $content2 = $rev->getContent( Revision::RAW ); 00387 $this->assertNotSame( $content, $content2, "expected a clone" ); // content is mutable, expect clone 00388 $this->assertEquals( "foo", $content2->getText() ); // clone should contain the original text 00389 00390 $content2->setText( "bla bla" ); 00391 $this->assertEquals( "bar", $content->getText() ); // clones should be independent 00392 } 00393 00394 00400 function testGetContentUncloned() { 00401 $rev = $this->newTestRevision( "hello", "testGetContentUncloned_dummy", CONTENT_MODEL_WIKITEXT ); 00402 $content = $rev->getContent( Revision::RAW ); 00403 $content2 = $rev->getContent( Revision::RAW ); 00404 00405 // for immutable content like wikitext, this should be the same object 00406 $this->assertSame( $content, $content2 ); 00407 } 00408 00409 } 00410 00411 class RevisionTestModifyableContent extends TextContent { 00412 public function __construct( $text ) { 00413 parent::__construct( $text, "RevisionTestModifyableContent" ); 00414 } 00415 00416 public function copy() { 00417 return new RevisionTestModifyableContent( $this->mText ); 00418 } 00419 00420 public function getText() { 00421 return $this->mText; 00422 } 00423 00424 public function setText( $text ) { 00425 $this->mText = $text; 00426 } 00427 00428 } 00429 00430 class RevisionTestModifyableContentHandler extends TextContentHandler { 00431 00432 public function __construct() { 00433 parent::__construct( "RevisionTestModifyableContent", array( CONTENT_FORMAT_TEXT ) ); 00434 } 00435 00436 public function unserializeContent( $text, $format = null ) { 00437 $this->checkFormat( $format ); 00438 00439 return new RevisionTestModifyableContent( $text ); 00440 } 00441 00442 public function makeEmptyContent() { 00443 return new RevisionTestModifyableContent( '' ); 00444 } 00445 }