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
|
<?php
namespace MediaWiki\Tests\Maintenance;
use AttachLatest;
use MediaWiki\Title\Title;
use Wikimedia\Rdbms\IDBAccessObject;
/**
* @covers \AttachLatest
* @group Database
* @author Dreamy Jazz
*/
class AttachLatestTest extends MaintenanceBaseTestCase {
protected function getMaintenanceClass() {
return AttachLatest::class;
}
/**
* @param int $count
* @return Title[] The test pages as values, with the keys as the expected values of page_latest after the
* maintenance script run.
*/
private function getPagesWithPageLatestAsZero( int $count ): array {
$returnArray = [];
for ( $i = 0; $i < $count; $i++ ) {
$existingTestPage = $this->getExistingTestPage();
$returnArray[$existingTestPage->getRevisionRecord()->getId()] = $existingTestPage->getTitle();
}
// Set the page_latest as 0 for the test on all pages in $returnArray
$pageIds = array_map( static function ( $page ) {
return $page->getId();
}, $returnArray );
if ( count( $pageIds ) ) {
$this->getDb()->newUpdateQueryBuilder()
->update( 'page' )
->set( [ 'page_latest' => 0 ] )
->where( [ 'page_id' => $pageIds ] )
->execute();
}
return $returnArray;
}
/** @dataProvider provideExecute */
public function testExecute( $brokenCount ) {
$pagesThatNeedFixing = $this->getPagesWithPageLatestAsZero( $brokenCount );
// Run the maintenance script with 'fix' specified to actually fix the broken page rows.
$this->maintenance->setOption( 'fix', 1 );
$this->maintenance->execute();
// Verify that the page_latest field has been fixed for all the pages that needed fixed.
$expectedOutputRegex = '/';
foreach ( $pagesThatNeedFixing as $expectedLatestRevId => $title ) {
$expectedOutputRegex .= '.*' . $title->getPrefixedText() . '.*' . $expectedLatestRevId . "\n";
$this->assertSame(
$expectedLatestRevId,
$title->getLatestRevID( IDBAccessObject::READ_LATEST ),
'page_latest was not properly fixed by the maintenance script'
);
}
$expectedOutputRegex .= ".*Done! Processed $brokenCount pages./";
$this->expectOutputRegex( $expectedOutputRegex );
}
public static function provideExecute() {
return [
'No broken pages' => [ 0 ],
'Five broken pages' => [ 5 ],
];
}
}
|