<?php
+use MediaWiki\Storage\BlobStoreFactory;
+use MediaWiki\Storage\MutableRevisionRecord;
+use MediaWiki\Storage\RevisionAccessException;
+use MediaWiki\Storage\RevisionRecord;
use MediaWiki\Storage\RevisionStore;
+use MediaWiki\Storage\SlotRecord;
use MediaWiki\Storage\SqlBlobStore;
use Wikimedia\Rdbms\IDatabase;
use Wikimedia\Rdbms\LoadBalancer;
->method( 'getPrefixedText' )
->will( $this->returnValue( 'RevisionTest' ) );
$mock->expects( $this->any() )
- ->method( 'getDBKey' )
+ ->method( 'getDBkey' )
->will( $this->returnValue( 'RevisionTest' ) );
$mock->expects( $this->any() )
->method( 'getArticleID' )
$this->assertNull( $rev->getContent(), 'no content object should be available' );
}
+ /**
+ * @covers Revision::__construct
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+ */
+ public function testConstructFromArrayWithBadPageId() {
+ MediaWiki\suppressWarnings();
+ $rev = new Revision( [ 'page' => 77777777 ] );
+ $this->assertSame( 77777777, $rev->getPage() );
+ MediaWiki\restoreWarnings();
+ }
+
public function provideConstructFromArray_userSetAsExpected() {
yield 'no user defaults to wgUser' => [
[
/**
* @dataProvider provideConstructFromArrayThrowsExceptions
* @covers Revision::__construct
- * @covers RevisionStore::newMutableRevisionFromArray
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
*/
public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
$this->setExpectedException(
/**
* @covers Revision::__construct
- * @covers RevisionStore::newMutableRevisionFromArray
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
*/
public function testConstructFromNothing() {
$this->setExpectedException(
/**
* @dataProvider provideConstructFromRow
* @covers Revision::__construct
- * @covers RevisionStore::newRevisionFromRow
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
*/
public function testConstructFromRow( array $arrayData, $assertions ) {
$data = 'Hello goat.'; // needs to match model and format
) );
// Note override internal service, so RevisionStore uses it as well.
- $this->setService( '_SqlBlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$row = (object)$arrayData;
$rev = new Revision( $row, 0, $this->getMockTitle() );
$assertions( $this, $rev );
}
+ /**
+ * @covers Revision::__construct
+ * @covers \MediaWiki\Storage\RevisionStore::newMutableRevisionFromArray
+ */
+ public function testConstructFromRowWithBadPageId() {
+ MediaWiki\suppressWarnings();
+ $rev = new Revision( (object)[ 'rev_page' => 77777777 ] );
+ $this->assertSame( 77777777, $rev->getPage() );
+ MediaWiki\restoreWarnings();
+ }
+
public function provideGetRevisionText() {
yield 'Generic test' => [
'This is a goat of revision text.',
return $blobStore;
}
+ private function mockBlobStoreFactory( $blobStore ) {
+ /** @var LoadBalancer $lb */
+ $factory = $this->getMockBuilder( BlobStoreFactory::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+ $factory->expects( $this->any() )
+ ->method( 'newBlobStore' )
+ ->willReturn( $blobStore );
+ $factory->expects( $this->any() )
+ ->method( 'newSqlBlobStore' )
+ ->willReturn( $blobStore );
+ return $factory;
+ }
+
/**
* @return RevisionStore
*/
public function testGetRevisionWithLegacyEncoding( $expected, $lang, $encoding, $rowData ) {
$blobStore = $this->getBlobStore();
$blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
- $this->setService( 'BlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$this->testGetRevisionText( $expected, $rowData );
}
$blobStore = $this->getBlobStore();
$blobStore->setLegacyEncoding( $encoding, Language::factory( $lang ) );
- $this->setService( 'BlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$this->testGetRevisionText( $expected, $rowData );
}
$blobStore = $this->getBlobStore();
$blobStore->setCompressBlobs( true );
- $this->setService( 'BlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$row = new stdClass;
$row->old_text = "Wiki est l'\xc3\xa9cole superieur !";
$blobStore->setLegacyEncoding( $legacyEncoding, Language::factory( 'en' ) );
}
- $this->setService( 'BlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$this->assertSame(
$expected,
Revision::decompressRevisionText( $text, $flags )
->getMock();
$blobStore = new SqlBlobStore( $lb, $cache );
- $this->setService( 'BlobStore', $blobStore );
+ $this->setService( 'BlobStoreFactory', $this->mockBlobStoreFactory( $blobStore ) );
$this->assertSame(
'AAAABBAAA',
);
}
+ /**
+ * @covers Revision::getSize
+ */
+ public function testGetSize() {
+ $title = $this->getMockTitle();
+
+ $rec = new MutableRevisionRecord( $title );
+ $rev = new Revision( $rec, 0, $title );
+
+ $this->assertSame( 0, $rev->getSize(), 'Size of no slots is 0' );
+
+ $rec->setSize( 13 );
+ $this->assertSame( 13, $rev->getSize() );
+ }
+
+ /**
+ * @covers Revision::getSize
+ */
+ public function testGetSize_failure() {
+ $title = $this->getMockTitle();
+
+ $rec = $this->getMockBuilder( RevisionRecord::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $rec->method( 'getSize' )
+ ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+ $rev = new Revision( $rec, 0, $title );
+ $this->assertNull( $rev->getSize() );
+ }
+
+ /**
+ * @covers Revision::getSha1
+ */
+ public function testGetSha1() {
+ $title = $this->getMockTitle();
+
+ $rec = new MutableRevisionRecord( $title );
+ $rev = new Revision( $rec, 0, $title );
+
+ $emptyHash = SlotRecord::base36Sha1( '' );
+ $this->assertSame( $emptyHash, $rev->getSha1(), 'Sha1 of no slots is hash of empty string' );
+
+ $rec->setSha1( 'deadbeef' );
+ $this->assertSame( 'deadbeef', $rev->getSha1() );
+ }
+
+ /**
+ * @covers Revision::getSha1
+ */
+ public function testGetSha1_failure() {
+ $title = $this->getMockTitle();
+
+ $rec = $this->getMockBuilder( RevisionRecord::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $rec->method( 'getSha1' )
+ ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+ $rev = new Revision( $rec, 0, $title );
+ $this->assertNull( $rev->getSha1() );
+ }
+
+ /**
+ * @covers Revision::getContent
+ */
+ public function testGetContent() {
+ $title = $this->getMockTitle();
+
+ $rec = new MutableRevisionRecord( $title );
+ $rev = new Revision( $rec, 0, $title );
+
+ $this->assertNull( $rev->getContent(), 'Content of no slots is null' );
+
+ $content = new TextContent( 'Hello Kittens!' );
+ $rec->setContent( 'main', $content );
+ $this->assertSame( $content, $rev->getContent() );
+ }
+
+ /**
+ * @covers Revision::getContent
+ */
+ public function testGetContent_failure() {
+ $title = $this->getMockTitle();
+
+ $rec = $this->getMockBuilder( RevisionRecord::class )
+ ->disableOriginalConstructor()
+ ->getMock();
+
+ $rec->method( 'getContent' )
+ ->willThrowException( new RevisionAccessException( 'Oops!' ) );
+
+ $rev = new Revision( $rec, 0, $title );
+ $this->assertNull( $rev->getContent() );
+ }
+
}