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
|
<?php
use MediaWiki\Tests\Maintenance\MaintenanceBaseTestCase;
/**
* @covers \DeleteUserEmail
* @group Database
*/
class DeleteUserEmailTest extends MaintenanceBaseTestCase {
public function getMaintenanceClass() {
return DeleteUserEmail::class;
}
private function commonTestEmailDeletion( $userArg, $userName, $oldEmail ) {
// Execute the maintenance script
$this->maintenance->loadWithArgv( [ $userArg ] );
$this->maintenance->execute();
// Check that the email address was changed and invalidated
$userFactory = $this->getServiceContainer()->getUserFactory();
$testUserAfterExecution = $userFactory->newFromName( $userName );
$this->assertNotEquals( $oldEmail, $testUserAfterExecution->getEmail() );
$this->assertSame( '', $testUserAfterExecution->getEmail() );
$this->assertNull( $testUserAfterExecution->getEmailAuthenticationTimestamp() );
// Check that the script returns the right output
$this->expectOutputRegex( '/Done!/' );
}
public function testEmailDeletionWhenProvidingName() {
// Target an existing user with an email attached
$testUserBeforeExecution = $this->getTestSysop()->getUser();
$oldEmail = $testUserBeforeExecution->getEmail();
$this->assertNotNull( $oldEmail );
// Test providing the maintenance script with a username.
$this->commonTestEmailDeletion(
$testUserBeforeExecution->getName(), $testUserBeforeExecution->getName(), $oldEmail
);
}
public function testEmailDeletionWhenProvidingId() {
// Target an existing user with an email attached
$testUserBeforeExecution = $this->getTestSysop()->getUser();
$oldEmail = $testUserBeforeExecution->getEmail();
$this->assertNotNull( $oldEmail );
// Test providing the maintenance script with a user ID.
$this->commonTestEmailDeletion(
"#" . $testUserBeforeExecution->getId(), $testUserBeforeExecution->getName(), $oldEmail
);
}
/** @dataProvider provideInvalidUsernameArgumentValues */
public function testEmailDeletionForInvalidUsername( $invalidUsernameArgument ) {
$this->expectCallToFatalError();
$this->expectOutputRegex( "/$invalidUsernameArgument.*could not be loaded/" );
// Execute the maintenance script
$this->maintenance->setArg( 'user', $invalidUsernameArgument );
$this->maintenance->execute();
}
public static function provideInvalidUsernameArgumentValues() {
return [
'Invalid username' => [ 'Template:test#test' ],
'Non-existent user' => [ 'Non-existent-test-user-1234' ],
];
}
}
|