| 1 |
<?php |
| 2 |
|
| 3 |
use MediaWiki\Config\HashConfig; |
| 4 |
use MediaWiki\Content\Content; |
| 5 |
use MediaWiki\Content\ContentHandler; |
| 6 |
use MediaWiki\Context\DerivativeContext; |
| 7 |
use MediaWiki\Context\RequestContext; |
| 8 |
use MediaWiki\MainConfigNames; |
| 9 |
use MediaWiki\Output\OutputPage; |
| 10 |
use MediaWiki\Permissions\SimpleAuthority; |
| 11 |
use MediaWiki\Request\FauxRequest; |
| 12 |
use MediaWiki\Revision\MutableRevisionRecord; |
| 13 |
use MediaWiki\Revision\RevisionRecord; |
| 14 |
use MediaWiki\Revision\SlotRecord; |
| 15 |
use MediaWiki\Title\Title; |
| 16 |
use MediaWiki\User\UserIdentityValue; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class DifferenceEngineTest extends MediaWikiIntegrationTestCase { |
| 29 |
use MockTitleTrait; |
| 30 |
|
| 31 |
|
| 32 |
protected $context; |
| 33 |
|
| 34 |
|
| 35 |
private static $revisions; |
| 36 |
|
| 37 |
protected function setUp(): void { |
| 38 |
parent::setUp(); |
| 39 |
|
| 40 |
$title = $this->getTitle(); |
| 41 |
|
| 42 |
$this->context = new RequestContext(); |
| 43 |
$this->context->setTitle( $title ); |
| 44 |
|
| 45 |
$this->overrideConfigValue( MainConfigNames::DiffEngine, 'php' ); |
| 46 |
|
| 47 |
$slotRoleRegistry = $this->getServiceContainer()->getSlotRoleRegistry(); |
| 48 |
|
| 49 |
if ( !$slotRoleRegistry->isDefinedRole( 'derivedslot' ) ) { |
| 50 |
$slotRoleRegistry->defineRoleWithModel( |
| 51 |
'derivedslot', |
| 52 |
CONTENT_MODEL_WIKITEXT, |
| 53 |
[], |
| 54 |
true |
| 55 |
); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
public function addDBDataOnce() { |
| 60 |
self::$revisions = $this->doEdits(); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
protected function getTitle() { |
| 67 |
$namespace = $this->getDefaultWikitextNS(); |
| 68 |
return Title::makeTitle( $namespace, 'Kitten' ); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
protected function doEdits() { |
| 75 |
$title = $this->getTitle(); |
| 76 |
|
| 77 |
$strings = [ |
| 78 |
0 => "no kittens", |
| 79 |
1 => "one kitten", |
| 80 |
2 => "two kittens", |
| 81 |
3 => "three kittens", |
| 82 |
4 => "fnord kittens", |
| 83 |
5 => "kitten's phone number is +1 303 503 229", |
| 84 |
6 => "six kittens", |
| 85 |
7 => "seven kittens", |
| 86 |
]; |
| 87 |
$revisions = []; |
| 88 |
|
| 89 |
$sysop = $this->getTestSysop()->getAuthority(); |
| 90 |
$user = $this->getTestUser()->getAuthority(); |
| 91 |
foreach ( $strings as $i => $string ) { |
| 92 |
$status = $this->editPage( |
| 93 |
$title, |
| 94 |
$string, |
| 95 |
'edit page', |
| 96 |
NS_MAIN, |
| 97 |
$i == 6 ? $user : $sysop |
| 98 |
); |
| 99 |
$revisions[] = $status->getNewRevision()->getId(); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
$this->revisionDelete( |
| 104 |
$revisions[4], |
| 105 |
[ |
| 106 |
RevisionRecord::DELETED_TEXT => 1, |
| 107 |
], |
| 108 |
'Testing' |
| 109 |
); |
| 110 |
|
| 111 |
|
| 112 |
$this->revisionDelete( |
| 113 |
$revisions[5], |
| 114 |
[ |
| 115 |
RevisionRecord::DELETED_TEXT => 1, |
| 116 |
RevisionRecord::DELETED_RESTRICTED => 1, |
| 117 |
], |
| 118 |
'Testing' |
| 119 |
); |
| 120 |
|
| 121 |
return $revisions; |
| 122 |
} |
| 123 |
|
| 124 |
private function expandData( $data ) { |
| 125 |
if ( is_array( $data ) ) { |
| 126 |
foreach ( $data as &$value ) { |
| 127 |
$value = $this->expandData( $value ); |
| 128 |
} |
| 129 |
} elseif ( is_string( $data ) ) { |
| 130 |
$data = preg_replace_callback( |
| 131 |
'/rev\[([0-9]+)]/', |
| 132 |
static function ( $m ) { |
| 133 |
return self::$revisions[(int)$m[1]]; |
| 134 |
}, |
| 135 |
$data |
| 136 |
); |
| 137 |
$data = str_replace( |
| 138 |
'rev[cur]', |
| 139 |
self::$revisions[array_key_last( self::$revisions )], |
| 140 |
$data |
| 141 |
); |
| 142 |
} |
| 143 |
return $data; |
| 144 |
} |
| 145 |
|
| 146 |
private function expandTestArgs( $args ) { |
| 147 |
foreach ( $args as &$arg ) { |
| 148 |
$arg = $this->expandData( $arg ); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function testMapDiffPrevNext( $expected, $old, $new, $message ) { |
| 156 |
$this->expandTestArgs( [ &$expected, &$old, &$new, &$message ] ); |
| 157 |
$diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false ); |
| 158 |
$diffMap = $diffEngine->mapDiffPrevNext( $old, $new ); |
| 159 |
$this->assertEquals( $expected, $diffMap, $message ); |
| 160 |
} |
| 161 |
|
| 162 |
public static function provideMapDiffPrevNext() { |
| 163 |
return [ |
| 164 |
[ [ 'rev[1]', 'rev[2]' ], 'rev[2]', 'prev', 'diff=prev' ], |
| 165 |
[ [ 'rev[2]', 'rev[3]' ], 'rev[2]', 'next', 'diff=next' ], |
| 166 |
[ [ 'rev[1]', 'rev[3]' ], 'rev[1]', 'rev[3]', 'diff=rev3' ] |
| 167 |
]; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public function testLoadRevisionData( $expectedOld, $expectedNew, $expectedRet, $old, $new ) { |
| 174 |
$this->expandTestArgs( [ &$expectedOld, &$expectedNew, &$expectedRet, &$old, &$new ] ); |
| 175 |
$diffEngine = new DifferenceEngine( $this->context, $old, $new, 2, true, false ); |
| 176 |
$ret = $diffEngine->loadRevisionData(); |
| 177 |
$ret2 = $diffEngine->loadRevisionData(); |
| 178 |
|
| 179 |
$this->assertEquals( $expectedOld, $diffEngine->getOldid() ); |
| 180 |
$this->assertEquals( $expectedNew, $diffEngine->getNewid() ); |
| 181 |
$this->assertEquals( $expectedRet, $ret ); |
| 182 |
$this->assertEquals( $expectedRet, $ret2 ); |
| 183 |
} |
| 184 |
|
| 185 |
public static function provideLoadRevision() { |
| 186 |
return [ |
| 187 |
'diff=prev' => [ 'rev[2]', 'rev[3]', true, 'rev[3]', 'prev' ], |
| 188 |
'diff=next' => [ 'rev[2]', 'rev[3]', true, 'rev[2]', 'next' ], |
| 189 |
'diff=rev[3]' => [ 'rev[1]', 'rev[3]', true, 'rev[1]', 'rev[3]' ], |
| 190 |
'diff=0' => [ 'rev[1]', 'rev[cur]', true, 'rev[1]', 0 ], |
| 191 |
'diff=prev&oldid=<first>' => [ false, 'rev[0]', true, 'rev[0]', 'prev' ], |
| 192 |
'invalid' => [ 123456789, 'rev[1]', false, 123456789, 'rev[1]' ], |
| 193 |
]; |
| 194 |
} |
| 195 |
|
| 196 |
public function testGetOldid() { |
| 197 |
$revs = self::$revisions; |
| 198 |
|
| 199 |
$diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false ); |
| 200 |
$this->assertEquals( $revs[1], $diffEngine->getOldid(), 'diff get old id' ); |
| 201 |
} |
| 202 |
|
| 203 |
public function testGetNewid() { |
| 204 |
$revs = self::$revisions; |
| 205 |
|
| 206 |
$diffEngine = new DifferenceEngine( $this->context, $revs[1], $revs[2], 2, true, false ); |
| 207 |
$this->assertEquals( $revs[2], $diffEngine->getNewid(), 'diff get new id' ); |
| 208 |
} |
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public function testGenerateContentDiffBody( |
| 214 |
array $oldContentArgs, array $newContentArgs, $expectedDiff |
| 215 |
) { |
| 216 |
$this->mergeMwGlobalArrayValue( 'wgContentHandlers', [ |
| 217 |
'testing-nontext' => DummyNonTextContentHandler::class, |
| 218 |
] ); |
| 219 |
$oldContent = ContentHandler::makeContent( ...$oldContentArgs ); |
| 220 |
$newContent = ContentHandler::makeContent( ...$newContentArgs ); |
| 221 |
|
| 222 |
$differenceEngine = new DifferenceEngine(); |
| 223 |
$diff = $differenceEngine->generateContentDiffBody( $oldContent, $newContent ); |
| 224 |
$this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) ); |
| 225 |
} |
| 226 |
|
| 227 |
public static function provideGenerateContentDiffBody() { |
| 228 |
$content1 = [ 'xxx', null, CONTENT_MODEL_TEXT ]; |
| 229 |
$content2 = [ 'yyy', null, CONTENT_MODEL_TEXT ]; |
| 230 |
|
| 231 |
return [ |
| 232 |
'self-diff' => [ $content1, $content1, '' ], |
| 233 |
'text diff' => [ $content1, $content2, '-xxx+yyy' ], |
| 234 |
]; |
| 235 |
} |
| 236 |
|
| 237 |
public function testGenerateTextDiffBody() { |
| 238 |
$oldText = "aaa\nbbb\nccc"; |
| 239 |
$newText = "aaa\nxxx\nccc"; |
| 240 |
$expectedDiff = " aaa aaa\n-bbb+xxx\n ccc ccc"; |
| 241 |
|
| 242 |
$differenceEngine = new DifferenceEngine(); |
| 243 |
$diff = $differenceEngine->generateTextDiffBody( $oldText, $newText ); |
| 244 |
$this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) ); |
| 245 |
} |
| 246 |
|
| 247 |
public function testSetContent() { |
| 248 |
$oldContent = ContentHandler::makeContent( 'xxx', null, CONTENT_MODEL_TEXT ); |
| 249 |
$newContent = ContentHandler::makeContent( 'yyy', null, CONTENT_MODEL_TEXT ); |
| 250 |
|
| 251 |
$differenceEngine = new DifferenceEngine(); |
| 252 |
$differenceEngine->setContent( $oldContent, $newContent ); |
| 253 |
$diff = $differenceEngine->getDiffBody(); |
| 254 |
$this->assertSame( "Line 1:\nLine 1:\n-xxx+yyy", $this->getPlainDiff( $diff ) ); |
| 255 |
} |
| 256 |
|
| 257 |
public function testSetRevisions() { |
| 258 |
$rev1 = $this->getRevisionRecord( [ SlotRecord::MAIN => 'xxx' ] ); |
| 259 |
$rev2 = $this->getRevisionRecord( [ SlotRecord::MAIN => 'yyy' ] ); |
| 260 |
|
| 261 |
$differenceEngine = new DifferenceEngine(); |
| 262 |
$differenceEngine->setRevisions( $rev1, $rev2 ); |
| 263 |
$this->assertSame( $rev1, $differenceEngine->getOldRevision() ); |
| 264 |
$this->assertSame( $rev2, $differenceEngine->getNewRevision() ); |
| 265 |
$this->assertSame( true, $differenceEngine->loadRevisionData() ); |
| 266 |
$this->assertSame( true, $differenceEngine->loadText() ); |
| 267 |
|
| 268 |
$differenceEngine->setRevisions( null, $rev2 ); |
| 269 |
$this->assertSame( null, $differenceEngine->getOldRevision() ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
public function testGetDiffBody( |
| 276 |
?array $oldSlots, ?array $newSlots, $slotDiffOptions, $expectedDiff |
| 277 |
) { |
| 278 |
$oldRevision = $this->getRevisionRecord( $oldSlots ); |
| 279 |
$newRevision = $this->getRevisionRecord( $newSlots ); |
| 280 |
if ( $expectedDiff instanceof Exception ) { |
| 281 |
$this->expectException( get_class( $expectedDiff ) ); |
| 282 |
$this->expectExceptionMessage( $expectedDiff->getMessage() ); |
| 283 |
} |
| 284 |
$differenceEngine = new DifferenceEngine(); |
| 285 |
$differenceEngine->setRevisions( $oldRevision, $newRevision ); |
| 286 |
if ( $slotDiffOptions !== null ) { |
| 287 |
$differenceEngine->setSlotDiffOptions( $slotDiffOptions ); |
| 288 |
} |
| 289 |
if ( $expectedDiff instanceof Exception ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
$diff = $differenceEngine->getDiffBody(); |
| 294 |
$this->assertSame( $expectedDiff, $this->getPlainDiff( $diff ) ); |
| 295 |
} |
| 296 |
|
| 297 |
public static function provideGetDiffBody() { |
| 298 |
$main1 = [ SlotRecord::MAIN => 'xxx' ]; |
| 299 |
$main2 = [ SlotRecord::MAIN => 'yyy' ]; |
| 300 |
$slot1 = [ 'slot' => 'aaa' ]; |
| 301 |
$slot2 = [ 'slot' => 'bbb' ]; |
| 302 |
$slot3 = [ 'derivedslot' => [ 'text' => 'aaa', 'derived' => true ] ]; |
| 303 |
$slot4 = [ 'derivedslot' => [ 'text' => 'bbb', 'derived' => true ] ]; |
| 304 |
$slot5 = [ 'slot' => [ 'model' => 'testing' ] ]; |
| 305 |
|
| 306 |
return [ |
| 307 |
'revision vs. null' => [ |
| 308 |
null, |
| 309 |
$main1 + $slot1, |
| 310 |
null, |
| 311 |
'', |
| 312 |
], |
| 313 |
'revision vs. itself' => [ |
| 314 |
$main1 + $slot1, |
| 315 |
$main1 + $slot1, |
| 316 |
null, |
| 317 |
'', |
| 318 |
], |
| 319 |
'different text in one slot' => [ |
| 320 |
$main1 + $slot1, |
| 321 |
$main1 + $slot2, |
| 322 |
null, |
| 323 |
"slotLine 1:\nLine 1:\n-aaa+bbb", |
| 324 |
], |
| 325 |
'different text in two slots' => [ |
| 326 |
$main1 + $slot1, |
| 327 |
$main2 + $slot2, |
| 328 |
null, |
| 329 |
"Line 1:\nLine 1:\n-xxx+yyy\nslotLine 1:\nLine 1:\n-aaa+bbb", |
| 330 |
], |
| 331 |
'new slot' => [ |
| 332 |
$main1, |
| 333 |
$main1 + $slot1, |
| 334 |
null, |
| 335 |
"slotLine 1:\nLine 1:\n- +aaa", |
| 336 |
], |
| 337 |
'ignored difference in derived slot' => [ |
| 338 |
$main1 + $slot3, |
| 339 |
$main1 + $slot4, |
| 340 |
null, |
| 341 |
'', |
| 342 |
], |
| 343 |
'incompatible slot' => [ |
| 344 |
$main1 + $slot5, |
| 345 |
$main2 + $slot1, |
| 346 |
null, |
| 347 |
"Line 1:\nLine 1:\n-xxx+yyy\nslotCannot compare content models \"testing\" and \"plain text\"", |
| 348 |
], |
| 349 |
'invalid diff-type' => [ |
| 350 |
$main1, |
| 351 |
$main2, |
| 352 |
[ 'diff-type' => 'invalid' ], |
| 353 |
"Line 1:\nLine 1:\n-xxx+yyy", |
| 354 |
] |
| 355 |
]; |
| 356 |
} |
| 357 |
|
| 358 |
public function testRecursion() { |
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
$customDifferenceEngine = $this->getMockBuilder( DifferenceEngine::class ) |
| 364 |
->enableProxyingToOriginalMethods() |
| 365 |
->getMock(); |
| 366 |
$customContentHandler = $this->getMockBuilder( ContentHandler::class ) |
| 367 |
->setConstructorArgs( [ 'foo', [] ] ) |
| 368 |
->onlyMethods( [ 'createDifferenceEngine' ] ) |
| 369 |
->getMockForAbstractClass(); |
| 370 |
$customContentHandler->method( 'createDifferenceEngine' ) |
| 371 |
->willReturn( $customDifferenceEngine ); |
| 372 |
|
| 373 |
$customContent = $this->getMockBuilder( Content::class ) |
| 374 |
->onlyMethods( [ 'getContentHandler' ] ) |
| 375 |
->getMockForAbstractClass(); |
| 376 |
$customContent->method( 'getContentHandler' ) |
| 377 |
->willReturn( $customContentHandler ); |
| 378 |
|
| 379 |
$customContent2 = clone $customContent; |
| 380 |
|
| 381 |
$slotDiffRenderer = $customContentHandler->getSlotDiffRenderer( RequestContext::getMain() ); |
| 382 |
$this->expectException( Exception::class ); |
| 383 |
$this->expectExceptionMessage( |
| 384 |
': could not maintain backwards compatibility. Please use a SlotDiffRenderer.' |
| 385 |
); |
| 386 |
$slotDiffRenderer->getDiff( $customContent, $customContent2 ); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
public function testMarkPatrolledLink( $group, $config, $expectedResult ) { |
| 393 |
$this->setUserLang( 'qqx' ); |
| 394 |
$user = $this->getTestUser( $group )->getUser(); |
| 395 |
$this->context->setUser( $user ); |
| 396 |
if ( $config ) { |
| 397 |
$this->context->setConfig( new HashConfig( $config ) ); |
| 398 |
} |
| 399 |
|
| 400 |
$page = $this->getNonexistingTestPage( 'Page1' ); |
| 401 |
$this->assertStatusGood( $this->editPage( $page, 'Edit1' ), 'edited a page' ); |
| 402 |
$rev1 = $page->getRevisionRecord(); |
| 403 |
$this->assertStatusGood( $this->editPage( $page, 'Edit2' ), 'edited a page' ); |
| 404 |
$rev2 = $page->getRevisionRecord(); |
| 405 |
|
| 406 |
$diffEngine = new DifferenceEngine( $this->context ); |
| 407 |
$diffEngine->setRevisions( $rev1, $rev2 ); |
| 408 |
|
| 409 |
$html = $diffEngine->markPatrolledLink(); |
| 410 |
$this->assertStringContainsString( $expectedResult, $html ); |
| 411 |
} |
| 412 |
|
| 413 |
public static function provideMarkPatrolledLink() { |
| 414 |
yield 'PatrollingEnabledUserAllowed' => [ |
| 415 |
'sysop', |
| 416 |
[ MainConfigNames::UseRCPatrol => true, MainConfigNames::LanguageCode => 'qxx' ], |
| 417 |
'Mark as patrolled' |
| 418 |
]; |
| 419 |
|
| 420 |
yield 'PatrollingEnabledUserNotAllowed' => [ |
| 421 |
null, |
| 422 |
[ MainConfigNames::UseRCPatrol => true, MainConfigNames::LanguageCode => 'qxx' ], |
| 423 |
'' |
| 424 |
]; |
| 425 |
|
| 426 |
yield 'PatrollingDisabledUserAllowed' => [ |
| 427 |
'sysop', |
| 428 |
null, |
| 429 |
'' |
| 430 |
]; |
| 431 |
|
| 432 |
yield 'PatrollingDisabledUserNotAllowed' => [ |
| 433 |
null, |
| 434 |
null, |
| 435 |
'' |
| 436 |
]; |
| 437 |
} |
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
private function getPlainDiff( $diff ) { |
| 445 |
$replacements = [ |
| 446 |
html_entity_decode( ' ' ) => ' ', |
| 447 |
html_entity_decode( '−' ) => '-', |
| 448 |
]; |
| 449 |
|
| 450 |
$diff = str_replace( '<td class="diff-marker"></td>', ' ', $diff ); |
| 451 |
$diff = str_replace( '<td colspan="2"></td>', ' ', $diff ); |
| 452 |
$diff = preg_replace( '/data-marker="([^"]*)">/', '>$1', $diff ); |
| 453 |
return str_replace( array_keys( $replacements ), array_values( $replacements ), |
| 454 |
trim( strip_tags( $diff ), "\n" ) ); |
| 455 |
} |
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
private function getRevisionRecord( $slots ) { |
| 467 |
if ( $slots === null ) { |
| 468 |
return null; |
| 469 |
} |
| 470 |
|
| 471 |
$contentHandlerFactory = $this->getServiceContainer()->getContentHandlerFactory(); |
| 472 |
$title = $this->makeMockTitle( __CLASS__ ); |
| 473 |
$revision = new MutableRevisionRecord( $title ); |
| 474 |
foreach ( $slots as $role => $info ) { |
| 475 |
if ( is_string( $info ) ) { |
| 476 |
$info = [ |
| 477 |
'text' => $info, |
| 478 |
]; |
| 479 |
} |
| 480 |
$info += [ 'text' => '', 'model' => CONTENT_MODEL_TEXT ]; |
| 481 |
|
| 482 |
if ( !$contentHandlerFactory->isDefinedModel( $info['model'] ) ) { |
| 483 |
$contentHandlerFactory->defineContentHandler( |
| 484 |
$info['model'], DummyContentHandlerForTesting::class ); |
| 485 |
} |
| 486 |
|
| 487 |
$content = ContentHandler::makeContent( $info['text'], null, $info['model'] ); |
| 488 |
if ( $info['derived'] ?? false ) { |
| 489 |
$slotRecord = SlotRecord::newDerived( $role, $content ); |
| 490 |
} else { |
| 491 |
$slotRecord = SlotRecord::newUnsaved( $role, $content ); |
| 492 |
} |
| 493 |
$revision->setSlot( $slotRecord ); |
| 494 |
} |
| 495 |
return $revision; |
| 496 |
} |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
public function testRevisionHeader( $deletedFlag, $allowedAction ) { |
| 502 |
$revs = self::$revisions; |
| 503 |
|
| 504 |
if ( $deletedFlag === 'none' ) { |
| 505 |
$oldRevId = $revs[1]; |
| 506 |
} elseif ( $deletedFlag === 'deleted' ) { |
| 507 |
$oldRevId = $revs[4]; |
| 508 |
} elseif ( $deletedFlag === 'suppressed' ) { |
| 509 |
$oldRevId = $revs[5]; |
| 510 |
} |
| 511 |
|
| 512 |
$context = new DerivativeContext( $this->context ); |
| 513 |
$context->setLanguage( 'qqx' ); |
| 514 |
$permissionSet = []; |
| 515 |
if ( $allowedAction !== 'none' ) { |
| 516 |
if ( $allowedAction === 'edit' ) { |
| 517 |
$permissionSet[] = 'edit'; |
| 518 |
} |
| 519 |
if ( $deletedFlag === 'suppressed' ) { |
| 520 |
$permissionSet[] = 'suppressrevision'; |
| 521 |
} else { |
| 522 |
$permissionSet[] = 'deletedtext'; |
| 523 |
} |
| 524 |
} |
| 525 |
$context->setAuthority( |
| 526 |
new SimpleAuthority( $this->getTestUser()->getUser(), $permissionSet ) |
| 527 |
); |
| 528 |
|
| 529 |
$diffEngine = new DifferenceEngine( $context, $oldRevId, $revs[2], 2, true, true ); |
| 530 |
$this->assertTrue( $diffEngine->loadRevisionData() ); |
| 531 |
$revisionHeaderHtml = $diffEngine->getRevisionHeader( $diffEngine->getOldRevision(), 'complete' ); |
| 532 |
|
| 533 |
|
| 534 |
$this->assertStringContainsString( '(revisionasof:', $revisionHeaderHtml ); |
| 535 |
|
| 536 |
if ( $allowedAction === 'none' ) { |
| 537 |
$this->assertStringNotContainsString( 'oldid=' . $oldRevId, $revisionHeaderHtml ); |
| 538 |
} else { |
| 539 |
$this->assertStringContainsString( 'oldid=' . $oldRevId, $revisionHeaderHtml ); |
| 540 |
} |
| 541 |
if ( $allowedAction === 'edit' ) { |
| 542 |
$this->assertStringContainsString( '(editold)', $revisionHeaderHtml ); |
| 543 |
} else { |
| 544 |
$this->assertStringNotContainsString( '(editold)', $revisionHeaderHtml ); |
| 545 |
} |
| 546 |
if ( $allowedAction === 'view' ) { |
| 547 |
$this->assertStringContainsString( '(viewsourceold)', $revisionHeaderHtml ); |
| 548 |
} else { |
| 549 |
$this->assertStringNotContainsString( '(viewsourceold)', $revisionHeaderHtml ); |
| 550 |
} |
| 551 |
|
| 552 |
if ( $deletedFlag === 'none' ) { |
| 553 |
$this->assertStringNotContainsString( 'history-deleted', $revisionHeaderHtml ); |
| 554 |
} else { |
| 555 |
$this->assertStringContainsString( 'history-deleted', $revisionHeaderHtml ); |
| 556 |
} |
| 557 |
if ( $deletedFlag === 'suppressed' ) { |
| 558 |
$this->assertStringContainsString( 'mw-history-suppressed', $revisionHeaderHtml ); |
| 559 |
} else { |
| 560 |
$this->assertStringNotContainsString( 'mw-history-suppressed', $revisionHeaderHtml ); |
| 561 |
} |
| 562 |
} |
| 563 |
|
| 564 |
public static function provideRevisionHeader() { |
| 565 |
return [ |
| 566 |
[ 'none', 'view' ], |
| 567 |
[ 'none', 'edit' ], |
| 568 |
[ 'deleted', 'none' ], |
| 569 |
[ 'deleted', 'view' ], |
| 570 |
[ 'deleted', 'edit' ], |
| 571 |
[ 'suppressed', 'none' ], |
| 572 |
[ 'suppressed', 'view' ], |
| 573 |
[ 'suppressed', 'edit' ], |
| 574 |
]; |
| 575 |
} |
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
public function testShowDiffPage( $reqParams, $userRights, $expected ) { |
| 584 |
$this->expandTestArgs( [ &$reqParams, &$expected ] ); |
| 585 |
$context = new DerivativeContext( $this->context ); |
| 586 |
|
| 587 |
$authority = new SimpleAuthority( |
| 588 |
new UserIdentityValue( 1, 'User' ), |
| 589 |
$userRights |
| 590 |
); |
| 591 |
$context->setAuthority( $authority ); |
| 592 |
|
| 593 |
$request = new FauxRequest( $reqParams ); |
| 594 |
$context->setRequest( $request ); |
| 595 |
|
| 596 |
$context->setLanguage( 'qqx' ); |
| 597 |
|
| 598 |
$out = new OutputPage( $context ); |
| 599 |
$out->enableOOUI(); |
| 600 |
$context->setOutput( $out ); |
| 601 |
|
| 602 |
$engine = new DifferenceEngine( |
| 603 |
$context, |
| 604 |
$request->getIntOrNull( 'oldid' ), |
| 605 |
$request->getVal( 'diff' ), |
| 606 |
0, |
| 607 |
false, |
| 608 |
$request->getInt( 'unhide' ) === 1 |
| 609 |
); |
| 610 |
|
| 611 |
if ( isset( $expected['exception'] ) ) { |
| 612 |
$this->expectException( $expected['exception'] ); |
| 613 |
} |
| 614 |
|
| 615 |
$engine->showDiffPage( $request->getBool( 'diffonly' ) ); |
| 616 |
|
| 617 |
if ( isset( $expected['html'] ) ) { |
| 618 |
$this->assertMatchesRegularExpression( |
| 619 |
'{' . $expected['html'] . '}s', |
| 620 |
$out->getHTML(), |
| 621 |
'OutputPage::getHTML' |
| 622 |
); |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
public static function provideShowDiffPage() { |
| 627 |
$cases = [ |
| 628 |
'missing oldid' => [ |
| 629 |
'params' => [ |
| 630 |
'oldid' => '1000000', |
| 631 |
'diff' => 'prev', |
| 632 |
], |
| 633 |
'expected' => [ |
| 634 |
'html' => '\(difference-missing-revision: 1000000, 1\)', |
| 635 |
] |
| 636 |
], |
| 637 |
'missing prev' => [ |
| 638 |
'params' => [ |
| 639 |
'oldid' => 'rev[0]', |
| 640 |
'diff' => 'prev' |
| 641 |
], |
| 642 |
'expected' => [ |
| 643 |
'html' => |
| 644 |
'\(diff-empty\).*' . |
| 645 |
'<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>no kittens' |
| 646 |
], |
| 647 |
], |
| 648 |
'normal diff=prev' => [ |
| 649 |
'params' => [ |
| 650 |
'oldid' => 'rev[1]', |
| 651 |
'diff' => 'prev' |
| 652 |
], |
| 653 |
'expected' => [ |
| 654 |
'html' => |
| 655 |
'\(viewsourceold\).*' . |
| 656 |
'<del class="diffchange diffchange-inline">no kittens</del>.*' . |
| 657 |
'<ins class="diffchange diffchange-inline">one kitten</ins>.*' . |
| 658 |
'<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>one kitten', |
| 659 |
] |
| 660 |
], |
| 661 |
'normal diff=number' => [ |
| 662 |
'params' => [ |
| 663 |
'oldid' => 'rev[0]', |
| 664 |
'diff' => 'rev[1]' |
| 665 |
], |
| 666 |
'expected' => [ |
| 667 |
'html' => |
| 668 |
'\(viewsourceold\).*' . |
| 669 |
'<del class="diffchange diffchange-inline">no kittens</del>.*' . |
| 670 |
'<ins class="diffchange diffchange-inline">one kitten</ins>.*' . |
| 671 |
'<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>one kitten', |
| 672 |
] |
| 673 |
], |
| 674 |
'user cannot read' => [ |
| 675 |
'params' => [ |
| 676 |
'oldid' => 'rev[1]', |
| 677 |
'diff' => 'prev', |
| 678 |
], |
| 679 |
'userRights' => [], |
| 680 |
'expected' => [ |
| 681 |
'exception' => PermissionsError::class, |
| 682 |
] |
| 683 |
], |
| 684 |
'user can rollback' => [ |
| 685 |
'params' => [ |
| 686 |
'oldid' => 'rev[6]', |
| 687 |
'diff' => 'rev[7]', |
| 688 |
], |
| 689 |
'userRights' => [ 'read', 'edit', 'rollback' ], |
| 690 |
'expected' => [ |
| 691 |
'html' => |
| 692 |
'\(editold\).*' . |
| 693 |
'\(rollbacklinkcount: 1\)', |
| 694 |
] |
| 695 |
], |
| 696 |
'diffonly' => [ |
| 697 |
'params' => [ |
| 698 |
'oldid' => 'rev[1]', |
| 699 |
'diff' => 'prev', |
| 700 |
'diffonly' => '1', |
| 701 |
], |
| 702 |
'expected' => [ |
| 703 |
'html' => |
| 704 |
'<del class="diffchange diffchange-inline">no kittens</del>.*' . |
| 705 |
'<ins class="diffchange diffchange-inline">one kitten</ins>.*' . |
| 706 |
'</table>$', |
| 707 |
] |
| 708 |
], |
| 709 |
'deleted LHS' => [ |
| 710 |
'params' => [ |
| 711 |
'oldid' => 'rev[4]', |
| 712 |
'diff' => 'rev[1]' |
| 713 |
], |
| 714 |
'expected' => [ |
| 715 |
'html' => '<div id="mw-diff-otitle1">.*' . |
| 716 |
'<span class="history-deleted">.*' . |
| 717 |
'<div id="mw-diff-ntitle1">.*' . |
| 718 |
'\(rev-deleted-no-diff\)', |
| 719 |
] |
| 720 |
], |
| 721 |
'deleted RHS' => [ |
| 722 |
'params' => [ |
| 723 |
'oldid' => 'rev[3]', |
| 724 |
'diff' => 'rev[4]' |
| 725 |
], |
| 726 |
'expected' => [ |
| 727 |
'html' => |
| 728 |
'<div id="mw-diff-otitle1">.*' . |
| 729 |
'<div id="mw-diff-ntitle1">.*' . |
| 730 |
'<span class="history-deleted">.*' . |
| 731 |
'\(rev-deleted-no-diff\)', |
| 732 |
] |
| 733 |
], |
| 734 |
'deleted LHS can unhide' => [ |
| 735 |
'params' => [ |
| 736 |
'oldid' => 'rev[4]', |
| 737 |
'diff' => 'rev[1]' |
| 738 |
], |
| 739 |
'userRights' => [ 'read', 'deletedtext' ], |
| 740 |
'expected' => [ |
| 741 |
'html' => |
| 742 |
'<div id="mw-diff-ntitle1">.*' . |
| 743 |
'\(rev-deleted-unhide-diff:.*' . |
| 744 |
'&unhide=1.*', |
| 745 |
] |
| 746 |
], |
| 747 |
'deleted RHS with unhide' => [ |
| 748 |
'params' => [ |
| 749 |
'oldid' => 'rev[3]', |
| 750 |
'diff' => 'rev[4]', |
| 751 |
'unhide' => '1' |
| 752 |
], |
| 753 |
'userRights' => [ 'read', 'deletedtext' ], |
| 754 |
'expected' => [ |
| 755 |
'html' => |
| 756 |
'\(rev-deleted-diff-view\).*' . |
| 757 |
'<del class="diffchange diffchange-inline">three </del>.*' . |
| 758 |
'<ins class="diffchange diffchange-inline">fnord </ins>.*' . |
| 759 |
'<div class="mw-content-ltr mw-parser-output" lang="en" dir="ltr"><p>fnord kittens', |
| 760 |
] |
| 761 |
], |
| 762 |
]; |
| 763 |
|
| 764 |
foreach ( $cases as $name => $case ) { |
| 765 |
yield $name => [ |
| 766 |
$case['params'], |
| 767 |
$case['userRights'] ?? [ 'read' ], |
| 768 |
$case['expected'] |
| 769 |
]; |
| 770 |
} |
| 771 |
} |
| 772 |
} |