| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\User\UserIdentity; |
| 6 |
use RemoveInvalidEmails; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class RemoveInvalidEmailsTest extends MaintenanceBaseTestCase { |
| 14 |
public function getMaintenanceClass() { |
| 15 |
return RemoveInvalidEmails::class; |
| 16 |
} |
| 17 |
|
| 18 |
public function testExecuteWhenNoInvalidEmails() { |
| 19 |
$testUser1 = $this->getTestUser()->getUser(); |
| 20 |
$testUser1EmailBeforeExecution = $testUser1->getEmail(); |
| 21 |
$this->maintenance->execute(); |
| 22 |
$testUser1->clearInstanceCache( 'name' ); |
| 23 |
$this->assertSame( $testUser1EmailBeforeExecution, $testUser1->getEmail() ); |
| 24 |
$this->expectOutputString( "Done.\n" ); |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private function getMutableTestUserWithCustomEmail( string $email, ?string $authenticationTimestamp ): UserIdentity { |
| 34 |
$testUser = $this->getMutableTestUser()->getUserIdentity(); |
| 35 |
$this->getDb()->newUpdateQueryBuilder() |
| 36 |
->update( 'user' ) |
| 37 |
->set( [ |
| 38 |
'user_email' => $email, |
| 39 |
'user_email_authenticated' => $this->getDb()->timestampOrNull( $authenticationTimestamp ) |
| 40 |
] ) |
| 41 |
->where( [ 'user_id' => $testUser->getId() ] ) |
| 42 |
->execute(); |
| 43 |
return $testUser; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private function checkUserHasEmail( int $id, string $email, ?string $authenticationTimestamp ) { |
| 54 |
$this->newSelectQueryBuilder() |
| 55 |
->select( [ 'user_email', 'user_email_authenticated' ] ) |
| 56 |
->from( 'user' ) |
| 57 |
->where( [ 'user_id' => $id ] ) |
| 58 |
->assertRowValue( [ $email, $this->getDb()->timestampOrNull( $authenticationTimestamp ) ] ); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
public function testExecuteWhenSomeInvalidEmails( $commit, $expectedOutputRegex ) { |
| 63 |
|
| 64 |
|
| 65 |
$testUser1 = $this->getMutableTestUserWithCustomEmail( '[email protected]', null ); |
| 66 |
$testUser2 = $this->getMutableTestUserWithCustomEmail( 'invalid', null ); |
| 67 |
$testUser3 = $this->getMutableTestUserWithCustomEmail( 'invalid2', '20240506070809' ); |
| 68 |
|
| 69 |
if ( $commit ) { |
| 70 |
$this->maintenance->setOption( 'commit', 1 ); |
| 71 |
} |
| 72 |
$this->maintenance->execute(); |
| 73 |
|
| 74 |
|
| 75 |
$this->checkUserHasEmail( $testUser1->getId(), '[email protected]', null ); |
| 76 |
$this->checkUserHasEmail( $testUser2->getId(), $commit ? '' : 'invalid', null ); |
| 77 |
$this->checkUserHasEmail( $testUser3->getId(), 'invalid2', '20240506070809' ); |
| 78 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 79 |
} |
| 80 |
|
| 81 |
public static function provideCommitValues() { |
| 82 |
return [ |
| 83 |
'--commit provided' => [ true, '/Removing 1 emails from the database[\s\S]*Done/' ], |
| 84 |
'--commit not provided' => [ false, '/Would have removed 1 emails from the database.[\s\S]*Done/' ], |
| 85 |
]; |
| 86 |
} |
| 87 |
} |