| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Maintenance; |
| 4 |
|
| 5 |
use EditCLI; |
| 6 |
use MediaWiki\Context\RequestContext; |
| 7 |
use MediaWiki\Maintenance\Maintenance; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\User\User; |
| 11 |
use PHPUnit\Framework\ExpectationFailedException; |
| 12 |
use WikiPage; |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class SemiMockedEditCLI extends EditCLI { |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
private ?string $mockStdinText = null; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public function mockStdin( $stdin ) { |
| 33 |
$this->mockStdinText = $stdin; |
| 34 |
} |
| 35 |
|
| 36 |
public function getStdin( $len = null ) { |
| 37 |
if ( $len !== Maintenance::STDIN_ALL ) { |
| 38 |
throw new ExpectationFailedException( "Tried to get stdin without using Maintenance::STDIN_ALL" ); |
| 39 |
} |
| 40 |
|
| 41 |
return file_get_contents( 'data://text/plain,' . $this->mockStdinText ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
class EditTest extends MaintenanceBaseTestCase { |
| 51 |
|
| 52 |
protected function getMaintenanceClass() { |
| 53 |
return SemiMockedEditCLI::class; |
| 54 |
} |
| 55 |
|
| 56 |
private function commonTextExecute( |
| 57 |
array $options, string $title, WikiPage $wikiPage, string $stdin, string $expectedWikitextContent |
| 58 |
) { |
| 59 |
$this->maintenance->mockStdin( $stdin ); |
| 60 |
$this->maintenance->setArg( 'title', $title ); |
| 61 |
foreach ( $options as $name => $value ) { |
| 62 |
$this->maintenance->setOption( $name, $value ); |
| 63 |
} |
| 64 |
$this->maintenance->execute(); |
| 65 |
$wikiPage->clear(); |
| 66 |
$actualWikitextContent = $wikiPage->getContent()->getWikitextForTransclusion(); |
| 67 |
$this->assertSame( $expectedWikitextContent, $actualWikitextContent ); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
public function testExecute( |
| 72 |
$options, $content, $expectedUserName, $expectedComment, $shouldBeAMinorEdit, $shouldBeSentToRecentChanges |
| 73 |
) { |
| 74 |
$testPage = $this->getExistingTestPage(); |
| 75 |
$this->commonTextExecute( $options, $testPage->getTitle()->getPrefixedText(), $testPage, $content, $content ); |
| 76 |
$lastRevision = $testPage->getRevisionRecord(); |
| 77 |
$this->assertSame( $expectedUserName, $lastRevision->getUser()->getName() ); |
| 78 |
$this->assertSame( $expectedComment, $lastRevision->getComment()->text ); |
| 79 |
$this->assertSame( $shouldBeAMinorEdit, $lastRevision->isMinor() ); |
| 80 |
$this->newSelectQueryBuilder() |
| 81 |
->select( 'COUNT(*)' ) |
| 82 |
->from( 'recentchanges' ) |
| 83 |
->where( [ 'rc_this_oldid' => $lastRevision->getId() ] ) |
| 84 |
->assertFieldValue( (int)$shouldBeSentToRecentChanges ); |
| 85 |
$this->expectOutputRegex( '/Saving.*done/' ); |
| 86 |
} |
| 87 |
|
| 88 |
public static function provideExecute() { |
| 89 |
return [ |
| 90 |
'No options provided' => [ [], "testcontent\ntest", User::MAINTENANCE_SCRIPT_USER, '', false, true ], |
| 91 |
'Summary provided, minor edit, not sent to recentchanges' => [ |
| 92 |
[ 'summary' => 'test', 'minor' => 1, 'no-rc' => 1 ], "testcontent", User::MAINTENANCE_SCRIPT_USER, |
| 93 |
'test', true, false, |
| 94 |
], |
| 95 |
'Autosummary enabled' => [ |
| 96 |
|
| 97 |
[ 'autosummary' => 1 ], "#REDIRECT [[Test]]", User::MAINTENANCE_SCRIPT_USER, |
| 98 |
'Redirected page to [[Test]]', false, true, |
| 99 |
], |
| 100 |
]; |
| 101 |
} |
| 102 |
|
| 103 |
public function testExecuteWithBotFlagAndAutosummaryForRedirect() { |
| 104 |
$testPage = $this->getExistingTestPage(); |
| 105 |
$oldContent = $testPage->getContent(); |
| 106 |
$this->commonTextExecute( |
| 107 |
[ 'bot' => 1, 'autosummary' => 1 ], |
| 108 |
$testPage->getTitle()->getPrefixedText(), |
| 109 |
$testPage, |
| 110 |
"#REDIRECT [[Test]]", |
| 111 |
"#REDIRECT [[Test]]" |
| 112 |
); |
| 113 |
$lastRevision = $testPage->getRevisionRecord(); |
| 114 |
$newContent = $lastRevision->getContent( SlotRecord::MAIN ); |
| 115 |
$this->assertSame( |
| 116 |
$testPage->getContentHandler()->getAutosummary( $oldContent, $newContent ), |
| 117 |
$lastRevision->getComment()->text |
| 118 |
); |
| 119 |
$this->newSelectQueryBuilder() |
| 120 |
->select( 'rc_bot' ) |
| 121 |
->from( 'recentchanges' ) |
| 122 |
->where( [ 'rc_this_oldid' => $lastRevision->getId() ] ) |
| 123 |
->assertFieldValue( 1 ); |
| 124 |
$this->expectOutputRegex( '/Saving.*done/' ); |
| 125 |
} |
| 126 |
|
| 127 |
public function testExecuteForParseTitle() { |
| 128 |
$wikiPage = $this->getServiceContainer()->getWikiPageFactory() |
| 129 |
->newFromTitle( Title::newFromText( RequestContext::getMain()->msg( 'mainpage' )->text() ) ); |
| 130 |
$this->commonTextExecute( |
| 131 |
[ 'parse-title' => 1 ], '{{int:mainpage}}', $wikiPage, |
| 132 |
"* testing1234abc", "* testing1234abc" |
| 133 |
); |
| 134 |
$this->expectOutputRegex( '/Saving.*done/' ); |
| 135 |
} |
| 136 |
|
| 137 |
public function testExecuteWithUserProvided() { |
| 138 |
$testUser = $this->getTestUser()->getUser(); |
| 139 |
$testPage = $this->getExistingTestPage(); |
| 140 |
$this->commonTextExecute( |
| 141 |
[ 'user' => $testUser->getName() ], |
| 142 |
$testPage->getTitle()->getPrefixedText(), |
| 143 |
$testPage, |
| 144 |
"test", |
| 145 |
"test" |
| 146 |
); |
| 147 |
$lastRevision = $testPage->getRevisionRecord(); |
| 148 |
$this->assertSame( $testUser->getName(), $lastRevision->getUser()->getName() ); |
| 149 |
$this->expectOutputRegex( '/Saving.*done/' ); |
| 150 |
} |
| 151 |
|
| 152 |
public function testExecuteForFailedEdit() { |
| 153 |
$testUser = $this->getTestUser()->getUser(); |
| 154 |
$testPage = $this->getExistingTestPage(); |
| 155 |
|
| 156 |
$this->setTemporaryHook( 'MultiContentSave', static function () { |
| 157 |
return false; |
| 158 |
} ); |
| 159 |
$this->commonTextExecute( |
| 160 |
[ 'user' => $testUser->getName() ], |
| 161 |
$testPage->getTitle()->getPrefixedText(), |
| 162 |
$testPage, |
| 163 |
"test", |
| 164 |
$testPage->getContent()->getWikitextForTransclusion() |
| 165 |
); |
| 166 |
$this->expectOutputRegex( '/Saving.*failed/' ); |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
public function testExecuteForFatalError( $options, $expectedOutputRegex, $title = null ) { |
| 171 |
$this->expectCallToFatalError(); |
| 172 |
$this->maintenance->setArg( 'title', $title ?? 'test' ); |
| 173 |
foreach ( $options as $name => $value ) { |
| 174 |
$this->maintenance->setOption( $name, $value ); |
| 175 |
} |
| 176 |
$this->maintenance->execute(); |
| 177 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 178 |
} |
| 179 |
|
| 180 |
public static function provideExecuteForFatalError() { |
| 181 |
return [ |
| 182 |
'Invalid username' => [ [ 'user' => 'Template:Testing#test' ], '/Invalid username/' ], |
| 183 |
'Invalid title' => [ [], '/Invalid title/', ':::' ], |
| 184 |
'Title does not exist and nocreate is set' => [ |
| 185 |
[ 'nocreate' => 1 ], '/Page does not exist/', 'Non-existing-test-page-1234', |
| 186 |
], |
| 187 |
'Attempts to remove the main slot' => [ |
| 188 |
[ 'remove' => 1, 'slot' => SlotRecord::MAIN ], '/Cannot remove main slot/', |
| 189 |
], |
| 190 |
]; |
| 191 |
} |
| 192 |
|
| 193 |
public function testExecuteForCreateOnlyWhenPageExists() { |
| 194 |
$this->testExecuteForFatalError( |
| 195 |
[ 'createonly' => 1 ], |
| 196 |
'/Page already exists/', |
| 197 |
$this->getExistingTestPage()->getTitle()->getPrefixedText() |
| 198 |
); |
| 199 |
} |
| 200 |
} |