Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / RemoveInvalidEmailsTest.php
bloba9d73661b3914c40d58306fa5961ee866bb3a7de
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use MediaWiki\User\UserIdentity;
6 use RemoveInvalidEmails;
8 /**
9 * @covers \RemoveInvalidEmails
10 * @group Database
11 * @author Dreamy Jazz
13 class RemoveInvalidEmailsTest extends MaintenanceBaseTestCase {
14 public function getMaintenanceClass() {
15 return RemoveInvalidEmails::class;
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" );
27 /**
28 * @param string $email The custom email to be used
29 * @param string|null $authenticationTimestamp The authentication timestamp of the email, or null if not
30 * authenticated
31 * @return UserIdentity
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;
46 /**
47 * @param int $id The user's ID
48 * @param string $email The email that the user should have
49 * @param bool $authenticationTimestamp The expected authentication timestamp for the email, or null if the email
50 * is expected to not be authenticated
51 * @return void
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 ) ] );
61 /** @dataProvider provideCommitValues */
62 public function testExecuteWhenSomeInvalidEmails( $commit, $expectedOutputRegex ) {
63 // Get a test users, one with a valid email, one with an invalid email, and one with an
64 // invalid email but is marked as authenticated.
65 $testUser1 = $this->getMutableTestUserWithCustomEmail( 'test@test.com', null );
66 $testUser2 = $this->getMutableTestUserWithCustomEmail( 'invalid', null );
67 $testUser3 = $this->getMutableTestUserWithCustomEmail( 'invalid2', '20240506070809' );
68 // Run the maintenance script, optionally with the --commit option
69 if ( $commit ) {
70 $this->maintenance->setOption( 'commit', 1 );
72 $this->maintenance->execute();
73 // All users, except the second user when --commit is set, should be untouched by the script
74 // so assert that this is the case
75 $this->checkUserHasEmail( $testUser1->getId(), 'test@test.com', null );
76 $this->checkUserHasEmail( $testUser2->getId(), $commit ? '' : 'invalid', null );
77 $this->checkUserHasEmail( $testUser3->getId(), 'invalid2', '20240506070809' );
78 $this->expectOutputRegex( $expectedOutputRegex );
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/' ],