| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\CommentStore\CommentStoreComment; |
| 4 |
use MediaWiki\MainConfigNames; |
| 5 |
use MediaWiki\Message\Message; |
| 6 |
use MediaWiki\Page\MergeHistory; |
| 7 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 8 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\Utils\MWTimestamp; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class MergeHistoryTest extends MediaWikiIntegrationTestCase { |
| 16 |
use MockAuthorityTrait; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
public function addDBDataOnce() { |
| 22 |
|
| 23 |
$this->insertPage( 'Test' ); |
| 24 |
$this->insertPage( 'Test2' ); |
| 25 |
|
| 26 |
|
| 27 |
$this->insertPage( 'Merge1' ); |
| 28 |
$this->insertPage( 'Merge2' ); |
| 29 |
|
| 30 |
|
| 31 |
$this->insertPage( 'Merge3' ); |
| 32 |
$this->insertPage( 'Merge4' ); |
| 33 |
|
| 34 |
|
| 35 |
$this->insertPage( 'Merge5' ); |
| 36 |
$this->insertPage( 'Merge6' ); |
| 37 |
} |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function testIsValidMerge( $source, $dest, $timestamp, $error ) { |
| 48 |
if ( $timestamp === true ) { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
$timestamp = time() + ( 24 * 3600 ); |
| 53 |
} |
| 54 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 55 |
$mh = $factory->newMergeHistory( |
| 56 |
Title::newFromText( $source ), |
| 57 |
Title::newFromText( $dest ), |
| 58 |
$timestamp |
| 59 |
); |
| 60 |
$status = $mh->isValidMerge(); |
| 61 |
if ( $error === true ) { |
| 62 |
$this->assertStatusGood( $status ); |
| 63 |
} else { |
| 64 |
$this->assertStatusError( $error, $status ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
public static function provideIsValidMerge() { |
| 69 |
return [ |
| 70 |
|
| 71 |
[ 'Test', 'Test2', false, true ], |
| 72 |
|
| 73 |
[ 'Test', 'Test2', true, true ], |
| 74 |
[ 'Test', 'Test', false, 'mergehistory-fail-self-merge' ], |
| 75 |
[ 'Nonexistant', 'Test2', false, 'mergehistory-fail-invalid-source' ], |
| 76 |
[ 'Test', 'Nonexistant', false, 'mergehistory-fail-invalid-dest' ], |
| 77 |
[ |
| 78 |
'Test', |
| 79 |
'Test2', |
| 80 |
'This is obviously an invalid timestamp', |
| 81 |
'mergehistory-fail-bad-timestamp' |
| 82 |
], |
| 83 |
]; |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public function testIsValidMergeRevisionLimit() { |
| 91 |
$limit = MergeHistory::REVISION_LIMIT; |
| 92 |
$mh = $this->getMockBuilder( MergeHistory::class ) |
| 93 |
->onlyMethods( [ 'getRevisionCount' ] ) |
| 94 |
->setConstructorArgs( [ |
| 95 |
Title::makeTitle( NS_MAIN, 'Test' ), |
| 96 |
Title::makeTitle( NS_MAIN, 'Test2' ), |
| 97 |
null, |
| 98 |
$this->getServiceContainer()->getConnectionProvider(), |
| 99 |
$this->getServiceContainer()->getContentHandlerFactory(), |
| 100 |
$this->getServiceContainer()->getRevisionStore(), |
| 101 |
$this->getServiceContainer()->getWatchedItemStore(), |
| 102 |
$this->getServiceContainer()->getSpamChecker(), |
| 103 |
$this->getServiceContainer()->getHookContainer(), |
| 104 |
$this->getServiceContainer()->getWikiPageFactory(), |
| 105 |
$this->getServiceContainer()->getTitleFormatter(), |
| 106 |
$this->getServiceContainer()->getTitleFactory(), |
| 107 |
$this->getServiceContainer()->getLinkTargetLookup(), |
| 108 |
$this->getServiceContainer()->getDeletePageFactory(), |
| 109 |
] ) |
| 110 |
->getMock(); |
| 111 |
$mh->expects( $this->once() ) |
| 112 |
->method( 'getRevisionCount' ) |
| 113 |
->willReturn( $limit + 1 ); |
| 114 |
|
| 115 |
$status = $mh->isValidMerge(); |
| 116 |
|
| 117 |
$this->assertStatusNotOK( $status ); |
| 118 |
$this->assertStatusMessagesExactly( |
| 119 |
StatusValue::newFatal( 'mergehistory-fail-toobig', Message::numParam( $limit ) ), |
| 120 |
$status |
| 121 |
); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function testCheckPermissions() { |
| 130 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 131 |
$mh = $factory->newMergeHistory( |
| 132 |
Title::makeTitle( NS_MAIN, 'Test' ), |
| 133 |
Title::makeTitle( NS_MAIN, 'Test2' ) |
| 134 |
); |
| 135 |
|
| 136 |
foreach ( [ 'authorizeMerge', 'probablyCanMerge' ] as $method ) { |
| 137 |
|
| 138 |
$status = $mh->$method( |
| 139 |
$this->mockRegisteredUltimateAuthority(), |
| 140 |
'' |
| 141 |
); |
| 142 |
$this->assertStatusOK( $status ); |
| 143 |
|
| 144 |
$status = $mh->$method( |
| 145 |
$this->mockRegisteredAuthorityWithoutPermissions( [ 'mergehistory' ] ), |
| 146 |
'' |
| 147 |
); |
| 148 |
$this->assertStatusError( 'mergehistory-fail-permission', $status ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
public function testGetMergedRevisionCount() { |
| 157 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 158 |
$mh = $factory->newMergeHistory( |
| 159 |
Title::makeTitle( NS_MAIN, 'Merge1' ), |
| 160 |
Title::makeTitle( NS_MAIN, 'Merge2' ) |
| 161 |
); |
| 162 |
|
| 163 |
$sysop = static::getTestSysop()->getUser(); |
| 164 |
$mh->merge( $sysop ); |
| 165 |
$this->assertSame( 1, $mh->getMergedRevisionCount() ); |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public function testSourceUpdateWithRedirectSupport() { |
| 175 |
$title = Title::makeTitle( NS_MAIN, 'Merge5' ); |
| 176 |
$title2 = Title::makeTitle( NS_MAIN, 'Merge6' ); |
| 177 |
|
| 178 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 179 |
$mh = $factory->newMergeHistory( $title, $title2 ); |
| 180 |
|
| 181 |
$this->assertTrue( $title->exists() ); |
| 182 |
|
| 183 |
$status = $mh->merge( static::getTestSysop()->getUser() ); |
| 184 |
$this->assertStatusOK( $status ); |
| 185 |
|
| 186 |
$this->assertTrue( $title->exists() ); |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
public function testSourceUpdateForNoRedirectSupport() { |
| 196 |
$this->overrideConfigValues( [ |
| 197 |
MainConfigNames::ExtraNamespaces => [ |
| 198 |
2030 => 'NoRedirect', |
| 199 |
2031 => 'NoRedirect_talk' |
| 200 |
], |
| 201 |
|
| 202 |
MainConfigNames::NamespaceContentModels => [ |
| 203 |
2030 => 'testing' |
| 204 |
], |
| 205 |
MainConfigNames::ContentHandlers => [ |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
'testing' => DummyContentHandlerForTesting::class |
| 210 |
] |
| 211 |
] ); |
| 212 |
|
| 213 |
$title = Title::makeTitle( NS_MAIN, 'Merge3' ); |
| 214 |
$title->setContentModel( 'testing' ); |
| 215 |
$title2 = Title::makeTitle( NS_MAIN, 'Merge4' ); |
| 216 |
$title2->setContentModel( 'testing' ); |
| 217 |
|
| 218 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 219 |
$mh = $factory->newMergeHistory( $title, $title2 ); |
| 220 |
|
| 221 |
$this->assertTrue( $title->exists() ); |
| 222 |
|
| 223 |
$status = $mh->merge( static::getTestSysop()->getUser() ); |
| 224 |
$this->assertStatusOK( $status ); |
| 225 |
|
| 226 |
$this->assertFalse( $title->exists() ); |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
public function testSplitTimestamp() { |
| 233 |
|
| 234 |
$user = static::getTestSysop()->getUser(); |
| 235 |
$title1 = $this->insertPage( "Merge7" )["title"]; |
| 236 |
$timestamp = MWTimestamp::now( TS_MW ); |
| 237 |
$store = $this->getServiceContainer()->getRevisionStore(); |
| 238 |
$revision = MutableRevisionRecord::newFromParentRevision( $store->getFirstRevision( $title1 ) ); |
| 239 |
$revision->setTimestamp( $timestamp ); |
| 240 |
$revision->setComment( CommentStoreComment::newUnsavedComment( "testing" ) ); |
| 241 |
$revision->setUser( $user ); |
| 242 |
$dbw = $this->getDB(); |
| 243 |
$revid1 = $store->insertRevisionOn( $revision, $dbw )->getID(); |
| 244 |
|
| 245 |
$revision2 = MutableRevisionRecord::newFromParentRevision( $store->getFirstRevision( $title1 ) ); |
| 246 |
$revision2->setTimestamp( $timestamp ); |
| 247 |
$revision2->setComment( CommentStoreComment::newUnsavedComment( "testing" ) ); |
| 248 |
$revision2->setUser( $user ); |
| 249 |
$revid2 = $store->insertRevisionOn( $revision2, $dbw )->getID(); |
| 250 |
|
| 251 |
$title2 = $this->insertPage( "Merge8" )["title"]; |
| 252 |
|
| 253 |
|
| 254 |
$factory = $this->getServiceContainer()->getMergeHistoryFactory(); |
| 255 |
$mh = $factory->newMergeHistory( $title1, $title2, $timestamp . '|' . $revid1 ); |
| 256 |
$status = $mh->merge( $user ); |
| 257 |
$this->assertStatusOK( $status ); |
| 258 |
|
| 259 |
$this->assertNull( $store->getRevisionByPageId( $title1->getId(), $revid1 ) ); |
| 260 |
$this->assertNotNull( $store->getRevisionByPageId( $title1->getId(), $revid2 ) ); |
| 261 |
$this->assertNotNull( $store->getRevisionByPageId( $title2->getId(), $revid1 ) ); |
| 262 |
$this->assertNull( $store->getRevisionByPageId( $title2->getId(), $revid2 ) ); |
| 263 |
} |
| 264 |
} |