3 namespace MediaWiki\Tests\Maintenance
;
5 use DeleteOrphanedRevisions
;
6 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait
;
7 use MediaWiki\Tests\User\TempUser\TempUserTestTrait
;
10 * @covers \DeleteOrphanedRevisions
14 class DeleteOrphanedRevisionsTest
extends MaintenanceBaseTestCase
{
16 use TempUserTestTrait
;
17 use MockAuthorityTrait
;
19 protected function getMaintenanceClass() {
20 return DeleteOrphanedRevisions
::class;
23 public function testExecuteForNoFoundRevisions() {
24 // Get revisions which are not orphaned, so that we know the script won't attempt to delete them.
25 $this->editPage( $this->getExistingTestPage(), 'testing1234' );
26 $this->maintenance
->execute();
27 $this->expectOutputRegex( "/Checking for orphaned revisions.*found 0.\n$/" );
30 public function testExecuteForOrphanedRevisions() {
31 $this->disableAutoCreateTempUser();
32 $testTitle = $this->getExistingTestPage();
33 // Get revisions which are not orphaned, so that we know the script won't attempt to delete them.
34 $this->editPage( $testTitle, 'testing1234' );
35 // Get revisions which are orphaned, one which has rev_page as 0 and the other which has a rev_page but
36 // the page ID does not exist.
37 $firstOrphanedRevId = $this->editPage( $testTitle, 'testing123456' )->getNewRevision()->getId();
38 $secondOrphanedRevId = $this->editPage(
39 $testTitle, 'testing1234567', '', NS_MAIN
, $this->mockAnonUltimateAuthority()
40 )->getNewRevision()->getId();
41 $this->getDb()->newUpdateQueryBuilder()
42 ->update( 'revision' )
43 ->set( [ 'rev_page' => 123456 ] )
44 ->where( [ 'rev_id' => $secondOrphanedRevId ] )
46 $this->getDb()->newUpdateQueryBuilder()
47 ->update( 'revision' )
48 ->set( [ 'rev_page' => 0 ] )
49 ->where( [ 'rev_id' => $firstOrphanedRevId ] )
51 // Check that a row exists for the second rev ID in ip_changes, as this is necessary for the assertion
52 // further down to check that the row was actually deleted.
53 $this->newSelectQueryBuilder()
54 ->select( 'COUNT(*)' )
55 ->from( 'ip_changes' )
56 ->where( [ 'ipc_rev_id' => $secondOrphanedRevId ] )
57 ->assertFieldValue( 1 );
58 // Run the maintenance script
59 $this->maintenance
->execute();
60 // Check that the orphaned revisions were actually deleted.
61 $this->expectOutputRegex( "/Checking for orphaned revisions.*found 2.\nDeleting.*done/" );
62 $this->newSelectQueryBuilder()
63 ->select( 'COUNT(*)' )
65 ->where( [ 'rev_id' => [ $firstOrphanedRevId, $secondOrphanedRevId ] ] )
66 ->assertFieldValue( 0 );
67 $this->newSelectQueryBuilder()
68 ->select( 'COUNT(*)' )
69 ->from( 'ip_changes' )
70 ->where( [ 'ipc_rev_id' => $secondOrphanedRevId ] )
71 ->assertFieldValue( 0 );