3 use Wikimedia\TestingAccessWrapper
;
11 class ApiStashEditTest
extends ApiTestCase
{
12 public function setUp() {
15 // We need caching here, but note that the cache gets cleared in between tests, so it
16 // doesn't work with @depends
17 $this->setMwGlobals( 'wgMainCacheType', 'hash' );
21 * Make a stashedit API call with suitable default parameters
23 * @param array $params Query parameters for API request. All are optional and will have
24 * sensible defaults filled in. To make a parameter actually not passed, set to null.
25 * @param User $user User to do the request
26 * @param string $expectedResult 'stashed', 'editconflict'
28 protected function doStash(
29 array $params = [], User
$user = null, $expectedResult = 'stashed'
31 $params = array_merge( [
32 'action' => 'stashedit',
34 'contentmodel' => 'wikitext',
35 'contentformat' => 'text/x-wiki',
38 if ( !array_key_exists( 'text', $params ) &&
39 !array_key_exists( 'stashedtexthash', $params )
41 $params['text'] = 'Content';
43 foreach ( $params as $key => $val ) {
44 if ( $val === null ) {
45 unset( $params[$key] );
49 if ( isset( $params['text'] ) ) {
50 $expectedText = $params['text'];
51 } elseif ( isset( $params['stashedtexthash'] ) ) {
52 $expectedText = $this->getStashedText( $params['stashedtexthash'] );
54 if ( isset( $expectedText ) ) {
55 $expectedText = rtrim( str_replace( "\r\n", "\n", $expectedText ) );
56 $expectedHash = sha1( $expectedText );
57 $origText = $this->getStashedText( $expectedHash );
60 $res = $this->doApiRequestWithToken( $params, null, $user );
62 $this->assertSame( $expectedResult, $res[0]['stashedit']['status'] );
63 $this->assertCount( $expectedResult === 'stashed' ?
2 : 1, $res[0]['stashedit'] );
65 if ( $expectedResult === 'stashed' ) {
66 $hash = $res[0]['stashedit']['texthash'];
68 $this->assertSame( $expectedText, $this->getStashedText( $hash ) );
70 $this->assertSame( $expectedHash, $hash );
72 if ( isset( $params['stashedtexthash'] ) ) {
73 $this->assertSame( $params['stashedtexthash'], $expectedHash, 'Sanity' );
76 $this->assertSame( $origText, $this->getStashedText( $expectedHash ) );
79 $this->assertArrayNotHasKey( 'warnings', $res[0] );
85 * Return the text stashed for $hash.
90 protected function getStashedText( $hash ) {
91 $cache = ObjectCache
::getLocalClusterInstance();
92 $key = $cache->makeKey( 'stashedit', 'text', $hash );
93 return $cache->get( $key );
97 * Return a key that can be passed to the cache to obtain a PreparedEdit object.
99 * @param string $title Title of page
100 * @param string Content $text Content of edit
101 * @param User $user User who made edit
104 protected function getStashKey( $title = __CLASS__
, $text = 'Content', User
$user = null ) {
105 $titleObj = Title
::newFromText( $title );
106 $content = new WikitextContent( $text );
108 $user = $this->getTestSysop()->getUser();
110 $wrapper = TestingAccessWrapper
::newFromClass( ApiStashEdit
::class );
111 return $wrapper->getStashKey( $titleObj, $wrapper->getContentHash( $content ), $user );
114 public function testBasicEdit() {
118 public function testBot() {
119 // @todo This restriction seems arbitrary, is there any good reason to keep it?
120 $this->setExpectedApiException( 'apierror-botsnotsupported' );
122 $this->doStash( [], $this->getTestUser( [ 'bot' ] )->getUser() );
125 public function testUnrecognizedFormat() {
126 $this->setExpectedApiException(
127 [ 'apierror-badformat-generic', 'application/json', 'wikitext' ] );
129 $this->doStash( [ 'contentformat' => 'application/json' ] );
132 public function testMissingTextAndStashedTextHash() {
133 $this->setExpectedApiException( [
134 'apierror-missingparam-one-of',
135 Message
::listParam( [ '<var>stashedtexthash</var>', '<var>text</var>' ] ),
138 $this->doStash( [ 'text' => null ] );
141 public function testStashedTextHash() {
142 $res = $this->doStash();
144 $this->doStash( [ 'stashedtexthash' => $res[0]['stashedit']['texthash'] ] );
147 public function testMalformedStashedTextHash() {
148 $this->setExpectedApiException( 'apierror-stashedit-missingtext' );
149 $this->doStash( [ 'stashedtexthash' => 'abc' ] );
152 public function testMissingStashedTextHash() {
153 $this->setExpectedApiException( 'apierror-stashedit-missingtext' );
154 $this->doStash( [ 'stashedtexthash' => str_repeat( '0', 40 ) ] );
157 public function testHashNormalization() {
158 $res1 = $this->doStash( [ 'text' => "a\r\nb\rc\nd \t\n\r" ] );
159 $res2 = $this->doStash( [ 'text' => "a\nb\rc\nd" ] );
161 $this->assertSame( $res1[0]['stashedit']['texthash'], $res2[0]['stashedit']['texthash'] );
162 $this->assertSame( "a\nb\rc\nd",
163 $this->getStashedText( $res1[0]['stashedit']['texthash'] ) );
166 public function testNonexistentBaseRevId() {
167 $this->setExpectedApiException( [ 'apierror-nosuchrevid', pow( 2, 31 ) - 1 ] );
169 $name = ucfirst( __FUNCTION__
);
170 $this->editPage( $name, '' );
171 $this->doStash( [ 'title' => $name, 'baserevid' => pow( 2, 31 ) - 1 ] );
174 public function testPageWithNoRevisions() {
175 $name = ucfirst( __FUNCTION__
);
176 $rev = $this->editPage( $name, '' )->value
['revision'];
178 $this->setExpectedApiException( [ 'apierror-missingrev-pageid', $rev->getPage() ] );
180 // Corrupt the database. @todo Does the API really need to fail gracefully for this case?
181 $dbw = wfGetDB( DB_MASTER
);
184 [ 'page_latest' => 0 ],
185 [ 'page_id' => $rev->getPage() ],
189 $this->doStash( [ 'title' => $name, 'baserevid' => $rev->getId() ] );
192 public function testExistingPage() {
193 $name = ucfirst( __FUNCTION__
);
194 $rev = $this->editPage( $name, '' )->value
['revision'];
196 $this->doStash( [ 'title' => $name, 'baserevid' => $rev->getId() ] );
199 public function testInterveningEdit() {
200 $name = ucfirst( __FUNCTION__
);
201 $oldRev = $this->editPage( $name, "A\n\nB" )->value
['revision'];
202 $this->editPage( $name, "A\n\nC" );
206 'baserevid' => $oldRev->getId(),
211 public function testEditConflict() {
212 $name = ucfirst( __FUNCTION__
);
213 $oldRev = $this->editPage( $name, 'A' )->value
['revision'];
214 $this->editPage( $name, 'B' );
218 'baserevid' => $oldRev->getId(),
220 ], null, 'editconflict' );
223 public function testDeletedRevision() {
224 $name = ucfirst( __FUNCTION__
);
225 $oldRev = $this->editPage( $name, 'A' )->value
['revision'];
226 $this->editPage( $name, 'B' );
228 $this->setExpectedApiException( [ 'apierror-missingcontent-pageid', $oldRev->getPage() ] );
230 $this->revisionDelete( $oldRev );
234 'baserevid' => $oldRev->getId(),
239 public function testDeletedRevisionSection() {
240 $name = ucfirst( __FUNCTION__
);
241 $oldRev = $this->editPage( $name, 'A' )->value
['revision'];
242 $this->editPage( $name, 'B' );
244 $this->setExpectedApiException( 'apierror-sectionreplacefailed' );
246 $this->revisionDelete( $oldRev );
250 'baserevid' => $oldRev->getId(),
256 public function testPingLimiter() {
257 $this->mergeMwGlobalArrayValue( 'wgRateLimits',
258 [ 'stashedit' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
260 $this->doStash( [ 'text' => 'A' ] );
262 $this->doStash( [ 'text' => 'B' ], null, 'ratelimited' );
266 * Shortcut for calling ApiStashEdit::checkCache() without having to create Titles and Contents
270 * @param string $text The text of the article
271 * @return stdClass|bool Return value of ApiStashEdit::checkCache(), false if not in cache
273 protected function doCheckCache( User
$user, $text = 'Content' ) {
274 return ApiStashEdit
::checkCache(
275 Title
::newFromText( __CLASS__
),
276 new WikitextContent( $text ),
281 public function testCheckCache() {
282 $user = $this->getMutableTestUser()->getUser();
284 $this->doStash( [], $user );
286 $this->assertInstanceOf( stdClass
::class, $this->doCheckCache( $user ) );
288 // Another user doesn't see the cache
290 $this->doCheckCache( $this->getTestUser()->getUser() ),
291 'Cache is user-specific'
294 // Nor does the original one if they become a bot
295 $user->addGroup( 'bot' );
297 $this->doCheckCache( $user ),
298 "We assume bots don't have cache entries"
301 // But other groups are okay
302 $user->removeGroup( 'bot' );
303 $user->addGroup( 'sysop' );
304 $this->assertInstanceOf( stdClass
::class, $this->doCheckCache( $user ) );
307 public function testCheckCacheAnon() {
310 $this->doStash( [], $user );
312 $this->assertInstanceOf( stdClass
::class, $this->docheckCache( $user ) );
316 * Stash an edit some time in the past, for testing expiry and freshness logic.
318 * @param User $user Who's doing the editing
319 * @param string $text What text should be cached
320 * @param int $howOld How many seconds is "old" (we actually set it one second before this)
322 protected function doStashOld(
323 User
$user, $text = 'Content', $howOld = ApiStashEdit
::PRESUME_FRESH_TTL_SEC
325 $this->doStash( [ 'text' => $text ], $user );
327 // Monkey with the cache to make the edit look old. @todo Is there a less fragile way to
329 $key = $this->getStashKey( __CLASS__
, $text, $user );
331 $cache = ObjectCache
::getLocalClusterInstance();
333 $editInfo = $cache->get( $key );
334 $editInfo->output
->setCacheTime( wfTimestamp( TS_MW
,
335 wfTimestamp( TS_UNIX
, $editInfo->output
->getCacheTime() ) - $howOld - 1 ) );
337 $cache->set( $key, $editInfo );
340 public function testCheckCacheOldNoEdits() {
341 $user = $this->getTestSysop()->getUser();
343 $this->doStashOld( $user );
345 // Should still be good, because no intervening edits
346 $this->assertInstanceOf( stdClass
::class, $this->doCheckCache( $user ) );
349 public function testCheckCacheOldNoEditsAnon() {
350 // Specify a made-up IP address to make sure no edits are lying around
351 $user = User
::newFromName( '192.0.2.77', false );
353 $this->doStashOld( $user );
355 // Should still be good, because no intervening edits
356 $this->assertInstanceOf( stdClass
::class, $this->doCheckCache( $user ) );
359 public function testCheckCacheInterveningEdits() {
360 $user = $this->getTestSysop()->getUser();
362 $this->doStashOld( $user );
364 // Now let's also increment our editcount
365 $this->editPage( ucfirst( __FUNCTION__
), '' );
367 $this->assertFalse( $this->doCheckCache( $user ),
368 "Cache should be invalidated when it's old and the user has an intervening edit" );
372 * @dataProvider signatureProvider
373 * @param string $text Which signature to test (~~~, ~~~~, or ~~~~~)
374 * @param int $ttl Expected TTL in seconds
376 public function testSignatureTtl( $text, $ttl ) {
377 $this->doStash( [ 'text' => $text ] );
379 $cache = ObjectCache
::getLocalClusterInstance();
380 $key = $this->getStashKey( __CLASS__
, $text );
382 $wrapper = TestingAccessWrapper
::newFromObject( $cache );
384 $this->assertEquals( $ttl, $wrapper->bag
[$key][HashBagOStuff
::KEY_EXP
] - time(), '', 1 );
387 public function signatureProvider() {
389 '~~~' => [ '~~~', ApiStashEdit
::MAX_SIGNATURE_TTL
],
390 '~~~~' => [ '~~~~', ApiStashEdit
::MAX_SIGNATURE_TTL
],
391 '~~~~~' => [ '~~~~~', ApiStashEdit
::MAX_SIGNATURE_TTL
],
395 public function testIsInternal() {
396 $res = $this->doApiRequest( [
397 'action' => 'paraminfo',
398 'modules' => 'stashedit',
401 $this->assertCount( 1, $res[0]['paraminfo']['modules'] );
402 $this->assertSame( true, $res[0]['paraminfo']['modules'][0]['internal'] );
405 public function testBusy() {
406 // @todo This doesn't work because both lock acquisitions are in the same MySQL session, so
407 // they don't conflict. How do I open a different session?
408 $this->markTestSkipped();
410 $key = $this->getStashKey();
411 $this->db
->lock( $key, __METHOD__
, 0 );
413 $this->doStash( [], null, 'busy' );
415 $this->db
->unlock( $key, __METHOD__
);