Merge "Fix use of GenderCache in ApiPageSet::processTitlesArray"
[lhc/web/wiklou.git] / tests / phpunit / unit / includes / libs / filebackend / filejournal / FileJournalTest.php
1 <?php
2
3 require_once __DIR__ . '/TestFileJournal.php';
4
5 use Wikimedia\Timestamp\ConvertibleTimestamp;
6
7 /**
8 * @coversDefaultClass FileJournal
9 */
10 class FileJournalTest extends MediaWikiUnitTestCase {
11 private function newObj( $options = [], $backend = '' ) {
12 return FileJournal::factory(
13 $options + [ 'class' => TestFileJournal::class ],
14 $backend
15 );
16 }
17
18 /**
19 * @covers ::factory
20 */
21 public function testConstructor_backend() {
22 $this->assertSame( 'some_backend', $this->newObj( [], 'some_backend' )->getBackend() );
23 }
24
25 /**
26 * @covers ::__construct
27 * @covers ::factory
28 */
29 public function testConstructor_ttlDays() {
30 $this->assertSame( 42, $this->newObj( [ 'ttlDays' => 42 ] )->getTtlDays() );
31 }
32
33 /**
34 * @covers ::__construct
35 * @covers ::factory
36 */
37 public function testConstructor_noTtlDays() {
38 $this->assertSame( false, $this->newObj()->getTtlDays() );
39 }
40
41 /**
42 * @covers ::__construct
43 * @covers ::factory
44 */
45 public function testConstructor_nullTtlDays() {
46 $this->assertSame( false, $this->newObj( [ 'ttlDays' => null ] )->getTtlDays() );
47 }
48
49 /**
50 * @covers ::factory
51 */
52 public function testFactory_invalidClass() {
53 $this->setExpectedException( UnexpectedValueException::class,
54 'Expected instance of FileJournal, got stdClass' );
55
56 FileJournal::factory( [ 'class' => 'stdclass' ], '' );
57 }
58
59 /**
60 * @covers ::getTimestampedUUID
61 */
62 public function testGetTimestampedUUID() {
63 $obj = FileJournal::factory( [ 'class' => 'NullFileJournal' ], '' );
64 $uuids = [];
65 for ( $i = 0; $i < 10; $i++ ) {
66 $time1 = time();
67 $uuid = $obj->getTimestampedUUID();
68 $time2 = time();
69 $this->assertRegexp( '/^[0-9a-z]{31}$/', $uuid );
70 $this->assertArrayNotHasKey( $uuid, $uuids );
71 $uuids[$uuid] = true;
72
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 ) );
76
77 $this->assertGreaterThanOrEqual( $time1, $time );
78 $this->assertLessThanOrEqual( $time2, $time );
79 }
80 }
81
82 /**
83 * @covers ::logChangeBatch
84 */
85 public function testLogChangeBatch() {
86 $this->assertEquals(
87 StatusValue::newGood( 'Logged' ), $this->newObj()->logChangeBatch( [ 1 ], '' ) );
88 }
89
90 /**
91 * @covers ::logChangeBatch
92 */
93 public function testLogChangeBatch_empty() {
94 $this->assertEquals( StatusValue::newGood(), $this->newObj()->logChangeBatch( [], '' ) );
95 }
96
97 /**
98 * @covers ::getCurrentPosition
99 */
100 public function testGetCurrentPosition() {
101 $this->assertEquals( 613, $this->newObj()->getCurrentPosition() );
102 }
103
104 /**
105 * @covers ::getPositionAtTime
106 */
107 public function testGetPositionAtTime() {
108 $this->assertEquals( 248, $this->newObj()->getPositionAtTime( 0 ) );
109 }
110
111 /**
112 * @dataProvider provideGetChangeEntries
113 * @covers ::getChangeEntries
114 * @param int|null $start
115 * @param int $limit
116 * @param string|null $expectedNext
117 * @param string[] $expectedReturn Expected id's of returned values
118 */
119 public function testGetChangeEntries( $start, $limit, $expectedNext, array $expectedReturn ) {
120 $expectedReturn = array_map(
121 function ( $val ) {
122 return [ 'id' => $val ];
123 }, $expectedReturn
124 );
125 $next = "Different from $expectedNext";
126 $ret = $this->newObj()->getChangeEntries( $start, $limit, $next );
127 $this->assertSame( $expectedNext, $next );
128 $this->assertSame( $expectedReturn, $ret );
129 }
130
131 public static function provideGetChangeEntries() {
132 return [
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 ] ],
139 [ 1, 1, 2, [ 1 ] ],
140 [ 2, 2, null, [ 2, 3 ] ],
141 ];
142 }
143
144 /**
145 * @covers ::purgeOldLogs
146 */
147 public function testPurgeOldLogs() {
148 $obj = $this->newObj();
149 $this->assertFalse( $obj->getPurged() );
150 $obj->purgeOldLogs();
151 $this->assertTrue( $obj->getPurged() );
152 }
153 }