Debian Codegraph @

tests/phpunit/unit/includes/Revision/RevisionStoreCacheRecordTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit\Revision;
4
5 use DummyContentForTesting;
6 use MediaWiki\CommentStore\CommentStoreComment;
7 use MediaWiki\Page\PageIdentityValue;
8 use MediaWiki\Revision\RevisionRecord;
9 use MediaWiki\Revision\RevisionSlots;
10 use MediaWiki\Revision\RevisionStoreCacheRecord;
11 use MediaWiki\Revision\RevisionStoreRecord;
12 use MediaWiki\Revision\SlotRecord;
13 use MediaWiki\User\UserIdentityValue;
14
15 /**
16 * @covers \MediaWiki\Revision\RevisionStoreCacheRecord
17 * @covers \MediaWiki\Revision\RevisionStoreRecord
18 * @covers \MediaWiki\Revision\RevisionRecord
19 */
20 class RevisionStoreCacheRecordTest extends RevisionStoreRecordTest {
21
22 use RevisionRecordTests;
23
24 /**
25 * @param array $rowOverrides
26 * @param callable|false $callback function to use, or false for a default no-op callback
27 *
28 * @return RevisionStoreRecord
29 */
30 protected function newRevision( array $rowOverrides = [], $callback = false ) {
31 $wikiId = $rowOverrides['wikiId'] ?? RevisionRecord::LOCAL;
32
33 $title = new PageIdentityValue( 17, NS_MAIN, 'Dummy', $wikiId );
34
35 $user = new UserIdentityValue( 11, 'Tester' );
36 $comment = CommentStoreComment::newUnsavedComment( 'Hello World' );
37
38 $main = SlotRecord::newUnsaved( SlotRecord::MAIN, new DummyContentForTesting( 'Lorem Ipsum' ) );
39 $aux = SlotRecord::newUnsaved( 'aux', new DummyContentForTesting( 'Frumious Bandersnatch' ) );
40 $slots = new RevisionSlots( [ $main, $aux ] );
41
42 $row = [
43 'rev_id' => '7',
44 'rev_page' => '17',
45 'rev_timestamp' => '20200101000000',
46 'rev_deleted' => 0,
47 'rev_minor_edit' => 0,
48 'rev_parent_id' => '5',
49 'rev_len' => $slots->computeSize(),
50 'rev_sha1' => $slots->computeSha1(),
51 'rev_user' => '11',
52 'page_latest' => '18',
53 ];
54
55 $row = array_merge( $row, $rowOverrides );
56
57 if ( !$callback ) {
58 $callback = function ( $revId ) use ( $row ) {
59 $this->assertSame( 7, $revId );
60 return [
61 $row['rev_deleted'],
62 new UserIdentityValue( intval( $row['rev_user'] ), 'Bla' )
63 ];
64 };
65 }
66
67 return new RevisionStoreCacheRecord(
68 $callback,
69 $title,
70 $user,
71 $comment,
72 (object)$row,
73 $slots,
74 $wikiId
75 );
76 }
77
78 public function testCallback() {
79 // Provide a callback that returns non-default values. Asserting the revision returns
80 // these values confirms callback execution and behavior. Also confirm the callback
81 // is only invoked once, even for multiple getter calls.
82 $callbackInvoked = 0;
83 $callback = function ( $revId ) use ( &$callbackInvoked ) {
84 $this->assertSame( 7, $revId );
85 $callbackInvoked++;
86 return [
87 RevisionRecord::DELETED_TEXT,
88 new UserIdentityValue( 12, 'Lalala' )
89 ];
90 };
91 $rev = $this->newRevision( [], $callback );
92
93 $this->assertSame( RevisionRecord::DELETED_TEXT, $rev->getVisibility() );
94 $this->assertSame( 12, $rev->getUser()->getId() );
95 $this->assertSame( 1, $callbackInvoked );
96 }
97
98 }