Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / maintenance / EditTest.php
blobf29cdc916cc4c6b277c4a157196f034ae214d072
1 <?php
3 namespace MediaWiki\Tests\Maintenance;
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;
14 /**
15 * Mock for the input/output of EditCLI
17 * EditCLI internally tries to access stdin and stdout. We mock those aspects
18 * for testing.
20 class SemiMockedEditCLI extends EditCLI {
22 /**
23 * @var string|null Text to pass as stdin
25 private ?string $mockStdinText = null;
27 /**
28 * Data for the fake stdin
30 * @param string $stdin The string to be used instead of stdin
32 public function mockStdin( $stdin ) {
33 $this->mockStdinText = $stdin;
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" );
41 return file_get_contents( 'data://text/plain,' . $this->mockStdinText );
45 /**
46 * @covers \EditCLI
47 * @group Database
48 * @author Dreamy Jazz
50 class EditTest extends MaintenanceBaseTestCase {
52 protected function getMaintenanceClass() {
53 return SemiMockedEditCLI::class;
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 );
64 $this->maintenance->execute();
65 $wikiPage->clear();
66 $actualWikitextContent = $wikiPage->getContent()->getWikitextForTransclusion();
67 $this->assertSame( $expectedWikitextContent, $actualWikitextContent );
70 /** @dataProvider provideExecute */
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/' );
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,
95 'Autosummary enabled' => [
96 // TODO: Don't hard code the output of the autosummary?
97 [ 'autosummary' => 1 ], "#REDIRECT [[Test]]", User::MAINTENANCE_SCRIPT_USER,
98 'Redirected page to [[Test]]', false, true,
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]]"
113 $lastRevision = $testPage->getRevisionRecord();
114 $newContent = $lastRevision->getContent( SlotRecord::MAIN );
115 $this->assertSame(
116 $testPage->getContentHandler()->getAutosummary( $oldContent, $newContent ),
117 $lastRevision->getComment()->text
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/' );
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"
134 $this->expectOutputRegex( '/Saving.*done/' );
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"
147 $lastRevision = $testPage->getRevisionRecord();
148 $this->assertSame( $testUser->getName(), $lastRevision->getUser()->getName() );
149 $this->expectOutputRegex( '/Saving.*done/' );
152 public function testExecuteForFailedEdit() {
153 $testUser = $this->getTestUser()->getUser();
154 $testPage = $this->getExistingTestPage();
155 // Prevent all edits using a hook.
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()
166 $this->expectOutputRegex( '/Saving.*failed/' );
169 /** @dataProvider provideExecuteForFatalError */
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 );
176 $this->maintenance->execute();
177 $this->expectOutputRegex( $expectedOutputRegex );
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',
187 'Attempts to remove the main slot' => [
188 [ 'remove' => 1, 'slot' => SlotRecord::MAIN ], '/Cannot remove main slot/',
193 public function testExecuteForCreateOnlyWhenPageExists() {
194 $this->testExecuteForFatalError(
195 [ 'createonly' => 1 ],
196 '/Page already exists/',
197 $this->getExistingTestPage()->getTitle()->getPrefixedText()