| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase; |
| 4 |
use MediaWiki\Title\Title; |
| 5 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ResetPageRandomTest extends MaintenanceBaseTestCase { |
| 13 |
public function getMaintenanceClass() { |
| 14 |
return ResetPageRandom::class; |
| 15 |
} |
| 16 |
|
| 17 |
public function testExecuteWhenFromAfterTo() { |
| 18 |
$this->maintenance->setOption( 'from', '20240605040302' ); |
| 19 |
$this->maintenance->setOption( 'to', '20240405060708' ); |
| 20 |
$this->maintenance->execute(); |
| 21 |
$this->expectOutputRegex( '/--from has to be smaller than --to/' ); |
| 22 |
} |
| 23 |
|
| 24 |
public function testExecuteWhenNoFrom() { |
| 25 |
$this->maintenance->setOption( 'to', '20240605040303' ); |
| 26 |
$this->maintenance->execute(); |
| 27 |
$this->expectOutputRegex( '/--from and --to have to be provided/' ); |
| 28 |
} |
| 29 |
|
| 30 |
public function testExecuteWhenNoTo() { |
| 31 |
$this->maintenance->setOption( 'from', '20240605040302' ); |
| 32 |
$this->maintenance->execute(); |
| 33 |
$this->expectOutputRegex( '/--from and --to have to be provided/' ); |
| 34 |
} |
| 35 |
|
| 36 |
public function testExecuteWhenNoPages() { |
| 37 |
$this->maintenance->setOption( 'to', '20240605040302' ); |
| 38 |
$this->maintenance->setOption( 'from', '20240405060708' ); |
| 39 |
$this->maintenance->execute(); |
| 40 |
$this->expectOutputRegex( |
| 41 |
'/Resetting page_random.*20240405060708.*20240605040302[\s\S]*' . |
| 42 |
'page_random reset complete.*changed 0 rows/' |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
private function getPageRandomValueForPage( Title $title ) { |
| 47 |
return $this->newSelectQueryBuilder() |
| 48 |
->select( 'page_random' ) |
| 49 |
->from( 'page' ) |
| 50 |
->where( [ 'page_id' => $title->getId() ] ) |
| 51 |
->fetchField(); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public function testExecuteWhenPages( $dryRun ) { |
| 56 |
|
| 57 |
ConvertibleTimestamp::setFakeTime( '20230505050505' ); |
| 58 |
$pageToBeUnmodified = $this->getExistingTestPage()->getTitle(); |
| 59 |
$pageRandomThatShouldNotChange = $this->getPageRandomValueForPage( $pageToBeUnmodified ); |
| 60 |
ConvertibleTimestamp::setFakeTime( '20240505050505' ); |
| 61 |
$pageToBeModified = $this->getExistingTestPage()->getTitle(); |
| 62 |
$pageRandomThatShouldChangeWhenNotDryRun = $this->getPageRandomValueForPage( $pageToBeModified ); |
| 63 |
|
| 64 |
$this->maintenance->setOption( 'to', '20240605040302' ); |
| 65 |
$this->maintenance->setOption( 'from', '20240405060708' ); |
| 66 |
$this->maintenance->setOption( 'dry', $dryRun ); |
| 67 |
$this->maintenance->execute(); |
| 68 |
$this->expectOutputRegex( |
| 69 |
'/Resetting page_random.*20240405060708.*20240605040302[\s\S]*page_random ' . |
| 70 |
'reset complete.*changed 1 rows/' |
| 71 |
); |
| 72 |
|
| 73 |
$this->assertSame( |
| 74 |
$pageRandomThatShouldNotChange, |
| 75 |
$this->getPageRandomValueForPage( $pageToBeUnmodified ), |
| 76 |
'Pages created outside the given time range should not be modified.' |
| 77 |
); |
| 78 |
|
| 79 |
if ( $dryRun ) { |
| 80 |
$this->assertSame( |
| 81 |
$pageRandomThatShouldChangeWhenNotDryRun, |
| 82 |
$this->getPageRandomValueForPage( $pageToBeModified ), |
| 83 |
'No modifications of page_random should occur on a dry run.' |
| 84 |
); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
public static function provideDryRunValues() { |
| 89 |
return [ |
| 90 |
'dry-run is set' => [ true ], |
| 91 |
'dry-run is not set' => [ false ], |
| 92 |
]; |
| 93 |
} |
| 94 |
} |