| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use MediaWiki\User\Options\StaticUserOptionsLookup; |
| 6 |
use UserOptionsMaintenance; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
class UserOptionsMaintenanceTest extends MaintenanceBaseTestCase { |
| 14 |
|
| 15 |
protected function getMaintenanceClass() { |
| 16 |
return UserOptionsMaintenance::class; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
public function testExecuteForFatalError( $options, $optionNameArg, $expectedOutputRegex ) { |
| 21 |
foreach ( $options as $name => $value ) { |
| 22 |
$this->maintenance->setOption( $name, $value ); |
| 23 |
} |
| 24 |
$this->maintenance->setArg( 0, $optionNameArg ); |
| 25 |
$this->maintenance->getParameters()->setName( 'userOptions.php' ); |
| 26 |
$this->expectCallToFatalError(); |
| 27 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 28 |
$this->maintenance->execute(); |
| 29 |
} |
| 30 |
|
| 31 |
public static function provideExecuteForFatalError() { |
| 32 |
return [ |
| 33 |
'--delete-defaults with no option argument' => [ |
| 34 |
[ 'delete-defaults' => 1 ], null, '/Option name is required/', |
| 35 |
], |
| 36 |
'--usage with invalid option argument' => [ [ 'usage' => 1 ], 'invalidoption', '/Invalid user option/' ], |
| 37 |
'No options provided' => [ |
| 38 |
[], 'option', |
| 39 |
|
| 40 |
'/Pass through all users and change or delete one of their options/', |
| 41 |
], |
| 42 |
]; |
| 43 |
} |
| 44 |
|
| 45 |
public function testListOptions() { |
| 46 |
$this->setService( 'UserOptionsLookup', new StaticUserOptionsLookup( |
| 47 |
[], [ 'requireemail' => 1, 'disablemail' => 0 ] |
| 48 |
) ); |
| 49 |
$this->maintenance->setOption( 'list', 1 ); |
| 50 |
$this->maintenance->execute(); |
| 51 |
$this->expectOutputString( "disablemail : 0\nrequireemail: 1\n" ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
public function testShowUsageStats( $optionArgName, $expectedOutputString ) { |
| 56 |
$testUser1 = $this->getMutableTestUser()->getUserIdentity(); |
| 57 |
$testUser2 = $this->getMutableTestUser()->getUserIdentity(); |
| 58 |
$this->setService( 'UserOptionsLookup', new StaticUserOptionsLookup( |
| 59 |
[ |
| 60 |
$testUser1->getName() => [ 'requireemail' => 0 ], |
| 61 |
$testUser2->getName() => [ 'disablemail' => 1 ], |
| 62 |
], |
| 63 |
[ 'requireemail' => 1, 'disablemail' => 0 ] |
| 64 |
) ); |
| 65 |
$this->maintenance->setOption( 'usage', 1 ); |
| 66 |
$this->maintenance->setArg( 0, $optionArgName ); |
| 67 |
$this->maintenance->execute(); |
| 68 |
$this->expectOutputString( $expectedOutputString ); |
| 69 |
} |
| 70 |
|
| 71 |
public static function provideShowUsageStats() { |
| 72 |
return [ |
| 73 |
'All options' => [ |
| 74 |
null, |
| 75 |
"Usage for <requireemail> (default: '1'):\n 1 user(s): '0'\n\n" . |
| 76 |
"Usage for <disablemail> (default: '0'):\n 1 user(s): '1'\n\n", |
| 77 |
], |
| 78 |
'Only the "requireemail" option' => [ |
| 79 |
'requireemail', |
| 80 |
"Usage for <requireemail> (default: '1'):\n 1 user(s): '0'\n\n", |
| 81 |
], |
| 82 |
]; |
| 83 |
} |
| 84 |
} |