File: RemoveInvalidEmailsTest.php

package info (click to toggle)
mediawiki 1%3A1.43.3%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 417,464 kB
  • sloc: php: 1,062,949; javascript: 664,290; sql: 9,714; python: 5,458; xml: 3,489; sh: 1,131; makefile: 64
file content (87 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php

namespace MediaWiki\Tests\Maintenance;

use MediaWiki\User\UserIdentity;
use RemoveInvalidEmails;

/**
 * @covers \RemoveInvalidEmails
 * @group Database
 * @author Dreamy Jazz
 */
class RemoveInvalidEmailsTest extends MaintenanceBaseTestCase {
	public function getMaintenanceClass() {
		return RemoveInvalidEmails::class;
	}

	public function testExecuteWhenNoInvalidEmails() {
		$testUser1 = $this->getTestUser()->getUser();
		$testUser1EmailBeforeExecution = $testUser1->getEmail();
		$this->maintenance->execute();
		$testUser1->clearInstanceCache( 'name' );
		$this->assertSame( $testUser1EmailBeforeExecution, $testUser1->getEmail() );
		$this->expectOutputString( "Done.\n" );
	}

	/**
	 * @param string $email The custom email to be used
	 * @param string|null $authenticationTimestamp The authentication timestamp of the email, or null if not
	 *   authenticated
	 * @return UserIdentity
	 */
	private function getMutableTestUserWithCustomEmail( string $email, ?string $authenticationTimestamp ): UserIdentity {
		$testUser = $this->getMutableTestUser()->getUserIdentity();
		$this->getDb()->newUpdateQueryBuilder()
			->update( 'user' )
			->set( [
				'user_email' => $email,
				'user_email_authenticated' => $this->getDb()->timestampOrNull( $authenticationTimestamp )
			] )
			->where( [ 'user_id' => $testUser->getId() ] )
			->execute();
		return $testUser;
	}

	/**
	 * @param int $id The user's ID
	 * @param string $email The email that the user should have
	 * @param bool $authenticationTimestamp The expected authentication timestamp for the email, or null if the email
	 *   is expected to not be authenticated
	 * @return void
	 */
	private function checkUserHasEmail( int $id, string $email, ?string $authenticationTimestamp ) {
		$this->newSelectQueryBuilder()
			->select( [ 'user_email', 'user_email_authenticated' ] )
			->from( 'user' )
			->where( [ 'user_id' => $id ] )
			->assertRowValue( [ $email, $this->getDb()->timestampOrNull( $authenticationTimestamp ) ] );
	}

	/** @dataProvider provideCommitValues */
	public function testExecuteWhenSomeInvalidEmails( $commit, $expectedOutputRegex ) {
		// Get a test users, one with a valid email, one with an invalid email, and one with an
		// invalid email but is marked as authenticated.
		$testUser1 = $this->getMutableTestUserWithCustomEmail( 'test@test.com', null );
		$testUser2 = $this->getMutableTestUserWithCustomEmail( 'invalid', null );
		$testUser3 = $this->getMutableTestUserWithCustomEmail( 'invalid2', '20240506070809' );
		// Run the maintenance script, optionally with the --commit option
		if ( $commit ) {
			$this->maintenance->setOption( 'commit', 1 );
		}
		$this->maintenance->execute();
		// All users, except the second user when --commit is set, should be untouched by the script
		// so assert that this is the case
		$this->checkUserHasEmail( $testUser1->getId(), 'test@test.com', null );
		$this->checkUserHasEmail( $testUser2->getId(), $commit ? '' : 'invalid', null );
		$this->checkUserHasEmail( $testUser3->getId(), 'invalid2', '20240506070809' );
		$this->expectOutputRegex( $expectedOutputRegex );
	}

	public static function provideCommitValues() {
		return [
			'--commit provided' => [ true, '/Removing 1 emails from the database[\s\S]*Done/' ],
			'--commit not provided' => [ false, '/Would have removed 1 emails from the database.[\s\S]*Done/' ],
		];
	}
}