Debian Codegraph @

tests/phpunit/maintenance/DeleteUserEmailTest.php

raw symbols SCIP
1 <?php
2 use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
3
4 /**
5 * @covers \DeleteUserEmail
6 * @group Database
7 */
8 class DeleteUserEmailTest extends MaintenanceBaseTestCase {
9 public function getMaintenanceClass() {
10 return DeleteUserEmail::class;
11 }
12
13 private function commonTestEmailDeletion( $userArg, $userName, $oldEmail ) {
14 // Execute the maintenance script
15 $this->maintenance->loadWithArgv( [ $userArg ] );
16 $this->maintenance->execute();
17
18 // Check that the email address was changed and invalidated
19 $userFactory = $this->getServiceContainer()->getUserFactory();
20 $testUserAfterExecution = $userFactory->newFromName( $userName );
21 $this->assertNotEquals( $oldEmail, $testUserAfterExecution->getEmail() );
22 $this->assertSame( '', $testUserAfterExecution->getEmail() );
23 $this->assertNull( $testUserAfterExecution->getEmailAuthenticationTimestamp() );
24
25 // Check that the script returns the right output
26 $this->expectOutputRegex( '/Done!/' );
27 }
28
29 public function testEmailDeletionWhenProvidingName() {
30 // Target an existing user with an email attached
31 $testUserBeforeExecution = $this->getTestSysop()->getUser();
32 $oldEmail = $testUserBeforeExecution->getEmail();
33 $this->assertNotNull( $oldEmail );
34 // Test providing the maintenance script with a username.
35 $this->commonTestEmailDeletion(
36 $testUserBeforeExecution->getName(), $testUserBeforeExecution->getName(), $oldEmail
37 );
38 }
39
40 public function testEmailDeletionWhenProvidingId() {
41 // Target an existing user with an email attached
42 $testUserBeforeExecution = $this->getTestSysop()->getUser();
43 $oldEmail = $testUserBeforeExecution->getEmail();
44 $this->assertNotNull( $oldEmail );
45 // Test providing the maintenance script with a user ID.
46 $this->commonTestEmailDeletion(
47 "#" . $testUserBeforeExecution->getId(), $testUserBeforeExecution->getName(), $oldEmail
48 );
49 }
50
51 /** @dataProvider provideInvalidUsernameArgumentValues */
52 public function testEmailDeletionForInvalidUsername( $invalidUsernameArgument ) {
53 $this->expectCallToFatalError();
54 $this->expectOutputRegex( "/$invalidUsernameArgument.*could not be loaded/" );
55 // Execute the maintenance script
56 $this->maintenance->setArg( 'user', $invalidUsernameArgument );
57 $this->maintenance->execute();
58 }
59
60 public static function provideInvalidUsernameArgumentValues() {
61 return [
62 'Invalid username' => [ 'Template:test#test' ],
63 'Non-existent user' => [ 'Non-existent-test-user-1234' ],
64 ];
65 }
66 }