| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\MainConfigNames; |
| 4 |
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase; |
| 5 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ResetUserEmailTest extends MaintenanceBaseTestCase { |
| 13 |
public function getMaintenanceClass() { |
| 14 |
return ResetUserEmail::class; |
| 15 |
} |
| 16 |
|
| 17 |
private function commonTestEmailReset( $userArg, $options, $userName, $oldEmail ) { |
| 18 |
ConvertibleTimestamp::setFakeTime( '20240506070809' ); |
| 19 |
|
| 20 |
$this->maintenance->loadWithArgv( [ $userArg, '[email protected]' ] ); |
| 21 |
foreach ( $options as $name => $value ) { |
| 22 |
$this->maintenance->setOption( $name, $value ); |
| 23 |
} |
| 24 |
$this->maintenance->execute(); |
| 25 |
|
| 26 |
|
| 27 |
$userFactory = $this->getServiceContainer()->getUserFactory(); |
| 28 |
$testUserAfterExecution = $userFactory->newFromName( $userName ); |
| 29 |
$this->assertNotEquals( $oldEmail, $testUserAfterExecution->getEmail() ); |
| 30 |
$this->assertSame( '[email protected]', $testUserAfterExecution->getEmail() ); |
| 31 |
$this->assertSame( |
| 32 |
'20240506070809', |
| 33 |
$testUserAfterExecution->getEmailAuthenticationTimestamp() |
| 34 |
); |
| 35 |
|
| 36 |
|
| 37 |
$this->expectOutputRegex( '/Done!/' ); |
| 38 |
} |
| 39 |
|
| 40 |
public function testEmailResetWithNoPasswordResetWhenProvidingName() { |
| 41 |
|
| 42 |
$testUserBeforeExecution = $this->getTestSysop()->getUser(); |
| 43 |
$oldEmail = $testUserBeforeExecution->getEmail(); |
| 44 |
$this->assertNotNull( $oldEmail ); |
| 45 |
|
| 46 |
$this->commonTestEmailReset( |
| 47 |
$testUserBeforeExecution->getName(), [ 'no-reset-password' => 1 ], $testUserBeforeExecution->getName(), |
| 48 |
$oldEmail |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
public function testEmailResetWithNoPasswordResetWhenProvidingId() { |
| 53 |
|
| 54 |
$testUserBeforeExecution = $this->getTestSysop()->getUser(); |
| 55 |
$oldEmail = $testUserBeforeExecution->getEmail(); |
| 56 |
$this->assertNotNull( $oldEmail ); |
| 57 |
$passwordHashBeforeExecution = $this->newSelectQueryBuilder() |
| 58 |
->select( 'user_password' ) |
| 59 |
->from( 'user' ) |
| 60 |
->where( [ 'user_id' => $testUserBeforeExecution->getId() ] ) |
| 61 |
->fetchField(); |
| 62 |
|
| 63 |
$this->commonTestEmailReset( |
| 64 |
"#" . $testUserBeforeExecution->getId(), [ 'no-reset-password' => 1 ], |
| 65 |
$testUserBeforeExecution->getName(), $oldEmail |
| 66 |
); |
| 67 |
|
| 68 |
$passwordAfterExecution = $this->newSelectQueryBuilder() |
| 69 |
->select( 'user_password' ) |
| 70 |
->from( 'user' ) |
| 71 |
->where( [ 'user_id' => $testUserBeforeExecution->getId() ] ) |
| 72 |
->fetchField(); |
| 73 |
$this->assertSame( $passwordHashBeforeExecution, $passwordAfterExecution ); |
| 74 |
} |
| 75 |
|
| 76 |
public function testEmailReset() { |
| 77 |
|
| 78 |
$testUserBeforeExecution = $this->getTestSysop()->getUser(); |
| 79 |
$oldEmail = $testUserBeforeExecution->getEmail(); |
| 80 |
$this->assertNotNull( $oldEmail ); |
| 81 |
$passwordHashBeforeExecution = $this->newSelectQueryBuilder() |
| 82 |
->select( 'user_password' ) |
| 83 |
->from( 'user' ) |
| 84 |
->where( [ 'user_id' => $testUserBeforeExecution->getId() ] ) |
| 85 |
->fetchField(); |
| 86 |
|
| 87 |
$this->commonTestEmailReset( |
| 88 |
"#" . $testUserBeforeExecution->getId(), [], |
| 89 |
$testUserBeforeExecution->getName(), $oldEmail |
| 90 |
); |
| 91 |
|
| 92 |
$passwordAfterExecution = $this->newSelectQueryBuilder() |
| 93 |
->select( 'user_password' ) |
| 94 |
->from( 'user' ) |
| 95 |
->where( [ 'user_id' => $testUserBeforeExecution->getId() ] ) |
| 96 |
->fetchField(); |
| 97 |
$this->assertNotSame( $passwordHashBeforeExecution, $passwordAfterExecution ); |
| 98 |
} |
| 99 |
|
| 100 |
public function testEmailResetWithNoPasswordResetAndEmailPasswordOnFailure() { |
| 101 |
$this->overrideConfigValue( MainConfigNames::EnableEmail, true ); |
| 102 |
|
| 103 |
$this->setTemporaryHook( 'SpecialPasswordResetOnSubmit', static function ( $users, $data, &$error ) { |
| 104 |
$error = 'test'; |
| 105 |
return false; |
| 106 |
} ); |
| 107 |
|
| 108 |
$testUserBeforeExecution = $this->getTestSysop()->getUser(); |
| 109 |
$oldEmail = $testUserBeforeExecution->getEmail(); |
| 110 |
$this->assertNotNull( $oldEmail ); |
| 111 |
|
| 112 |
$this->commonTestEmailReset( |
| 113 |
$testUserBeforeExecution->getName(), [ 'no-reset-password' => 1, 'email-password' => 1 ], |
| 114 |
$testUserBeforeExecution->getName(), |
| 115 |
$oldEmail |
| 116 |
); |
| 117 |
$this->expectOutputRegex( "/Email couldn't be sent because[\s\S]*Done/" ); |
| 118 |
} |
| 119 |
|
| 120 |
public function testEmailResetOnInvalidNewEmail() { |
| 121 |
$this->expectCallToFatalError(); |
| 122 |
$this->expectOutputRegex( "/testemail.*is not valid/" ); |
| 123 |
|
| 124 |
$this->maintenance->setArg( 0, $this->getTestUser()->getUserIdentity()->getName() ); |
| 125 |
$this->maintenance->setArg( 1, 'testemail' ); |
| 126 |
$this->maintenance->execute(); |
| 127 |
} |
| 128 |
|
| 129 |
public function testEmailResetOnInvalidUsername() { |
| 130 |
$this->expectCallToFatalError(); |
| 131 |
$this->expectOutputRegex( "/Non-existent-test-user.*does not exist/" ); |
| 132 |
|
| 133 |
$this->maintenance->setArg( 0, 'Non-existent-test-user' ); |
| 134 |
$this->maintenance->setArg( 1, '[email protected]' ); |
| 135 |
$this->maintenance->execute(); |
| 136 |
} |
| 137 |
} |