Debian Codegraph @

tests/phpunit/maintenance/DeleteEqualMessagesTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Maintenance;
4
5 use DeleteEqualMessages;
6 use MediaWiki\Deferred\DeferredUpdates;
7 use MediaWiki\Title\Title;
8
9 /**
10 * @covers \DeleteEqualMessages
11 * @group Database
12 * @author Dreamy Jazz
13 */
14 class DeleteEqualMessagesTest extends MaintenanceBaseTestCase {
15
16 private bool $wasMessageCacheDisabled;
17
18 protected function getMaintenanceClass() {
19 return DeleteEqualMessages::class;
20 }
21
22 protected function setUp(): void {
23 parent::setUp();
24 // Set the message cache to enabled if it is not already, as we need it to be enabled for the tests to work.
25 $messageCache = $this->getServiceContainer()->getMessageCache();
26 $this->wasMessageCacheDisabled = $messageCache->isDisabled();
27 if ( $this->wasMessageCacheDisabled ) {
28 $messageCache->enable();
29 }
30 }
31
32 protected function tearDown(): void {
33 parent::tearDown();
34 // Set the message cache back to disabled after the test so we don't affect other tests.
35 if ( $this->wasMessageCacheDisabled ) {
36 $this->getServiceContainer()->getMessageCache()->disable();
37 }
38 }
39
40 /** @dataProvider provideExecuteForNoRelevantOverrides */
41 public function testExecuteForNoMessageOverrides( $langCode, $expectedOutputRegex ) {
42 if ( $langCode !== null ) {
43 $this->maintenance->setOption( 'lang-code', $langCode );
44 }
45 $this->maintenance->execute();
46 $this->expectOutputRegex( $expectedOutputRegex );
47 }
48
49 public static function provideExecuteForNoRelevantOverrides() {
50 return [
51 'No lang-code provided' => [
52 null,
53 "/Checking for pages with default message[\s\S]*fetching message info for content language\ndone/",
54 ],
55 'Spanish used as lang-code' => [
56 'es', "/Checking for pages with default message[\s\S]*fetching message info for language: es\ndone/",
57 ],
58 ];
59 }
60
61 public function testExecuteForInvalidLanguageCode() {
62 $this->maintenance->setOption( 'lang-code', 'invalidlanguagecode' );
63 $this->expectCallToFatalError();
64 $this->expectOutputRegex( '/Invalid language code: invalidlanguagecode/' );
65 $this->maintenance->execute();
66 }
67
68 private function performMediaWikiOverrideEdit( $title, $content ) {
69 $this->editPage( $title, $content, 'test', NS_MEDIAWIKI );
70 }
71
72 /** @dataProvider provideExecuteWhenNoMessageOverridesContainDefaultContent */
73 public function testExecuteWhenNoMessageOverridesContainDefaultContent( $title, $langCode, $expectedOutputRegex ) {
74 // Create the message override with content not equal to the default value.
75 $this->performMediaWikiOverrideEdit( $title, 'test-1234' );
76 // Run deferred updates as this refreshes the message cache after the MediaWiki namespace edit.
77 DeferredUpdates::doUpdates();
78 // Call the maintenance script, and expect that it does not consider the override to be the default value.
79 if ( $langCode !== null ) {
80 $this->maintenance->setOption( 'lang-code', $langCode );
81 }
82 $this->maintenance->execute();
83 $this->expectOutputRegex( $expectedOutputRegex );
84 // Check that the message override was not deleted.
85 Title::clearCaches();
86 $this->assertTrue(
87 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
88 "$title was deleted when it should not have been."
89 );
90 }
91
92 public static function provideExecuteWhenNoMessageOverridesContainDefaultContent() {
93 return [
94 'No lang-code provided' => [
95 'MediaWiki:Aboutpage', null,
96 "/Checking for pages with default message[\s\S]*fetching message info for content language\ndone/",
97 ],
98 'Spanish used as lang-code' => [
99 'MediaWiki:Aboutpage/es', 'es',
100 "/Checking for pages with default message[\s\S]*fetching message info for language: es\ndone/",
101 ],
102 ];
103 }
104
105 /** @dataProvider provideExecuteForDryRun */
106 public function testExecuteForDryRun( $msgKey, $title, $langCode ) {
107 // Get the default value for the message key.
108 $aboutMessageDefault = wfMessage( $msgKey );
109 if ( $langCode ) {
110 $aboutMessageDefault->inLanguage( $langCode );
111 }
112 $aboutMessageDefault = $aboutMessageDefault->text();
113 // Create the override such that the override is equal to the default
114 $this->performMediaWikiOverrideEdit( $title, 'testing-1234' );
115 $this->performMediaWikiOverrideEdit( $title, $aboutMessageDefault );
116 // Create a talk page for this override, so that we can test how the script handles that page.
117 /** @var Title $talkPage */
118 $talkPage = Title::newFromText( $title )->getTalkPageIfDefined();
119 $this->performMediaWikiOverrideEdit(
120 $talkPage->getPrefixedText(),
121 'testing1234'
122 );
123 // Run deferred updates as this refreshes the message cache after the MediaWiki namespace edit.
124 DeferredUpdates::doUpdates();
125 // Call the maintenance script, and expect that it finds the override that equals the default value.
126 if ( $langCode !== null ) {
127 $this->maintenance->setOption( 'lang-code', $langCode );
128 }
129 $this->maintenance->execute();
130 $this->expectOutputRegex(
131 "/1 pages are equal to the default message \(\+ 1 talk pages\)[\s\S]*" .
132 preg_quote( $title, '/' ) . "[\s\S]*" . preg_quote( $talkPage->getPrefixedText(), '/' ) .
133 "[\s\S]*Run the script again with --delete to delete these pages/"
134 );
135 // Check that the message override was not deleted, as a dry-run should not actually perform deletions.
136 Title::clearCaches();
137 $this->assertTrue(
138 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
139 "$title should not have been deleted, as it is a dry-run."
140 );
141 }
142
143 public static function provideExecuteForDryRun() {
144 return [
145 'No lang-code provided' => [ 'aboutpage', 'MediaWiki:Aboutpage', null ],
146 'Spanish used as lang-code' => [ 'aboutpage', 'MediaWiki:Aboutpage/es', 'es' ],
147 ];
148 }
149
150 /** @dataProvider provideExecute */
151 public function testExecute( $msgKey, $title, $langCode ) {
152 // Get the default value for the message key.
153 $aboutMessageDefault = wfMessage( $msgKey );
154 if ( $langCode ) {
155 $aboutMessageDefault->inLanguage( $langCode );
156 }
157 $aboutMessageDefault = $aboutMessageDefault->text();
158 // Create the override such that the override is equal to the default
159 $this->performMediaWikiOverrideEdit( $title, 'testing-1234' );
160 $this->performMediaWikiOverrideEdit( $title, $aboutMessageDefault );
161 // Create a talk page for this override, so that we can test that it gets deleted by the maintenance script.
162 /** @var Title $talkPage */
163 $talkPage = Title::newFromText( $title )->getTalkPageIfDefined();
164 $this->performMediaWikiOverrideEdit(
165 $talkPage->getPrefixedText(),
166 'testing1234'
167 );
168 // Run deferred updates as this refreshes the message cache after the MediaWiki namespace edit.
169 DeferredUpdates::doUpdates();
170 // Call the maintenance script, and expect that it finds the override that equals the default value.
171 if ( $langCode !== null ) {
172 $this->maintenance->setOption( 'lang-code', $langCode );
173 }
174 $this->maintenance->setOption( 'delete', 1 );
175 $this->maintenance->setOption( 'delete-talk', 1 );
176 $this->maintenance->execute();
177 $this->expectOutputRegex(
178 "/1 pages are equal to the default message \(\+ 1 talk pages\)[\s\S]*" .
179 "deleting equal messages[\s\S]*" . preg_quote( $title, '/' ) . "[\s\S]*" .
180 preg_quote( $talkPage->getPrefixedText(), '/' ) . "[\s\S]*done/"
181 );
182 Title::clearCaches();
183 $this->assertFalse(
184 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
185 "$title should not have been deleted, as it duplicates the default."
186 );
187 }
188
189 public static function provideExecute() {
190 return [
191 'No lang-code provided' => [ 'aboutpage', 'MediaWiki:Aboutpage', null, ],
192 'Spanish used as lang-code' => [ 'aboutpage', 'MediaWiki:Aboutpage/es', 'es' ],
193 ];
194 }
195 }