Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / AttachLatestTest.php
blob6236a34449336b277cded85a0db517ed165cd385
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use AttachLatest;
6 use MediaWiki\Title\Title;
7 use Wikimedia\Rdbms\IDBAccessObject;
9 /**
10 * @covers \AttachLatest
11 * @group Database
12 * @author Dreamy Jazz
14 class AttachLatestTest extends MaintenanceBaseTestCase {
16 protected function getMaintenanceClass() {
17 return AttachLatest::class;
20 /**
21 * @param int $count
22 * @return Title[] The test pages as values, with the keys as the expected values of page_latest after the
23 * maintenance script run.
25 private function getPagesWithPageLatestAsZero( int $count ): array {
26 $returnArray = [];
27 for ( $i = 0; $i < $count; $i++ ) {
28 $existingTestPage = $this->getExistingTestPage();
29 $returnArray[$existingTestPage->getRevisionRecord()->getId()] = $existingTestPage->getTitle();
31 // Set the page_latest as 0 for the test on all pages in $returnArray
32 $pageIds = array_map( static function ( $page ) {
33 return $page->getId();
34 }, $returnArray );
35 if ( count( $pageIds ) ) {
36 $this->getDb()->newUpdateQueryBuilder()
37 ->update( 'page' )
38 ->set( [ 'page_latest' => 0 ] )
39 ->where( [ 'page_id' => $pageIds ] )
40 ->execute();
42 return $returnArray;
45 /** @dataProvider provideExecute */
46 public function testExecute( $brokenCount ) {
47 $pagesThatNeedFixing = $this->getPagesWithPageLatestAsZero( $brokenCount );
48 // Run the maintenance script with 'fix' specified to actually fix the broken page rows.
49 $this->maintenance->setOption( 'fix', 1 );
50 $this->maintenance->execute();
51 // Verify that the page_latest field has been fixed for all the pages that needed fixed.
52 $expectedOutputRegex = '/';
53 foreach ( $pagesThatNeedFixing as $expectedLatestRevId => $title ) {
54 $expectedOutputRegex .= '.*' . $title->getPrefixedText() . '.*' . $expectedLatestRevId . "\n";
55 $this->assertSame(
56 $expectedLatestRevId,
57 $title->getLatestRevID( IDBAccessObject::READ_LATEST ),
58 'page_latest was not properly fixed by the maintenance script'
61 $expectedOutputRegex .= ".*Done! Processed $brokenCount pages./";
62 $this->expectOutputRegex( $expectedOutputRegex );
65 public static function provideExecute() {
66 return [
67 'No broken pages' => [ 0 ],
68 'Five broken pages' => [ 5 ],