ChangesListSpecialPage: Simplify tests using setFakeTime()

Instead of using these terrible regexp assertions to match any SQL
query regardless of current time, set a fake time instead and use
simple string assertions like in the other tests.

Change-Id: If88e0f1487f79e2126d75a5123872d1f75c48403
This commit is contained in:
Bartosz Dziewoński 2024-08-01 19:07:32 +02:00
parent d9abc826ac
commit 52c572924b
2 changed files with 32 additions and 20 deletions

View file

@ -46,6 +46,7 @@ use Wikimedia\Rdbms\IExpression;
use Wikimedia\Rdbms\IReadableDatabase;
use Wikimedia\Rdbms\IResultWrapper;
use Wikimedia\Rdbms\RawSQLValue;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* Special page which uses a ChangesList to show query results.
@ -1521,7 +1522,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
}
// Calculate cutoff
$cutoff_unixtime = time() - $opts['days'] * 3600 * 24;
$cutoff_unixtime = ConvertibleTimestamp::time() - $opts['days'] * 3600 * 24;
$cutoff = $dbr->timestamp( $cutoff_unixtime );
$fromValid = preg_match( '/^[0-9]{14}$/', $opts['from'] );
@ -1823,7 +1824,7 @@ abstract class ChangesListSpecialPage extends SpecialPage {
'experienced' => $config->get( MainConfigNames::ExperiencedUserMemberSince ),
][$level];
if ( $now === 0 ) {
$now = time();
$now = ConvertibleTimestamp::time();
}
$secondsPerDay = 86400;
$timeCutoff = $now - $configSince * $secondsPerDay;

View file

@ -14,6 +14,7 @@ use MediaWiki\User\User;
use Wikimedia\Rdbms\Database;
use Wikimedia\Rdbms\IExpression;
use Wikimedia\TestingAccessWrapper;
use Wikimedia\Timestamp\ConvertibleTimestamp;
/**
* Test class for ChangesListSpecialPage class
@ -681,36 +682,46 @@ class ChangesListSpecialPageTest extends AbstractChangesListSpecialPageTestCase
public function testFilterUserExpLevelUnregisteredOrExperienced() {
$this->disableAutoCreateTempUser();
[ $cond ] = $this->buildQuery( [ 'userExpLevel' => 'unregistered;experienced' ] );
$this->assertMatchesRegularExpression(
'/\(actor_user IS NULL OR '
. '\(actor_user IS NOT NULL AND \('
. 'user_editcount >= 500 AND \(user_registration IS NULL OR '
. 'user_registration <= \'[^\']+\'\)'
. '\)\)\)/',
$cond->toSql( $this->getDb() ),
ConvertibleTimestamp::setFakeTime( '20201231000000' );
$this->assertConditions(
[
# expected
"(actor_user IS NULL OR "
. "(actor_user IS NOT NULL AND ("
. "user_editcount >= 500 AND (user_registration IS NULL OR "
. "user_registration <= '{$this->getDb()->timestamp( '20201201000000' )}')"
. ")))"
],
[
'userExpLevel' => 'unregistered;experienced'
],
"rc conditions: userExpLevel=unregistered;experienced"
);
}
public function testFilterUserExpLevelUnregisteredOrExperiencedWhenTemporaryAccountsEnabled() {
$this->enableAutoCreateTempUser();
[ $cond ] = $this->buildQuery( [ 'userExpLevel' => 'unregistered;experienced' ] );
ConvertibleTimestamp::setFakeTime( '20201231000000' );
$notLikeTempUserMatchExpression = $this->getServiceContainer()->getTempUserConfig()
->getMatchCondition( $this->getDb(), 'actor_name', IExpression::NOT_LIKE )
->toSql( $this->getDb() );
$notLikeTempUserMatchExpression = preg_quote( $notLikeTempUserMatchExpression );
$likeTempUserMatchExpression = $this->getServiceContainer()->getTempUserConfig()
->getMatchCondition( $this->getDb(), 'actor_name', IExpression::LIKE )
->toSql( $this->getDb() );
$likeTempUserMatchExpression = preg_quote( $likeTempUserMatchExpression );
$this->assertMatchesRegularExpression(
"/\(\(actor_user IS NULL OR $likeTempUserMatchExpression\) OR "
. "\(\(actor_user IS NOT NULL AND $notLikeTempUserMatchExpression\) AND \("
. 'user_editcount >= 500 AND \(user_registration IS NULL OR '
. 'user_registration <= \'[^\']+\'\)'
. '\)\)\)/',
$cond->toSql( $this->getDb() ),
$this->assertConditions(
[
# expected
"((actor_user IS NULL OR $likeTempUserMatchExpression) OR "
. "((actor_user IS NOT NULL AND $notLikeTempUserMatchExpression) AND ("
. "user_editcount >= 500 AND (user_registration IS NULL OR "
. "user_registration <= '{$this->getDb()->timestamp( '20201201000000' )}')"
. ")))"
],
[
'userExpLevel' => 'unregistered;experienced'
],
"rc conditions: userExpLevel=unregistered;experienced"
);
}