aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/includes/filebackend/filejournal/DBFileJournalIntegrationTest.php
blob: ddb0984826bd14478cce2ee9f8dbd22195797f60 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
<?php

use MediaWiki\MediaWikiServices;
use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @coversDefaultClass DBFileJournal
 * @covers ::__construct
 * @covers ::getMasterDB
 * @group Database
 */
class DBFileJournalIntegrationTest extends MediaWikiIntegrationTestCase {
	public function addDBDataOnce() {
		global $IP;
		$db = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_MASTER );
		if ( $db->getType() !== 'mysql' ) {
			return;
		}
		if ( !$db->tableExists( 'filejournal' ) ) {
			$db->sourceFile( "$IP/maintenance/archives/patch-filejournal.sql" );
		}
	}

	protected function setUp() : void {
		parent::setUp();

		$db = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_MASTER );
		if ( $db->getType() !== 'mysql' ) {
			$this->markTestSkipped( 'No filejournal schema available for this database type' );
		}

		$this->tablesUsed[] = 'filejournal';
	}

	private function getJournal( $options = [] ) {
		return new DBFileJournal(
			$options + [ 'domain' => wfWikiID(), 'backend' => 'local-backend' ] );
	}

	/**
	 * @covers ::doLogChangeBatch
	 */
	public function testDoLogChangeBatch_exceptionDbConnect() {
		$this->setNullLogger( 'DBConnection' );
		$journal = $this->getJournal( [ 'domain' => 'no-such-domain' ] );

		$this->assertEquals(
			StatusValue::newFatal( 'filejournal-fail-dbconnect', 'local-backend' ),
			$journal->logChangeBatch( [ [] ], 'batch' ) );
	}

	/**
	 * @covers ::doLogChangeBatch
	 */
	public function testDoLogChangeBatch_exceptionDbQuery() {
		$this->setNullLogger( 'DBQuery' );
		$journal = $this->getJournal();

		// Null value for 'op' will make the query fail
		$this->assertEquals(
			StatusValue::newFatal( 'filejournal-fail-dbquery', 'local-backend' ),
			$journal->logChangeBatch(
				[ [ 'op' => null, 'path' => '', 'newSha1' => false ] ], 'batch' ) );
	}

	/**
	 * @covers ::doLogChangeBatch
	 * @covers ::doGetCurrentPosition
	 */
	public function testDoGetCurrentPosition() {
		$journal = $this->getJournal();

		$this->assertNull( $journal->getCurrentPosition() );

		$journal->logChangeBatch(
			[ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch1' );

		$this->assertSame( '1', $journal->getCurrentPosition() );

		$journal->logChangeBatch(
			[ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch2' );

		$this->assertSame( '2', $journal->getCurrentPosition() );
	}

	/**
	 * @covers ::doLogChangeBatch
	 * @covers ::doGetPositionAtTime
	 */
	public function testDoGetPositionAtTime() {
		$journal = $this->getJournal();

		$now = time();

		$this->assertFalse( $journal->getPositionAtTime( $now ) );

		ConvertibleTimestamp::setFakeTime( $now - 86400 );

		$journal->logChangeBatch(
			[ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch1' );

		ConvertibleTimestamp::setFakeTime( $now - 3600 );

		$journal->logChangeBatch(
			[ [ 'op' => 'create', 'path' => '/path', 'newSha1' => false ] ], 'batch2' );

		$this->assertFalse( $journal->getPositionAtTime( $now - 86401 ) );
		$this->assertSame( '1', $journal->getPositionAtTime( $now - 86400 ) );
		$this->assertSame( '1', $journal->getPositionAtTime( $now - 3601 ) );
		$this->assertSame( '2', $journal->getPositionAtTime( $now - 3600 ) );
	}

	/**
	 * @param int $expectedStart First index expected to be returned (0-based)
	 * @param int|null $expectedCount Number of entries expected to be returned (null for all)
	 * @param string|null|false $expectedNext Expected value of $next, or false not to pass
	 * @param array $args If any third argument is present, $next will also be tested
	 * @dataProvider provideDoGetChangeEntries
	 * @covers ::doLogChangeBatch
	 * @covers ::doGetChangeEntries
	 */
	public function testDoGetChangeEntries(
		$expectedStart, $expectedCount, $expectedNext, array $args
	) {
		$journal = $this->getJournal();

		$i = 0;
		$makeExpectedEntry = function ( $op, $path, $newSha1, $batch, $time ) use ( &$i ) {
			$i++;
			return [
				'id' => (string)$i,
				'batch_uuid' => $batch,
				'backend' => 'local-backend',
				'path' => $path,
				'op' => $op ?? '',
				'new_sha1' => $newSha1 !== false ? $newSha1 : '0',
				'timestamp' => ConvertibleTimestamp::convert( TS_MW, $time ),
			];
		};

		$expectedEntries = [];

		$now = time();

		ConvertibleTimestamp::setFakeTime( $now - 3600 );
		$changes = [
			[ 'op' => 'create', 'path' => '/path1',
				'newSha1' => base_convert( sha1( 'a' ), 16, 36 ) ],
			[ 'op' => 'delete', 'path' => '/path2', 'newSha1' => false ],
			[ 'op' => 'null', 'path' => '', 'newSha1' => false ],
		];
		$this->assertEquals( StatusValue::newGood(),
			$journal->logChangeBatch( $changes, 'batch1' ) );
		foreach ( $changes as $change ) {
			$expectedEntries[] = $makeExpectedEntry(
				...array_merge( array_values( $change ), [ 'batch1', $now - 3600 ] ) );
		}

		ConvertibleTimestamp::setFakeTime( $now - 60 );
		$change = [ 'op' => 'update', 'path' => '/path1',
			'newSha1' => base_convert( sha1( 'b' ), 16, 36 ) ];
		$this->assertEquals(
		   StatusValue::newGood(), $journal->logChangeBatch( [ $change ], 'batch2' ) );
		$expectedEntries[] = $makeExpectedEntry(
			...array_merge( array_values( $change ), [ 'batch2', $now - 60 ] ) );

		if ( $expectedNext === false ) {
			$this->assertSame(
				array_slice( $expectedEntries, $expectedStart, $expectedCount ),
				$journal->getChangeEntries( ...$args )
			);
		} else {
			$next = false;
			$this->assertSame(
				array_slice( $expectedEntries, $expectedStart, $expectedCount ),
				$journal->getChangeEntries( $args[0], $args[1], $next )
			);
			$this->assertSame( $expectedNext, $next );
		}
	}

	public static function provideDoGetChangeEntries() {
		return [
			'No args' => [ 0, 4, false, [] ],
			'null' => [ 0, 4, false, [ null ] ],
			'1' => [ 0, 4, false, [ 1 ] ],
			'2' => [ 1, 3, false, [ 2 ] ],
			'4' => [ 3, 1, false, [ 4 ] ],
			'5' => [ 0, 0, false, [ 5 ] ],
			'null, 0' => [ 0, 4, null, [ null, 0 ] ],
			'1, 0' => [ 0, 4, null, [ 1, 0 ] ],
			'2, 0' => [ 1, 3, null, [ 2, 0 ] ],
			'4, 0' => [ 3, 1, null, [ 4, 0 ] ],
			'5, 0' => [ 0, 0, null, [ 5, 0 ] ],
			'1, 1' => [ 0, 1, '2', [ 1, 1 ] ],
			'1, 2' => [ 0, 2, '3', [ 1, 2 ] ],
			'1, 4' => [ 0, 4, null, [ 1, 4 ] ],
			'1, 5' => [ 0, 4, null, [ 1, 5 ] ],
			'2, 2' => [ 1, 2, '4', [ 2, 2 ] ],
			'1, 2 with no $next' => [ 0, 2, false, [ 1, 2 ] ],
		];
	}

	/**
	 * @covers ::doPurgeOldLogs
	 */
	public function testDoPurgeOldLogs_noop() {
		// If we tried to access the database, it would throw, because the domain doesn't exist
		$journal = $this->getJournal( [ 'domain' => 'no-such-domain' ] );
		$this->assertEquals( StatusValue::newGood(), $journal->purgeOldLogs() );
	}

	/**
	 * @covers ::doPurgeOldLogs
	 * @covers ::doLogChangeBatch
	 * @covers ::doGetChangeEntries
	 */
	public function testDoPurgeOldLogs() {
		$journal = $this->getJournal( [ 'ttlDays' => 1 ] );
		$now = time();

		// One day and one second ago
		ConvertibleTimestamp::setFakeTime( $now - 86401 );
		$this->assertEquals( StatusValue::newGood(), $journal->logChangeBatch(
			[ [ 'op' => 'null', 'path' => '', 'newSha1' => false ] ], 'batch1' ) );

		// One day ago exactly, won't get purged
		ConvertibleTimestamp::setFakeTime( $now - 86400 );
		$this->assertEquals( StatusValue::newGood(), $journal->logChangeBatch(
			[ [ 'op' => 'null', 'path' => '', 'newSha1' => false ] ], 'batch2' ) );

		ConvertibleTimestamp::setFakeTime( $now );
		$this->assertCount( 2, $journal->getChangeEntries() );
		$journal->purgeOldLogs();
		$this->assertCount( 1, $journal->getChangeEntries() );
	}
}