diff options
Diffstat (limited to 'www/wiki/tests/phpunit/includes/Storage')
12 files changed, 4428 insertions, 0 deletions
diff --git a/www/wiki/tests/phpunit/includes/Storage/BlobStoreFactoryTest.php b/www/wiki/tests/phpunit/includes/Storage/BlobStoreFactoryTest.php new file mode 100644 index 00000000..252c6578 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/BlobStoreFactoryTest.php @@ -0,0 +1,46 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use MediaWiki\MediaWikiServices; +use MediaWiki\Storage\BlobStore; +use MediaWiki\Storage\SqlBlobStore; +use MediaWikiTestCase; +use Wikimedia\TestingAccessWrapper; + +/** + * @covers \MediaWiki\Storage\BlobStoreFactory + */ +class BlobStoreFactoryTest extends MediaWikiTestCase { + + public function provideWikiIds() { + yield [ false ]; + yield [ 'someWiki' ]; + } + + /** + * @dataProvider provideWikiIds + */ + public function testNewBlobStore( $wikiId ) { + $factory = MediaWikiServices::getInstance()->getBlobStoreFactory(); + $store = $factory->newBlobStore( $wikiId ); + $this->assertInstanceOf( BlobStore::class, $store ); + + // This only works as we currently know this is a SqlBlobStore object + $wrapper = TestingAccessWrapper::newFromObject( $store ); + $this->assertEquals( $wikiId, $wrapper->wikiId ); + } + + /** + * @dataProvider provideWikiIds + */ + public function testNewSqlBlobStore( $wikiId ) { + $factory = MediaWikiServices::getInstance()->getBlobStoreFactory(); + $store = $factory->newSqlBlobStore( $wikiId ); + $this->assertInstanceOf( SqlBlobStore::class, $store ); + + $wrapper = TestingAccessWrapper::newFromObject( $store ); + $this->assertEquals( $wikiId, $wrapper->wikiId ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/MutableRevisionRecordTest.php b/www/wiki/tests/phpunit/includes/Storage/MutableRevisionRecordTest.php new file mode 100644 index 00000000..dd2c4b68 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/MutableRevisionRecordTest.php @@ -0,0 +1,212 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use CommentStoreComment; +use InvalidArgumentException; +use MediaWiki\Storage\MutableRevisionRecord; +use MediaWiki\Storage\RevisionAccessException; +use MediaWiki\Storage\RevisionRecord; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\User\UserIdentityValue; +use MediaWikiTestCase; +use TextContent; +use Title; +use WikitextContent; + +/** + * @covers \MediaWiki\Storage\MutableRevisionRecord + * @covers \MediaWiki\Storage\RevisionRecord + */ +class MutableRevisionRecordTest extends MediaWikiTestCase { + + use RevisionRecordTests; + + /** + * @param array $rowOverrides + * + * @return MutableRevisionRecord + */ + protected function newRevision( array $rowOverrides = [] ) { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $record = new MutableRevisionRecord( $title ); + + if ( isset( $rowOverrides['rev_deleted'] ) ) { + $record->setVisibility( $rowOverrides['rev_deleted'] ); + } + + if ( isset( $rowOverrides['rev_id'] ) ) { + $record->setId( $rowOverrides['rev_id'] ); + } + + if ( isset( $rowOverrides['rev_page'] ) ) { + $record->setPageId( $rowOverrides['rev_page'] ); + } + + $record->setContent( 'main', new TextContent( 'Lorem Ipsum' ) ); + $record->setComment( $comment ); + $record->setUser( $user ); + + return $record; + } + + public function provideConstructor() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + yield [ + $title, + 'acmewiki' + ]; + } + + /** + * @dataProvider provideConstructor + * + * @param Title $title + * @param bool $wikiId + */ + public function testConstructorAndGetters( + Title $title, + $wikiId = false + ) { + $rec = new MutableRevisionRecord( $title, $wikiId ); + + $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' ); + $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' ); + } + + public function provideConstructorFailure() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + yield 'not a wiki id' => [ + $title, + null + ]; + } + + /** + * @dataProvider provideConstructorFailure + * + * @param Title $title + * @param bool $wikiId + */ + public function testConstructorFailure( + Title $title, + $wikiId = false + ) { + $this->setExpectedException( InvalidArgumentException::class ); + new MutableRevisionRecord( $title, $wikiId ); + } + + public function testSetGetId() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertNull( $record->getId() ); + $record->setId( 888 ); + $this->assertSame( 888, $record->getId() ); + } + + public function testSetGetUser() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $user = $this->getTestSysop()->getUser(); + $this->assertNull( $record->getUser() ); + $record->setUser( $user ); + $this->assertSame( $user, $record->getUser() ); + } + + public function testSetGetPageId() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertSame( 0, $record->getPageId() ); + $record->setPageId( 999 ); + $this->assertSame( 999, $record->getPageId() ); + } + + public function testSetGetParentId() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertNull( $record->getParentId() ); + $record->setParentId( 100 ); + $this->assertSame( 100, $record->getParentId() ); + } + + public function testGetMainContentWhenEmpty() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->setExpectedException( RevisionAccessException::class ); + $this->assertNull( $record->getContent( 'main' ) ); + } + + public function testSetGetMainContent() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $content = new WikitextContent( 'Badger' ); + $record->setContent( 'main', $content ); + $this->assertSame( $content, $record->getContent( 'main' ) ); + } + + public function testGetSlotWhenEmpty() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertFalse( $record->hasSlot( 'main' ) ); + + $this->setExpectedException( RevisionAccessException::class ); + $record->getSlot( 'main' ); + } + + public function testSetGetSlot() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $slot = SlotRecord::newUnsaved( + 'main', + new WikitextContent( 'x' ) + ); + $record->setSlot( $slot ); + $this->assertTrue( $record->hasSlot( 'main' ) ); + $this->assertSame( $slot, $record->getSlot( 'main' ) ); + } + + public function testSetGetMinor() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertFalse( $record->isMinor() ); + $record->setMinorEdit( true ); + $this->assertSame( true, $record->isMinor() ); + } + + public function testSetGetTimestamp() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertNull( $record->getTimestamp() ); + $record->setTimestamp( '20180101010101' ); + $this->assertSame( '20180101010101', $record->getTimestamp() ); + } + + public function testSetGetVisibility() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertSame( 0, $record->getVisibility() ); + $record->setVisibility( RevisionRecord::DELETED_USER ); + $this->assertSame( RevisionRecord::DELETED_USER, $record->getVisibility() ); + } + + public function testSetGetSha1() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertSame( 'phoiac9h4m842xq45sp7s6u21eteeq1', $record->getSha1() ); + $record->setSha1( 'someHash' ); + $this->assertSame( 'someHash', $record->getSha1() ); + } + + public function testSetGetSize() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $this->assertSame( 0, $record->getSize() ); + $record->setSize( 775 ); + $this->assertSame( 775, $record->getSize() ); + } + + public function testSetGetComment() { + $record = new MutableRevisionRecord( Title::newFromText( 'Foo' ) ); + $comment = new CommentStoreComment( 1, 'foo' ); + $this->assertNull( $record->getComment() ); + $record->setComment( $comment ); + $this->assertSame( $comment, $record->getComment() ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/MutableRevisionSlotsTest.php b/www/wiki/tests/phpunit/includes/Storage/MutableRevisionSlotsTest.php new file mode 100644 index 00000000..0416bcfa --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/MutableRevisionSlotsTest.php @@ -0,0 +1,76 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use MediaWiki\Storage\MutableRevisionSlots; +use MediaWiki\Storage\RevisionAccessException; +use MediaWiki\Storage\SlotRecord; +use WikitextContent; + +/** + * @covers \MediaWiki\Storage\MutableRevisionSlots + */ +class MutableRevisionSlotsTest extends RevisionSlotsTest { + + public function testSetMultipleSlots() { + $slots = new MutableRevisionSlots(); + + $this->assertSame( [], $slots->getSlots() ); + + $slotA = SlotRecord::newUnsaved( 'some', new WikitextContent( 'A' ) ); + $slots->setSlot( $slotA ); + $this->assertTrue( $slots->hasSlot( 'some' ) ); + $this->assertSame( $slotA, $slots->getSlot( 'some' ) ); + $this->assertSame( [ 'some' => $slotA ], $slots->getSlots() ); + + $slotB = SlotRecord::newUnsaved( 'other', new WikitextContent( 'B' ) ); + $slots->setSlot( $slotB ); + $this->assertTrue( $slots->hasSlot( 'other' ) ); + $this->assertSame( $slotB, $slots->getSlot( 'other' ) ); + $this->assertSame( [ 'some' => $slotA, 'other' => $slotB ], $slots->getSlots() ); + } + + public function testSetExistingSlotOverwritesSlot() { + $slots = new MutableRevisionSlots(); + + $this->assertSame( [], $slots->getSlots() ); + + $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $slots->setSlot( $slotA ); + $this->assertSame( $slotA, $slots->getSlot( 'main' ) ); + $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() ); + + $slotB = SlotRecord::newUnsaved( 'main', new WikitextContent( 'B' ) ); + $slots->setSlot( $slotB ); + $this->assertSame( $slotB, $slots->getSlot( 'main' ) ); + $this->assertSame( [ 'main' => $slotB ], $slots->getSlots() ); + } + + public function testSetContentOfExistingSlotOverwritesContent() { + $slots = new MutableRevisionSlots(); + + $this->assertSame( [], $slots->getSlots() ); + + $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $slots->setSlot( $slotA ); + $this->assertSame( $slotA, $slots->getSlot( 'main' ) ); + $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() ); + + $newContent = new WikitextContent( 'B' ); + $slots->setContent( 'main', $newContent ); + $this->assertSame( $newContent, $slots->getContent( 'main' ) ); + } + + public function testRemoveExistingSlot() { + $slotA = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $slots = new MutableRevisionSlots( [ $slotA ] ); + + $this->assertSame( [ 'main' => $slotA ], $slots->getSlots() ); + + $slots->removeSlot( 'main' ); + $this->assertSame( [], $slots->getSlots() ); + $this->setExpectedException( RevisionAccessException::class ); + $slots->getSlot( 'main' ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/NameTableStoreTest.php b/www/wiki/tests/phpunit/includes/Storage/NameTableStoreTest.php new file mode 100644 index 00000000..0cd164b7 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/NameTableStoreTest.php @@ -0,0 +1,298 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use BagOStuff; +use EmptyBagOStuff; +use HashBagOStuff; +use MediaWiki\Storage\NameTableAccessException; +use MediaWiki\Storage\NameTableStore; +use MediaWikiTestCase; +use Psr\Log\NullLogger; +use WANObjectCache; +use Wikimedia\Rdbms\Database; +use Wikimedia\Rdbms\LoadBalancer; +use Wikimedia\TestingAccessWrapper; + +/** + * @author Addshore + * @group Database + * @covers \MediaWiki\Storage\NameTableStore + */ +class NameTableStoreTest extends MediaWikiTestCase { + + public function setUp() { + $this->tablesUsed[] = 'slot_roles'; + parent::setUp(); + } + + private function populateTable( $values ) { + $insertValues = []; + foreach ( $values as $name ) { + $insertValues[] = [ 'role_name' => $name ]; + } + $this->db->insert( 'slot_roles', $insertValues ); + } + + private function getHashWANObjectCache( $cacheBag ) { + return new WANObjectCache( [ 'cache' => $cacheBag ] ); + } + + /** + * @param $db + * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer + */ + private function getMockLoadBalancer( $db ) { + $mock = $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor() + ->getMock(); + $mock->expects( $this->any() ) + ->method( 'getConnection' ) + ->willReturn( $db ); + return $mock; + } + + private function getCallCheckingDb( $insertCalls, $selectCalls ) { + $mock = $this->getMockBuilder( Database::class ) + ->disableOriginalConstructor() + ->getMock(); + $mock->expects( $this->exactly( $insertCalls ) ) + ->method( 'insert' ) + ->willReturnCallback( function () { + return call_user_func_array( [ $this->db, 'insert' ], func_get_args() ); + } ); + $mock->expects( $this->exactly( $selectCalls ) ) + ->method( 'select' ) + ->willReturnCallback( function () { + return call_user_func_array( [ $this->db, 'select' ], func_get_args() ); + } ); + $mock->expects( $this->exactly( $insertCalls ) ) + ->method( 'affectedRows' ) + ->willReturnCallback( function () { + return call_user_func_array( [ $this->db, 'affectedRows' ], func_get_args() ); + } ); + $mock->expects( $this->any() ) + ->method( 'insertId' ) + ->willReturnCallback( function () { + return call_user_func_array( [ $this->db, 'insertId' ], func_get_args() ); + } ); + return $mock; + } + + private function getNameTableSqlStore( + BagOStuff $cacheBag, + $insertCalls, + $selectCalls, + $normalizationCallback = null + ) { + return new NameTableStore( + $this->getMockLoadBalancer( $this->getCallCheckingDb( $insertCalls, $selectCalls ) ), + $this->getHashWANObjectCache( $cacheBag ), + new NullLogger(), + 'slot_roles', 'role_id', 'role_name', + $normalizationCallback + ); + } + + public function provideGetAndAcquireId() { + return [ + 'no wancache, empty table' => + [ new EmptyBagOStuff(), true, 1, [], 'foo', 1 ], + 'no wancache, one matching value' => + [ new EmptyBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ], + 'no wancache, one not matching value' => + [ new EmptyBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ], + 'no wancache, multiple, one matching value' => + [ new EmptyBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ], + 'no wancache, multiple, no matching value' => + [ new EmptyBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ], + 'wancache, empty table' => + [ new HashBagOStuff(), true, 1, [], 'foo', 1 ], + 'wancache, one matching value' => + [ new HashBagOStuff(), false, 1, [ 'foo' ], 'foo', 1 ], + 'wancache, one not matching value' => + [ new HashBagOStuff(), true, 1, [ 'bar' ], 'foo', 2 ], + 'wancache, multiple, one matching value' => + [ new HashBagOStuff(), false, 1, [ 'foo', 'bar' ], 'bar', 2 ], + 'wancache, multiple, no matching value' => + [ new HashBagOStuff(), true, 1, [ 'foo', 'bar' ], 'baz', 3 ], + ]; + } + + /** + * @dataProvider provideGetAndAcquireId + * @param BagOStuff $cacheBag to use in the WANObjectCache service + * @param bool $needsInsert Does the value we are testing need to be inserted? + * @param int $selectCalls Number of times the select DB method will be called + * @param string[] $existingValues to be added to the db table + * @param string $name name to acquire + * @param int $expectedId the id we expect the name to have + */ + public function testGetAndAcquireId( + $cacheBag, + $needsInsert, + $selectCalls, + $existingValues, + $name, + $expectedId + ) { + $this->populateTable( $existingValues ); + $store = $this->getNameTableSqlStore( $cacheBag, (int)$needsInsert, $selectCalls ); + + // Some names will not initially exist + try { + $result = $store->getId( $name ); + $this->assertSame( $expectedId, $result ); + } catch ( NameTableAccessException $e ) { + if ( $needsInsert ) { + $this->assertTrue( true ); // Expected exception + } else { + $this->fail( 'Did not expect an exception, but got one: ' . $e->getMessage() ); + } + } + + // All names should return their id here + $this->assertSame( $expectedId, $store->acquireId( $name ) ); + + // acquireId inserted these names, so now everything should exist with getId + $this->assertSame( $expectedId, $store->getId( $name ) ); + + // calling getId again will also still work, and not result in more selects + $this->assertSame( $expectedId, $store->getId( $name ) ); + } + + public function provideTestGetAndAcquireIdNameNormalization() { + yield [ 'A', 'a', 'strtolower' ]; + yield [ 'b', 'B', 'strtoupper' ]; + yield [ + 'X', + 'X', + function ( $name ) { + return $name; + } + ]; + yield [ 'ZZ', 'ZZ-a', __CLASS__ . '::appendDashAToString' ]; + } + + public static function appendDashAToString( $string ) { + return $string . '-a'; + } + + /** + * @dataProvider provideTestGetAndAcquireIdNameNormalization + */ + public function testGetAndAcquireIdNameNormalization( + $nameIn, + $nameOut, + $normalizationCallback + ) { + $store = $this->getNameTableSqlStore( + new EmptyBagOStuff(), + 1, + 1, + $normalizationCallback + ); + $acquiredId = $store->acquireId( $nameIn ); + $this->assertSame( $nameOut, $store->getName( $acquiredId ) ); + } + + public function provideGetName() { + return [ + [ new HashBagOStuff(), 3, 3 ], + [ new EmptyBagOStuff(), 3, 3 ], + ]; + } + + /** + * @dataProvider provideGetName + */ + public function testGetName( $cacheBag, $insertCalls, $selectCalls ) { + $store = $this->getNameTableSqlStore( $cacheBag, $insertCalls, $selectCalls ); + + // Get 1 ID and make sure getName returns correctly + $fooId = $store->acquireId( 'foo' ); + $this->assertSame( 'foo', $store->getName( $fooId ) ); + + // Get another ID and make sure getName returns correctly + $barId = $store->acquireId( 'bar' ); + $this->assertSame( 'bar', $store->getName( $barId ) ); + + // Blitz the cache and make sure it still returns + TestingAccessWrapper::newFromObject( $store )->tableCache = null; + $this->assertSame( 'foo', $store->getName( $fooId ) ); + $this->assertSame( 'bar', $store->getName( $barId ) ); + + // Blitz the cache again and get another ID and make sure getName returns correctly + TestingAccessWrapper::newFromObject( $store )->tableCache = null; + $bazId = $store->acquireId( 'baz' ); + $this->assertSame( 'baz', $store->getName( $bazId ) ); + $this->assertSame( 'baz', $store->getName( $bazId ) ); + } + + public function testGetName_masterFallback() { + $store = $this->getNameTableSqlStore( new EmptyBagOStuff(), 1, 2 ); + + // Insert a new name + $fooId = $store->acquireId( 'foo' ); + + // Empty the process cache, getCachedTable() will now return this empty array + TestingAccessWrapper::newFromObject( $store )->tableCache = []; + + // getName should fallback to master, which is why we assert 2 selectCalls above + $this->assertSame( 'foo', $store->getName( $fooId ) ); + } + + public function testGetMap_empty() { + $this->populateTable( [] ); + $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 ); + $table = $store->getMap(); + $this->assertSame( [], $table ); + } + + public function testGetMap_twoValues() { + $this->populateTable( [ 'foo', 'bar' ] ); + $store = $this->getNameTableSqlStore( new HashBagOStuff(), 0, 1 ); + + // We are using a cache, so 2 calls should only result in 1 select on the db + $store->getMap(); + $table = $store->getMap(); + + $expected = [ 1 => 'foo', 2 => 'bar' ]; + $this->assertSame( $expected, $table ); + // Make sure the table returned is the same as the cached table + $this->assertSame( $expected, TestingAccessWrapper::newFromObject( $store )->tableCache ); + } + + public function testCacheRaceCondition() { + $wanHashBag = new HashBagOStuff(); + $store1 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 ); + $store2 = $this->getNameTableSqlStore( $wanHashBag, 1, 0 ); + $store3 = $this->getNameTableSqlStore( $wanHashBag, 1, 1 ); + + // Cache the current table in the instances we will use + // This simulates multiple requests running simultaneously + $store1->getMap(); + $store2->getMap(); + $store3->getMap(); + + // Store 2 separate names using different instances + $fooId = $store1->acquireId( 'foo' ); + $barId = $store2->acquireId( 'bar' ); + + // Each of these instances should be aware of what they have inserted + $this->assertSame( $fooId, $store1->acquireId( 'foo' ) ); + $this->assertSame( $barId, $store2->acquireId( 'bar' ) ); + + // A new store should be able to get both of these new Ids + // Note: before there was a race condition here where acquireId( 'bar' ) would update the + // cache with data missing the 'foo' key that it was not aware of + $store4 = $this->getNameTableSqlStore( $wanHashBag, 0, 1 ); + $this->assertSame( $fooId, $store4->getId( 'foo' ) ); + $this->assertSame( $barId, $store4->getId( 'bar' ) ); + + // If a store with old cached data tries to acquire these we will get the same ids. + $this->assertSame( $fooId, $store3->acquireId( 'foo' ) ); + $this->assertSame( $barId, $store3->acquireId( 'bar' ) ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionArchiveRecordTest.php b/www/wiki/tests/phpunit/includes/Storage/RevisionArchiveRecordTest.php new file mode 100644 index 00000000..f959d680 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionArchiveRecordTest.php @@ -0,0 +1,272 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use CommentStoreComment; +use InvalidArgumentException; +use MediaWiki\Storage\RevisionRecord; +use MediaWiki\Storage\RevisionSlots; +use MediaWiki\Storage\RevisionArchiveRecord; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\User\UserIdentity; +use MediaWiki\User\UserIdentityValue; +use MediaWikiTestCase; +use TextContent; +use Title; + +/** + * @covers \MediaWiki\Storage\RevisionArchiveRecord + * @covers \MediaWiki\Storage\RevisionRecord + */ +class RevisionArchiveRecordTest extends MediaWikiTestCase { + + use RevisionRecordTests; + + /** + * @param array $rowOverrides + * + * @return RevisionArchiveRecord + */ + protected function newRevision( array $rowOverrides = [] ) { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $row = [ + 'ar_id' => '5', + 'ar_rev_id' => '7', + 'ar_page_id' => strval( $title->getArticleID() ), + 'ar_timestamp' => '20200101000000', + 'ar_deleted' => 0, + 'ar_minor_edit' => 0, + 'ar_parent_id' => '5', + 'ar_len' => $slots->computeSize(), + 'ar_sha1' => $slots->computeSha1(), + ]; + + foreach ( $rowOverrides as $field => $value ) { + $field = preg_replace( '/^rev_/', 'ar_', $field ); + $row[$field] = $value; + } + + return new RevisionArchiveRecord( $title, $user, $comment, (object)$row, $slots ); + } + + public function provideConstructor() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $protoRow = [ + 'ar_id' => '5', + 'ar_rev_id' => '7', + 'ar_page_id' => strval( $title->getArticleID() ), + 'ar_timestamp' => '20200101000000', + 'ar_deleted' => 0, + 'ar_minor_edit' => 0, + 'ar_parent_id' => '5', + 'ar_len' => $slots->computeSize(), + 'ar_sha1' => $slots->computeSha1(), + ]; + + $row = $protoRow; + yield 'all info' => [ + $title, + $user, + $comment, + (object)$row, + $slots, + 'acmewiki' + ]; + + $row = $protoRow; + $row['ar_minor_edit'] = '1'; + $row['ar_deleted'] = strval( RevisionRecord::DELETED_USER ); + + yield 'minor deleted' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + unset( $row['ar_parent'] ); + + yield 'no parent' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + $row['ar_len'] = null; + $row['ar_sha1'] = ''; + + yield 'ar_len is null, ar_sha1 is ""' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + yield 'no length, no hash' => [ + Title::newFromText( 'DummyDoesNotExist' ), + $user, + $comment, + (object)$row, + $slots + ]; + } + + /** + * @dataProvider provideConstructor + * + * @param Title $title + * @param UserIdentity $user + * @param CommentStoreComment $comment + * @param object $row + * @param RevisionSlots $slots + * @param bool $wikiId + */ + public function testConstructorAndGetters( + Title $title, + UserIdentity $user, + CommentStoreComment $comment, + $row, + RevisionSlots $slots, + $wikiId = false + ) { + $rec = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $wikiId ); + + $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' ); + $this->assertSame( $user, $rec->getUser( RevisionRecord::RAW ), 'getUser' ); + $this->assertSame( $comment, $rec->getComment(), 'getComment' ); + + $this->assertSame( $slots->getSlotRoles(), $rec->getSlotRoles(), 'getSlotRoles' ); + $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' ); + + $this->assertSame( (int)$row->ar_id, $rec->getArchiveId(), 'getArchiveId' ); + $this->assertSame( (int)$row->ar_rev_id, $rec->getId(), 'getId' ); + $this->assertSame( (int)$row->ar_page_id, $rec->getPageId(), 'getId' ); + $this->assertSame( $row->ar_timestamp, $rec->getTimestamp(), 'getTimestamp' ); + $this->assertSame( (int)$row->ar_deleted, $rec->getVisibility(), 'getVisibility' ); + $this->assertSame( (bool)$row->ar_minor_edit, $rec->isMinor(), 'getIsMinor' ); + + if ( isset( $row->ar_parent_id ) ) { + $this->assertSame( (int)$row->ar_parent_id, $rec->getParentId(), 'getParentId' ); + } else { + $this->assertSame( 0, $rec->getParentId(), 'getParentId' ); + } + + if ( isset( $row->ar_len ) ) { + $this->assertSame( (int)$row->ar_len, $rec->getSize(), 'getSize' ); + } else { + $this->assertSame( $slots->computeSize(), $rec->getSize(), 'getSize' ); + } + + if ( !empty( $row->ar_sha1 ) ) { + $this->assertSame( $row->ar_sha1, $rec->getSha1(), 'getSha1' ); + } else { + $this->assertSame( $slots->computeSha1(), $rec->getSha1(), 'getSha1' ); + } + } + + public function provideConstructorFailure() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $protoRow = [ + 'ar_id' => '5', + 'ar_rev_id' => '7', + 'ar_page_id' => strval( $title->getArticleID() ), + 'ar_timestamp' => '20200101000000', + 'ar_deleted' => 0, + 'ar_minor_edit' => 0, + 'ar_parent_id' => '5', + 'ar_len' => $slots->computeSize(), + 'ar_sha1' => $slots->computeSha1(), + ]; + + yield 'not a row' => [ + $title, + $user, + $comment, + 'not a row', + $slots, + 'acmewiki' + ]; + + $row = $protoRow; + $row['ar_timestamp'] = 'kittens'; + + yield 'bad timestamp' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + + yield 'bad wiki' => [ + $title, + $user, + $comment, + (object)$row, + $slots, + 12345 + ]; + + // NOTE: $title->getArticleID does *not* have to match ar_page_id in all cases! + } + + /** + * @dataProvider provideConstructorFailure + * + * @param Title $title + * @param UserIdentity $user + * @param CommentStoreComment $comment + * @param object $row + * @param RevisionSlots $slots + * @param bool $wikiId + */ + public function testConstructorFailure( + Title $title, + UserIdentity $user, + CommentStoreComment $comment, + $row, + RevisionSlots $slots, + $wikiId = false + ) { + $this->setExpectedException( InvalidArgumentException::class ); + new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $wikiId ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionRecordTests.php b/www/wiki/tests/phpunit/includes/Storage/RevisionRecordTests.php new file mode 100644 index 00000000..607f7829 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionRecordTests.php @@ -0,0 +1,512 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use CommentStoreComment; +use LogicException; +use MediaWiki\Storage\RevisionRecord; +use MediaWiki\Storage\RevisionSlots; +use MediaWiki\Storage\RevisionStoreRecord; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\Storage\SuppressedDataException; +use MediaWiki\User\UserIdentityValue; +use TextContent; +use Title; + +// PHPCS should not complain about @covers and @dataProvider being used in traits, see T192384 +// phpcs:disable MediaWiki.Commenting.PhpunitAnnotations.NotTestClass + +/** + * @covers \MediaWiki\Storage\RevisionRecord + * + * @note Expects to be used in classes that extend MediaWikiTestCase. + */ +trait RevisionRecordTests { + + /** + * @param array $rowOverrides + * + * @return RevisionRecord + */ + protected abstract function newRevision( array $rowOverrides = [] ); + + private function provideAudienceCheckData( $field ) { + yield 'field accessible for oversighter (ALL)' => [ + RevisionRecord::SUPPRESSED_ALL, + [ 'oversight' ], + true, + false + ]; + + yield 'field accessible for oversighter' => [ + RevisionRecord::DELETED_RESTRICTED | $field, + [ 'oversight' ], + true, + false + ]; + + yield 'field not accessible for sysops (ALL)' => [ + RevisionRecord::SUPPRESSED_ALL, + [ 'sysop' ], + false, + false + ]; + + yield 'field not accessible for sysops' => [ + RevisionRecord::DELETED_RESTRICTED | $field, + [ 'sysop' ], + false, + false + ]; + + yield 'field accessible for sysops' => [ + $field, + [ 'sysop' ], + true, + false + ]; + + yield 'field suppressed for logged in users' => [ + $field, + [ 'user' ], + false, + false + ]; + + yield 'unrelated field suppressed' => [ + $field === RevisionRecord::DELETED_COMMENT + ? RevisionRecord::DELETED_USER + : RevisionRecord::DELETED_COMMENT, + [ 'user' ], + true, + true + ]; + + yield 'nothing suppressed' => [ + 0, + [ 'user' ], + true, + true + ]; + } + + public function testSerialization_fails() { + $this->setExpectedException( LogicException::class ); + $rev = $this->newRevision(); + serialize( $rev ); + } + + public function provideGetComment_audience() { + return $this->provideAudienceCheckData( RevisionRecord::DELETED_COMMENT ); + } + + private function forceStandardPermissions() { + $this->setMwGlobals( + 'wgGroupPermissions', + [ + 'user' => [ + 'viewsuppressed' => false, + 'suppressrevision' => false, + 'deletedtext' => false, + 'deletedhistory' => false, + ], + 'sysop' => [ + 'viewsuppressed' => false, + 'suppressrevision' => false, + 'deletedtext' => true, + 'deletedhistory' => true, + ], + 'oversight' => [ + 'deletedtext' => true, + 'deletedhistory' => true, + 'viewsuppressed' => true, + 'suppressrevision' => true, + ], + ] + ); + } + + /** + * @dataProvider provideGetComment_audience + */ + public function testGetComment_audience( $visibility, $groups, $userCan, $publicCan ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $groups )->getUser(); + $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] ); + + $this->assertNotNull( $rev->getComment( RevisionRecord::RAW ), 'raw can' ); + + $this->assertSame( + $publicCan, + $rev->getComment( RevisionRecord::FOR_PUBLIC ) !== null, + 'public can' + ); + $this->assertSame( + $userCan, + $rev->getComment( RevisionRecord::FOR_THIS_USER, $user ) !== null, + 'user can' + ); + } + + public function provideGetUser_audience() { + return $this->provideAudienceCheckData( RevisionRecord::DELETED_USER ); + } + + /** + * @dataProvider provideGetUser_audience + */ + public function testGetUser_audience( $visibility, $groups, $userCan, $publicCan ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $groups )->getUser(); + $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] ); + + $this->assertNotNull( $rev->getUser( RevisionRecord::RAW ), 'raw can' ); + + $this->assertSame( + $publicCan, + $rev->getUser( RevisionRecord::FOR_PUBLIC ) !== null, + 'public can' + ); + $this->assertSame( + $userCan, + $rev->getUser( RevisionRecord::FOR_THIS_USER, $user ) !== null, + 'user can' + ); + } + + public function provideGetSlot_audience() { + return $this->provideAudienceCheckData( RevisionRecord::DELETED_TEXT ); + } + + /** + * @dataProvider provideGetSlot_audience + */ + public function testGetSlot_audience( $visibility, $groups, $userCan, $publicCan ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $groups )->getUser(); + $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] ); + + // NOTE: slot meta-data is never suppressed, just the content is! + $this->assertTrue( $rev->hasSlot( 'main' ), 'hasSlot is never suppressed' ); + $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::RAW ), 'raw meta' ); + $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC ), 'public meta' ); + + $this->assertNotNull( + $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user ), + 'user can' + ); + + try { + $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC )->getContent(); + $exception = null; + } catch ( SuppressedDataException $ex ) { + $exception = $ex; + } + + $this->assertSame( + $publicCan, + $exception === null, + 'public can' + ); + + try { + $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user )->getContent(); + $exception = null; + } catch ( SuppressedDataException $ex ) { + $exception = $ex; + } + + $this->assertSame( + $userCan, + $exception === null, + 'user can' + ); + } + + /** + * @dataProvider provideGetSlot_audience + */ + public function testGetContent_audience( $visibility, $groups, $userCan, $publicCan ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $groups )->getUser(); + $rev = $this->newRevision( [ 'rev_deleted' => $visibility ] ); + + $this->assertNotNull( $rev->getContent( 'main', RevisionRecord::RAW ), 'raw can' ); + + $this->assertSame( + $publicCan, + $rev->getContent( 'main', RevisionRecord::FOR_PUBLIC ) !== null, + 'public can' + ); + $this->assertSame( + $userCan, + $rev->getContent( 'main', RevisionRecord::FOR_THIS_USER, $user ) !== null, + 'user can' + ); + } + + public function testGetSlot() { + $rev = $this->newRevision(); + + $slot = $rev->getSlot( 'main' ); + $this->assertNotNull( $slot, 'getSlot()' ); + $this->assertSame( 'main', $slot->getRole(), 'getRole()' ); + } + + public function testHasSlot() { + $rev = $this->newRevision(); + + $this->assertTrue( $rev->hasSlot( 'main' ) ); + $this->assertFalse( $rev->hasSlot( 'xyz' ) ); + } + + public function testGetContent() { + $rev = $this->newRevision(); + + $content = $rev->getSlot( 'main' ); + $this->assertNotNull( $content, 'getContent()' ); + $this->assertSame( CONTENT_MODEL_TEXT, $content->getModel(), 'getModel()' ); + } + + public function provideUserCanBitfield() { + yield [ 0, 0, [], null, true ]; + // Bitfields match, user has no permissions + yield [ + RevisionRecord::DELETED_TEXT, + RevisionRecord::DELETED_TEXT, + [], + null, + false + ]; + yield [ + RevisionRecord::DELETED_COMMENT, + RevisionRecord::DELETED_COMMENT, + [], + null, + false, + ]; + yield [ + RevisionRecord::DELETED_USER, + RevisionRecord::DELETED_USER, + [], + null, + false + ]; + yield [ + RevisionRecord::DELETED_RESTRICTED, + RevisionRecord::DELETED_RESTRICTED, + [], + null, + false, + ]; + // Bitfields match, user (admin) does have permissions + yield [ + RevisionRecord::DELETED_TEXT, + RevisionRecord::DELETED_TEXT, + [ 'sysop' ], + null, + true, + ]; + yield [ + RevisionRecord::DELETED_COMMENT, + RevisionRecord::DELETED_COMMENT, + [ 'sysop' ], + null, + true, + ]; + yield [ + RevisionRecord::DELETED_USER, + RevisionRecord::DELETED_USER, + [ 'sysop' ], + null, + true, + ]; + // Bitfields match, user (admin) does not have permissions + yield [ + RevisionRecord::DELETED_RESTRICTED, + RevisionRecord::DELETED_RESTRICTED, + [ 'sysop' ], + null, + false, + ]; + // Bitfields match, user (oversight) does have permissions + yield [ + RevisionRecord::DELETED_RESTRICTED, + RevisionRecord::DELETED_RESTRICTED, + [ 'oversight' ], + null, + true, + ]; + // Check permissions using the title + yield [ + RevisionRecord::DELETED_TEXT, + RevisionRecord::DELETED_TEXT, + [ 'sysop' ], + Title::newFromText( __METHOD__ ), + true, + ]; + yield [ + RevisionRecord::DELETED_TEXT, + RevisionRecord::DELETED_TEXT, + [], + Title::newFromText( __METHOD__ ), + false, + ]; + } + + /** + * @dataProvider provideUserCanBitfield + * @covers \MediaWiki\Storage\RevisionRecord::userCanBitfield + */ + public function testUserCanBitfield( $bitField, $field, $userGroups, $title, $expected ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $userGroups )->getUser(); + + $this->assertSame( + $expected, + RevisionRecord::userCanBitfield( $bitField, $field, $user, $title ) + ); + } + + public function provideHasSameContent() { + /** + * @param SlotRecord[] $slots + * @param int $revId + * @return RevisionStoreRecord + */ + $recordCreator = function ( array $slots, $revId ) { + $title = Title::newFromText( 'provideHasSameContent' ); + $title->resetArticleID( 19 ); + $slots = new RevisionSlots( $slots ); + + return new RevisionStoreRecord( + $title, + new UserIdentityValue( 11, __METHOD__, 0 ), + CommentStoreComment::newUnsavedComment( __METHOD__ ), + (object)[ + 'rev_id' => strval( $revId ), + 'rev_page' => strval( $title->getArticleID() ), + 'rev_timestamp' => '20200101000000', + 'rev_deleted' => 0, + 'rev_minor_edit' => 0, + 'rev_parent_id' => '5', + 'rev_len' => $slots->computeSize(), + 'rev_sha1' => $slots->computeSha1(), + 'page_latest' => '18', + ], + $slots + ); + }; + + // Create some slots with content + $mainA = SlotRecord::newUnsaved( 'main', new TextContent( 'A' ) ); + $mainB = SlotRecord::newUnsaved( 'main', new TextContent( 'B' ) ); + $auxA = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) ); + $auxB = SlotRecord::newUnsaved( 'aux', new TextContent( 'A' ) ); + + $initialRecord = $recordCreator( [ $mainA ], 12 ); + + return [ + 'same record object' => [ + true, + $initialRecord, + $initialRecord, + ], + 'same record content, different object' => [ + true, + $recordCreator( [ $mainA ], 12 ), + $recordCreator( [ $mainA ], 13 ), + ], + 'same record content, aux slot, different object' => [ + true, + $recordCreator( [ $auxA ], 12 ), + $recordCreator( [ $auxB ], 13 ), + ], + 'different content' => [ + false, + $recordCreator( [ $mainA ], 12 ), + $recordCreator( [ $mainB ], 13 ), + ], + 'different content and number of slots' => [ + false, + $recordCreator( [ $mainA ], 12 ), + $recordCreator( [ $mainA, $mainB ], 13 ), + ], + ]; + } + + /** + * @dataProvider provideHasSameContent + * @covers \MediaWiki\Storage\RevisionRecord::hasSameContent + * @group Database + */ + public function testHasSameContent( + $expected, + RevisionRecord $record1, + RevisionRecord $record2 + ) { + $this->assertSame( + $expected, + $record1->hasSameContent( $record2 ) + ); + } + + public function provideIsDeleted() { + yield 'no deletion' => [ + 0, + [ + RevisionRecord::DELETED_TEXT => false, + RevisionRecord::DELETED_COMMENT => false, + RevisionRecord::DELETED_USER => false, + RevisionRecord::DELETED_RESTRICTED => false, + ] + ]; + yield 'text deleted' => [ + RevisionRecord::DELETED_TEXT, + [ + RevisionRecord::DELETED_TEXT => true, + RevisionRecord::DELETED_COMMENT => false, + RevisionRecord::DELETED_USER => false, + RevisionRecord::DELETED_RESTRICTED => false, + ] + ]; + yield 'text and comment deleted' => [ + RevisionRecord::DELETED_TEXT + RevisionRecord::DELETED_COMMENT, + [ + RevisionRecord::DELETED_TEXT => true, + RevisionRecord::DELETED_COMMENT => true, + RevisionRecord::DELETED_USER => false, + RevisionRecord::DELETED_RESTRICTED => false, + ] + ]; + yield 'all 4 deleted' => [ + RevisionRecord::DELETED_TEXT + + RevisionRecord::DELETED_COMMENT + + RevisionRecord::DELETED_RESTRICTED + + RevisionRecord::DELETED_USER, + [ + RevisionRecord::DELETED_TEXT => true, + RevisionRecord::DELETED_COMMENT => true, + RevisionRecord::DELETED_USER => true, + RevisionRecord::DELETED_RESTRICTED => true, + ] + ]; + } + + /** + * @dataProvider provideIsDeleted + * @covers \MediaWiki\Storage\RevisionRecord::isDeleted + */ + public function testIsDeleted( $revDeleted, $assertionMap ) { + $rev = $this->newRevision( [ 'rev_deleted' => $revDeleted ] ); + foreach ( $assertionMap as $deletionLevel => $expected ) { + $this->assertSame( $expected, $rev->isDeleted( $deletionLevel ) ); + } + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionSlotsTest.php b/www/wiki/tests/phpunit/includes/Storage/RevisionSlotsTest.php new file mode 100644 index 00000000..b9f833ca --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionSlotsTest.php @@ -0,0 +1,139 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use MediaWiki\Storage\RevisionAccessException; +use MediaWiki\Storage\RevisionSlots; +use MediaWiki\Storage\SlotRecord; +use MediaWikiTestCase; +use WikitextContent; + +class RevisionSlotsTest extends MediaWikiTestCase { + + /** + * @param SlotRecord[] $slots + * @return RevisionSlots + */ + protected function newRevisionSlots( $slots = [] ) { + return new RevisionSlots( $slots ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::getSlot + */ + public function testGetSlot() { + $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) ); + $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] ); + + $this->assertSame( $mainSlot, $slots->getSlot( 'main' ) ); + $this->assertSame( $auxSlot, $slots->getSlot( 'aux' ) ); + $this->setExpectedException( RevisionAccessException::class ); + $slots->getSlot( 'nothere' ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::hasSlot + */ + public function testHasSlot() { + $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) ); + $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] ); + + $this->assertTrue( $slots->hasSlot( 'main' ) ); + $this->assertTrue( $slots->hasSlot( 'aux' ) ); + $this->assertFalse( $slots->hasSlot( 'AUX' ) ); + $this->assertFalse( $slots->hasSlot( 'xyz' ) ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::getContent + */ + public function testGetContent() { + $mainContent = new WikitextContent( 'A' ); + $auxContent = new WikitextContent( 'B' ); + $mainSlot = SlotRecord::newUnsaved( 'main', $mainContent ); + $auxSlot = SlotRecord::newUnsaved( 'aux', $auxContent ); + $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] ); + + $this->assertSame( $mainContent, $slots->getContent( 'main' ) ); + $this->assertSame( $auxContent, $slots->getContent( 'aux' ) ); + $this->setExpectedException( RevisionAccessException::class ); + $slots->getContent( 'nothere' ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles + */ + public function testGetSlotRoles_someSlots() { + $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) ); + $slots = $this->newRevisionSlots( [ $mainSlot, $auxSlot ] ); + + $this->assertSame( [ 'main', 'aux' ], $slots->getSlotRoles() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::getSlotRoles + */ + public function testGetSlotRoles_noSlots() { + $slots = $this->newRevisionSlots( [] ); + + $this->assertSame( [], $slots->getSlotRoles() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionSlots::getSlots + */ + public function testGetSlots() { + $mainSlot = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $auxSlot = SlotRecord::newUnsaved( 'aux', new WikitextContent( 'B' ) ); + $slotsArray = [ $mainSlot, $auxSlot ]; + $slots = $this->newRevisionSlots( $slotsArray ); + + $this->assertEquals( [ 'main' => $mainSlot, 'aux' => $auxSlot ], $slots->getSlots() ); + } + + public function provideComputeSize() { + yield [ 1, [ 'A' ] ]; + yield [ 2, [ 'AA' ] ]; + yield [ 4, [ 'AA', 'X', 'H' ] ]; + } + + /** + * @dataProvider provideComputeSize + * @covers \MediaWiki\Storage\RevisionSlots::computeSize + */ + public function testComputeSize( $expected, $contentStrings ) { + $slotsArray = []; + foreach ( $contentStrings as $key => $contentString ) { + $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) ); + } + $slots = $this->newRevisionSlots( $slotsArray ); + + $this->assertSame( $expected, $slots->computeSize() ); + } + + public function provideComputeSha1() { + yield [ 'ctqm7794fr2dp1taki8a88ovwnvmnmj', [ 'A' ] ]; + yield [ 'eyq8wiwlcofnaiy4eid97gyfy60uw51', [ 'AA' ] ]; + yield [ 'lavctqfpxartyjr31f853drgfl4kj1g', [ 'AA', 'X', 'H' ] ]; + } + + /** + * @dataProvider provideComputeSha1 + * @covers \MediaWiki\Storage\RevisionSlots::computeSha1 + * @note this test is a bit brittle as the hashes are hardcoded, perhaps just check that strings + * are returned and different Slots objects return different strings? + */ + public function testComputeSha1( $expected, $contentStrings ) { + $slotsArray = []; + foreach ( $contentStrings as $key => $contentString ) { + $slotsArray[] = SlotRecord::newUnsaved( strval( $key ), new WikitextContent( $contentString ) ); + } + $slots = $this->newRevisionSlots( $slotsArray ); + + $this->assertSame( $expected, $slots->computeSha1() ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionStoreDbTest.php b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreDbTest.php new file mode 100644 index 00000000..7d6906c1 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreDbTest.php @@ -0,0 +1,1281 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use CommentStoreComment; +use Exception; +use HashBagOStuff; +use InvalidArgumentException; +use Language; +use MediaWiki\Linker\LinkTarget; +use MediaWiki\MediaWikiServices; +use MediaWiki\Storage\BlobStoreFactory; +use MediaWiki\Storage\IncompleteRevisionException; +use MediaWiki\Storage\MutableRevisionRecord; +use MediaWiki\Storage\RevisionRecord; +use MediaWiki\Storage\RevisionStore; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\Storage\SqlBlobStore; +use MediaWikiTestCase; +use Revision; +use TestUserRegistry; +use Title; +use WANObjectCache; +use Wikimedia\Rdbms\Database; +use Wikimedia\Rdbms\DatabaseSqlite; +use Wikimedia\Rdbms\FakeResultWrapper; +use Wikimedia\Rdbms\LoadBalancer; +use Wikimedia\Rdbms\TransactionProfiler; +use WikiPage; +use WikitextContent; + +/** + * @group Database + */ +class RevisionStoreDbTest extends MediaWikiTestCase { + + public function setUp() { + parent::setUp(); + $this->tablesUsed[] = 'archive'; + $this->tablesUsed[] = 'page'; + $this->tablesUsed[] = 'revision'; + $this->tablesUsed[] = 'comment'; + } + + /** + * @return LoadBalancer + */ + private function getLoadBalancerMock( array $server ) { + $lb = $this->getMockBuilder( LoadBalancer::class ) + ->setMethods( [ 'reallyOpenConnection' ] ) + ->setConstructorArgs( [ [ 'servers' => [ $server ] ] ] ) + ->getMock(); + + $lb->method( 'reallyOpenConnection' )->willReturnCallback( + function ( array $server, $dbNameOverride ) { + return $this->getDatabaseMock( $server ); + } + ); + + return $lb; + } + + /** + * @return Database + */ + private function getDatabaseMock( array $params ) { + $db = $this->getMockBuilder( DatabaseSqlite::class ) + ->setMethods( [ 'select', 'doQuery', 'open', 'closeConnection', 'isOpen' ] ) + ->setConstructorArgs( [ $params ] ) + ->getMock(); + + $db->method( 'select' )->willReturn( new FakeResultWrapper( [] ) ); + $db->method( 'isOpen' )->willReturn( true ); + + return $db; + } + + public function provideDomainCheck() { + yield [ false, 'test', '' ]; + yield [ 'test', 'test', '' ]; + + yield [ false, 'test', 'foo_' ]; + yield [ 'test-foo_', 'test', 'foo_' ]; + + yield [ false, 'dash-test', '' ]; + yield [ 'dash-test', 'dash-test', '' ]; + + yield [ false, 'underscore_test', 'foo_' ]; + yield [ 'underscore_test-foo_', 'underscore_test', 'foo_' ]; + } + + /** + * @dataProvider provideDomainCheck + * @covers \MediaWiki\Storage\RevisionStore::checkDatabaseWikiId + */ + public function testDomainCheck( $wikiId, $dbName, $dbPrefix ) { + $this->setMwGlobals( + [ + 'wgDBname' => $dbName, + 'wgDBprefix' => $dbPrefix, + ] + ); + + $loadBalancer = $this->getLoadBalancerMock( + [ + 'host' => '*dummy*', + 'dbDirectory' => '*dummy*', + 'user' => 'test', + 'password' => 'test', + 'flags' => 0, + 'variables' => [], + 'schema' => '', + 'cliMode' => true, + 'agent' => '', + 'load' => 100, + 'profiler' => null, + 'trxProfiler' => new TransactionProfiler(), + 'connLogger' => new \Psr\Log\NullLogger(), + 'queryLogger' => new \Psr\Log\NullLogger(), + 'errorLogger' => function () { + }, + 'deprecationLogger' => function () { + }, + 'type' => 'test', + 'dbname' => $dbName, + 'tablePrefix' => $dbPrefix, + ] + ); + $db = $loadBalancer->getConnection( DB_REPLICA ); + + $blobStore = $this->getMockBuilder( SqlBlobStore::class ) + ->disableOriginalConstructor() + ->getMock(); + + $store = new RevisionStore( + $loadBalancer, + $blobStore, + new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ), + MediaWikiServices::getInstance()->getCommentStore(), + MediaWikiServices::getInstance()->getActorMigration(), + $wikiId + ); + + $count = $store->countRevisionsByPageId( $db, 0 ); + + // Dummy check to make PhpUnit happy. We are really only interested in + // countRevisionsByPageId not failing due to the DB domain check. + $this->assertSame( 0, $count ); + } + + private function assertLinkTargetsEqual( LinkTarget $l1, LinkTarget $l2 ) { + $this->assertEquals( $l1->getDBkey(), $l2->getDBkey() ); + $this->assertEquals( $l1->getNamespace(), $l2->getNamespace() ); + $this->assertEquals( $l1->getFragment(), $l2->getFragment() ); + $this->assertEquals( $l1->getInterwiki(), $l2->getInterwiki() ); + } + + private function assertRevisionRecordsEqual( RevisionRecord $r1, RevisionRecord $r2 ) { + $this->assertEquals( $r1->getUser()->getName(), $r2->getUser()->getName() ); + $this->assertEquals( $r1->getUser()->getId(), $r2->getUser()->getId() ); + $this->assertEquals( $r1->getComment(), $r2->getComment() ); + $this->assertEquals( $r1->getPageAsLinkTarget(), $r2->getPageAsLinkTarget() ); + $this->assertEquals( $r1->getTimestamp(), $r2->getTimestamp() ); + $this->assertEquals( $r1->getVisibility(), $r2->getVisibility() ); + $this->assertEquals( $r1->getSha1(), $r2->getSha1() ); + $this->assertEquals( $r1->getParentId(), $r2->getParentId() ); + $this->assertEquals( $r1->getSize(), $r2->getSize() ); + $this->assertEquals( $r1->getPageId(), $r2->getPageId() ); + $this->assertEquals( $r1->getSlotRoles(), $r2->getSlotRoles() ); + $this->assertEquals( $r1->getWikiId(), $r2->getWikiId() ); + $this->assertEquals( $r1->isMinor(), $r2->isMinor() ); + foreach ( $r1->getSlotRoles() as $role ) { + $this->assertSlotRecordsEqual( $r1->getSlot( $role ), $r2->getSlot( $role ) ); + $this->assertTrue( $r1->getContent( $role )->equals( $r2->getContent( $role ) ) ); + } + foreach ( [ + RevisionRecord::DELETED_TEXT, + RevisionRecord::DELETED_COMMENT, + RevisionRecord::DELETED_USER, + RevisionRecord::DELETED_RESTRICTED, + ] as $field ) { + $this->assertEquals( $r1->isDeleted( $field ), $r2->isDeleted( $field ) ); + } + } + + private function assertSlotRecordsEqual( SlotRecord $s1, SlotRecord $s2 ) { + $this->assertSame( $s1->getRole(), $s2->getRole() ); + $this->assertSame( $s1->getModel(), $s2->getModel() ); + $this->assertSame( $s1->getFormat(), $s2->getFormat() ); + $this->assertSame( $s1->getSha1(), $s2->getSha1() ); + $this->assertSame( $s1->getSize(), $s2->getSize() ); + $this->assertTrue( $s1->getContent()->equals( $s2->getContent() ) ); + + $s1->hasRevision() ? $this->assertSame( $s1->getRevision(), $s2->getRevision() ) : null; + $s1->hasAddress() ? $this->assertSame( $s1->hasAddress(), $s2->hasAddress() ) : null; + } + + private function assertRevisionCompleteness( RevisionRecord $r ) { + foreach ( $r->getSlotRoles() as $role ) { + $this->assertSlotCompleteness( $r, $r->getSlot( $role ) ); + } + } + + private function assertSlotCompleteness( RevisionRecord $r, SlotRecord $slot ) { + $this->assertTrue( $slot->hasAddress() ); + $this->assertSame( $r->getId(), $slot->getRevision() ); + } + + /** + * @param mixed[] $details + * + * @return RevisionRecord + */ + private function getRevisionRecordFromDetailsArray( $title, $details = [] ) { + // Convert some values that can't be provided by dataProviders + $page = WikiPage::factory( $title ); + if ( isset( $details['user'] ) && $details['user'] === true ) { + $details['user'] = $this->getTestUser()->getUser(); + } + if ( isset( $details['page'] ) && $details['page'] === true ) { + $details['page'] = $page->getId(); + } + if ( isset( $details['parent'] ) && $details['parent'] === true ) { + $details['parent'] = $page->getLatest(); + } + + // Create the RevisionRecord with any available data + $rev = new MutableRevisionRecord( $title ); + isset( $details['slot'] ) ? $rev->setSlot( $details['slot'] ) : null; + isset( $details['parent'] ) ? $rev->setParentId( $details['parent'] ) : null; + isset( $details['page'] ) ? $rev->setPageId( $details['page'] ) : null; + isset( $details['size'] ) ? $rev->setSize( $details['size'] ) : null; + isset( $details['sha1'] ) ? $rev->setSha1( $details['sha1'] ) : null; + isset( $details['comment'] ) ? $rev->setComment( $details['comment'] ) : null; + isset( $details['timestamp'] ) ? $rev->setTimestamp( $details['timestamp'] ) : null; + isset( $details['minor'] ) ? $rev->setMinorEdit( $details['minor'] ) : null; + isset( $details['user'] ) ? $rev->setUser( $details['user'] ) : null; + isset( $details['visibility'] ) ? $rev->setVisibility( $details['visibility'] ) : null; + isset( $details['id'] ) ? $rev->setId( $details['id'] ) : null; + + return $rev; + } + + private function getRandomCommentStoreComment() { + return CommentStoreComment::newUnsavedComment( __METHOD__ . '.' . rand( 0, 1000 ) ); + } + + public function provideInsertRevisionOn_successes() { + yield 'Bare minimum revision insertion' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'parent' => true, + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + 'user' => true, + ], + ]; + yield 'Detailed revision insertion' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'parent' => true, + 'page' => true, + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + 'user' => true, + 'minor' => true, + 'visibility' => RevisionRecord::DELETED_RESTRICTED, + ], + ]; + } + + /** + * @dataProvider provideInsertRevisionOn_successes + * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn + */ + public function testInsertRevisionOn_successes( Title $title, array $revDetails = [] ) { + $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails ); + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $return = $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) ); + + $this->assertLinkTargetsEqual( $title, $return->getPageAsLinkTarget() ); + $this->assertRevisionRecordsEqual( $rev, $return ); + $this->assertRevisionCompleteness( $return ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn + */ + public function testInsertRevisionOn_blobAddressExists() { + $title = Title::newFromText( 'UTPage' ); + $revDetails = [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'parent' => true, + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + 'user' => true, + ]; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + + // Insert the first revision + $revOne = $this->getRevisionRecordFromDetailsArray( $title, $revDetails ); + $firstReturn = $store->insertRevisionOn( $revOne, wfGetDB( DB_MASTER ) ); + $this->assertLinkTargetsEqual( $title, $firstReturn->getPageAsLinkTarget() ); + $this->assertRevisionRecordsEqual( $revOne, $firstReturn ); + + // Insert a second revision inheriting the same blob address + $revDetails['slot'] = SlotRecord::newInherited( $firstReturn->getSlot( 'main' ) ); + $revTwo = $this->getRevisionRecordFromDetailsArray( $title, $revDetails ); + $secondReturn = $store->insertRevisionOn( $revTwo, wfGetDB( DB_MASTER ) ); + $this->assertLinkTargetsEqual( $title, $secondReturn->getPageAsLinkTarget() ); + $this->assertRevisionRecordsEqual( $revTwo, $secondReturn ); + + // Assert that the same blob address has been used. + $this->assertEquals( + $firstReturn->getSlot( 'main' )->getAddress(), + $secondReturn->getSlot( 'main' )->getAddress() + ); + // And that different revisions have been created. + $this->assertNotSame( + $firstReturn->getId(), + $secondReturn->getId() + ); + } + + public function provideInsertRevisionOn_failures() { + yield 'no slot' => [ + Title::newFromText( 'UTPage' ), + [ + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + 'user' => true, + ], + new InvalidArgumentException( 'At least one slot needs to be defined!' ) + ]; + yield 'slot that is not main slot' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'lalala', new WikitextContent( 'Chicken' ) ), + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + 'user' => true, + ], + new InvalidArgumentException( 'Only the main slot is supported for now!' ) + ]; + yield 'no timestamp' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'comment' => $this->getRandomCommentStoreComment(), + 'user' => true, + ], + new IncompleteRevisionException( 'timestamp field must not be NULL!' ) + ]; + yield 'no comment' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'timestamp' => '20171117010101', + 'user' => true, + ], + new IncompleteRevisionException( 'comment must not be NULL!' ) + ]; + yield 'no user' => [ + Title::newFromText( 'UTPage' ), + [ + 'slot' => SlotRecord::newUnsaved( 'main', new WikitextContent( 'Chicken' ) ), + 'comment' => $this->getRandomCommentStoreComment(), + 'timestamp' => '20171117010101', + ], + new IncompleteRevisionException( 'user must not be NULL!' ) + ]; + } + + /** + * @dataProvider provideInsertRevisionOn_failures + * @covers \MediaWiki\Storage\RevisionStore::insertRevisionOn + */ + public function testInsertRevisionOn_failures( + Title $title, + array $revDetails = [], + Exception $exception ) { + $rev = $this->getRevisionRecordFromDetailsArray( $title, $revDetails ); + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + + $this->setExpectedException( + get_class( $exception ), + $exception->getMessage(), + $exception->getCode() + ); + $store->insertRevisionOn( $rev, wfGetDB( DB_MASTER ) ); + } + + public function provideNewNullRevision() { + yield [ + Title::newFromText( 'UTPage' ), + CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment1' ), + true, + ]; + yield [ + Title::newFromText( 'UTPage' ), + CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment2', [ 'a' => 1 ] ), + false, + ]; + } + + /** + * @dataProvider provideNewNullRevision + * @covers \MediaWiki\Storage\RevisionStore::newNullRevision + */ + public function testNewNullRevision( Title $title, $comment, $minor ) { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $user = TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser(); + + $parent = $store->getRevisionByTitle( $title ); + $record = $store->newNullRevision( + wfGetDB( DB_MASTER ), + $title, + $comment, + $minor, + $user + ); + + $this->assertEquals( $title->getNamespace(), $record->getPageAsLinkTarget()->getNamespace() ); + $this->assertEquals( $title->getDBkey(), $record->getPageAsLinkTarget()->getDBkey() ); + $this->assertEquals( $comment, $record->getComment() ); + $this->assertEquals( $minor, $record->isMinor() ); + $this->assertEquals( $user->getName(), $record->getUser()->getName() ); + $this->assertEquals( $parent->getId(), $record->getParentId() ); + + $parentSlot = $parent->getSlot( 'main' ); + $slot = $record->getSlot( 'main' ); + + $this->assertTrue( $slot->isInherited(), 'isInherited' ); + $this->assertSame( $parentSlot->getOrigin(), $slot->getOrigin(), 'getOrigin' ); + $this->assertSame( $parentSlot->getAddress(), $slot->getAddress(), 'getAddress' ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newNullRevision + */ + public function testNewNullRevision_nonExistingTitle() { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $record = $store->newNullRevision( + wfGetDB( DB_MASTER ), + Title::newFromText( __METHOD__ . '.iDontExist!' ), + CommentStoreComment::newUnsavedComment( __METHOD__ . ' comment' ), + false, + TestUserRegistry::getMutableTestUser( __METHOD__ )->getUser() + ); + $this->assertNull( $record ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled + */ + public function testGetRcIdIfUnpatrolled_returnsRecentChangesId() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $status = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revisionRecord = $store->getRevisionById( $rev->getId() ); + $result = $store->getRcIdIfUnpatrolled( $revisionRecord ); + + $this->assertGreaterThan( 0, $result ); + $this->assertSame( + $page->getRevision()->getRecentChange()->getAttribute( 'rc_id' ), + $result + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRcIdIfUnpatrolled + */ + public function testGetRcIdIfUnpatrolled_returnsZeroIfPatrolled() { + // This assumes that sysops are auto patrolled + $sysop = $this->getTestSysop()->getUser(); + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $status = $page->doEditContent( + new WikitextContent( __METHOD__ ), + __METHOD__, + 0, + false, + $sysop + ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revisionRecord = $store->getRevisionById( $rev->getId() ); + $result = $store->getRcIdIfUnpatrolled( $revisionRecord ); + + $this->assertSame( 0, $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRecentChange + */ + public function testGetRecentChange() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $content = new WikitextContent( __METHOD__ ); + $status = $page->doEditContent( $content, __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revRecord = $store->getRevisionById( $rev->getId() ); + $recentChange = $store->getRecentChange( $revRecord ); + + $this->assertEquals( $rev->getId(), $recentChange->getAttribute( 'rc_this_oldid' ) ); + $this->assertEquals( $rev->getRecentChange(), $recentChange ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRevisionById + */ + public function testGetRevisionById() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $content = new WikitextContent( __METHOD__ ); + $status = $page->doEditContent( $content, __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revRecord = $store->getRevisionById( $rev->getId() ); + + $this->assertSame( $rev->getId(), $revRecord->getId() ); + $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) ); + $this->assertSame( __METHOD__, $revRecord->getComment()->text ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTitle + */ + public function testGetRevisionByTitle() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $content = new WikitextContent( __METHOD__ ); + $status = $page->doEditContent( $content, __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revRecord = $store->getRevisionByTitle( $page->getTitle() ); + + $this->assertSame( $rev->getId(), $revRecord->getId() ); + $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) ); + $this->assertSame( __METHOD__, $revRecord->getComment()->text ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRevisionByPageId + */ + public function testGetRevisionByPageId() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $content = new WikitextContent( __METHOD__ ); + $status = $page->doEditContent( $content, __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revRecord = $store->getRevisionByPageId( $page->getId() ); + + $this->assertSame( $rev->getId(), $revRecord->getId() ); + $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) ); + $this->assertSame( __METHOD__, $revRecord->getComment()->text ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getRevisionByTimestamp + */ + public function testGetRevisionByTimestamp() { + // Make sure there is 1 second between the last revision and the rev we create... + // Otherwise we might not get the correct revision and the test may fail... + // :( + sleep( 1 ); + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $content = new WikitextContent( __METHOD__ ); + $status = $page->doEditContent( $content, __METHOD__ ); + /** @var Revision $rev */ + $rev = $status->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $revRecord = $store->getRevisionByTimestamp( + $page->getTitle(), + $rev->getTimestamp() + ); + + $this->assertSame( $rev->getId(), $revRecord->getId() ); + $this->assertTrue( $revRecord->getSlot( 'main' )->getContent()->equals( $content ) ); + $this->assertSame( __METHOD__, $revRecord->getComment()->text ); + } + + private function revisionToRow( Revision $rev ) { + $page = WikiPage::factory( $rev->getTitle() ); + + return (object)[ + 'rev_id' => (string)$rev->getId(), + 'rev_page' => (string)$rev->getPage(), + 'rev_text_id' => (string)$rev->getTextId(), + 'rev_timestamp' => (string)$rev->getTimestamp(), + 'rev_user_text' => (string)$rev->getUserText(), + 'rev_user' => (string)$rev->getUser(), + 'rev_minor_edit' => $rev->isMinor() ? '1' : '0', + 'rev_deleted' => (string)$rev->getVisibility(), + 'rev_len' => (string)$rev->getSize(), + 'rev_parent_id' => (string)$rev->getParentId(), + 'rev_sha1' => (string)$rev->getSha1(), + 'rev_comment_text' => $rev->getComment(), + 'rev_comment_data' => null, + 'rev_comment_cid' => null, + 'rev_content_format' => $rev->getContentFormat(), + 'rev_content_model' => $rev->getContentModel(), + 'page_namespace' => (string)$page->getTitle()->getNamespace(), + 'page_title' => $page->getTitle()->getDBkey(), + 'page_id' => (string)$page->getId(), + 'page_latest' => (string)$page->getLatest(), + 'page_is_redirect' => $page->isRedirect() ? '1' : '0', + 'page_len' => (string)$page->getContent()->getSize(), + 'user_name' => (string)$rev->getUserText(), + ]; + } + + private function assertRevisionRecordMatchesRevision( + Revision $rev, + RevisionRecord $record + ) { + $this->assertSame( $rev->getId(), $record->getId() ); + $this->assertSame( $rev->getPage(), $record->getPageId() ); + $this->assertSame( $rev->getTimestamp(), $record->getTimestamp() ); + $this->assertSame( $rev->getUserText(), $record->getUser()->getName() ); + $this->assertSame( $rev->getUser(), $record->getUser()->getId() ); + $this->assertSame( $rev->isMinor(), $record->isMinor() ); + $this->assertSame( $rev->getVisibility(), $record->getVisibility() ); + $this->assertSame( $rev->getSize(), $record->getSize() ); + /** + * @note As of MW 1.31, the database schema allows the parent ID to be + * NULL to indicate that it is unknown. + */ + $expectedParent = $rev->getParentId(); + if ( $expectedParent === null ) { + $expectedParent = 0; + } + $this->assertSame( $expectedParent, $record->getParentId() ); + $this->assertSame( $rev->getSha1(), $record->getSha1() ); + $this->assertSame( $rev->getComment(), $record->getComment()->text ); + $this->assertSame( $rev->getContentFormat(), $record->getContent( 'main' )->getDefaultFormat() ); + $this->assertSame( $rev->getContentModel(), $record->getContent( 'main' )->getModel() ); + $this->assertLinkTargetsEqual( $rev->getTitle(), $record->getPageAsLinkTarget() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29 + */ + public function testNewRevisionFromRow_anonEdit() { + $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH ); + $this->overrideMwServices(); + + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $text = __METHOD__ . 'a-ä'; + /** @var Revision $rev */ + $rev = $page->doEditContent( + new WikitextContent( $text ), + __METHOD__ . 'a' + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $record = $store->newRevisionFromRow( + $this->revisionToRow( $rev ), + [], + $page->getTitle() + ); + $this->assertRevisionRecordMatchesRevision( $rev, $record ); + $this->assertSame( $text, $rev->getContent()->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29 + */ + public function testNewRevisionFromRow_anonEdit_legacyEncoding() { + $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' ); + $this->overrideMwServices(); + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $text = __METHOD__ . 'a-ä'; + /** @var Revision $rev */ + $rev = $page->doEditContent( + new WikitextContent( $text ), + __METHOD__. 'a' + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $record = $store->newRevisionFromRow( + $this->revisionToRow( $rev ), + [], + $page->getTitle() + ); + $this->assertRevisionRecordMatchesRevision( $rev, $record ); + $this->assertSame( $text, $rev->getContent()->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromRow_1_29 + */ + public function testNewRevisionFromRow_userEdit() { + $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_WRITE_BOTH ); + $this->overrideMwServices(); + + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $text = __METHOD__ . 'b-ä'; + /** @var Revision $rev */ + $rev = $page->doEditContent( + new WikitextContent( $text ), + __METHOD__ . 'b', + 0, + false, + $this->getTestUser()->getUser() + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $record = $store->newRevisionFromRow( + $this->revisionToRow( $rev ), + [], + $page->getTitle() + ); + $this->assertRevisionRecordMatchesRevision( $rev, $record ); + $this->assertSame( $text, $rev->getContent()->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow + */ + public function testNewRevisionFromArchiveRow() { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $title = Title::newFromText( __METHOD__ ); + $text = __METHOD__ . '-bä'; + $page = WikiPage::factory( $title ); + /** @var Revision $orig */ + $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ ) + ->value['revision']; + $page->doDeleteArticle( __METHOD__ ); + + $db = wfGetDB( DB_MASTER ); + $arQuery = $store->getArchiveQueryInfo(); + $res = $db->select( + $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ], + __METHOD__, [], $arQuery['joins'] + ); + $this->assertTrue( is_object( $res ), 'query failed' ); + + $row = $res->fetchObject(); + $res->free(); + $record = $store->newRevisionFromArchiveRow( $row ); + + $this->assertRevisionRecordMatchesRevision( $orig, $record ); + $this->assertSame( $text, $record->getContent( 'main' )->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::newRevisionFromArchiveRow + */ + public function testNewRevisionFromArchiveRow_legacyEncoding() { + $this->setMwGlobals( 'wgLegacyEncoding', 'windows-1252' ); + $this->overrideMwServices(); + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $title = Title::newFromText( __METHOD__ ); + $text = __METHOD__ . '-bä'; + $page = WikiPage::factory( $title ); + /** @var Revision $orig */ + $orig = $page->doEditContent( new WikitextContent( $text ), __METHOD__ ) + ->value['revision']; + $page->doDeleteArticle( __METHOD__ ); + + $db = wfGetDB( DB_MASTER ); + $arQuery = $store->getArchiveQueryInfo(); + $res = $db->select( + $arQuery['tables'], $arQuery['fields'], [ 'ar_rev_id' => $orig->getId() ], + __METHOD__, [], $arQuery['joins'] + ); + $this->assertTrue( is_object( $res ), 'query failed' ); + + $row = $res->fetchObject(); + $res->free(); + $record = $store->newRevisionFromArchiveRow( $row ); + + $this->assertRevisionRecordMatchesRevision( $orig, $record ); + $this->assertSame( $text, $record->getContent( 'main' )->serialize() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromId + */ + public function testLoadRevisionFromId() { + $title = Title::newFromText( __METHOD__ ); + $page = WikiPage::factory( $title ); + /** @var Revision $rev */ + $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->loadRevisionFromId( wfGetDB( DB_MASTER ), $rev->getId() ); + $this->assertRevisionRecordMatchesRevision( $rev, $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromPageId + */ + public function testLoadRevisionFromPageId() { + $title = Title::newFromText( __METHOD__ ); + $page = WikiPage::factory( $title ); + /** @var Revision $rev */ + $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->loadRevisionFromPageId( wfGetDB( DB_MASTER ), $page->getId() ); + $this->assertRevisionRecordMatchesRevision( $rev, $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTitle + */ + public function testLoadRevisionFromTitle() { + $title = Title::newFromText( __METHOD__ ); + $page = WikiPage::factory( $title ); + /** @var Revision $rev */ + $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->loadRevisionFromTitle( wfGetDB( DB_MASTER ), $title ); + $this->assertRevisionRecordMatchesRevision( $rev, $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::loadRevisionFromTimestamp + */ + public function testLoadRevisionFromTimestamp() { + $title = Title::newFromText( __METHOD__ ); + $page = WikiPage::factory( $title ); + /** @var Revision $revOne */ + $revOne = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + // Sleep to ensure different timestamps... )(evil) + sleep( 1 ); + /** @var Revision $revTwo */ + $revTwo = $page->doEditContent( new WikitextContent( __METHOD__ . 'a' ), '' ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $this->assertNull( + $store->loadRevisionFromTimestamp( wfGetDB( DB_MASTER ), $title, '20150101010101' ) + ); + $this->assertSame( + $revOne->getId(), + $store->loadRevisionFromTimestamp( + wfGetDB( DB_MASTER ), + $title, + $revOne->getTimestamp() + )->getId() + ); + $this->assertSame( + $revTwo->getId(), + $store->loadRevisionFromTimestamp( + wfGetDB( DB_MASTER ), + $title, + $revTwo->getTimestamp() + )->getId() + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::listRevisionSizes + */ + public function testGetParentLengths() { + $page = WikiPage::factory( Title::newFromText( __METHOD__ ) ); + /** @var Revision $revOne */ + $revOne = $page->doEditContent( + new WikitextContent( __METHOD__ ), __METHOD__ + )->value['revision']; + /** @var Revision $revTwo */ + $revTwo = $page->doEditContent( + new WikitextContent( __METHOD__ . '2' ), __METHOD__ + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $this->assertSame( + [ + $revOne->getId() => strlen( __METHOD__ ), + ], + $store->listRevisionSizes( + wfGetDB( DB_MASTER ), + [ $revOne->getId() ] + ) + ); + $this->assertSame( + [ + $revOne->getId() => strlen( __METHOD__ ), + $revTwo->getId() => strlen( __METHOD__ ) + 1, + ], + $store->listRevisionSizes( + wfGetDB( DB_MASTER ), + [ $revOne->getId(), $revTwo->getId() ] + ) + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getPreviousRevision + */ + public function testGetPreviousRevision() { + $page = WikiPage::factory( Title::newFromText( __METHOD__ ) ); + /** @var Revision $revOne */ + $revOne = $page->doEditContent( + new WikitextContent( __METHOD__ ), __METHOD__ + )->value['revision']; + /** @var Revision $revTwo */ + $revTwo = $page->doEditContent( + new WikitextContent( __METHOD__ . '2' ), __METHOD__ + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $this->assertNull( + $store->getPreviousRevision( $store->getRevisionById( $revOne->getId() ) ) + ); + $this->assertSame( + $revOne->getId(), + $store->getPreviousRevision( $store->getRevisionById( $revTwo->getId() ) )->getId() + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getNextRevision + */ + public function testGetNextRevision() { + $page = WikiPage::factory( Title::newFromText( __METHOD__ ) ); + /** @var Revision $revOne */ + $revOne = $page->doEditContent( + new WikitextContent( __METHOD__ ), __METHOD__ + )->value['revision']; + /** @var Revision $revTwo */ + $revTwo = $page->doEditContent( + new WikitextContent( __METHOD__ . '2' ), __METHOD__ + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $this->assertSame( + $revTwo->getId(), + $store->getNextRevision( $store->getRevisionById( $revOne->getId() ) )->getId() + ); + $this->assertNull( + $store->getNextRevision( $store->getRevisionById( $revTwo->getId() ) ) + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId + */ + public function testGetTimestampFromId_found() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + /** @var Revision $rev */ + $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->getTimestampFromId( + $page->getTitle(), + $rev->getId() + ); + + $this->assertSame( $rev->getTimestamp(), $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getTimestampFromId + */ + public function testGetTimestampFromId_notFound() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + /** @var Revision $rev */ + $rev = $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ) + ->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->getTimestampFromId( + $page->getTitle(), + $rev->getId() + 1 + ); + + $this->assertFalse( $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByPageId + */ + public function testCountRevisionsByPageId() { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $page = WikiPage::factory( Title::newFromText( __METHOD__ ) ); + + $this->assertSame( + 0, + $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() ) + ); + $page->doEditContent( new WikitextContent( 'a' ), 'a' ); + $this->assertSame( + 1, + $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() ) + ); + $page->doEditContent( new WikitextContent( 'b' ), 'b' ); + $this->assertSame( + 2, + $store->countRevisionsByPageId( wfGetDB( DB_MASTER ), $page->getId() ) + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::countRevisionsByTitle + */ + public function testCountRevisionsByTitle() { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $page = WikiPage::factory( Title::newFromText( __METHOD__ ) ); + + $this->assertSame( + 0, + $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() ) + ); + $page->doEditContent( new WikitextContent( 'a' ), 'a' ); + $this->assertSame( + 1, + $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() ) + ); + $page->doEditContent( new WikitextContent( 'b' ), 'b' ); + $this->assertSame( + 2, + $store->countRevisionsByTitle( wfGetDB( DB_MASTER ), $page->getTitle() ) + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit + */ + public function testUserWasLastToEdit_false() { + $sysop = $this->getTestSysop()->getUser(); + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $page->doEditContent( new WikitextContent( __METHOD__ ), __METHOD__ ); + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->userWasLastToEdit( + wfGetDB( DB_MASTER ), + $page->getId(), + $sysop->getId(), + '20160101010101' + ); + $this->assertFalse( $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::userWasLastToEdit + */ + public function testUserWasLastToEdit_true() { + $startTime = wfTimestampNow(); + $sysop = $this->getTestSysop()->getUser(); + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + $page->doEditContent( + new WikitextContent( __METHOD__ ), + __METHOD__, + 0, + false, + $sysop + ); + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $result = $store->userWasLastToEdit( + wfGetDB( DB_MASTER ), + $page->getId(), + $sysop->getId(), + $startTime + ); + $this->assertTrue( $result ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getKnownCurrentRevision + */ + public function testGetKnownCurrentRevision() { + $page = WikiPage::factory( Title::newFromText( 'UTPage' ) ); + /** @var Revision $rev */ + $rev = $page->doEditContent( + new WikitextContent( __METHOD__ . 'b' ), + __METHOD__ . 'b', + 0, + false, + $this->getTestUser()->getUser() + )->value['revision']; + + $store = MediaWikiServices::getInstance()->getRevisionStore(); + $record = $store->getKnownCurrentRevision( + $page->getTitle(), + $rev->getId() + ); + + $this->assertRevisionRecordMatchesRevision( $rev, $record ); + } + + public function provideNewMutableRevisionFromArray() { + yield 'Basic array, with page & id' => [ + [ + 'id' => 2, + 'page' => 1, + 'text_id' => 2, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.2', + 'user' => 0, + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'content_format' => 'text/x-wiki', + 'content_model' => 'wikitext', + ] + ]; + yield 'Basic array, content object' => [ + [ + 'id' => 2, + 'page' => 1, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.2', + 'user' => 0, + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'content' => new WikitextContent( 'Some Content' ), + ] + ]; + yield 'Basic array, serialized text' => [ + [ + 'id' => 2, + 'page' => 1, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.2', + 'user' => 0, + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(), + ] + ]; + yield 'Basic array, serialized text, utf-8 flags' => [ + [ + 'id' => 2, + 'page' => 1, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.2', + 'user' => 0, + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'text' => ( new WikitextContent( 'Söme Content' ) )->serialize(), + 'flags' => 'utf-8', + ] + ]; + yield 'Basic array, with title' => [ + [ + 'title' => Title::newFromText( 'SomeText' ), + 'text_id' => 2, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.2', + 'user' => 0, + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'content_format' => 'text/x-wiki', + 'content_model' => 'wikitext', + ] + ]; + yield 'Basic array, no user field' => [ + [ + 'id' => 2, + 'page' => 1, + 'text_id' => 2, + 'timestamp' => '20171017114835', + 'user_text' => '111.0.1.3', + 'minor_edit' => false, + 'deleted' => 0, + 'len' => 46, + 'parent_id' => 1, + 'sha1' => 'rdqbbzs3pkhihgbs8qf2q9jsvheag5z', + 'comment' => 'Goat Comment!', + 'content_format' => 'text/x-wiki', + 'content_model' => 'wikitext', + ] + ]; + } + + /** + * @dataProvider provideNewMutableRevisionFromArray + * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray + */ + public function testNewMutableRevisionFromArray( array $array ) { + $store = MediaWikiServices::getInstance()->getRevisionStore(); + + $result = $store->newMutableRevisionFromArray( $array ); + + if ( isset( $array['id'] ) ) { + $this->assertSame( $array['id'], $result->getId() ); + } + if ( isset( $array['page'] ) ) { + $this->assertSame( $array['page'], $result->getPageId() ); + } + $this->assertSame( $array['timestamp'], $result->getTimestamp() ); + $this->assertSame( $array['user_text'], $result->getUser()->getName() ); + if ( isset( $array['user'] ) ) { + $this->assertSame( $array['user'], $result->getUser()->getId() ); + } + $this->assertSame( (bool)$array['minor_edit'], $result->isMinor() ); + $this->assertSame( $array['deleted'], $result->getVisibility() ); + $this->assertSame( $array['len'], $result->getSize() ); + $this->assertSame( $array['parent_id'], $result->getParentId() ); + $this->assertSame( $array['sha1'], $result->getSha1() ); + $this->assertSame( $array['comment'], $result->getComment()->text ); + if ( isset( $array['content'] ) ) { + $this->assertTrue( + $result->getSlot( 'main' )->getContent()->equals( $array['content'] ) + ); + } elseif ( isset( $array['text'] ) ) { + $this->assertSame( $array['text'], $result->getSlot( 'main' )->getContent()->serialize() ); + } else { + $this->assertSame( + $array['content_format'], + $result->getSlot( 'main' )->getContent()->getDefaultFormat() + ); + $this->assertSame( $array['content_model'], $result->getSlot( 'main' )->getModel() ); + } + } + + /** + * @dataProvider provideNewMutableRevisionFromArray + * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray + */ + public function testNewMutableRevisionFromArray_legacyEncoding( array $array ) { + $cache = new WANObjectCache( [ 'cache' => new HashBagOStuff() ] ); + $blobStore = new SqlBlobStore( wfGetLB(), $cache ); + $blobStore->setLegacyEncoding( 'windows-1252', Language::factory( 'en' ) ); + + $factory = $this->getMockBuilder( BlobStoreFactory::class ) + ->setMethods( [ 'newBlobStore', 'newSqlBlobStore' ] ) + ->disableOriginalConstructor() + ->getMock(); + $factory->expects( $this->any() ) + ->method( 'newBlobStore' ) + ->willReturn( $blobStore ); + $factory->expects( $this->any() ) + ->method( 'newSqlBlobStore' ) + ->willReturn( $blobStore ); + + $this->setService( 'BlobStoreFactory', $factory ); + + $this->testNewMutableRevisionFromArray( $array ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionStoreRecordTest.php b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreRecordTest.php new file mode 100644 index 00000000..0295e900 --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreRecordTest.php @@ -0,0 +1,363 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use CommentStoreComment; +use InvalidArgumentException; +use MediaWiki\Storage\RevisionRecord; +use MediaWiki\Storage\RevisionSlots; +use MediaWiki\Storage\RevisionStoreRecord; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\User\UserIdentity; +use MediaWiki\User\UserIdentityValue; +use MediaWikiTestCase; +use TextContent; +use Title; + +/** + * @covers \MediaWiki\Storage\RevisionStoreRecord + * @covers \MediaWiki\Storage\RevisionRecord + */ +class RevisionStoreRecordTest extends MediaWikiTestCase { + + use RevisionRecordTests; + + /** + * @param array $rowOverrides + * + * @return RevisionStoreRecord + */ + protected function newRevision( array $rowOverrides = [] ) { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $row = [ + 'rev_id' => '7', + 'rev_page' => strval( $title->getArticleID() ), + 'rev_timestamp' => '20200101000000', + 'rev_deleted' => 0, + 'rev_minor_edit' => 0, + 'rev_parent_id' => '5', + 'rev_len' => $slots->computeSize(), + 'rev_sha1' => $slots->computeSha1(), + 'page_latest' => '18', + ]; + + $row = array_merge( $row, $rowOverrides ); + + return new RevisionStoreRecord( $title, $user, $comment, (object)$row, $slots ); + } + + public function provideConstructor() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $protoRow = [ + 'rev_id' => '7', + 'rev_page' => strval( $title->getArticleID() ), + 'rev_timestamp' => '20200101000000', + 'rev_deleted' => 0, + 'rev_minor_edit' => 0, + 'rev_parent_id' => '5', + 'rev_len' => $slots->computeSize(), + 'rev_sha1' => $slots->computeSha1(), + 'page_latest' => '18', + ]; + + $row = $protoRow; + yield 'all info' => [ + $title, + $user, + $comment, + (object)$row, + $slots, + 'acmewiki' + ]; + + $row = $protoRow; + $row['rev_minor_edit'] = '1'; + $row['rev_deleted'] = strval( RevisionRecord::DELETED_USER ); + + yield 'minor deleted' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + $row['page_latest'] = $row['rev_id']; + + yield 'latest' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + unset( $row['rev_parent'] ); + + yield 'no parent' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + $row['rev_len'] = null; + $row['rev_sha1'] = ''; + + yield 'rev_len is null, rev_sha1 is ""' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + yield 'no length, no hash' => [ + Title::newFromText( 'DummyDoesNotExist' ), + $user, + $comment, + (object)$row, + $slots + ]; + } + + /** + * @dataProvider provideConstructor + * + * @param Title $title + * @param UserIdentity $user + * @param CommentStoreComment $comment + * @param object $row + * @param RevisionSlots $slots + * @param bool $wikiId + */ + public function testConstructorAndGetters( + Title $title, + UserIdentity $user, + CommentStoreComment $comment, + $row, + RevisionSlots $slots, + $wikiId = false + ) { + $rec = new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $wikiId ); + + $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' ); + $this->assertSame( $user, $rec->getUser( RevisionRecord::RAW ), 'getUser' ); + $this->assertSame( $comment, $rec->getComment(), 'getComment' ); + + $this->assertSame( $slots->getSlotRoles(), $rec->getSlotRoles(), 'getSlotRoles' ); + $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' ); + + $this->assertSame( (int)$row->rev_id, $rec->getId(), 'getId' ); + $this->assertSame( (int)$row->rev_page, $rec->getPageId(), 'getId' ); + $this->assertSame( $row->rev_timestamp, $rec->getTimestamp(), 'getTimestamp' ); + $this->assertSame( (int)$row->rev_deleted, $rec->getVisibility(), 'getVisibility' ); + $this->assertSame( (bool)$row->rev_minor_edit, $rec->isMinor(), 'getIsMinor' ); + + if ( isset( $row->rev_parent_id ) ) { + $this->assertSame( (int)$row->rev_parent_id, $rec->getParentId(), 'getParentId' ); + } else { + $this->assertSame( 0, $rec->getParentId(), 'getParentId' ); + } + + if ( isset( $row->rev_len ) ) { + $this->assertSame( (int)$row->rev_len, $rec->getSize(), 'getSize' ); + } else { + $this->assertSame( $slots->computeSize(), $rec->getSize(), 'getSize' ); + } + + if ( !empty( $row->rev_sha1 ) ) { + $this->assertSame( $row->rev_sha1, $rec->getSha1(), 'getSha1' ); + } else { + $this->assertSame( $slots->computeSha1(), $rec->getSha1(), 'getSha1' ); + } + + if ( isset( $row->page_latest ) ) { + $this->assertSame( + (int)$row->rev_id === (int)$row->page_latest, + $rec->isCurrent(), + 'isCurrent' + ); + } else { + $this->assertSame( + false, + $rec->isCurrent(), + 'isCurrent' + ); + } + } + + public function provideConstructorFailure() { + $title = Title::newFromText( 'Dummy' ); + $title->resetArticleID( 17 ); + + $user = new UserIdentityValue( 11, 'Tester', 0 ); + + $comment = CommentStoreComment::newUnsavedComment( 'Hello World' ); + + $main = SlotRecord::newUnsaved( 'main', new TextContent( 'Lorem Ipsum' ) ); + $aux = SlotRecord::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) ); + $slots = new RevisionSlots( [ $main, $aux ] ); + + $protoRow = [ + 'rev_id' => '7', + 'rev_page' => strval( $title->getArticleID() ), + 'rev_timestamp' => '20200101000000', + 'rev_deleted' => 0, + 'rev_minor_edit' => 0, + 'rev_parent_id' => '5', + 'rev_len' => $slots->computeSize(), + 'rev_sha1' => $slots->computeSha1(), + 'page_latest' => '18', + ]; + + yield 'not a row' => [ + $title, + $user, + $comment, + 'not a row', + $slots, + 'acmewiki' + ]; + + $row = $protoRow; + $row['rev_timestamp'] = 'kittens'; + + yield 'bad timestamp' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + $row['rev_page'] = 99; + + yield 'page ID mismatch' => [ + $title, + $user, + $comment, + (object)$row, + $slots + ]; + + $row = $protoRow; + + yield 'bad wiki' => [ + $title, + $user, + $comment, + (object)$row, + $slots, + 12345 + ]; + } + + /** + * @dataProvider provideConstructorFailure + * + * @param Title $title + * @param UserIdentity $user + * @param CommentStoreComment $comment + * @param object $row + * @param RevisionSlots $slots + * @param bool $wikiId + */ + public function testConstructorFailure( + Title $title, + UserIdentity $user, + CommentStoreComment $comment, + $row, + RevisionSlots $slots, + $wikiId = false + ) { + $this->setExpectedException( InvalidArgumentException::class ); + new RevisionStoreRecord( $title, $user, $comment, $row, $slots, $wikiId ); + } + + public function provideIsCurrent() { + yield [ + [ + 'rev_id' => 11, + 'page_latest' => 11, + ], + true, + ]; + yield [ + [ + 'rev_id' => 11, + 'page_latest' => 10, + ], + false, + ]; + } + + /** + * @dataProvider provideIsCurrent + */ + public function testIsCurrent( $row, $current ) { + $rev = $this->newRevision( $row ); + + $this->assertSame( $current, $rev->isCurrent(), 'isCurrent()' ); + } + + public function provideGetSlot_audience_latest() { + return $this->provideAudienceCheckData( RevisionRecord::DELETED_TEXT ); + } + + /** + * @dataProvider provideGetSlot_audience_latest + */ + public function testGetSlot_audience_latest( $visibility, $groups, $userCan, $publicCan ) { + $this->forceStandardPermissions(); + + $user = $this->getTestUser( $groups )->getUser(); + $rev = $this->newRevision( + [ + 'rev_deleted' => $visibility, + 'rev_id' => 11, + 'page_latest' => 11, // revision is current + ] + ); + + // NOTE: slot meta-data is never suppressed, just the content is! + $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::RAW ), 'raw can' ); + $this->assertNotNull( $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC ), 'public can' ); + + $this->assertNotNull( + $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user ), + 'user can' + ); + + $rev->getSlot( 'main', RevisionRecord::RAW )->getContent(); + // NOTE: the content of the current revision is never suppressed! + // Check that getContent() doesn't throw SuppressedDataException + $rev->getSlot( 'main', RevisionRecord::FOR_PUBLIC )->getContent(); + $rev->getSlot( 'main', RevisionRecord::FOR_THIS_USER, $user )->getContent(); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/RevisionStoreTest.php b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreTest.php new file mode 100644 index 00000000..0bce572d --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/RevisionStoreTest.php @@ -0,0 +1,690 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use HashBagOStuff; +use Language; +use MediaWiki\MediaWikiServices; +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 { + + /** + * @param LoadBalancer $loadBalancer + * @param SqlBlobStore $blobStore + * @param WANObjectCache $WANObjectCache + * + * @return RevisionStore + */ + private function getRevisionStore( + $loadBalancer = null, + $blobStore = null, + $WANObjectCache = null + ) { + return new RevisionStore( + $loadBalancer ? $loadBalancer : $this->getMockLoadBalancer(), + $blobStore ? $blobStore : $this->getMockSqlBlobStore(), + $WANObjectCache ? $WANObjectCache : $this->getHashWANObjectCache(), + MediaWikiServices::getInstance()->getCommentStore(), + MediaWikiServices::getInstance()->getActorMigration() + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|LoadBalancer + */ + private function getMockLoadBalancer() { + return $this->getMockBuilder( LoadBalancer::class ) + ->disableOriginalConstructor()->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|Database + */ + private function getMockDatabase() { + return $this->getMockBuilder( Database::class ) + ->disableOriginalConstructor()->getMock(); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject|SqlBlobStore + */ + private function getMockSqlBlobStore() { + return $this->getMockBuilder( SqlBlobStore::class ) + ->disableOriginalConstructor()->getMock(); + } + + private function getHashWANObjectCache() { + return new WANObjectCache( [ 'cache' => new \HashBagOStuff() ] ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getContentHandlerUseDB + * @covers \MediaWiki\Storage\RevisionStore::setContentHandlerUseDB + */ + public function testGetSetContentHandlerDb() { + $store = $this->getRevisionStore(); + $this->assertTrue( $store->getContentHandlerUseDB() ); + $store->setContentHandlerUseDB( false ); + $this->assertFalse( $store->getContentHandlerUseDB() ); + $store->setContentHandlerUseDB( true ); + $this->assertTrue( $store->getContentHandlerUseDB() ); + } + + private function getDefaultQueryFields() { + return [ + 'rev_id', + 'rev_page', + 'rev_text_id', + 'rev_timestamp', + 'rev_minor_edit', + 'rev_deleted', + 'rev_len', + 'rev_parent_id', + 'rev_sha1', + ]; + } + + private function getCommentQueryFields() { + return [ + 'rev_comment_text' => 'rev_comment', + 'rev_comment_data' => 'NULL', + 'rev_comment_cid' => 'NULL', + ]; + } + + private function getActorQueryFields() { + return [ + 'rev_user' => 'rev_user', + 'rev_user_text' => 'rev_user_text', + 'rev_actor' => 'NULL', + ]; + } + + private function getContentHandlerQueryFields() { + return [ + 'rev_content_format', + 'rev_content_model', + ]; + } + + public function provideGetQueryInfo() { + yield [ + true, + [], + [ + 'tables' => [ 'revision' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields(), + $this->getContentHandlerQueryFields() + ), + 'joins' => [], + ] + ]; + yield [ + false, + [], + [ + 'tables' => [ 'revision' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields() + ), + 'joins' => [], + ] + ]; + yield [ + false, + [ 'page' ], + [ + 'tables' => [ 'revision', 'page' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields(), + [ + 'page_namespace', + 'page_title', + 'page_id', + 'page_latest', + 'page_is_redirect', + 'page_len', + ] + ), + 'joins' => [ + 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ], + ], + ] + ]; + yield [ + false, + [ 'user' ], + [ + 'tables' => [ 'revision', 'user' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields(), + [ + 'user_name', + ] + ), + 'joins' => [ + 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ], + ], + ] + ]; + yield [ + false, + [ 'text' ], + [ + 'tables' => [ 'revision', 'text' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields(), + [ + 'old_text', + 'old_flags', + ] + ), + 'joins' => [ + 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ], + ], + ] + ]; + yield [ + true, + [ 'page', 'user', 'text' ], + [ + 'tables' => [ 'revision', 'page', 'user', 'text' ], + 'fields' => array_merge( + $this->getDefaultQueryFields(), + $this->getCommentQueryFields(), + $this->getActorQueryFields(), + $this->getContentHandlerQueryFields(), + [ + 'page_namespace', + 'page_title', + 'page_id', + 'page_latest', + 'page_is_redirect', + 'page_len', + 'user_name', + 'old_text', + 'old_flags', + ] + ), + 'joins' => [ + 'page' => [ 'INNER JOIN', [ 'page_id = rev_page' ] ], + 'user' => [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ], + 'text' => [ 'INNER JOIN', [ 'rev_text_id=old_id' ] ], + ], + ] + ]; + } + + /** + * @dataProvider provideGetQueryInfo + * @covers \MediaWiki\Storage\RevisionStore::getQueryInfo + */ + public function testGetQueryInfo( $contentHandlerUseDb, $options, $expected ) { + $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD ); + $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD ); + $this->overrideMwServices(); + $store = $this->getRevisionStore(); + $store->setContentHandlerUseDB( $contentHandlerUseDb ); + $this->assertEquals( $expected, $store->getQueryInfo( $options ) ); + } + + private function getDefaultArchiveFields() { + return [ + 'ar_id', + 'ar_page_id', + 'ar_namespace', + 'ar_title', + 'ar_rev_id', + 'ar_text_id', + 'ar_timestamp', + 'ar_minor_edit', + 'ar_deleted', + 'ar_len', + 'ar_parent_id', + 'ar_sha1', + ]; + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo + */ + public function testGetArchiveQueryInfo_contentHandlerDb() { + $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD ); + $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD ); + $this->overrideMwServices(); + $store = $this->getRevisionStore(); + $store->setContentHandlerUseDB( true ); + $this->assertEquals( + [ + 'tables' => [ + 'archive' + ], + 'fields' => array_merge( + $this->getDefaultArchiveFields(), + [ + 'ar_comment_text' => 'ar_comment', + 'ar_comment_data' => 'NULL', + 'ar_comment_cid' => 'NULL', + 'ar_user_text' => 'ar_user_text', + 'ar_user' => 'ar_user', + 'ar_actor' => 'NULL', + 'ar_content_format', + 'ar_content_model', + ] + ), + 'joins' => [], + ], + $store->getArchiveQueryInfo() + ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getArchiveQueryInfo + */ + public function testGetArchiveQueryInfo_noContentHandlerDb() { + $this->setMwGlobals( 'wgCommentTableSchemaMigrationStage', MIGRATION_OLD ); + $this->setMwGlobals( 'wgActorTableSchemaMigrationStage', MIGRATION_OLD ); + $this->overrideMwServices(); + $store = $this->getRevisionStore(); + $store->setContentHandlerUseDB( false ); + $this->assertEquals( + [ + 'tables' => [ + 'archive' + ], + 'fields' => array_merge( + $this->getDefaultArchiveFields(), + [ + 'ar_comment_text' => 'ar_comment', + 'ar_comment_data' => 'NULL', + 'ar_comment_cid' => 'NULL', + 'ar_user_text' => 'ar_user_text', + 'ar_user' => 'ar_user', + 'ar_actor' => 'NULL', + ] + ), + 'joins' => [], + ], + $store->getArchiveQueryInfo() + ); + } + + 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_successFromPageIdOnFallback() { + $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 + // Assert that the first call uses a REPLICA and the second falls back to master + $mockLoadBalancer->expects( $this->exactly( 2 ) ) + ->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 ); + + // Second call to Title::newFromID, no result + $db->expects( $this->at( 2 ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( (object)[ + 'page_namespace' => '2', + 'page_title' => 'Foodey', + ] ); + + $store = $this->getRevisionStore( $mockLoadBalancer ); + $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL ); + + $this->assertSame( 2, $title->getNamespace() ); + $this->assertSame( 'Foodey', $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() ); + } + + public function testGetTitle_successFromRevIdOnFallback() { + $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 + // Assert that the first call uses a REPLICA and the second falls back to master + $mockLoadBalancer->expects( $this->exactly( 2 ) ) + ->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 ); + + // Second call to Title::newFromID, no result + $db->expects( $this->at( 2 ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( false ); + + // Second select using rev_id, result + $db->expects( $this->at( 3 ) ) + ->method( 'selectRow' ) + ->with( + [ 'revision', 'page' ], + $this->anything(), + [ 'rev_id' => 2 ] + ) + ->willReturn( (object)[ + 'page_namespace' => '2', + 'page_title' => 'Foodey', + ] ); + + $store = $this->getRevisionStore( $mockLoadBalancer ); + $title = $store->getTitle( 1, 2, RevisionStore::READ_NORMAL ); + + $this->assertSame( 2, $title->getNamespace() ); + $this->assertSame( 'Foodey', $title->getDBkey() ); + } + + /** + * @covers \MediaWiki\Storage\RevisionStore::getTitle + */ + public function testGetTitle_correctFallbackAndthrowsExceptionAfterFallbacks() { + $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 + // Assert that the first call uses a REPLICA and the second falls back to master + + // RevisionStore getTitle uses getConnectionRef + // Title::newFromID uses getConnection + foreach ( [ 'getConnection', 'getConnectionRef' ] as $method ) { + $mockLoadBalancer->expects( $this->exactly( 2 ) ) + ->method( $method ) + ->willReturnCallback( function ( $masterOrReplica ) use ( $db ) { + static $callCounter = 0; + $callCounter++; + // The first call should be to a REPLICA, and the second a MASTER. + if ( $callCounter === 1 ) { + $this->assertSame( DB_REPLICA, $masterOrReplica ); + } elseif ( $callCounter === 2 ) { + $this->assertSame( DB_MASTER, $masterOrReplica ); + } + return $db; + } ); + } + // First and third call to Title::newFromID, faking no result + foreach ( [ 0, 2 ] as $counter ) { + $db->expects( $this->at( $counter ) ) + ->method( 'selectRow' ) + ->with( + 'page', + $this->anything(), + [ 'page_id' => 1 ] + ) + ->willReturn( false ); + } + + foreach ( [ 1, 3 ] as $counter ) { + $db->expects( $this->at( $counter ) ) + ->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 = $this->getRevisionStore( 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 = $this->getRevisionStore( 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; + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/SlotRecordTest.php b/www/wiki/tests/phpunit/includes/Storage/SlotRecordTest.php new file mode 100644 index 00000000..8f26494d --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/SlotRecordTest.php @@ -0,0 +1,298 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use InvalidArgumentException; +use LogicException; +use MediaWiki\Storage\IncompleteRevisionException; +use MediaWiki\Storage\SlotRecord; +use MediaWiki\Storage\SuppressedDataException; +use MediaWikiTestCase; +use WikitextContent; + +/** + * @covers \MediaWiki\Storage\SlotRecord + */ +class SlotRecordTest extends MediaWikiTestCase { + + private function makeRow( $data = [] ) { + $data = $data + [ + 'slot_id' => 1234, + 'slot_content_id' => 33, + 'content_size' => '5', + 'content_sha1' => 'someHash', + 'content_address' => 'tt:456', + 'model_name' => CONTENT_MODEL_WIKITEXT, + 'format_name' => CONTENT_FORMAT_WIKITEXT, + 'slot_revision_id' => '2', + 'slot_origin' => '1', + 'role_name' => 'myRole', + ]; + return (object)$data; + } + + public function testCompleteConstruction() { + $row = $this->makeRow(); + $record = new SlotRecord( $row, new WikitextContent( 'A' ) ); + + $this->assertTrue( $record->hasAddress() ); + $this->assertTrue( $record->hasRevision() ); + $this->assertTrue( $record->isInherited() ); + $this->assertSame( 'A', $record->getContent()->getNativeData() ); + $this->assertSame( 5, $record->getSize() ); + $this->assertSame( 'someHash', $record->getSha1() ); + $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() ); + $this->assertSame( 2, $record->getRevision() ); + $this->assertSame( 1, $record->getOrigin() ); + $this->assertSame( 'tt:456', $record->getAddress() ); + $this->assertSame( 33, $record->getContentId() ); + $this->assertSame( CONTENT_FORMAT_WIKITEXT, $record->getFormat() ); + $this->assertSame( 'myRole', $record->getRole() ); + } + + public function testConstructionDeferred() { + $row = $this->makeRow( [ + 'content_size' => null, // to be computed + 'content_sha1' => null, // to be computed + 'format_name' => function () { + return CONTENT_FORMAT_WIKITEXT; + }, + 'slot_revision_id' => '2', + 'slot_origin' => '2', + ] ); + + $content = function () { + return new WikitextContent( 'A' ); + }; + + $record = new SlotRecord( $row, $content ); + + $this->assertTrue( $record->hasAddress() ); + $this->assertTrue( $record->hasRevision() ); + $this->assertFalse( $record->isInherited() ); + $this->assertSame( 'A', $record->getContent()->getNativeData() ); + $this->assertSame( 1, $record->getSize() ); + $this->assertNotNull( $record->getSha1() ); + $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() ); + $this->assertSame( 2, $record->getRevision() ); + $this->assertSame( 2, $record->getRevision() ); + $this->assertSame( 'tt:456', $record->getAddress() ); + $this->assertSame( 33, $record->getContentId() ); + $this->assertSame( CONTENT_FORMAT_WIKITEXT, $record->getFormat() ); + $this->assertSame( 'myRole', $record->getRole() ); + } + + public function testNewUnsaved() { + $record = SlotRecord::newUnsaved( 'myRole', new WikitextContent( 'A' ) ); + + $this->assertFalse( $record->hasAddress() ); + $this->assertFalse( $record->hasRevision() ); + $this->assertFalse( $record->isInherited() ); + $this->assertSame( 'A', $record->getContent()->getNativeData() ); + $this->assertSame( 1, $record->getSize() ); + $this->assertNotNull( $record->getSha1() ); + $this->assertSame( CONTENT_MODEL_WIKITEXT, $record->getModel() ); + $this->assertSame( 'myRole', $record->getRole() ); + } + + public function provideInvalidConstruction() { + yield 'both null' => [ null, null ]; + yield 'null row' => [ null, new WikitextContent( 'A' ) ]; + yield 'array row' => [ [], new WikitextContent( 'A' ) ]; + yield 'empty row' => [ (object)[], new WikitextContent( 'A' ) ]; + yield 'null content' => [ (object)[], null ]; + } + + /** + * @dataProvider provideInvalidConstruction + */ + public function testInvalidConstruction( $row, $content ) { + $this->setExpectedException( InvalidArgumentException::class ); + new SlotRecord( $row, $content ); + } + + public function testGetContentId_fails() { + $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $this->setExpectedException( IncompleteRevisionException::class ); + + $record->getContentId(); + } + + public function testGetAddress_fails() { + $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $this->setExpectedException( IncompleteRevisionException::class ); + + $record->getAddress(); + } + + public function provideIncomplete() { + $unsaved = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + yield 'unsaved' => [ $unsaved ]; + + $parent = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) ); + $inherited = SlotRecord::newInherited( $parent ); + yield 'inherited' => [ $inherited ]; + } + + /** + * @dataProvider provideIncomplete + */ + public function testGetRevision_fails( SlotRecord $record ) { + $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $this->setExpectedException( IncompleteRevisionException::class ); + + $record->getRevision(); + } + + /** + * @dataProvider provideIncomplete + */ + public function testGetOrigin_fails( SlotRecord $record ) { + $record = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + $this->setExpectedException( IncompleteRevisionException::class ); + + $record->getOrigin(); + } + + public function provideHashStability() { + yield [ '', 'phoiac9h4m842xq45sp7s6u21eteeq1' ]; + yield [ 'Lorem ipsum', 'hcr5u40uxr81d3nx89nvwzclfz6r9c5' ]; + } + + /** + * @dataProvider provideHashStability + */ + public function testHashStability( $text, $hash ) { + // Changing the output of the hash function will break things horribly! + + $this->assertSame( $hash, SlotRecord::base36Sha1( $text ) ); + + $record = SlotRecord::newUnsaved( 'main', new WikitextContent( $text ) ); + $this->assertSame( $hash, $record->getSha1() ); + } + + public function testNewWithSuppressedContent() { + $input = new SlotRecord( $this->makeRow(), new WikitextContent( 'A' ) ); + $output = SlotRecord::newWithSuppressedContent( $input ); + + $this->setExpectedException( SuppressedDataException::class ); + $output->getContent(); + } + + public function testNewInherited() { + $row = $this->makeRow( [ 'slot_revision_id' => 7, 'slot_origin' => 7 ] ); + $parent = new SlotRecord( $row, new WikitextContent( 'A' ) ); + + // This would happen while doing an edit, before saving revision meta-data. + $inherited = SlotRecord::newInherited( $parent ); + + $this->assertSame( $parent->getContentId(), $inherited->getContentId() ); + $this->assertSame( $parent->getAddress(), $inherited->getAddress() ); + $this->assertSame( $parent->getContent(), $inherited->getContent() ); + $this->assertTrue( $inherited->isInherited() ); + $this->assertFalse( $inherited->hasRevision() ); + + // make sure we didn't mess with the internal state of $parent + $this->assertFalse( $parent->isInherited() ); + $this->assertSame( 7, $parent->getRevision() ); + + // This would happen while doing an edit, after saving the revision meta-data + // and content meta-data. + $saved = SlotRecord::newSaved( + 10, + $inherited->getContentId(), + $inherited->getAddress(), + $inherited + ); + $this->assertSame( $parent->getContentId(), $saved->getContentId() ); + $this->assertSame( $parent->getAddress(), $saved->getAddress() ); + $this->assertSame( $parent->getContent(), $saved->getContent() ); + $this->assertTrue( $saved->isInherited() ); + $this->assertTrue( $saved->hasRevision() ); + $this->assertSame( 10, $saved->getRevision() ); + + // make sure we didn't mess with the internal state of $parent or $inherited + $this->assertSame( 7, $parent->getRevision() ); + $this->assertFalse( $inherited->hasRevision() ); + } + + public function testNewSaved() { + // This would happen while doing an edit, before saving revision meta-data. + $unsaved = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + + // This would happen while doing an edit, after saving the revision meta-data + // and content meta-data. + $saved = SlotRecord::newSaved( 10, 20, 'theNewAddress', $unsaved ); + $this->assertFalse( $saved->isInherited() ); + $this->assertTrue( $saved->hasRevision() ); + $this->assertTrue( $saved->hasAddress() ); + $this->assertSame( 'theNewAddress', $saved->getAddress() ); + $this->assertSame( 20, $saved->getContentId() ); + $this->assertSame( 'A', $saved->getContent()->getNativeData() ); + $this->assertSame( 10, $saved->getRevision() ); + $this->assertSame( 10, $saved->getOrigin() ); + + // make sure we didn't mess with the internal state of $unsaved + $this->assertFalse( $unsaved->hasAddress() ); + $this->assertFalse( $unsaved->hasRevision() ); + } + + public function provideNewSaved_LogicException() { + $freshRow = $this->makeRow( [ + 'content_id' => 10, + 'content_address' => 'address:1', + 'slot_origin' => 1, + 'slot_revision_id' => 1, + ] ); + + $freshSlot = new SlotRecord( $freshRow, new WikitextContent( 'A' ) ); + yield 'mismatching address' => [ 1, 10, 'address:BAD', $freshSlot ]; + yield 'mismatching revision' => [ 5, 10, 'address:1', $freshSlot ]; + yield 'mismatching content ID' => [ 1, 17, 'address:1', $freshSlot ]; + + $inheritedRow = $this->makeRow( [ + 'content_id' => null, + 'content_address' => null, + 'slot_origin' => 0, + 'slot_revision_id' => 1, + ] ); + + $inheritedSlot = new SlotRecord( $inheritedRow, new WikitextContent( 'A' ) ); + yield 'inherited, but no address' => [ 1, 10, 'address:2', $inheritedSlot ]; + } + + /** + * @dataProvider provideNewSaved_LogicException + */ + public function testNewSaved_LogicException( + $revisionId, + $contentId, + $contentAddress, + SlotRecord $protoSlot + ) { + $this->setExpectedException( LogicException::class ); + SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot ); + } + + public function provideNewSaved_InvalidArgumentException() { + $unsaved = SlotRecord::newUnsaved( 'main', new WikitextContent( 'A' ) ); + + yield 'bad revision id' => [ 'xyzzy', 5, 'address', $unsaved ]; + yield 'bad content id' => [ 7, 'xyzzy', 'address', $unsaved ]; + yield 'bad content address' => [ 7, 5, 77, $unsaved ]; + } + + /** + * @dataProvider provideNewSaved_InvalidArgumentException + */ + public function testNewSaved_InvalidArgumentException( + $revisionId, + $contentId, + $contentAddress, + SlotRecord $protoSlot + ) { + $this->setExpectedException( InvalidArgumentException::class ); + SlotRecord::newSaved( $revisionId, $contentId, $contentAddress, $protoSlot ); + } + +} diff --git a/www/wiki/tests/phpunit/includes/Storage/SqlBlobStoreTest.php b/www/wiki/tests/phpunit/includes/Storage/SqlBlobStoreTest.php new file mode 100644 index 00000000..dbbef11e --- /dev/null +++ b/www/wiki/tests/phpunit/includes/Storage/SqlBlobStoreTest.php @@ -0,0 +1,241 @@ +<?php + +namespace MediaWiki\Tests\Storage; + +use Language; +use MediaWiki\MediaWikiServices; +use MediaWiki\Storage\SqlBlobStore; +use MediaWikiTestCase; +use stdClass; +use TitleValue; + +/** + * @covers \MediaWiki\Storage\SqlBlobStore + * @group Database + */ +class SqlBlobStoreTest extends MediaWikiTestCase { + + /** + * @return SqlBlobStore + */ + public function getBlobStore( $legacyEncoding = false, $compressRevisions = false ) { + $services = MediaWikiServices::getInstance(); + + $store = new SqlBlobStore( + $services->getDBLoadBalancer(), + $services->getMainWANObjectCache() + ); + + if ( $compressRevisions ) { + $store->setCompressBlobs( $compressRevisions ); + } + if ( $legacyEncoding ) { + $store->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) ); + } + + return $store; + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::getCompressBlobs() + * @covers \MediaWiki\Storage\SqlBlobStore::setCompressBlobs() + */ + public function testGetSetCompressRevisions() { + $store = $this->getBlobStore(); + $this->assertFalse( $store->getCompressBlobs() ); + $store->setCompressBlobs( true ); + $this->assertTrue( $store->getCompressBlobs() ); + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncoding() + * @covers \MediaWiki\Storage\SqlBlobStore::getLegacyEncodingConversionLang() + * @covers \MediaWiki\Storage\SqlBlobStore::setLegacyEncoding() + */ + public function testGetSetLegacyEncoding() { + $store = $this->getBlobStore(); + $this->assertFalse( $store->getLegacyEncoding() ); + $this->assertNull( $store->getLegacyEncodingConversionLang() ); + $en = Language::factory( 'en' ); + $store->setLegacyEncoding( 'foo', $en ); + $this->assertSame( 'foo', $store->getLegacyEncoding() ); + $this->assertSame( $en, $store->getLegacyEncodingConversionLang() ); + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::getCacheExpiry() + * @covers \MediaWiki\Storage\SqlBlobStore::setCacheExpiry() + */ + public function testGetSetCacheExpiry() { + $store = $this->getBlobStore(); + $this->assertSame( 604800, $store->getCacheExpiry() ); + $store->setCacheExpiry( 12 ); + $this->assertSame( 12, $store->getCacheExpiry() ); + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::getUseExternalStore() + * @covers \MediaWiki\Storage\SqlBlobStore::setUseExternalStore() + */ + public function testGetSetUseExternalStore() { + $store = $this->getBlobStore(); + $this->assertFalse( $store->getUseExternalStore() ); + $store->setUseExternalStore( true ); + $this->assertTrue( $store->getUseExternalStore() ); + } + + public function provideDecompress() { + yield '(no legacy encoding), false in false out' => [ false, false, [], false ]; + yield '(no legacy encoding), empty in empty out' => [ false, '', [], '' ]; + yield '(no legacy encoding), empty in empty out' => [ false, 'A', [], 'A' ]; + yield '(no legacy encoding), string in with gzip flag returns string' => [ + // gzip string below generated with gzdeflate( 'AAAABBAAA' ) + false, "sttttr\002\022\000", [ 'gzip' ], 'AAAABBAAA', + ]; + yield '(no legacy encoding), string in with object flag returns false' => [ + // gzip string below generated with serialize( 'JOJO' ) + false, "s:4:\"JOJO\";", [ 'object' ], false, + ]; + yield '(no legacy encoding), serialized object in with object flag returns string' => [ + false, + // Using a TitleValue object as it has a getText method (which is needed) + serialize( new TitleValue( 0, 'HHJJDDFF' ) ), + [ 'object' ], + 'HHJJDDFF', + ]; + yield '(no legacy encoding), serialized object in with object & gzip flag returns string' => [ + false, + // Using a TitleValue object as it has a getText method (which is needed) + gzdeflate( serialize( new TitleValue( 0, '8219JJJ840' ) ) ), + [ 'object', 'gzip' ], + '8219JJJ840', + ]; + yield '(ISO-8859-1 encoding), string in string out' => [ + 'ISO-8859-1', + iconv( 'utf-8', 'ISO-8859-1', "1®Àþ1" ), + [], + '1®Àþ1', + ]; + yield '(ISO-8859-1 encoding), serialized object in with gzip flags returns string' => [ + 'ISO-8859-1', + gzdeflate( iconv( 'utf-8', 'ISO-8859-1', "4®Àþ4" ) ), + [ 'gzip' ], + '4®Àþ4', + ]; + yield '(ISO-8859-1 encoding), serialized object in with object flags returns string' => [ + 'ISO-8859-1', + serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "3®Àþ3" ) ) ), + [ 'object' ], + '3®Àþ3', + ]; + yield '(ISO-8859-1 encoding), serialized object in with object & gzip flags returns string' => [ + 'ISO-8859-1', + gzdeflate( serialize( new TitleValue( 0, iconv( 'utf-8', 'ISO-8859-1', "2®Àþ2" ) ) ) ), + [ 'gzip', 'object' ], + '2®Àþ2', + ]; + yield 'T184749 (windows-1252 encoding), string in string out' => [ + 'windows-1252', + iconv( 'utf-8', 'windows-1252', "sammansättningar" ), + [], + 'sammansättningar', + ]; + yield 'T184749 (windows-1252 encoding), string in string out with gzip' => [ + 'windows-1252', + gzdeflate( iconv( 'utf-8', 'windows-1252', "sammansättningar" ) ), + [ 'gzip' ], + 'sammansättningar', + ]; + } + + /** + * @dataProvider provideDecompress + * @covers \MediaWiki\Storage\SqlBlobStore::decompressData + * + * @param string|bool $legacyEncoding + * @param mixed $data + * @param array $flags + * @param mixed $expected + */ + public function testDecompressData( $legacyEncoding, $data, $flags, $expected ) { + $store = $this->getBlobStore( $legacyEncoding ); + $this->assertSame( + $expected, + $store->decompressData( $data, $flags ) + ); + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::compressData + */ + public function testCompressRevisionTextUtf8() { + $store = $this->getBlobStore(); + $row = new stdClass; + $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; + $row->old_flags = $store->compressData( $row->old_text ); + $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), + "Flags should contain 'utf-8'" ); + $this->assertFalse( false !== strpos( $row->old_flags, 'gzip' ), + "Flags should not contain 'gzip'" ); + $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", + $row->old_text, "Direct check" ); + } + + /** + * @covers \MediaWiki\Storage\SqlBlobStore::compressData + */ + public function testCompressRevisionTextUtf8Gzip() { + $store = $this->getBlobStore( false, true ); + $this->checkPHPExtension( 'zlib' ); + + $row = new stdClass; + $row->old_text = "Wiki est l'\xc3\xa9cole superieur !"; + $row->old_flags = $store->compressData( $row->old_text ); + $this->assertTrue( false !== strpos( $row->old_flags, 'utf-8' ), + "Flags should contain 'utf-8'" ); + $this->assertTrue( false !== strpos( $row->old_flags, 'gzip' ), + "Flags should contain 'gzip'" ); + $this->assertEquals( "Wiki est l'\xc3\xa9cole superieur !", + gzinflate( $row->old_text ), "Direct check" ); + } + + public function provideBlobs() { + yield [ '' ]; + yield [ 'someText' ]; + yield [ "sammansättningar" ]; + } + + /** + * @dataProvider provideBlobs + * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob + * @covers \MediaWiki\Storage\SqlBlobStore::getBlob + */ + public function testSimpleStoreGetBlobSimpleRoundtrip( $blob ) { + $store = $this->getBlobStore(); + $address = $store->storeBlob( $blob ); + $this->assertSame( $blob, $store->getBlob( $address ) ); + } + + /** + * @dataProvider provideBlobs + * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob + * @covers \MediaWiki\Storage\SqlBlobStore::getBlob + */ + public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncoding( $blob ) { + $store = $this->getBlobStore( 'windows-1252' ); + $address = $store->storeBlob( $blob ); + $this->assertSame( $blob, $store->getBlob( $address ) ); + } + + /** + * @dataProvider provideBlobs + * @covers \MediaWiki\Storage\SqlBlobStore::storeBlob + * @covers \MediaWiki\Storage\SqlBlobStore::getBlob + */ + public function testSimpleStoreGetBlobSimpleRoundtripWindowsLegacyEncodingGzip( $blob ) { + $store = $this->getBlobStore( 'windows-1252', true ); + $address = $store->storeBlob( $blob ); + $this->assertSame( $blob, $store->getBlob( $address ) ); + } + +} |