X-Git-Url: https://git.heureux-cyclage.org/?p=lhc%2Fweb%2Fwiklou.git;a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2FStorage%2FRevisionStoreTest.php;h=c9e99780e144952af845ff87e5499a1c08447441;hp=50c0884c2b780bdb5c056c690c7a1583e57d069b;hb=1c65dd29a3c77173625b9a0f8b59ea1c1c12fc2c;hpb=35b9d17334941d8b77d811a1e5be56684566f273 diff --git a/tests/phpunit/includes/Storage/RevisionStoreTest.php b/tests/phpunit/includes/Storage/RevisionStoreTest.php index 50c0884c2b..c9e99780e1 100644 --- a/tests/phpunit/includes/Storage/RevisionStoreTest.php +++ b/tests/phpunit/includes/Storage/RevisionStoreTest.php @@ -2,10 +2,15 @@ namespace MediaWiki\Tests\Storage; +use HashBagOStuff; +use Language; +use MediaWiki\Storage\RevisionAccessException; use MediaWiki\Storage\RevisionStore; use MediaWiki\Storage\SqlBlobStore; use MediaWikiTestCase; +use Title; use WANObjectCache; +use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\LoadBalancer; class RevisionStoreTest extends MediaWikiTestCase { @@ -37,6 +42,14 @@ class RevisionStoreTest extends MediaWikiTestCase { ->disableOriginalConstructor()->getMock(); } + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Database + */ + private function getMockDatabase() { + return $this->getMockBuilder( Database::class ) + ->disableOriginalConstructor()->getMock(); + } + /** * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore */ @@ -291,6 +304,227 @@ class RevisionStoreTest extends MediaWikiTestCase { ); } - // FIXME: test getRevisionSizes + public function testGetTitle_successFromPageId() { + $mockLoadBalancer = $this->getMockLoadBalancer(); + // Title calls wfGetDB() so we have to set the main service + $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); + + $db = $this->getMockDatabase(); + // Title calls wfGetDB() which uses a regular Connection + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getConnection' ) + ->willReturn( $db ); + + // First call to Title::newFromID, faking no result (db lag?) + $db->expects( $this->at( 0 ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( (object)[ + 'page_namespace' => '1', + 'page_title' => 'Food', + ] ); + + $store = $this->getRevisionStore( $mockLoadBalancer ); + $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL ); + + $this->assertSame( 1, $title->getNamespace() ); + $this->assertSame( 'Food', $title->getDBkey() ); + } + + public function testGetTitle_successFromRevId() { + $mockLoadBalancer = $this->getMockLoadBalancer(); + // Title calls wfGetDB() so we have to set the main service + $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); + + $db = $this->getMockDatabase(); + // Title calls wfGetDB() which uses a regular Connection + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getConnection' ) + ->willReturn( $db ); + // RevisionStore getTitle uses a ConnectionRef + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getConnectionRef' ) + ->willReturn( $db ); + + // First call to Title::newFromID, faking no result (db lag?) + $db->expects( $this->at( 0 ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( false ); + + // First select using rev_id, faking no result (db lag?) + $db->expects( $this->at( 1 ) ) + ->method( 'selectRow' ) + ->with( + [ 'revision', 'page' ], + $this->anything(), + [ 'rev_id' => 2 ] + ) + ->willReturn( (object)[ + 'page_namespace' => '1', + 'page_title' => 'Food2', + ] ); + + $store = $this->getRevisionStore( $mockLoadBalancer ); + $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL ); + + $this->assertSame( 1, $title->getNamespace() ); + $this->assertSame( 'Food2', $title->getDBkey() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getTitle + */ + public function testGetTitle_throwsExceptionAfterFallbacks() { + $mockLoadBalancer = $this->getMockLoadBalancer(); + // Title calls wfGetDB() so we have to set the main service + $this->setService( 'DBLoadBalancer', $mockLoadBalancer ); + + $db = $this->getMockDatabase(); + // Title calls wfGetDB() which uses a regular Connection + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getConnection' ) + ->willReturn( $db ); + // RevisionStore getTitle uses a ConnectionRef + $mockLoadBalancer->expects( $this->atLeastOnce() ) + ->method( 'getConnectionRef' ) + ->willReturn( $db ); + + // First call to Title::newFromID, faking no result (db lag?) + $db->expects( $this->at( 0 ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( false ); + + // First select using rev_id, faking no result (db lag?) + $db->expects( $this->at( 1 ) ) + ->method( 'selectRow' ) + ->with( + [ 'revision', 'page' ], + $this->anything(), + [ 'rev_id' => 2 ] + ) + ->willReturn( false ); + + $store = $this->getRevisionStore( $mockLoadBalancer ); + + $this->setExpectedException( RevisionAccessException::class ); + $store->getTitle( 1, 2, RevisionStore::READ_NORMAL ); + } + + public function provideNewRevisionFromRow_legacyEncoding_applied() { + yield 'windows-1252, old_flags is empty' => [ + 'windows-1252', + 'en', + [ + 'old_flags' => '', + 'old_text' => "S\xF6me Content", + ], + 'Söme Content' + ]; + + yield 'windows-1252, old_flags is null' => [ + 'windows-1252', + 'en', + [ + 'old_flags' => null, + 'old_text' => "S\xF6me Content", + ], + 'Söme Content' + ]; + } + + /** + * @dataProvider provideNewRevisionFromRow_legacyEncoding_applied + * + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29 + */ + public function testNewRevisionFromRow_legacyEncoding_applied( $encoding, $locale, $row, $text ) { + $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); + + $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $blobStore->setLegacyEncoding( $encoding, Language::factory( $locale ) ); + + $store = new RevisionStore( wfGetLB(), $blobStore, $cache ); + + $record = $store->newRevisionFromRow( + $this->makeRow( $row ), + 0, + Title::newFromText( __METHOD__ . '-UTPage' ) + ); + + $this->assertSame( $text, $record->getContent( 'main' )->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29 + */ + public function testNewRevisionFromRow_legacyEncoding_ignored() { + $row = [ + 'old_flags' => 'utf-8', + 'old_text' => 'Söme Content', + ]; + + $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); + + $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) ); + + $store = new RevisionStore( wfGetLB(), $blobStore, $cache ); + + $record = $store->newRevisionFromRow( + $this->makeRow( $row ), + 0, + Title::newFromText( __METHOD__ . '-UTPage' ) + ); + $this->assertSame( 'Söme Content', $record->getContent( 'main' )->serialize() ); + } + + private function makeRow( array $array ) { + $row = $array + [ + 'rev_id' => 7, + 'rev_page' => 5, + 'rev_text_id' => 11, + 'rev_timestamp' => '20110101000000', + 'rev_user_text' => 'Tester', + 'rev_user' => 17, + 'rev_minor_edit' => 0, + 'rev_deleted' => 0, + 'rev_len' => 100, + 'rev_parent_id' => 0, + 'rev_sha1' => 'deadbeef', + 'rev_comment_text' => 'Testing', + 'rev_comment_data' => '{}', + 'rev_comment_cid' => 111, + 'rev_content_format' => CONTENT_FORMAT_TEXT, + 'rev_content_model' => CONTENT_MODEL_TEXT, + 'page_namespace' => 0, + 'page_title' => 'TEST', + 'page_id' => 5, + 'page_latest' => 7, + 'page_is_redirect' => 0, + 'page_len' => 100, + 'user_name' => 'Tester', + 'old_is' => 13, + 'old_text' => 'Hello World', + 'old_flags' => 'utf-8', + ]; + + return (object)$row; + } }