Test CheckSignatures.php
Why: * The maintenance scripts in core are mostly untested and testing the less complex scripts will improve the test coverage in core. What: * Add CheckSignaturesTest.php Bug: T371167 Change-Id: Ie9a8035716148cd91fd3c9886c8b23c3894b866a
This commit is contained in:
parent
aeded8f65d
commit
7a3c4b0a87
1 changed files with 77 additions and 0 deletions
77
tests/phpunit/maintenance/CheckSignaturesTest.php
Normal file
77
tests/phpunit/maintenance/CheckSignaturesTest.php
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
|
||||
namespace MediaWiki\Tests\Maintenance;
|
||||
|
||||
use CheckSignatures;
|
||||
use MediaWiki\User\UserIdentity;
|
||||
|
||||
/**
|
||||
* @covers \CheckSignatures
|
||||
* @group Database
|
||||
* @author Dreamy Jazz
|
||||
*/
|
||||
class CheckSignaturesTest extends MaintenanceBaseTestCase {
|
||||
|
||||
protected function getMaintenanceClass() {
|
||||
return CheckSignatures::class;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $count
|
||||
* @return UserIdentity[]
|
||||
*/
|
||||
private function generateMutableTestUsers( int $count ): array {
|
||||
$returnArray = [];
|
||||
for ( $i = 0; $i < $count; $i++ ) {
|
||||
$testUser = $this->getMutableTestUser()->getUserIdentity();
|
||||
$returnArray[] = $testUser;
|
||||
}
|
||||
return $returnArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $count
|
||||
* @return UserIdentity[]
|
||||
*/
|
||||
private function generateUsersWithInvalidSignatures( int $count ): array {
|
||||
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
|
||||
$returnArray = $this->generateMutableTestUsers( $count );
|
||||
foreach ( $returnArray as $user ) {
|
||||
// Set the signature to invalid HTML
|
||||
$userOptionsManager->setOption( $user, 'nickname', '<b>{{test' );
|
||||
$userOptionsManager->setOption( $user, 'fancysig', true );
|
||||
$userOptionsManager->saveOptions( $user );
|
||||
}
|
||||
return $returnArray;
|
||||
}
|
||||
|
||||
private function generateUsersWithValidSignatures( int $count ) {
|
||||
$userOptionsManager = $this->getServiceContainer()->getUserOptionsManager();
|
||||
foreach ( $this->generateMutableTestUsers( $count ) as $user ) {
|
||||
// Set the signature to invalid HTML
|
||||
$userOptionsManager->setOption( $user, 'nickname', '[[User:' . $user->getName() . "]]" );
|
||||
$userOptionsManager->setOption( $user, 'fancysig', true );
|
||||
$userOptionsManager->saveOptions( $user );
|
||||
}
|
||||
}
|
||||
|
||||
/** @dataProvider provideExecute */
|
||||
public function testExecute( $validCount, $invalidCount ) {
|
||||
$usersWithInvalidSignatures = $this->generateUsersWithInvalidSignatures( $invalidCount );
|
||||
$this->generateUsersWithValidSignatures( $validCount );
|
||||
$this->maintenance->execute();
|
||||
$expectedOutputRegex = '/';
|
||||
foreach ( $usersWithInvalidSignatures as $user ) {
|
||||
$expectedOutputRegex .= $user->getName() . "\n";
|
||||
}
|
||||
$expectedOutputRegex .= ".*$invalidCount invalid signatures/";
|
||||
$this->expectOutputRegex( $expectedOutputRegex );
|
||||
}
|
||||
|
||||
public static function provideExecute() {
|
||||
return [
|
||||
'All signatures valid' => [ 4, 0 ],
|
||||
'Some signatures valid' => [ 2, 3 ],
|
||||
];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue