3 namespace MediaWiki\Tests\Revision
;
5 use CommentStoreComment
;
6 use InvalidArgumentException
;
7 use MediaWiki\Revision\RevisionRecord
;
8 use MediaWiki\Revision\RevisionSlots
;
9 use MediaWiki\Revision\RevisionArchiveRecord
;
10 use MediaWiki\Revision\SlotRecord
;
11 use MediaWiki\User\UserIdentity
;
12 use MediaWiki\User\UserIdentityValue
;
13 use MediaWikiTestCase
;
18 * @covers \MediaWiki\Revision\RevisionArchiveRecord
19 * @covers \MediaWiki\Revision\RevisionRecord
21 class RevisionArchiveRecordTest
extends MediaWikiTestCase
{
23 use RevisionRecordTests
;
26 * @param array $rowOverrides
28 * @return RevisionArchiveRecord
30 protected function newRevision( array $rowOverrides = [] ) {
31 $title = Title
::newFromText( 'Dummy' );
32 $title->resetArticleID( 17 );
34 $user = new UserIdentityValue( 11, 'Tester', 0 );
35 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
37 $main = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new TextContent( 'Lorem Ipsum' ) );
38 $aux = SlotRecord
::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
39 $slots = new RevisionSlots( [ $main, $aux ] );
44 'ar_page_id' => strval( $title->getArticleID() ),
45 'ar_timestamp' => '20200101000000',
48 'ar_parent_id' => '5',
49 'ar_len' => $slots->computeSize(),
50 'ar_sha1' => $slots->computeSha1(),
53 foreach ( $rowOverrides as $field => $value ) {
54 $field = preg_replace( '/^rev_/', 'ar_', $field );
55 $row[$field] = $value;
58 return new RevisionArchiveRecord( $title, $user, $comment, (object)$row, $slots );
61 public function provideConstructor() {
62 $title = Title
::newFromText( 'Dummy' );
63 $title->resetArticleID( 17 );
65 $user = new UserIdentityValue( 11, 'Tester', 0 );
66 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
68 $main = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new TextContent( 'Lorem Ipsum' ) );
69 $aux = SlotRecord
::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
70 $slots = new RevisionSlots( [ $main, $aux ] );
75 'ar_page_id' => strval( $title->getArticleID() ),
76 'ar_timestamp' => '20200101000000',
79 'ar_parent_id' => '5',
80 'ar_len' => $slots->computeSize(),
81 'ar_sha1' => $slots->computeSha1(),
95 $row['ar_minor_edit'] = '1';
96 $row['ar_deleted'] = strval( RevisionRecord
::DELETED_USER
);
98 yield
'minor deleted' => [
107 unset( $row['ar_parent'] );
109 yield
'no parent' => [
118 $row['ar_len'] = null;
119 $row['ar_sha1'] = '';
121 yield
'ar_len is null, ar_sha1 is ""' => [
130 yield
'no length, no hash' => [
131 Title
::newFromText( 'DummyDoesNotExist' ),
140 * @dataProvider provideConstructor
142 * @param Title $title
143 * @param UserIdentity $user
144 * @param CommentStoreComment $comment
146 * @param RevisionSlots $slots
147 * @param bool $wikiId
149 public function testConstructorAndGetters(
152 CommentStoreComment
$comment,
154 RevisionSlots
$slots,
157 $rec = new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $wikiId );
159 $this->assertSame( $title, $rec->getPageAsLinkTarget(), 'getPageAsLinkTarget' );
160 $this->assertSame( $user, $rec->getUser( RevisionRecord
::RAW
), 'getUser' );
161 $this->assertSame( $comment, $rec->getComment(), 'getComment' );
163 $this->assertSame( $slots->getSlotRoles(), $rec->getSlotRoles(), 'getSlotRoles' );
164 $this->assertSame( $wikiId, $rec->getWikiId(), 'getWikiId' );
166 $this->assertSame( (int)$row->ar_id
, $rec->getArchiveId(), 'getArchiveId' );
167 $this->assertSame( (int)$row->ar_rev_id
, $rec->getId(), 'getId' );
168 $this->assertSame( (int)$row->ar_page_id
, $rec->getPageId(), 'getId' );
169 $this->assertSame( $row->ar_timestamp
, $rec->getTimestamp(), 'getTimestamp' );
170 $this->assertSame( (int)$row->ar_deleted
, $rec->getVisibility(), 'getVisibility' );
171 $this->assertSame( (bool)$row->ar_minor_edit
, $rec->isMinor(), 'getIsMinor' );
173 if ( isset( $row->ar_parent_id
) ) {
174 $this->assertSame( (int)$row->ar_parent_id
, $rec->getParentId(), 'getParentId' );
176 $this->assertSame( 0, $rec->getParentId(), 'getParentId' );
179 if ( isset( $row->ar_len
) ) {
180 $this->assertSame( (int)$row->ar_len
, $rec->getSize(), 'getSize' );
182 $this->assertSame( $slots->computeSize(), $rec->getSize(), 'getSize' );
185 if ( !empty( $row->ar_sha1
) ) {
186 $this->assertSame( $row->ar_sha1
, $rec->getSha1(), 'getSha1' );
188 $this->assertSame( $slots->computeSha1(), $rec->getSha1(), 'getSha1' );
192 public function provideConstructorFailure() {
193 $title = Title
::newFromText( 'Dummy' );
194 $title->resetArticleID( 17 );
196 $user = new UserIdentityValue( 11, 'Tester', 0 );
198 $comment = CommentStoreComment
::newUnsavedComment( 'Hello World' );
200 $main = SlotRecord
::newUnsaved( SlotRecord
::MAIN
, new TextContent( 'Lorem Ipsum' ) );
201 $aux = SlotRecord
::newUnsaved( 'aux', new TextContent( 'Frumious Bandersnatch' ) );
202 $slots = new RevisionSlots( [ $main, $aux ] );
207 'ar_page_id' => strval( $title->getArticleID() ),
208 'ar_timestamp' => '20200101000000',
210 'ar_minor_edit' => 0,
211 'ar_parent_id' => '5',
212 'ar_len' => $slots->computeSize(),
213 'ar_sha1' => $slots->computeSha1(),
216 yield
'not a row' => [
226 $row['ar_timestamp'] = 'kittens';
228 yield
'bad timestamp' => [
238 yield
'bad wiki' => [
247 // NOTE: $title->getArticleID does *not* have to match ar_page_id in all cases!
251 * @dataProvider provideConstructorFailure
253 * @param Title $title
254 * @param UserIdentity $user
255 * @param CommentStoreComment $comment
257 * @param RevisionSlots $slots
258 * @param bool $wikiId
260 public function testConstructorFailure(
263 CommentStoreComment
$comment,
265 RevisionSlots
$slots,
268 $this->setExpectedException( InvalidArgumentException
::class );
269 new RevisionArchiveRecord( $title, $user, $comment, $row, $slots, $wikiId );