Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / ResetPageRandomTest.php
blob56a2d95c19632fe7ce2e81cbee4f0d3178dc5e6d
1 <?php
3 use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
4 use MediaWiki\Title\Title;
5 use Wikimedia\Timestamp\ConvertibleTimestamp;
7 /**
8 * @covers \ResetPageRandom
9 * @group Database
10 * @author Dreamy Jazz
12 class ResetPageRandomTest extends MaintenanceBaseTestCase {
13 public function getMaintenanceClass() {
14 return ResetPageRandom::class;
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/' );
24 public function testExecuteWhenNoFrom() {
25 $this->maintenance->setOption( 'to', '20240605040303' );
26 $this->maintenance->execute();
27 $this->expectOutputRegex( '/--from and --to have to be provided/' );
30 public function testExecuteWhenNoTo() {
31 $this->maintenance->setOption( 'from', '20240605040302' );
32 $this->maintenance->execute();
33 $this->expectOutputRegex( '/--from and --to have to be provided/' );
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/'
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();
54 /** @dataProvider provideDryRunValues */
55 public function testExecuteWhenPages( $dryRun ) {
56 // Create two pages, one which should be reset and the other which should not.
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 // Run the maintenance script and assert on the output
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/'
72 // Assert that page_random for the page that should not be modified was left as-is.
73 $this->assertSame(
74 $pageRandomThatShouldNotChange,
75 $this->getPageRandomValueForPage( $pageToBeUnmodified ),
76 'Pages created outside the given time range should not be modified.'
78 // Unless the run was a dry run, we cannot assert on the value of the
79 if ( $dryRun ) {
80 $this->assertSame(
81 $pageRandomThatShouldChangeWhenNotDryRun,
82 $this->getPageRandomValueForPage( $pageToBeModified ),
83 'No modifications of page_random should occur on a dry run.'
88 public static function provideDryRunValues() {
89 return [
90 'dry-run is set' => [ true ],
91 'dry-run is not set' => [ false ],