Merge ".mailmap: Correct two contributor names"
[mediawiki.git] / tests / phpunit / includes / recentchanges / RecentChangesUpdateJobTest.php
blob1aa1c45212d411840a6635a59f149b85ea637daf
1 <?php
3 use MediaWiki\MainConfigNames;
4 use Wikimedia\Timestamp\ConvertibleTimestamp;
6 /**
7 * @group Database
8 * @covers \RecentChangesUpdateJob
9 * @author Dreamy Jazz
11 class RecentChangesUpdateJobTest extends MediaWikiIntegrationTestCase {
13 private function addTestingExpiredRows() {
14 // Make three testing edits, which will trigger a recentchanges insert. Two of the edits will be made
15 // over wgRCMaxAge seconds ago while the other will be made a day ago
16 $testPage = $this->getExistingTestPage();
17 $testUser = $this->getTestUser()->getAuthority();
18 // So that only our two testing edits are present, and nothing from creating the test page or test user
19 $this->truncateTable( 'recentchanges' );
20 // Fix wgRCMaxAge at a high value to ensure that the recentchanges entries we are creating are not purged
21 // by later testing edits.
22 $this->overrideConfigValue( MainConfigNames::RCMaxAge, 24 * 3600 * 1000 );
23 ConvertibleTimestamp::setFakeTime( '20230405060708' );
24 $this->editPage( $testPage, 'testing1234', '', NS_MAIN, $testUser );
25 ConvertibleTimestamp::setFakeTime( '20230705060708' );
26 $this->editPage( $testPage, 'testing12345', '', NS_MAIN, $testUser );
27 ConvertibleTimestamp::setFakeTime( '20240405060708' );
28 $this->editPage( $testPage, 'testing123456', '', NS_MAIN, $testUser );
29 // Verify that the recentchanges table row count is as expected for the test
30 $this->newSelectQueryBuilder()
31 ->field( 'COUNT(*)' )
32 ->table( 'recentchanges' )
33 ->assertFieldValue( 3 );
36 public function testNewPurgeJob() {
37 $this->addTestingExpiredRows();
38 // Set the time as one day beyond the last test edit
39 ConvertibleTimestamp::setFakeTime( '20240406060708' );
40 // Fix wgRCMaxAge for the test, in case the default value changes.
41 $this->overrideConfigValue( MainConfigNames::RCMaxAge, 90 * 24 * 3600 );
42 $hookRunAtLeastOnce = false;
43 $this->setTemporaryHook( 'RecentChangesPurgeRows', function ( $rows ) use ( &$hookRunAtLeastOnce ) {
44 // Check that the first row has the expected columns. Checking just the first row should be fine
45 // as the value of $rows should come from ::fetchResultSet which returns the same columns for each
46 // returned row.
47 $rowAsArray = (array)$rows[0];
48 // To get the expected fields, use the value of the items in the 'fields' array. The exception to this
49 // is where the key is a string, when it should be used instead (as this is an alias).
50 $recentChangeQueryFields = RecentChange::getQueryInfo()['fields'];
51 $expectedFields = [];
52 foreach ( $recentChangeQueryFields as $key => $value ) {
53 if ( is_string( $key ) ) {
54 $expectedFields[] = $key;
55 } else {
56 $expectedFields[] = $value;
59 $this->assertArrayEquals(
60 $expectedFields,
61 array_keys( $rowAsArray ),
62 false,
63 true,
64 'Columns in the provided $row are not as expected'
66 $hookRunAtLeastOnce = true;
67 } );
68 // Call the code we are testing
69 $objectUnderTest = RecentChangesUpdateJob::newPurgeJob();
70 $this->assertInstanceOf( RecentChangesUpdateJob::class, $objectUnderTest );
71 $objectUnderTest->run();
72 // Verify that only the edit made a day ago is now in the recentchanges table
73 $this->newSelectQueryBuilder()
74 ->field( 'rc_timestamp' )
75 ->table( 'recentchanges' )
76 ->assertFieldValue( $this->getDb()->timestamp( '20240405060708' ) );
77 // Verify that the lock placed to do the purge is no longer active.
78 $this->assertTrue( $this->getDb()->lockIsFree(
79 $this->getDb()->getDomainID() . ':recentchanges-prune', __METHOD__
80 ) );
81 // Check that the RecentChangesPurgeRows hook was run at least once
82 $this->assertTrue( $hookRunAtLeastOnce, 'RecentChangesPurgeRows hook was not run' );
85 /** @dataProvider provideInvalidTypes */
86 public function testWhenTypeForInvalidType( $type ) {
87 $this->expectException( InvalidArgumentException::class );
88 $objectUnderTest = new RecentChangesUpdateJob( $this->getExistingTestPage()->getTitle(), [ 'type' => $type ] );
89 $objectUnderTest->run();
92 public static function provideInvalidTypes() {
93 return [
94 'Type is null' => [ null ],
95 'Type is a unrecognised string' => [ 'unknown-type' ],