| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Content\CssContent; |
| 6 |
use MediaWiki\Content\WikitextContent; |
| 7 |
use MediaWiki\Storage\PageEditStash; |
| 8 |
use MediaWiki\Tests\User\TempUser\TempUserTestTrait; |
| 9 |
use MediaWiki\Title\Title; |
| 10 |
use MediaWiki\User\User; |
| 11 |
use MediaWiki\User\UserIdentity; |
| 12 |
use MediaWiki\User\UserRigorOptions; |
| 13 |
use Psr\Log\NullLogger; |
| 14 |
use stdClass; |
| 15 |
use Wikimedia\ObjectCache\HashBagOStuff; |
| 16 |
use Wikimedia\Stats\StatsFactory; |
| 17 |
use Wikimedia\TestingAccessWrapper; |
| 18 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
class ApiStashEditTest extends ApiTestCase { |
| 29 |
use TempUserTestTrait; |
| 30 |
|
| 31 |
private const CLASS_NAME = 'ApiStashEditTest'; |
| 32 |
|
| 33 |
protected function setUp(): void { |
| 34 |
parent::setUp(); |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
$this->setService( 'UserEditTracker', $this->getServiceContainer() |
| 40 |
->getUserEditTracker() ); |
| 41 |
$this->setService( 'PageEditStash', new PageEditStash( |
| 42 |
new HashBagOStuff( [] ), |
| 43 |
$this->getServiceContainer()->getConnectionProvider(), |
| 44 |
new NullLogger(), |
| 45 |
StatsFactory::newNull(), |
| 46 |
$this->getServiceContainer()->getUserEditTracker(), |
| 47 |
$this->getServiceContainer()->getUserFactory(), |
| 48 |
$this->getServiceContainer()->getWikiPageFactory(), |
| 49 |
$this->getServiceContainer()->getHookContainer(), |
| 50 |
PageEditStash::INITIATOR_USER |
| 51 |
) ); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
protected function doStash( |
| 64 |
array $params = [], ?User $user = null, $expectedResult = 'stashed' |
| 65 |
) { |
| 66 |
$params = array_merge( [ |
| 67 |
'action' => 'stashedit', |
| 68 |
'title' => self::CLASS_NAME, |
| 69 |
'contentmodel' => 'wikitext', |
| 70 |
'contentformat' => 'text/x-wiki', |
| 71 |
'baserevid' => 0, |
| 72 |
], $params ); |
| 73 |
if ( !array_key_exists( 'text', $params ) && |
| 74 |
!array_key_exists( 'stashedtexthash', $params ) |
| 75 |
) { |
| 76 |
$params['text'] = 'Content'; |
| 77 |
} |
| 78 |
foreach ( $params as $key => $val ) { |
| 79 |
if ( $val === null ) { |
| 80 |
unset( $params[$key] ); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( isset( $params['text'] ) ) { |
| 85 |
$expectedText = $params['text']; |
| 86 |
} elseif ( isset( $params['stashedtexthash'] ) ) { |
| 87 |
$expectedText = $this->getStashedText( $params['stashedtexthash'] ); |
| 88 |
} |
| 89 |
if ( isset( $expectedText ) ) { |
| 90 |
$expectedText = rtrim( str_replace( "\r\n", "\n", $expectedText ) ); |
| 91 |
$expectedHash = sha1( $expectedText ); |
| 92 |
$origText = $this->getStashedText( $expectedHash ); |
| 93 |
} |
| 94 |
|
| 95 |
$res = $this->doApiRequestWithToken( $params, null, $user ); |
| 96 |
|
| 97 |
$this->assertSame( $expectedResult, $res[0]['stashedit']['status'] ); |
| 98 |
$this->assertCount( $expectedResult === 'stashed' ? 2 : 1, $res[0]['stashedit'] ); |
| 99 |
|
| 100 |
if ( $expectedResult === 'stashed' ) { |
| 101 |
$hash = $res[0]['stashedit']['texthash']; |
| 102 |
|
| 103 |
$this->assertSame( $expectedText, $this->getStashedText( $hash ) ); |
| 104 |
|
| 105 |
$this->assertSame( $expectedHash, $hash ); |
| 106 |
|
| 107 |
if ( isset( $params['stashedtexthash'] ) ) { |
| 108 |
$this->assertSame( $expectedHash, $params['stashedtexthash'] ); |
| 109 |
} |
| 110 |
} else { |
| 111 |
$this->assertSame( $origText, $this->getStashedText( $expectedHash ) ); |
| 112 |
} |
| 113 |
|
| 114 |
$this->assertArrayNotHasKey( 'warnings', $res[0] ); |
| 115 |
|
| 116 |
return $res; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
protected function getStashedText( $hash ) { |
| 126 |
return $this->getServiceContainer()->getPageEditStash()->fetchInputText( $hash ); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
protected function getStashKey( $title = self::CLASS_NAME, $text = 'Content', ?User $user = null ) { |
| 138 |
$titleObj = Title::newFromText( $title ); |
| 139 |
$content = new WikitextContent( $text ); |
| 140 |
if ( !$user ) { |
| 141 |
$user = $this->getTestSysop()->getUser(); |
| 142 |
} |
| 143 |
$editStash = TestingAccessWrapper::newFromObject( |
| 144 |
$this->getServiceContainer()->getPageEditStash() ); |
| 145 |
|
| 146 |
return $editStash->getStashKey( $titleObj, $editStash->getContentHash( $content ), $user ); |
| 147 |
} |
| 148 |
|
| 149 |
public function testBasicEdit() { |
| 150 |
$this->doStash(); |
| 151 |
} |
| 152 |
|
| 153 |
public function testBot() { |
| 154 |
|
| 155 |
$this->expectApiErrorCode( 'botsnotsupported' ); |
| 156 |
|
| 157 |
$this->doStash( [], $this->getTestUser( [ 'bot' ] )->getUser() ); |
| 158 |
} |
| 159 |
|
| 160 |
public function testUnrecognizedFormat() { |
| 161 |
$this->expectApiErrorCode( 'badmodelformat' ); |
| 162 |
|
| 163 |
$this->doStash( [ 'contentformat' => 'application/json' ] ); |
| 164 |
} |
| 165 |
|
| 166 |
public function testMissingTextAndStashedTextHash() { |
| 167 |
$this->expectApiErrorCode( 'missingparam' ); |
| 168 |
$this->doStash( [ 'text' => null ] ); |
| 169 |
} |
| 170 |
|
| 171 |
public function testStashedTextHash() { |
| 172 |
$res = $this->doStash(); |
| 173 |
|
| 174 |
$this->doStash( [ 'stashedtexthash' => $res[0]['stashedit']['texthash'] ] ); |
| 175 |
} |
| 176 |
|
| 177 |
public function testMalformedStashedTextHash() { |
| 178 |
$this->expectApiErrorCode( 'missingtext' ); |
| 179 |
$this->doStash( [ 'stashedtexthash' => 'abc' ] ); |
| 180 |
} |
| 181 |
|
| 182 |
public function testMissingStashedTextHash() { |
| 183 |
$this->expectApiErrorCode( 'missingtext' ); |
| 184 |
$this->doStash( [ 'stashedtexthash' => str_repeat( '0', 40 ) ] ); |
| 185 |
} |
| 186 |
|
| 187 |
public function testHashNormalization() { |
| 188 |
$res1 = $this->doStash( [ 'text' => "a\r\nb\rc\nd \t\n\r" ] ); |
| 189 |
$res2 = $this->doStash( [ 'text' => "a\nb\rc\nd" ] ); |
| 190 |
|
| 191 |
$this->assertSame( $res1[0]['stashedit']['texthash'], $res2[0]['stashedit']['texthash'] ); |
| 192 |
$this->assertSame( "a\nb\rc\nd", |
| 193 |
$this->getStashedText( $res1[0]['stashedit']['texthash'] ) ); |
| 194 |
} |
| 195 |
|
| 196 |
public function testNonexistentBaseRevId() { |
| 197 |
$this->expectApiErrorCode( 'nosuchrevid' ); |
| 198 |
|
| 199 |
$name = ucfirst( __FUNCTION__ ); |
| 200 |
$this->editPage( $name, '' ); |
| 201 |
$this->doStash( [ 'title' => $name, 'baserevid' => pow( 2, 31 ) - 1 ] ); |
| 202 |
} |
| 203 |
|
| 204 |
public function testPageWithNoRevisions() { |
| 205 |
$name = ucfirst( __FUNCTION__ ); |
| 206 |
$revRecord = $this->editPage( $name, '' )->getNewRevision(); |
| 207 |
|
| 208 |
$this->expectApiErrorCode( 'missingrev' ); |
| 209 |
|
| 210 |
|
| 211 |
$this->getDb()->newUpdateQueryBuilder() |
| 212 |
->update( 'page' ) |
| 213 |
->set( [ 'page_latest' => 0 ] ) |
| 214 |
->where( [ 'page_id' => $revRecord->getPageId() ] ) |
| 215 |
->caller( __METHOD__ )->execute(); |
| 216 |
|
| 217 |
$this->doStash( [ 'title' => $name, 'baserevid' => $revRecord->getId() ] ); |
| 218 |
} |
| 219 |
|
| 220 |
public function testExistingPage() { |
| 221 |
$name = ucfirst( __FUNCTION__ ); |
| 222 |
$revRecord = $this->editPage( $name, '' )->getNewRevision(); |
| 223 |
|
| 224 |
$this->doStash( [ 'title' => $name, 'baserevid' => $revRecord->getId() ] ); |
| 225 |
} |
| 226 |
|
| 227 |
public function testInterveningEdit() { |
| 228 |
$this->markTestSkippedIfNoDiff3(); |
| 229 |
|
| 230 |
$name = ucfirst( __FUNCTION__ ); |
| 231 |
$oldRevRecord = $this->editPage( $name, "A\n\nB" )->getNewRevision(); |
| 232 |
$this->editPage( $name, "A\n\nC" ); |
| 233 |
|
| 234 |
$this->doStash( [ |
| 235 |
'title' => $name, |
| 236 |
'baserevid' => $oldRevRecord->getId(), |
| 237 |
'text' => "D\n\nB", |
| 238 |
] ); |
| 239 |
} |
| 240 |
|
| 241 |
public function testEditConflict() { |
| 242 |
$name = ucfirst( __FUNCTION__ ); |
| 243 |
$oldRevRecord = $this->editPage( $name, 'A' )->getNewRevision(); |
| 244 |
$this->editPage( $name, 'B' ); |
| 245 |
|
| 246 |
$this->doStash( [ |
| 247 |
'title' => $name, |
| 248 |
'baserevid' => $oldRevRecord->getId(), |
| 249 |
'text' => 'C', |
| 250 |
], null, 'editconflict' ); |
| 251 |
} |
| 252 |
|
| 253 |
public function testMidEditContentModelMismatch() { |
| 254 |
$name = ucfirst( __FUNCTION__ ); |
| 255 |
$title = Title::makeTitle( NS_MAIN, $name ); |
| 256 |
$content = new CssContent( 'Css' ); |
| 257 |
$performer = $this->getTestSysop()->getAuthority(); |
| 258 |
$revRecord = $this->editPage( |
| 259 |
$title, |
| 260 |
$content, |
| 261 |
'', |
| 262 |
NS_MAIN, |
| 263 |
$performer |
| 264 |
)->getNewRevision(); |
| 265 |
$this->editPage( |
| 266 |
$title, |
| 267 |
new WikitextContent( 'Text' ), |
| 268 |
'', |
| 269 |
NS_MAIN, |
| 270 |
$performer |
| 271 |
); |
| 272 |
|
| 273 |
$this->expectApiErrorCode( 'contentmodel-mismatch' ); |
| 274 |
$this->doStash( [ 'title' => $title->getPrefixedText(), 'baserevid' => $revRecord->getId() ] ); |
| 275 |
} |
| 276 |
|
| 277 |
public function testDeletedRevision() { |
| 278 |
$name = ucfirst( __FUNCTION__ ); |
| 279 |
$oldRevRecord = $this->editPage( $name, 'A' )->getNewRevision(); |
| 280 |
$this->editPage( $name, 'B' ); |
| 281 |
|
| 282 |
$this->expectApiErrorCode( 'missingrev' ); |
| 283 |
|
| 284 |
$this->revisionDelete( $oldRevRecord ); |
| 285 |
|
| 286 |
$this->doStash( [ |
| 287 |
'title' => $name, |
| 288 |
'baserevid' => $oldRevRecord->getId(), |
| 289 |
'text' => 'C', |
| 290 |
] ); |
| 291 |
} |
| 292 |
|
| 293 |
public function testDeletedRevisionSection() { |
| 294 |
$name = ucfirst( __FUNCTION__ ); |
| 295 |
$oldRevRecord = $this->editPage( $name, 'A' )->getNewRevision(); |
| 296 |
$this->editPage( $name, 'B' ); |
| 297 |
|
| 298 |
$this->expectApiErrorCode( 'replacefailed' ); |
| 299 |
|
| 300 |
$this->revisionDelete( $oldRevRecord ); |
| 301 |
|
| 302 |
$this->doStash( [ |
| 303 |
'title' => $name, |
| 304 |
'baserevid' => $oldRevRecord->getId(), |
| 305 |
'text' => 'C', |
| 306 |
'section' => '1', |
| 307 |
] ); |
| 308 |
} |
| 309 |
|
| 310 |
public function testPingLimiter() { |
| 311 |
$this->mergeMwGlobalArrayValue( 'wgRateLimits', |
| 312 |
[ 'stashedit' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] ); |
| 313 |
|
| 314 |
$this->doStash( [ 'text' => 'A' ] ); |
| 315 |
|
| 316 |
$this->doStash( [ 'text' => 'B' ], null, 'ratelimited' ); |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
protected function doCheckCache( UserIdentity $user, $text = 'Content' ) { |
| 328 |
return $this->getServiceContainer()->getPageEditStash()->checkCache( |
| 329 |
Title::makeTitle( NS_MAIN, 'ApiStashEditTest' ), |
| 330 |
new WikitextContent( $text ), |
| 331 |
$user |
| 332 |
); |
| 333 |
} |
| 334 |
|
| 335 |
public function testCheckCache() { |
| 336 |
$user = $this->getMutableTestUser()->getUser(); |
| 337 |
$permissionManager = $this->getServiceContainer()->getPermissionManager(); |
| 338 |
$userGroupManager = $this->getServiceContainer()->getUserGroupManager(); |
| 339 |
|
| 340 |
$this->doStash( [], $user ); |
| 341 |
|
| 342 |
$this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) ); |
| 343 |
|
| 344 |
|
| 345 |
$this->assertFalse( |
| 346 |
$this->doCheckCache( $this->getTestUser()->getUser() ), |
| 347 |
'Cache is user-specific' |
| 348 |
); |
| 349 |
|
| 350 |
|
| 351 |
$userGroupManager->addUserToGroup( $user, 'bot' ); |
| 352 |
$permissionManager->invalidateUsersRightsCache(); |
| 353 |
$this->assertFalse( |
| 354 |
$this->doCheckCache( $user ), |
| 355 |
"We assume bots don't have cache entries" |
| 356 |
); |
| 357 |
|
| 358 |
|
| 359 |
$userGroupManager->removeUserFromGroup( $user, 'bot' ); |
| 360 |
$userGroupManager->addUserToGroup( $user, 'sysop' ); |
| 361 |
$permissionManager->invalidateUsersRightsCache(); |
| 362 |
$this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) ); |
| 363 |
} |
| 364 |
|
| 365 |
public function testCheckCacheAnon() { |
| 366 |
$this->disableAutoCreateTempUser(); |
| 367 |
$user = $this->getServiceContainer()->getUserFactory()->newFromName( '174.5.4.6', UserRigorOptions::RIGOR_NONE ); |
| 368 |
|
| 369 |
$this->doStash( [], $user ); |
| 370 |
|
| 371 |
$this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) ); |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
protected function doStashOld( |
| 382 |
User $user, $text = 'Content', $howOld = PageEditStash::PRESUME_FRESH_TTL_SEC |
| 383 |
) { |
| 384 |
ConvertibleTimestamp::setFakeTime( ConvertibleTimestamp::now( TS_UNIX ) - $howOld - 1 ); |
| 385 |
$this->doStash( [ 'text' => $text ], $user ); |
| 386 |
} |
| 387 |
|
| 388 |
public function testCheckCacheOldNoEdits() { |
| 389 |
$user = $this->getTestSysop()->getUser(); |
| 390 |
|
| 391 |
$this->doStashOld( $user ); |
| 392 |
|
| 393 |
|
| 394 |
$this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) ); |
| 395 |
} |
| 396 |
|
| 397 |
public function testCheckCacheOldNoEditsAnon() { |
| 398 |
$this->disableAutoCreateTempUser(); |
| 399 |
|
| 400 |
$user = $this->getServiceContainer()->getUserFactory()->newFromName( '172.0.2.77', UserRigorOptions::RIGOR_NONE ); |
| 401 |
|
| 402 |
$this->doStashOld( $user ); |
| 403 |
|
| 404 |
|
| 405 |
$this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) ); |
| 406 |
} |
| 407 |
|
| 408 |
public function testCheckCacheInterveningEdits() { |
| 409 |
$user = $this->getTestSysop()->getUser(); |
| 410 |
|
| 411 |
$this->doStashOld( $user ); |
| 412 |
|
| 413 |
|
| 414 |
$this->editPage( ucfirst( __FUNCTION__ ), '', '', NS_MAIN, $user ); |
| 415 |
|
| 416 |
$user->clearInstanceCache(); |
| 417 |
$this->assertFalse( $this->doCheckCache( $user ), |
| 418 |
"Cache should be invalidated when it's old and the user has an intervening edit" ); |
| 419 |
} |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
public function testSignatureTtl( $text, $ttl ) { |
| 427 |
$this->doStash( [ 'text' => $text ] ); |
| 428 |
|
| 429 |
$editStash = TestingAccessWrapper::newFromObject( |
| 430 |
$this->getServiceContainer()->getPageEditStash() ); |
| 431 |
$cache = $editStash->cache; |
| 432 |
$key = $this->getStashKey( self::CLASS_NAME, $text ); |
| 433 |
|
| 434 |
$wrapper = TestingAccessWrapper::newFromObject( $cache ); |
| 435 |
|
| 436 |
$this->assertEqualsWithDelta( $ttl, $wrapper->bag[$key][HashBagOStuff::KEY_EXP] - time(), 1 ); |
| 437 |
} |
| 438 |
|
| 439 |
public function signatureProvider() { |
| 440 |
return [ |
| 441 |
'~~~' => [ '~~~', PageEditStash::MAX_SIGNATURE_TTL ], |
| 442 |
'~~~~' => [ '~~~~', PageEditStash::MAX_SIGNATURE_TTL ], |
| 443 |
'~~~~~' => [ '~~~~~', PageEditStash::MAX_SIGNATURE_TTL ], |
| 444 |
]; |
| 445 |
} |
| 446 |
|
| 447 |
public function testIsInternal() { |
| 448 |
$res = $this->doApiRequest( [ |
| 449 |
'action' => 'paraminfo', |
| 450 |
'modules' => 'stashedit', |
| 451 |
] ); |
| 452 |
|
| 453 |
$this->assertCount( 1, $res[0]['paraminfo']['modules'] ); |
| 454 |
$this->assertSame( true, $res[0]['paraminfo']['modules'][0]['internal'] ); |
| 455 |
} |
| 456 |
|
| 457 |
public function testBusy() { |
| 458 |
|
| 459 |
|
| 460 |
$this->markTestSkipped(); |
| 461 |
|
| 462 |
$key = $this->getStashKey(); |
| 463 |
$this->getDb()->lock( $key, __METHOD__, 0 ); |
| 464 |
try { |
| 465 |
$this->doStash( [], null, 'busy' ); |
| 466 |
} finally { |
| 467 |
$this->getDb()->unlock( $key, __METHOD__ ); |
| 468 |
} |
| 469 |
} |
| 470 |
} |