| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use Protect; |
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class ProtectTest extends MaintenanceBaseTestCase { |
| 13 |
|
| 14 |
protected function getMaintenanceClass() { |
| 15 |
return Protect::class; |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
public function testExecute( $options, $expectedProtectionLevel ) { |
| 20 |
$testPage = $this->getExistingTestPage(); |
| 21 |
|
| 22 |
foreach ( $options as $name => $option ) { |
| 23 |
$this->maintenance->setOption( $name, $option ); |
| 24 |
} |
| 25 |
|
| 26 |
$this->maintenance->setArg( 'title', $testPage ); |
| 27 |
$this->maintenance->execute(); |
| 28 |
|
| 29 |
$this->expectOutputString( "Updating protection status..." . "done\n" ); |
| 30 |
$resultingPageRestrictions = $this->getServiceContainer()->getRestrictionStore() |
| 31 |
->getAllRestrictions( $testPage ); |
| 32 |
foreach ( $resultingPageRestrictions as $restrictions ) { |
| 33 |
$this->assertContains( $expectedProtectionLevel, $restrictions ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
public static function provideExecute() { |
| 38 |
return [ |
| 39 |
'Sysop protection' => [ [], 'sysop' ], |
| 40 |
'Autoconfirmed protection' => [ [ 'semiprotect' => 1 ], 'autoconfirmed' ], |
| 41 |
]; |
| 42 |
} |
| 43 |
|
| 44 |
public function testExecuteForUnprotect() { |
| 45 |
$testPage = $this->getExistingTestPage(); |
| 46 |
|
| 47 |
$cascade = false; |
| 48 |
$testPage->doUpdateRestrictions( |
| 49 |
[ 'edit' => 'sysop', 'move' => 'sysop' ], [], $cascade, '', $this->getTestSysop()->getUser() |
| 50 |
); |
| 51 |
|
| 52 |
$resultingPageRestrictions = $this->getServiceContainer()->getRestrictionStore() |
| 53 |
->getAllRestrictions( $testPage ); |
| 54 |
foreach ( $resultingPageRestrictions as $restrictions ) { |
| 55 |
$this->assertContains( 'sysop', $restrictions ); |
| 56 |
} |
| 57 |
|
| 58 |
$this->maintenance->setArg( 'title', $testPage ); |
| 59 |
$this->maintenance->setOption( 'unprotect', 1 ); |
| 60 |
$this->maintenance->execute(); |
| 61 |
|
| 62 |
$this->expectOutputString( "Updating protection status...done\n" ); |
| 63 |
$resultingPageRestrictions = $this->getServiceContainer()->getRestrictionStore() |
| 64 |
->getAllRestrictions( $testPage ); |
| 65 |
foreach ( $resultingPageRestrictions as $restrictions ) { |
| 66 |
$this->assertCount( 0, $restrictions ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function testExecuteWhenReadOnly() { |
| 71 |
|
| 72 |
$testPage = $this->getExistingTestPage(); |
| 73 |
$this->maintenance->setArg( 'title', $testPage ); |
| 74 |
|
| 75 |
$this->getServiceContainer()->getReadOnlyMode()->setReason( 'test' ); |
| 76 |
|
| 77 |
$this->maintenance->execute(); |
| 78 |
|
| 79 |
$this->expectOutputString( "Updating protection status..." . "failed\n" ); |
| 80 |
} |
| 81 |
|
| 82 |
public function testExecuteOnInvalidUserOption() { |
| 83 |
$this->expectCallToFatalError(); |
| 84 |
$this->expectOutputRegex( '/Invalid username/' ); |
| 85 |
$this->maintenance->setOption( 'user', 'Template:Testing#test' ); |
| 86 |
$this->maintenance->setArg( 'title', 'unused-for-this-test' ); |
| 87 |
$this->maintenance->execute(); |
| 88 |
} |
| 89 |
|
| 90 |
public function testExecuteOnInvalidTitleArgument() { |
| 91 |
$this->expectCallToFatalError(); |
| 92 |
$this->expectOutputRegex( '/Invalid title/' ); |
| 93 |
$this->maintenance->setArg( 'title', ':::' ); |
| 94 |
$this->maintenance->execute(); |
| 95 |
} |
| 96 |
} |