Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / InvalidateUserSessionsTest.php
blob794ffcb366d8c2c8d25579e9ab7b49736a9ff475
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
5 use InvalidateUserSessions;
6 use MediaWiki\Session\SessionManager;
7 use MediaWiki\Tests\Session\TestUtils;
8 use RuntimeException;
10 /**
11 * @covers \InvalidateUserSessions
12 * @group Database
13 * @author Dreamy Jazz
15 class InvalidateUserSessionsTest extends MaintenanceBaseTestCase {
16 public function getMaintenanceClass() {
17 return InvalidateUserSessions::class;
20 /** @dataProvider provideExecuteForFatalError */
21 public function testExecuteForFatalError( $options, $expectedOutputRegex ) {
22 $this->expectCallToFatalError();
23 foreach ( $options as $name => $value ) {
24 $this->maintenance->setOption( $name, $value );
26 $this->maintenance->execute();
27 $this->expectOutputRegex( $expectedOutputRegex );
30 public static function provideExecuteForFatalError() {
31 return [
32 'No options provided' => [ [], '/Either --user or --file is required/' ],
33 'Both user and file provided' => [
34 [ 'user' => 'test', 'file' => 'test' ], '/Cannot use both --user and --file/',
36 'Filename is not valid' => [
37 [ 'file' => '/test/invalidpath/testing' ],
38 '/Could not open ' . preg_quote( '/test/invalidpath/testing', '/' ) . '/',
43 /** @dataProvider provideExecute */
44 public function testExecute( $options, $expectedUsernames, $expectedOutputString ) {
45 // Mock the SessionManager::singleton() instance to expect calls to ::invalidateSessionsForUser
46 $mockSessionManager = $this->createMock( SessionManager::class );
47 $mockSessionManager->expects( $this->exactly( count( $expectedUsernames ) ) )
48 ->method( 'invalidateSessionsForUser' )
49 ->willReturnCallback( function ( $actualUser ) use ( $expectedUsernames ) {
50 $this->assertContains( $actualUser->getName(), $expectedUsernames );
51 } );
52 $resetSingleton = TestUtils::setSessionManagerSingleton( $mockSessionManager );
53 // Run the maintenance script
54 foreach ( $options as $name => $value ) {
55 $this->maintenance->setOption( $name, $value );
57 $this->maintenance->execute();
58 $this->expectOutputString( $expectedOutputString );
61 public static function provideExecute() {
62 return [
63 'User argument for non-existing user' => [
64 [ 'user' => 'Non-existing test user' ],
65 [ 'Non-existing test user' ],
66 "Could not find user Non-existing test user, tried to invalidate anyway\n",
71 public function testExecuteForFileOfUsernames() {
72 $testUser1 = $this->getTestUser()->getUserIdentity();
73 $testUser2 = $this->getTestSysop()->getUserIdentity();
74 $testFilename = $this->getNewTempFile();
75 file_put_contents( $testFilename, "Non-existing test user\n$testUser1\n$testUser2" );
76 $this->testExecute(
77 [ 'file' => $testFilename, 'batch-size' => 1 ],
78 [ 'Non-existing test user', $testUser1->getName(), $testUser2->getName() ],
79 "Could not find user Non-existing test user, tried to invalidate anyway\n" .
80 "Invalidated sessions for user $testUser1\nInvalidated sessions for user $testUser2\n",
84 public function testExecuteForThrownException() {
85 // Mock the SessionManager::singleton() instance to throw an error when ::invalidateSessionsForUser is called.
86 $mockSessionManager = $this->createMock( SessionManager::class );
87 $mockSessionManager->method( 'invalidateSessionsForUser' )
88 ->willThrowException( new RuntimeException( "Testing\nTest" ) );
89 $resetSingleton = TestUtils::setSessionManagerSingleton( $mockSessionManager );
90 // Run the maintenance script
91 $this->maintenance->setOption( 'user', 'Testing' );
92 $this->maintenance->execute();
93 $this->expectOutputString( "Failed to invalidate sessions for user Testing | Testing Test\n" );