From 47cbdfa7faf41ea24cd3a74284024195f519a59d Mon Sep 17 00:00:00 2001 From: addshore Date: Thu, 11 Jan 2018 16:56:29 +0000 Subject: [PATCH] RevisionStore tests for current behaviour of getTitle Change-Id: I6e345b10abb81f6eae94ab9475563f690aaee012 --- .../includes/Storage/RevisionStoreTest.php | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) diff --git a/tests/phpunit/includes/Storage/RevisionStoreTest.php b/tests/phpunit/includes/Storage/RevisionStoreTest.php index 50c0884c2b..2cdd31605e 100644 --- a/tests/phpunit/includes/Storage/RevisionStoreTest.php +++ b/tests/phpunit/includes/Storage/RevisionStoreTest.php @@ -2,10 +2,12 @@ namespace MediaWiki\Tests\Storage; +use MediaWiki\Storage\RevisionAccessException; use MediaWiki\Storage\RevisionStore; use MediaWiki\Storage\SqlBlobStore; use MediaWikiTestCase; use WANObjectCache; +use Wikimedia\Rdbms\Database; use Wikimedia\Rdbms\LoadBalancer; class RevisionStoreTest extends MediaWikiTestCase { @@ -37,6 +39,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 +301,126 @@ class RevisionStoreTest extends MediaWikiTestCase { ); } + 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 ); + } + // FIXME: test getRevisionSizes } -- 2.20.1