File: FileJournalTest.php

package info (click to toggle)
mediawiki 1%3A1.35.13-1%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 274,932 kB
  • sloc: php: 677,563; javascript: 572,709; sql: 11,565; python: 4,447; xml: 3,145; sh: 892; perl: 788; ruby: 496; pascal: 365; makefile: 128
file content (138 lines) | stat: -rw-r--r-- 3,504 bytes parent folder | download
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
<?php

require_once __DIR__ . '/TestFileJournal.php';

use Wikimedia\Timestamp\ConvertibleTimestamp;

/**
 * @coversDefaultClass FileJournal
 */
class FileJournalTest extends MediaWikiUnitTestCase {
	private function newObj( $options = [] ) {
		return new TestFileJournal( $options + [ 'backend' => '' ] );
	}

	/**
	 * @covers ::factory
	 */
	public function testConstructor_backend() {
		$this->assertSame( 'some_backend',
			$this->newObj( [ 'backend' => 'some_backend' ] )->getBackend() );
	}

	/**
	 * @covers ::__construct
	 */
	public function testConstructor_ttlDays() {
		$this->assertSame( 42, $this->newObj( [ 'ttlDays' => 42 ] )->getTtlDays() );
	}

	/**
	 * @covers ::__construct
	 */
	public function testConstructor_noTtlDays() {
		$this->assertSame( false, $this->newObj()->getTtlDays() );
	}

	/**
	 * @covers ::__construct
	 */
	public function testConstructor_nullTtlDays() {
		$this->assertSame( false, $this->newObj( [ 'ttlDays' => null ] )->getTtlDays() );
	}

	/**
	 * @covers ::getTimestampedUUID
	 */
	public function testGetTimestampedUUID() {
		$obj = new NullFileJournal;
		$uuids = [];
		for ( $i = 0; $i < 10; $i++ ) {
			$time1 = time();
			$uuid = $obj->getTimestampedUUID();
			$time2 = time();
			$this->assertRegExp( '/^[0-9a-z]{31}$/', $uuid );
			$this->assertArrayNotHasKey( $uuid, $uuids );
			$uuids[$uuid] = true;

			// Now test that the timestamp portion is as expected.
			$time = ConvertibleTimestamp::convert( TS_UNIX, Wikimedia\base_convert(
				substr( $uuid, 0, 9 ), 36, 10 ) );

			$this->assertGreaterThanOrEqual( $time1, $time );
			$this->assertLessThanOrEqual( $time2, $time );
		}
	}

	/**
	 * @covers ::logChangeBatch
	 */
	public function testLogChangeBatch() {
		$this->assertEquals(
			StatusValue::newGood( 'Logged' ), $this->newObj()->logChangeBatch( [ 1 ], '' ) );
	}

	/**
	 * @covers ::logChangeBatch
	 */
	public function testLogChangeBatch_empty() {
		$this->assertEquals( StatusValue::newGood(), $this->newObj()->logChangeBatch( [], '' ) );
	}

	/**
	 * @covers ::getCurrentPosition
	 */
	public function testGetCurrentPosition() {
		$this->assertEquals( 613, $this->newObj()->getCurrentPosition() );
	}

	/**
	 * @covers ::getPositionAtTime
	 */
	public function testGetPositionAtTime() {
		$this->assertEquals( 248, $this->newObj()->getPositionAtTime( 0 ) );
	}

	/**
	 * @dataProvider provideGetChangeEntries
	 * @covers ::getChangeEntries
	 * @param int|null $start
	 * @param int $limit
	 * @param string|null $expectedNext
	 * @param string[] $expectedReturn Expected id's of returned values
	 */
	public function testGetChangeEntries( $start, $limit, $expectedNext, array $expectedReturn ) {
		$expectedReturn = array_map(
			function ( $val ) {
				return [ 'id' => $val ];
			}, $expectedReturn
		);
		$next = "Different from $expectedNext";
		$ret = $this->newObj()->getChangeEntries( $start, $limit, $next );
		$this->assertSame( $expectedNext, $next );
		$this->assertSame( $expectedReturn, $ret );
	}

	public static function provideGetChangeEntries() {
		return [
			[ null, 0, null, [ 1, 2, 3 ] ],
			[ null, 1, 2, [ 1 ] ],
			[ null, 2, 3, [ 1, 2 ] ],
			[ null, 3, null, [ 1, 2, 3 ] ],
			[ 1, 0, null, [ 1, 2, 3 ] ],
			[ 1, 2, 3, [ 1, 2 ] ],
			[ 1, 1, 2, [ 1 ] ],
			[ 2, 2, null, [ 2, 3 ] ],
		];
	}

	/**
	 * @covers ::purgeOldLogs
	 */
	public function testPurgeOldLogs() {
		$obj = $this->newObj();
		$this->assertFalse( $obj->getPurged() );
		$obj->purgeOldLogs();
		$this->assertTrue( $obj->getPurged() );
	}
}