3 require_once __DIR__
. '/TestFileJournal.php';
5 use Wikimedia\Timestamp\ConvertibleTimestamp
;
8 * @coversDefaultClass FileJournal
10 class FileJournalTest
extends MediaWikiUnitTestCase
{
11 private function newObj( $options = [], $backend = '' ) {
12 return FileJournal
::factory(
13 $options +
[ 'class' => TestFileJournal
::class ],
21 public function testConstructor_backend() {
22 $this->assertSame( 'some_backend', $this->newObj( [], 'some_backend' )->getBackend() );
26 * @covers ::__construct
29 public function testConstructor_ttlDays() {
30 $this->assertSame( 42, $this->newObj( [ 'ttlDays' => 42 ] )->getTtlDays() );
34 * @covers ::__construct
37 public function testConstructor_noTtlDays() {
38 $this->assertSame( false, $this->newObj()->getTtlDays() );
42 * @covers ::__construct
45 public function testConstructor_nullTtlDays() {
46 $this->assertSame( false, $this->newObj( [ 'ttlDays' => null ] )->getTtlDays() );
52 public function testFactory_invalidClass() {
53 $this->setExpectedException( UnexpectedValueException
::class,
54 'Expected instance of FileJournal, got stdClass' );
56 FileJournal
::factory( [ 'class' => 'stdclass' ], '' );
60 * @covers ::getTimestampedUUID
62 public function testGetTimestampedUUID() {
63 $obj = FileJournal
::factory( [ 'class' => 'NullFileJournal' ], '' );
65 for ( $i = 0; $i < 10; $i++
) {
67 $uuid = $obj->getTimestampedUUID();
69 $this->assertRegexp( '/^[0-9a-z]{31}$/', $uuid );
70 $this->assertArrayNotHasKey( $uuid, $uuids );
73 // Now test that the timestamp portion is as expected.
74 $time = ConvertibleTimestamp
::convert( TS_UNIX
, Wikimedia\base_convert
(
75 substr( $uuid, 0, 9 ), 36, 10 ) );
77 $this->assertGreaterThanOrEqual( $time1, $time );
78 $this->assertLessThanOrEqual( $time2, $time );
83 * @covers ::logChangeBatch
85 public function testLogChangeBatch() {
87 StatusValue
::newGood( 'Logged' ), $this->newObj()->logChangeBatch( [ 1 ], '' ) );
91 * @covers ::logChangeBatch
93 public function testLogChangeBatch_empty() {
94 $this->assertEquals( StatusValue
::newGood(), $this->newObj()->logChangeBatch( [], '' ) );
98 * @covers ::getCurrentPosition
100 public function testGetCurrentPosition() {
101 $this->assertEquals( 613, $this->newObj()->getCurrentPosition() );
105 * @covers ::getPositionAtTime
107 public function testGetPositionAtTime() {
108 $this->assertEquals( 248, $this->newObj()->getPositionAtTime( 0 ) );
112 * @dataProvider provideGetChangeEntries
113 * @covers ::getChangeEntries
114 * @param int|null $start
116 * @param string|null $expectedNext
117 * @param string[] $expectedReturn Expected id's of returned values
119 public function testGetChangeEntries( $start, $limit, $expectedNext, array $expectedReturn ) {
120 $expectedReturn = array_map(
122 return [ 'id' => $val ];
125 $next = "Different from $expectedNext";
126 $ret = $this->newObj()->getChangeEntries( $start, $limit, $next );
127 $this->assertSame( $expectedNext, $next );
128 $this->assertSame( $expectedReturn, $ret );
131 public static function provideGetChangeEntries() {
133 [ null, 0, null, [ 1, 2, 3 ] ],
134 [ null, 1, 2, [ 1 ] ],
135 [ null, 2, 3, [ 1, 2 ] ],
136 [ null, 3, null, [ 1, 2, 3 ] ],
137 [ 1, 0, null, [ 1, 2, 3 ] ],
138 [ 1, 2, 3, [ 1, 2 ] ],
140 [ 2, 2, null, [ 2, 3 ] ],
145 * @covers ::purgeOldLogs
147 public function testPurgeOldLogs() {
148 $obj = $this->newObj();
149 $this->assertFalse( $obj->getPurged() );
150 $obj->purgeOldLogs();
151 $this->assertTrue( $obj->getPurged() );