wiki.techinc.nl/tests/phpunit/maintenance/PopulateUserIsTempTest.php
Dreamy Jazz 6487791074 Add a maintenance script to populate user_is_temp column
Why:
* The user_is_temp column exists in the user table to allow finding
  temporary users when reading from the DB directly.
* This column was added in f283c0e990,
  but this was not written to until
  6e68107b3a which was several months
  later and after a release version was branched.
* As such, we need a maintenance script to populate the user_is_temp
  column for wikis that have enabled temporary account creation.

What:
* Add a maintenance script named populateUserIsTemp.php which
  populates the user_is_temp column by looking for rows in the
  user table that have user_is_temp as 0 and have user_name match
  at least one temporary account match pattern. These rows are then
  updated to have user_is_temp as 1.
* This script will be added to update.php in a future commit, so that
  betawikis can have this run manually.
* Create unit tests and an integration test for this maintenance
  script.

Bug: T355181
Change-Id: I6223496d7aee65e3ab207fe86e386b01bef8b388
2024-01-24 14:16:39 +00:00

67 lines
1.9 KiB
PHP

<?php
namespace MediaWiki\Tests\Maintenance;
use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
use PopulateUserIsTemp;
/**
* @covers PopulateUserIsTemp
* @group Database
*/
class PopulateUserIsTempTest extends MaintenanceBaseTestCase {
use TempUserTestTrait;
protected function getMaintenanceClass() {
return PopulateUserIsTemp::class;
}
public function testDoDBUpdates() {
$this->enableAutoCreateTempUser( [
'matchPattern' => [ '*$1', '~$1' ],
] );
$this->maintenance->setOption( 'batch-size', 2 );
$this->assertSame(
2,
(int)$this->getDb()->newSelectQueryBuilder()
->select( 'COUNT(*)' )
->from( 'user' )
->where( [ 'user_is_temp' => 1 ] )
->fetchField(),
'The database should have 2 users with user_is_temp set to 1 before the execute method is called.'
);
$this->assertTrue(
$this->maintenance->execute(),
'The execute method did not return true as expected.'
);
$this->assertSame(
5,
(int)$this->getDb()->newSelectQueryBuilder()
->select( 'COUNT(*)' )
->from( 'user' )
->where( [ 'user_is_temp' => 1 ] )
->fetchField(),
'The number of users with user_is_temp set to 1 is not as expected.'
);
}
public function addDBData() {
parent::addDBData();
// Create some temporary users and then set the user table to have user_is_temp as 0 for some of them.
$this->enableAutoCreateTempUser( [
'matchPattern' => [ '*$1', '~$1' ],
] );
$tempUserCreator = $this->getServiceContainer()->getTempUserCreator();
$tempUserCreator->create( '*Unregistered 1' );
$tempUserCreator->create( '*Unregistered 2' );
$tempUserCreator->create( '~Unregistered 3' );
$tempUserCreator->create( '~Unregistered 4567' );
$tempUserCreator->create( '~Unregistered 456789' );
$this->getDb()->newUpdateQueryBuilder()
->update( 'user' )
->set( [ 'user_is_temp' => 0 ] )
->where( [ 'user_name' => [ '*Unregistered 2', '~Unregistered 3', '~Unregistered 456789' ] ] )
->execute();
}
}