3 namespace MediaWiki\Tests\Maintenance
;
5 use DeleteEqualMessages
;
6 use MediaWiki\Deferred\DeferredUpdates
;
7 use MediaWiki\Title\Title
;
10 * @covers \DeleteEqualMessages
14 class DeleteEqualMessagesTest
extends MaintenanceBaseTestCase
{
16 private bool $wasMessageCacheDisabled;
18 protected function getMaintenanceClass() {
19 return DeleteEqualMessages
::class;
22 protected function setUp(): void
{
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();
32 protected function tearDown(): void
{
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();
40 /** @dataProvider provideExecuteForNoRelevantOverrides */
41 public function testExecuteForNoMessageOverrides( $langCode, $expectedOutputRegex ) {
42 if ( $langCode !== null ) {
43 $this->maintenance
->setOption( 'lang-code', $langCode );
45 $this->maintenance
->execute();
46 $this->expectOutputRegex( $expectedOutputRegex );
49 public static function provideExecuteForNoRelevantOverrides() {
51 'No lang-code provided' => [
53 "/Checking for pages with default message[\s\S]*fetching message info for content language\ndone/",
55 'Spanish used as lang-code' => [
56 'es', "/Checking for pages with default message[\s\S]*fetching message info for language: es\ndone/",
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();
68 private function performMediaWikiOverrideEdit( $title, $content ) {
69 $this->editPage( $title, $content, 'test', NS_MEDIAWIKI
);
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 );
82 $this->maintenance
->execute();
83 $this->expectOutputRegex( $expectedOutputRegex );
84 // Check that the message override was not deleted.
87 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
88 "$title was deleted when it should not have been."
92 public static function provideExecuteWhenNoMessageOverridesContainDefaultContent() {
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/",
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/",
105 /** @dataProvider provideExecuteForDryRun */
106 public function testExecuteForDryRun( $msgKey, $title, $langCode ) {
107 // Get the default value for the message key.
108 $aboutMessageDefault = wfMessage( $msgKey );
110 $aboutMessageDefault->inLanguage( $langCode );
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(),
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 );
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/"
135 // Check that the message override was not deleted, as a dry-run should not actually perform deletions.
136 Title
::clearCaches();
138 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
139 "$title should not have been deleted, as it is a dry-run."
143 public static function provideExecuteForDryRun() {
145 'No lang-code provided' => [ 'aboutpage', 'MediaWiki:Aboutpage', null ],
146 'Spanish used as lang-code' => [ 'aboutpage', 'MediaWiki:Aboutpage/es', 'es' ],
150 /** @dataProvider provideExecute */
151 public function testExecute( $msgKey, $title, $langCode ) {
152 // Get the default value for the message key.
153 $aboutMessageDefault = wfMessage( $msgKey );
155 $aboutMessageDefault->inLanguage( $langCode );
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(),
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 );
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/"
182 Title
::clearCaches();
184 $this->getServiceContainer()->getTitleFactory()->newFromText( $title )->exists(),
185 "$title should not have been deleted, as it duplicates the default."
189 public static function provideExecute() {
191 'No lang-code provided' => [ 'aboutpage', 'MediaWiki:Aboutpage', null, ],
192 'Spanish used as lang-code' => [ 'aboutpage', 'MediaWiki:Aboutpage/es', 'es' ],