| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\Request\FauxRequest; |
| 6 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 7 |
use PopulateUserIsTemp; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class PopulateUserIsTempTest extends MaintenanceBaseTestCase { |
| 14 |
use TempUserTestTrait; |
| 15 |
|
| 16 |
protected function getMaintenanceClass() { |
| 17 |
return PopulateUserIsTemp::class; |
| 18 |
} |
| 19 |
|
| 20 |
public function testDoDBUpdates() { |
| 21 |
$this->enableAutoCreateTempUser( [ |
| 22 |
'matchPattern' => [ '*$1', '~$1' ], |
| 23 |
] ); |
| 24 |
$this->maintenance->setOption( 'batch-size', 2 ); |
| 25 |
$this->assertSame( |
| 26 |
2, |
| 27 |
(int)$this->getDb()->newSelectQueryBuilder() |
| 28 |
->select( 'COUNT(*)' ) |
| 29 |
->from( 'user' ) |
| 30 |
->where( [ 'user_is_temp' => 1 ] ) |
| 31 |
->fetchField(), |
| 32 |
'The database should have 2 users with user_is_temp set to 1 before the execute method is called.' |
| 33 |
); |
| 34 |
$this->assertTrue( |
| 35 |
$this->maintenance->execute(), |
| 36 |
'The execute method did not return true as expected.' |
| 37 |
); |
| 38 |
$this->assertSame( |
| 39 |
5, |
| 40 |
(int)$this->getDb()->newSelectQueryBuilder() |
| 41 |
->select( 'COUNT(*)' ) |
| 42 |
->from( 'user' ) |
| 43 |
->where( [ 'user_is_temp' => 1 ] ) |
| 44 |
->fetchField(), |
| 45 |
'The number of users with user_is_temp set to 1 is not as expected.' |
| 46 |
); |
| 47 |
} |
| 48 |
|
| 49 |
public function addDBData() { |
| 50 |
parent::addDBData(); |
| 51 |
|
| 52 |
|
| 53 |
$this->enableAutoCreateTempUser( [ |
| 54 |
'matchPattern' => [ '*$1', '~$1' ], |
| 55 |
] ); |
| 56 |
$tempUserCreator = $this->getServiceContainer()->getTempUserCreator(); |
| 57 |
$tempUserCreator->create( '*Unregistered 1', new FauxRequest() ); |
| 58 |
$tempUserCreator->create( '*Unregistered 2', new FauxRequest() ); |
| 59 |
$tempUserCreator->create( '~Unregistered 3', new FauxRequest() ); |
| 60 |
$tempUserCreator->create( '~Unregistered 4567', new FauxRequest() ); |
| 61 |
$tempUserCreator->create( '~Unregistered 456789', new FauxRequest() ); |
| 62 |
$this->getDb()->newUpdateQueryBuilder() |
| 63 |
->update( 'user' ) |
| 64 |
->set( [ 'user_is_temp' => 0 ] ) |
| 65 |
->where( [ 'user_name' => [ '*Unregistered 2', '~Unregistered 3', '~Unregistered 456789' ] ] ) |
| 66 |
->execute(); |
| 67 |
} |
| 68 |
} |