<?php
+use Wikimedia\TestingAccessWrapper;
+
/**
* @group ContentHandler
*/
/**
* @dataProvider provideConstructFromArray
+ * @covers Revision::__construct
+ * @covers Revision::constructFromRowArray
*/
- public function testConstructFromArray( $rowArray ) {
+ public function testConstructFromArray( array $rowArray ) {
$rev = new Revision( $rowArray );
$this->assertNotNull( $rev->getContent(), 'no content object available' );
$this->assertEquals( CONTENT_MODEL_JAVASCRIPT, $rev->getContent()->getModel() );
/**
* @dataProvider provideConstructFromArrayThrowsExceptions
+ * @covers Revision::__construct
+ * @covers Revision::constructFromRowArray
*/
public function testConstructFromArrayThrowsExceptions( $rowArray, Exception $expectedException ) {
$this->setExpectedException(
];
}
+ public function provideGetId() {
+ yield [
+ [],
+ null
+ ];
+ yield [
+ [ 'id' => 998 ],
+ 998
+ ];
+ }
+
+ /**
+ * @dataProvider provideGetId
+ * @covers Revision::getId
+ */
+ public function testGetId( $rowArray, $expectedId ) {
+ $rev = new Revision( $rowArray );
+ $this->assertEquals( $expectedId, $rev->getId() );
+ }
+
+ public function provideSetId() {
+ yield [ '123', 123 ];
+ yield [ 456, 456 ];
+ }
+
+ /**
+ * @dataProvider provideSetId
+ * @covers Revision::setId
+ */
+ public function testSetId( $input, $expected ) {
+ $rev = new Revision( [] );
+ $rev->setId( $input );
+ $this->assertSame( $expected, $rev->getId() );
+ }
+
+ public function provideSetUserIdAndName() {
+ yield [ '123', 123, 'GOaT' ];
+ yield [ 456, 456, 'GOaT' ];
+ }
+
+ /**
+ * @dataProvider provideSetUserIdAndName
+ * @covers Revision::setUserIdAndName
+ */
+ public function testSetUserIdAndName( $inputId, $expectedId, $name ) {
+ $rev = new Revision( [] );
+ $rev->setUserIdAndName( $inputId, $name );
+ $this->assertSame( $expectedId, $rev->getUser( Revision::RAW ) );
+ $this->assertEquals( $name, $rev->getUserText( Revision::RAW ) );
+ }
+
+ public function provideGetTextId() {
+ yield [ [], null ];
+ yield [ [ 'text_id' => '123' ], 123 ];
+ yield [ [ 'text_id' => 456 ], 456 ];
+ }
+
+ /**
+ * @dataProvider provideGetTextId
+ * @covers Revision::getTextId()
+ */
+ public function testGetTextId( $rowArray, $expected ) {
+ $rev = new Revision( $rowArray );
+ $this->assertSame( $expected, $rev->getTextId() );
+ }
+
+ public function provideGetParentId() {
+ yield [ [], null ];
+ yield [ [ 'parent_id' => '123' ], 123 ];
+ yield [ [ 'parent_id' => 456 ], 456 ];
+ }
+
+ /**
+ * @dataProvider provideGetParentId
+ * @covers Revision::getParentId()
+ */
+ public function testGetParentId( $rowArray, $expected ) {
+ $rev = new Revision( $rowArray );
+ $this->assertSame( $expected, $rev->getParentId() );
+ }
+
/**
* @covers Revision::getRevisionText
* @dataProvider provideGetRevisionText
Revision::getRevisionText( $row ), "getRevisionText" );
}
+ /**
+ * @covers Revision::userJoinCond
+ */
+ public function testUserJoinCond() {
+ $this->assertEquals(
+ [ 'LEFT JOIN', [ 'rev_user != 0', 'user_id = rev_user' ] ],
+ Revision::userJoinCond()
+ );
+ }
+
+ /**
+ * @covers Revision::pageJoinCond
+ */
+ public function testPageJoinCond() {
+ $this->assertEquals(
+ [ 'INNER JOIN', [ 'page_id = rev_page' ] ],
+ Revision::pageJoinCond()
+ );
+ }
+
+ public function provideSelectFields() {
+ yield [
+ true,
+ [
+ 'rev_id',
+ 'rev_page',
+ 'rev_text_id',
+ 'rev_timestamp',
+ 'rev_user_text',
+ 'rev_user',
+ 'rev_minor_edit',
+ 'rev_deleted',
+ 'rev_len',
+ 'rev_parent_id',
+ 'rev_sha1',
+ 'rev_comment_text' => 'rev_comment',
+ 'rev_comment_data' => 'NULL',
+ 'rev_comment_cid' => 'NULL',
+ 'rev_content_format',
+ 'rev_content_model',
+ ]
+ ];
+ yield [
+ false,
+ [
+ 'rev_id',
+ 'rev_page',
+ 'rev_text_id',
+ 'rev_timestamp',
+ 'rev_user_text',
+ 'rev_user',
+ 'rev_minor_edit',
+ 'rev_deleted',
+ 'rev_len',
+ 'rev_parent_id',
+ 'rev_sha1',
+ 'rev_comment_text' => 'rev_comment',
+ 'rev_comment_data' => 'NULL',
+ 'rev_comment_cid' => 'NULL',
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideSelectFields
+ * @covers Revision::selectFields
+ * @todo a true unit test would mock CommentStore
+ */
+ public function testSelectFields( $contentHandlerUseDB, $expected ) {
+ $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
+ $this->assertEquals( $expected, Revision::selectFields() );
+ }
+
+ public function provideSelectArchiveFields() {
+ yield [
+ true,
+ [
+ 'ar_id',
+ 'ar_page_id',
+ 'ar_rev_id',
+ 'ar_text',
+ 'ar_text_id',
+ 'ar_timestamp',
+ 'ar_user_text',
+ 'ar_user',
+ 'ar_minor_edit',
+ 'ar_deleted',
+ 'ar_len',
+ 'ar_parent_id',
+ 'ar_sha1',
+ 'ar_comment_text' => 'ar_comment',
+ 'ar_comment_data' => 'NULL',
+ 'ar_comment_cid' => 'NULL',
+ 'ar_content_format',
+ 'ar_content_model',
+ ]
+ ];
+ yield [
+ false,
+ [
+ 'ar_id',
+ 'ar_page_id',
+ 'ar_rev_id',
+ 'ar_text',
+ 'ar_text_id',
+ 'ar_timestamp',
+ 'ar_user_text',
+ 'ar_user',
+ 'ar_minor_edit',
+ 'ar_deleted',
+ 'ar_len',
+ 'ar_parent_id',
+ 'ar_sha1',
+ 'ar_comment_text' => 'ar_comment',
+ 'ar_comment_data' => 'NULL',
+ 'ar_comment_cid' => 'NULL',
+ ]
+ ];
+ }
+
+ /**
+ * @dataProvider provideSelectArchiveFields
+ * @covers Revision::selectArchiveFields
+ * @todo a true unit test would mock CommentStore
+ */
+ public function testSelectArchiveFields( $contentHandlerUseDB, $expected ) {
+ $this->setMwGlobals( 'wgContentHandlerUseDB', $contentHandlerUseDB );
+ $this->assertEquals( $expected, Revision::selectArchiveFields() );
+ }
+
+ /**
+ * @covers Revision::selectTextFields
+ */
+ public function testSelectTextFields() {
+ $this->assertEquals(
+ [
+ 'old_text',
+ 'old_flags',
+ ],
+ Revision::selectTextFields()
+ );
+ }
+
+ /**
+ * @covers Revision::selectPageFields
+ */
+ public function testSelectPageFields() {
+ $this->assertEquals(
+ [
+ 'page_namespace',
+ 'page_title',
+ 'page_id',
+ 'page_latest',
+ 'page_is_redirect',
+ 'page_len',
+ ],
+ Revision::selectPageFields()
+ );
+ }
+
+ /**
+ * @covers Revision::selectUserFields
+ */
+ public function testSelectUserFields() {
+ $this->assertEquals(
+ [
+ 'user_name',
+ ],
+ Revision::selectUserFields()
+ );
+ }
+
+ public function provideFetchFromConds() {
+ yield [ 0, [] ];
+ yield [ Revision::READ_LOCKING, [ 'FOR UPDATE' ] ];
+ }
+
+ /**
+ * @dataProvider provideFetchFromConds
+ * @covers Revision::fetchFromConds
+ */
+ public function testFetchFromConds( $flags, array $options ) {
+ $conditions = [ 'conditionsArray' ];
+
+ $db = $this->getMock( IDatabase::class );
+ $db->expects( $this->once() )
+ ->method( 'selectRow' )
+ ->with(
+ $this->equalTo( [ 'revision', 'page', 'user' ] ),
+ // We don't really care about the fields are they come from the selectField methods
+ $this->isType( 'array' ),
+ $this->equalTo( $conditions ),
+ // Method name
+ $this->equalTo( 'Revision::fetchFromConds' ),
+ $this->equalTo( $options ),
+ // We don't really care about the join conds are they come from the joinCond methods
+ $this->isType( 'array' )
+ )
+ ->willReturn( 'RETURNVALUE' );
+
+ $wrapper = TestingAccessWrapper::newFromClass( Revision::class );
+ $result = $wrapper->fetchFromConds( $db, $conditions, $flags );
+
+ $this->assertEquals( 'RETURNVALUE', $result );
+ }
}