3 namespace MediaWiki\Tests\Maintenance
;
12 class ProtectTest
extends MaintenanceBaseTestCase
{
14 protected function getMaintenanceClass() {
15 return Protect
::class;
18 /** @dataProvider provideExecute */
19 public function testExecute( $options, $expectedProtectionLevel ) {
20 $testPage = $this->getExistingTestPage();
21 // Set the options from $options.
22 foreach ( $options as $name => $option ) {
23 $this->maintenance
->setOption( $name, $option );
26 $this->maintenance
->setArg( 'title', $testPage );
27 $this->maintenance
->execute();
28 // Verify that the specified protection level has been applied
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 );
37 public static function provideExecute() {
39 'Sysop protection' => [ [], 'sysop' ],
40 'Autoconfirmed protection' => [ [ 'semiprotect' => 1 ], 'autoconfirmed' ],
44 public function testExecuteForUnprotect() {
45 $testPage = $this->getExistingTestPage();
46 // Protect the test page so that we can unprotect it
48 $testPage->doUpdateRestrictions(
49 [ 'edit' => 'sysop', 'move' => 'sysop' ], [], $cascade, '', $this->getTestSysop()->getUser()
51 // Verify that the specified protection level has been applied
52 $resultingPageRestrictions = $this->getServiceContainer()->getRestrictionStore()
53 ->getAllRestrictions( $testPage );
54 foreach ( $resultingPageRestrictions as $restrictions ) {
55 $this->assertContains( 'sysop', $restrictions );
57 // Call ::execute to unprotect the page
58 $this->maintenance
->setArg( 'title', $testPage );
59 $this->maintenance
->setOption( 'unprotect', 1 );
60 $this->maintenance
->execute();
61 // Verify that the specified protection level has been applied
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 );
70 public function testExecuteWhenReadOnly() {
71 // Get a test page and then add it as an argument to the maintenance script
72 $testPage = $this->getExistingTestPage();
73 $this->maintenance
->setArg( 'title', $testPage );
74 // Enable read-only mode
75 $this->getServiceContainer()->getReadOnlyMode()->setReason( 'test' );
77 $this->maintenance
->execute();
78 // Verify that the updating the protection status failed.
79 $this->expectOutputString( "Updating protection status..." . "failed\n" );
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();
90 public function testExecuteOnInvalidTitleArgument() {
91 $this->expectCallToFatalError();
92 $this->expectOutputRegex( '/Invalid title/' );
93 $this->maintenance
->setArg( 'title', ':::' );
94 $this->maintenance
->execute();