wiki.techinc.nl/tests/phpunit/maintenance/DeleteUserEmailTest.php
Dreamy Jazz 2dc2345565 Expand DeleteUserEmailTest test invalid username argument
Why:
* The deleteUserEmail.php maintenance script can be further tested
  by checking what happens when an invalid or non-existing
  username
* Doing this will improve the low test coverage on maintenance
  scripts in core.

What:
* Expand DeleteUserEmailTest to verify that a fatal error is
  thrown when an invalid username or non-existent user is provided
  to the maintenance script.

Bug: T371167
Change-Id: I72b6dee34266a761bbc194fbcd4e7c5d43075214
2024-08-04 16:11:22 +00:00

66 lines
2.4 KiB
PHP

<?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' ],
];
}
}