wiki.techinc.nl/tests/phpunit/maintenance/InvalidateBotPasswordsTest.php
Dreamy Jazz 974c14e216 Test invalidateBotPasswords.php
Why:
* Maintenance scripts in core are mostly untested and testing them
  will help to avoid regressions.

What:
* Create InvalidateBotPasswordsTest that fully tests the associated
  maintenance script by mocking the BotPasswordStore service to
  either expect or not expect calls.
** Doing this is preferrable as the BotPasswordStore service
   should be separately tested, instead of being indirectly
   tested via a maintenance script.

Bug: T371167
Change-Id: Id2391afc57457ba1a83d43f6c118db338ca3334c
2024-08-19 10:50:45 +00:00

64 lines
2.2 KiB
PHP

<?php
namespace MediaWiki\Tests\Maintenance;
use InvalidateBotPasswords;
use MediaWiki\User\BotPasswordStore;
/**
* @covers \InvalidateBotPasswords
* @group Database
* @author Dreamy Jazz
*/
class InvalidateBotPasswordsTest extends MaintenanceBaseTestCase {
public function getMaintenanceClass() {
return InvalidateBotPasswords::class;
}
/** @dataProvider provideExecuteForFatalError */
public function testExecuteForFatalError( $options, $expectedOutputRegex = null ) {
$this->expectCallToFatalError();
foreach ( $options as $name => $value ) {
$this->maintenance->setOption( $name, $value );
}
$this->maintenance->execute();
if ( $expectedOutputRegex ) {
$this->expectOutputRegex( $expectedOutputRegex );
}
}
public static function provideExecuteForFatalError() {
return [
'No options provided' => [ [], '/A "user" or "userid" must be set/' ],
'User ID option for ID which does not exist' => [ [ 'userid' => 0 ] ],
'User option for user which does not exist' => [ [ 'user' => 'Non-existent-test-user' ] ],
];
}
/** @dataProvider provideExecute */
public function testExecute( $mockBotPasswordsWereInvalidated, $expectedOutputRegex ) {
$name = $this->getTestUser()->getUserIdentity()->getName();
// Mock the BotPasswordStore to expect a call to ::invalidateUserPasswords
// This is done to avoid indirectly testing the BotPasswordStore as that should
// be tested separately.
$this->setService( 'BotPasswordStore', function () use ( $name, $mockBotPasswordsWereInvalidated ) {
$mockBotPasswordStore = $this->createMock( BotPasswordStore::class );
$mockBotPasswordStore->expects( $this->once() )
->method( 'invalidateUserPasswords' )
->with( $name )
->willReturn( $mockBotPasswordsWereInvalidated );
return $mockBotPasswordStore;
} );
// Run the maintenance script
$this->maintenance->setOption( 'user', $name );
$this->maintenance->execute();
$this->expectOutputRegex( $expectedOutputRegex );
}
public static function provideExecute() {
return [
'No bot passwords were invalidated' => [ false, '/No bot passwords invalidated for/' ],
'Bot passwords were invalidated' => [ true, '/Bot passwords invalidated/' ],
];
}
}