| 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 |
|
| 11 |
|
| 12 |
|
| 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 |
|
| 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 |
|
| 35 |
if ( $this->wasMessageCacheDisabled ) { |
| 36 |
$this->getServiceContainer()->getMessageCache()->disable(); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 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 |
|
| 73 |
public function testExecuteWhenNoMessageOverridesContainDefaultContent( $title, $langCode, $expectedOutputRegex ) { |
| 74 |
|
| 75 |
$this->performMediaWikiOverrideEdit( $title, 'test-1234' ); |
| 76 |
|
| 77 |
DeferredUpdates::doUpdates(); |
| 78 |
|
| 79 |
if ( $langCode !== null ) { |
| 80 |
$this->maintenance->setOption( 'lang-code', $langCode ); |
| 81 |
} |
| 82 |
$this->maintenance->execute(); |
| 83 |
$this->expectOutputRegex( $expectedOutputRegex ); |
| 84 |
|
| 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 |
|
| 106 |
public function testExecuteForDryRun( $msgKey, $title, $langCode ) { |
| 107 |
|
| 108 |
$aboutMessageDefault = wfMessage( $msgKey ); |
| 109 |
if ( $langCode ) { |
| 110 |
$aboutMessageDefault->inLanguage( $langCode ); |
| 111 |
} |
| 112 |
$aboutMessageDefault = $aboutMessageDefault->text(); |
| 113 |
|
| 114 |
$this->performMediaWikiOverrideEdit( $title, 'testing-1234' ); |
| 115 |
$this->performMediaWikiOverrideEdit( $title, $aboutMessageDefault ); |
| 116 |
|
| 117 |
|
| 118 |
$talkPage = Title::newFromText( $title )->getTalkPageIfDefined(); |
| 119 |
$this->performMediaWikiOverrideEdit( |
| 120 |
$talkPage->getPrefixedText(), |
| 121 |
'testing1234' |
| 122 |
); |
| 123 |
|
| 124 |
DeferredUpdates::doUpdates(); |
| 125 |
|
| 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 |
|
| 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 |
|
| 151 |
public function testExecute( $msgKey, $title, $langCode ) { |
| 152 |
|
| 153 |
$aboutMessageDefault = wfMessage( $msgKey ); |
| 154 |
if ( $langCode ) { |
| 155 |
$aboutMessageDefault->inLanguage( $langCode ); |
| 156 |
} |
| 157 |
$aboutMessageDefault = $aboutMessageDefault->text(); |
| 158 |
|
| 159 |
$this->performMediaWikiOverrideEdit( $title, 'testing-1234' ); |
| 160 |
$this->performMediaWikiOverrideEdit( $title, $aboutMessageDefault ); |
| 161 |
|
| 162 |
|
| 163 |
$talkPage = Title::newFromText( $title )->getTalkPageIfDefined(); |
| 164 |
$this->performMediaWikiOverrideEdit( |
| 165 |
$talkPage->getPrefixedText(), |
| 166 |
'testing1234' |
| 167 |
); |
| 168 |
|
| 169 |
DeferredUpdates::doUpdates(); |
| 170 |
|
| 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 |
} |