3 use MediaWiki\MediaWikiServices
;
4 use Wikimedia\Timestamp\ConvertibleTimestamp
;
7 * @coversDefaultClass DBFileJournal
8 * @covers ::__construct
9 * @covers ::getMasterDB
12 class DBFileJournalIntegrationTest
extends MediaWikiIntegrationTestCase
{
13 public function addDBDataOnce() {
15 $db = MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection( DB_MASTER
);
16 if ( $db->getType() !== 'mysql' ) {
19 if ( !$db->tableExists( 'filejournal' ) ) {
20 $db->sourceFile( "$IP/maintenance/archives/patch-filejournal.sql" );
24 protected function setUp() {
27 $db = MediaWikiServices
::getInstance()->getDBLoadBalancer()->getConnection( DB_MASTER
);
28 if ( $db->getType() !== 'mysql' ) {
29 $this->markTestSkipped( 'No filejournal schema available for this database type' );
32 $this->tablesUsed
[] = 'filejournal';
35 private function getJournal( $options = [] ) {
36 return FileJournal
::factory(
37 $options +
[ 'class' => DBFileJournal
::class, 'domain' => wfWikiID() ],
42 * @covers ::doLogChangeBatch
44 public function testDoLogChangeBatch_exceptionDbConnect() {
45 $journal = $this->getJournal( [ 'domain' => 'no-such-domain' ] );
48 StatusValue
::newFatal( 'filejournal-fail-dbconnect', 'local-backend' ),
49 $journal->logChangeBatch( [ [] ], 'batch' ) );
53 * @covers ::doLogChangeBatch
55 public function testDoLogChangeBatch_exceptionDbQuery() {
56 MediaWikiServices
::getInstance()->getConfiguredReadOnlyMode()->setReason( 'testing' );
58 $journal = $this->getJournal();
61 StatusValue
::newFatal( 'filejournal-fail-dbquery', 'local-backend' ),
62 $journal->logChangeBatch(
63 [ [ 'op' => null, 'path' => '', 'newSha1' => false ] ], 'batch' ) );
67 * @covers ::doLogChangeBatch
68 * @covers ::doGetCurrentPosition
70 public function testDoGetCurrentPosition() {
71 $journal = $this->getJournal();
73 $this->assertNull( $journal->getCurrentPosition() );
75 $journal->logChangeBatch(
76 [ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch1' );
78 $this->assertSame( '1', $journal->getCurrentPosition() );
80 $journal->logChangeBatch(
81 [ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch2' );
83 $this->assertSame( '2', $journal->getCurrentPosition() );
87 * @covers ::doLogChangeBatch
88 * @covers ::doGetPositionAtTime
90 public function testDoGetPositionAtTime() {
91 $journal = $this->getJournal();
95 $this->assertFalse( $journal->getPositionAtTime( $now ) );
97 ConvertibleTimestamp
::setFakeTime( $now - 86400 );
99 $journal->logChangeBatch(
100 [ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch1' );
102 ConvertibleTimestamp
::setFakeTime( $now - 3600 );
104 $journal->logChangeBatch(
105 [ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch2' );
107 $this->assertFalse( $journal->getPositionAtTime( $now - 86401 ) );
108 $this->assertSame( '1', $journal->getPositionAtTime( $now - 86400 ) );
109 $this->assertSame( '1', $journal->getPositionAtTime( $now - 3601 ) );
110 $this->assertSame( '2', $journal->getPositionAtTime( $now - 3600 ) );
114 * @param int $expectedStart First index expected to be returned (0-based)
115 * @param int|null $expectedCount Number of entries expected to be returned (null for all)
116 * @param string|null|false $expectedNext Expected value of $next, or false not to pass
117 * @param array $args If any third argument is present, $next will also be tested
118 * @dataProvider provideDoGetChangeEntries
119 * @covers ::doLogChangeBatch
120 * @covers ::doGetChangeEntries
122 public function testDoGetChangeEntries(
123 $expectedStart, $expectedCount, $expectedNext, array $args
125 $journal = $this->getJournal();
128 $makeExpectedEntry = function ( $op, $path, $newSha1, $batch, $time ) use ( &$i ) {
132 'batch_uuid' => $batch,
133 'backend' => 'local-backend',
136 'new_sha1' => $newSha1 !== false ?
$newSha1 : '0',
137 'timestamp' => ConvertibleTimestamp
::convert( TS_MW
, $time ),
141 $expectedEntries = [];
145 ConvertibleTimestamp
::setFakeTime( $now - 3600 );
147 [ 'op' => 'create', 'path' => '/path1',
148 'newSha1' => base_convert( sha1( 'a' ), 16, 36 ) ],
149 [ 'op' => 'delete', 'path' => '/path2', 'newSha1' => false ],
150 [ 'op' => 'null', 'path' => '', 'newSha1' => false ],
152 $this->assertEquals( StatusValue
::newGood(),
153 $journal->logChangeBatch( $changes, 'batch1' ) );
154 foreach ( $changes as $change ) {
155 $expectedEntries[] = $makeExpectedEntry(
156 ...array_merge( array_values( $change ), [ 'batch1', $now - 3600 ] ) );
159 ConvertibleTimestamp
::setFakeTime( $now - 60 );
160 $change = [ 'op' => 'update', 'path' => '/path1',
161 'newSha1' => base_convert( sha1( 'b' ), 16, 36 ) ];
163 StatusValue
::newGood(), $journal->logChangeBatch( [ $change ], 'batch2' ) );
164 $expectedEntries[] = $makeExpectedEntry(
165 ...array_merge( array_values( $change ), [ 'batch2', $now - 60 ] ) );
167 if ( $expectedNext === false ) {
169 array_slice( $expectedEntries, $expectedStart, $expectedCount ),
170 $journal->getChangeEntries( ...$args )
175 array_slice( $expectedEntries, $expectedStart, $expectedCount ),
176 $journal->getChangeEntries( $args[0], $args[1], $next )
178 $this->assertSame( $expectedNext, $next );
182 public static function provideDoGetChangeEntries() {
184 'No args' => [ 0, 4, false, [] ],
185 'null' => [ 0, 4, false, [ null ] ],
186 '1' => [ 0, 4, false, [ 1 ] ],
187 '2' => [ 1, 3, false, [ 2 ] ],
188 '4' => [ 3, 1, false, [ 4 ] ],
189 '5' => [ 0, 0, false, [ 5 ] ],
190 'null, 0' => [ 0, 4, null, [ null, 0 ] ],
191 '1, 0' => [ 0, 4, null, [ 1, 0 ] ],
192 '2, 0' => [ 1, 3, null, [ 2, 0 ] ],
193 '4, 0' => [ 3, 1, null, [ 4, 0 ] ],
194 '5, 0' => [ 0, 0, null, [ 5, 0 ] ],
195 '1, 1' => [ 0, 1, '2', [ 1, 1 ] ],
196 '1, 2' => [ 0, 2, '3', [ 1, 2 ] ],
197 '1, 4' => [ 0, 4, null, [ 1, 4 ] ],
198 '1, 5' => [ 0, 4, null, [ 1, 5 ] ],
199 '2, 2' => [ 1, 2, '4', [ 2, 2 ] ],
200 '1, 2 with no $next' => [ 0, 2, false, [ 1, 2 ] ],
205 * @covers ::doPurgeOldLogs
207 public function testDoPurgeOldLogs_noop() {
208 // If we tried to access the database, it would throw, because the domain doesn't exist
209 $journal = $this->getJournal( [ 'domain' => 'no-such-domain' ] );
210 $this->assertEquals( StatusValue
::newGood(), $journal->purgeOldLogs() );
214 * @covers ::doPurgeOldLogs
215 * @covers ::doLogChangeBatch
216 * @covers ::doGetChangeEntries
218 public function testDoPurgeOldLogs() {
219 $journal = $this->getJournal( [ 'ttlDays' => 1 ] );
222 // One day and one second ago
223 ConvertibleTimestamp
::setFakeTime( $now - 86401 );
224 $this->assertEquals( StatusValue
::newGood(), $journal->logChangeBatch(
225 [ [ 'op' => 'null', 'path' => '', 'newSha1' => false ] ], 'batch1' ) );
227 // One day ago exactly, won't get purged
228 ConvertibleTimestamp
::setFakeTime( $now - 86400 );
229 $this->assertEquals( StatusValue
::newGood(), $journal->logChangeBatch(
230 [ [ 'op' => 'null', 'path' => '', 'newSha1' => false ] ], 'batch2' ) );
232 ConvertibleTimestamp
::setFakeTime( $now );
233 $this->assertCount( 2, $journal->getChangeEntries() );
234 $journal->purgeOldLogs();
235 $this->assertCount( 1, $journal->getChangeEntries() );