Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiStashEditTest.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Storage\PageEditStash;
5 use Wikimedia\TestingAccessWrapper;
6 use Psr\Log\NullLogger;
7
8 /**
9 * @covers ApiStashEdit
10 * @covers \MediaWiki\Storage\PageEditStash
11 * @group API
12 * @group medium
13 * @group Database
14 */
15 class ApiStashEditTest extends ApiTestCase {
16 public function setUp() {
17 parent::setUp();
18 $this->setService( 'PageEditStash', new PageEditStash(
19 new HashBagOStuff( [] ),
20 MediaWikiServices::getInstance()->getDBLoadBalancer(),
21 new NullLogger(),
22 new NullStatsdDataFactory(),
23 PageEditStash::INITIATOR_USER
24 ) );
25 // Clear rate-limiting cache between tests
26 $this->setMwGlobals( 'wgMainCacheType', 'hash' );
27 }
28
29 public function tearDown() {
30 parent::tearDown();
31 }
32
33 /**
34 * Make a stashedit API call with suitable default parameters
35 *
36 * @param array $params Query parameters for API request. All are optional and will have
37 * sensible defaults filled in. To make a parameter actually not passed, set to null.
38 * @param User $user User to do the request
39 * @param string $expectedResult 'stashed', 'editconflict'
40 * @return array
41 */
42 protected function doStash(
43 array $params = [], User $user = null, $expectedResult = 'stashed'
44 ) {
45 $params = array_merge( [
46 'action' => 'stashedit',
47 'title' => __CLASS__,
48 'contentmodel' => 'wikitext',
49 'contentformat' => 'text/x-wiki',
50 'baserevid' => 0,
51 ], $params );
52 if ( !array_key_exists( 'text', $params ) &&
53 !array_key_exists( 'stashedtexthash', $params )
54 ) {
55 $params['text'] = 'Content';
56 }
57 foreach ( $params as $key => $val ) {
58 if ( $val === null ) {
59 unset( $params[$key] );
60 }
61 }
62
63 if ( isset( $params['text'] ) ) {
64 $expectedText = $params['text'];
65 } elseif ( isset( $params['stashedtexthash'] ) ) {
66 $expectedText = $this->getStashedText( $params['stashedtexthash'] );
67 }
68 if ( isset( $expectedText ) ) {
69 $expectedText = rtrim( str_replace( "\r\n", "\n", $expectedText ) );
70 $expectedHash = sha1( $expectedText );
71 $origText = $this->getStashedText( $expectedHash );
72 }
73
74 $res = $this->doApiRequestWithToken( $params, null, $user );
75
76 $this->assertSame( $expectedResult, $res[0]['stashedit']['status'] );
77 $this->assertCount( $expectedResult === 'stashed' ? 2 : 1, $res[0]['stashedit'] );
78
79 if ( $expectedResult === 'stashed' ) {
80 $hash = $res[0]['stashedit']['texthash'];
81
82 $this->assertSame( $expectedText, $this->getStashedText( $hash ) );
83
84 $this->assertSame( $expectedHash, $hash );
85
86 if ( isset( $params['stashedtexthash'] ) ) {
87 $this->assertSame( $params['stashedtexthash'], $expectedHash, 'Sanity' );
88 }
89 } else {
90 $this->assertSame( $origText, $this->getStashedText( $expectedHash ) );
91 }
92
93 $this->assertArrayNotHasKey( 'warnings', $res[0] );
94
95 return $res;
96 }
97
98 /**
99 * Return the text stashed for $hash.
100 *
101 * @param string $hash
102 * @return string
103 */
104 protected function getStashedText( $hash ) {
105 return MediaWikiServices::getInstance()->getPageEditStash()->fetchInputText( $hash );
106 }
107
108 /**
109 * Return a key that can be passed to the cache to obtain a stashed edit object.
110 *
111 * @param string $title Title of page
112 * @param string Content $text Content of edit
113 * @param User $user User who made edit
114 * @return string
115 */
116 protected function getStashKey( $title = __CLASS__, $text = 'Content', User $user = null ) {
117 $titleObj = Title::newFromText( $title );
118 $content = new WikitextContent( $text );
119 if ( !$user ) {
120 $user = $this->getTestSysop()->getUser();
121 }
122 $editStash = TestingAccessWrapper::newFromObject(
123 MediaWikiServices::getInstance()->getPageEditStash() );
124
125 return $editStash->getStashKey( $titleObj, $editStash->getContentHash( $content ), $user );
126 }
127
128 public function testBasicEdit() {
129 $this->doStash();
130 }
131
132 public function testBot() {
133 // @todo This restriction seems arbitrary, is there any good reason to keep it?
134 $this->setExpectedApiException( 'apierror-botsnotsupported' );
135
136 $this->doStash( [], $this->getTestUser( [ 'bot' ] )->getUser() );
137 }
138
139 public function testUnrecognizedFormat() {
140 $this->setExpectedApiException(
141 [ 'apierror-badformat-generic', 'application/json', 'wikitext' ] );
142
143 $this->doStash( [ 'contentformat' => 'application/json' ] );
144 }
145
146 public function testMissingTextAndStashedTextHash() {
147 $this->setExpectedApiException( [
148 'apierror-missingparam-one-of',
149 Message::listParam( [ '<var>stashedtexthash</var>', '<var>text</var>' ] ),
150 2
151 ] );
152 $this->doStash( [ 'text' => null ] );
153 }
154
155 public function testStashedTextHash() {
156 $res = $this->doStash();
157
158 $this->doStash( [ 'stashedtexthash' => $res[0]['stashedit']['texthash'] ] );
159 }
160
161 public function testMalformedStashedTextHash() {
162 $this->setExpectedApiException( 'apierror-stashedit-missingtext' );
163 $this->doStash( [ 'stashedtexthash' => 'abc' ] );
164 }
165
166 public function testMissingStashedTextHash() {
167 $this->setExpectedApiException( 'apierror-stashedit-missingtext' );
168 $this->doStash( [ 'stashedtexthash' => str_repeat( '0', 40 ) ] );
169 }
170
171 public function testHashNormalization() {
172 $res1 = $this->doStash( [ 'text' => "a\r\nb\rc\nd \t\n\r" ] );
173 $res2 = $this->doStash( [ 'text' => "a\nb\rc\nd" ] );
174
175 $this->assertSame( $res1[0]['stashedit']['texthash'], $res2[0]['stashedit']['texthash'] );
176 $this->assertSame( "a\nb\rc\nd",
177 $this->getStashedText( $res1[0]['stashedit']['texthash'] ) );
178 }
179
180 public function testNonexistentBaseRevId() {
181 $this->setExpectedApiException( [ 'apierror-nosuchrevid', pow( 2, 31 ) - 1 ] );
182
183 $name = ucfirst( __FUNCTION__ );
184 $this->editPage( $name, '' );
185 $this->doStash( [ 'title' => $name, 'baserevid' => pow( 2, 31 ) - 1 ] );
186 }
187
188 public function testPageWithNoRevisions() {
189 $name = ucfirst( __FUNCTION__ );
190 $rev = $this->editPage( $name, '' )->value['revision'];
191
192 $this->setExpectedApiException( [ 'apierror-missingrev-pageid', $rev->getPage() ] );
193
194 // Corrupt the database. @todo Does the API really need to fail gracefully for this case?
195 $dbw = wfGetDB( DB_MASTER );
196 $dbw->update(
197 'page',
198 [ 'page_latest' => 0 ],
199 [ 'page_id' => $rev->getPage() ],
200 __METHOD__
201 );
202
203 $this->doStash( [ 'title' => $name, 'baserevid' => $rev->getId() ] );
204 }
205
206 public function testExistingPage() {
207 $name = ucfirst( __FUNCTION__ );
208 $rev = $this->editPage( $name, '' )->value['revision'];
209
210 $this->doStash( [ 'title' => $name, 'baserevid' => $rev->getId() ] );
211 }
212
213 public function testInterveningEdit() {
214 $name = ucfirst( __FUNCTION__ );
215 $oldRev = $this->editPage( $name, "A\n\nB" )->value['revision'];
216 $this->editPage( $name, "A\n\nC" );
217
218 $this->doStash( [
219 'title' => $name,
220 'baserevid' => $oldRev->getId(),
221 'text' => "D\n\nB",
222 ] );
223 }
224
225 public function testEditConflict() {
226 $name = ucfirst( __FUNCTION__ );
227 $oldRev = $this->editPage( $name, 'A' )->value['revision'];
228 $this->editPage( $name, 'B' );
229
230 $this->doStash( [
231 'title' => $name,
232 'baserevid' => $oldRev->getId(),
233 'text' => 'C',
234 ], null, 'editconflict' );
235 }
236
237 public function testDeletedRevision() {
238 $name = ucfirst( __FUNCTION__ );
239 $oldRev = $this->editPage( $name, 'A' )->value['revision'];
240 $this->editPage( $name, 'B' );
241
242 $this->setExpectedApiException( [ 'apierror-missingcontent-pageid', $oldRev->getPage() ] );
243
244 $this->revisionDelete( $oldRev );
245
246 $this->doStash( [
247 'title' => $name,
248 'baserevid' => $oldRev->getId(),
249 'text' => 'C',
250 ] );
251 }
252
253 public function testDeletedRevisionSection() {
254 $name = ucfirst( __FUNCTION__ );
255 $oldRev = $this->editPage( $name, 'A' )->value['revision'];
256 $this->editPage( $name, 'B' );
257
258 $this->setExpectedApiException( 'apierror-sectionreplacefailed' );
259
260 $this->revisionDelete( $oldRev );
261
262 $this->doStash( [
263 'title' => $name,
264 'baserevid' => $oldRev->getId(),
265 'text' => 'C',
266 'section' => '1',
267 ] );
268 }
269
270 public function testPingLimiter() {
271 $this->mergeMwGlobalArrayValue( 'wgRateLimits',
272 [ 'stashedit' => [ '&can-bypass' => false, 'user' => [ 1, 60 ] ] ] );
273
274 $this->doStash( [ 'text' => 'A' ] );
275
276 $this->doStash( [ 'text' => 'B' ], null, 'ratelimited' );
277 }
278
279 /**
280 * Shortcut for calling PageStashEdit::checkCache() without
281 * having to create Titles and Contents in every test.
282 *
283 * @param User $user
284 * @param string $text The text of the article
285 * @return stdClass|bool Return value of PageStashEdit::checkCache(), false if not in cache
286 */
287 protected function doCheckCache( User $user, $text = 'Content' ) {
288 return MediaWikiServices::getInstance()->getPageEditStash()->checkCache(
289 Title::newFromText( __CLASS__ ),
290 new WikitextContent( $text ),
291 $user
292 );
293 }
294
295 public function testCheckCache() {
296 $user = $this->getMutableTestUser()->getUser();
297
298 $this->doStash( [], $user );
299
300 $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
301
302 // Another user doesn't see the cache
303 $this->assertFalse(
304 $this->doCheckCache( $this->getTestUser()->getUser() ),
305 'Cache is user-specific'
306 );
307
308 // Nor does the original one if they become a bot
309 $user->addGroup( 'bot' );
310 MediaWikiServices::getInstance()->getPermissionManager()->invalidateUsersRightsCache();
311 $this->assertFalse(
312 $this->doCheckCache( $user ),
313 "We assume bots don't have cache entries"
314 );
315
316 // But other groups are okay
317 $user->removeGroup( 'bot' );
318 $user->addGroup( 'sysop' );
319 MediaWikiServices::getInstance()->getPermissionManager()->invalidateUsersRightsCache();
320 $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
321 }
322
323 public function testCheckCacheAnon() {
324 $user = User::newFromName( '174.5.4.6', false );
325
326 $this->doStash( [], $user );
327
328 $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
329 }
330
331 /**
332 * Stash an edit some time in the past, for testing expiry and freshness logic.
333 *
334 * @param User $user Who's doing the editing
335 * @param string $text What text should be cached
336 * @param int $howOld How many seconds is "old" (we actually set it one second before this)
337 */
338 protected function doStashOld(
339 User $user, $text = 'Content', $howOld = PageEditStash::PRESUME_FRESH_TTL_SEC
340 ) {
341 $this->doStash( [ 'text' => $text ], $user );
342
343 // Monkey with the cache to make the edit look old. @todo Is there a less fragile way to
344 // fake the time?
345 $key = $this->getStashKey( __CLASS__, $text, $user );
346
347 $editStash = TestingAccessWrapper::newFromObject(
348 MediaWikiServices::getInstance()->getPageEditStash() );
349 $cache = $editStash->cache;
350
351 $editInfo = $cache->get( $key );
352 $editInfo->output->setCacheTime( wfTimestamp( TS_MW,
353 wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() ) - $howOld - 1 ) );
354
355 $cache->set( $key, $editInfo );
356 }
357
358 public function testCheckCacheOldNoEdits() {
359 $user = $this->getTestSysop()->getUser();
360
361 $this->doStashOld( $user );
362
363 // Should still be good, because no intervening edits
364 $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
365 }
366
367 public function testCheckCacheOldNoEditsAnon() {
368 // Specify a made-up IP address to make sure no edits are lying around
369 $user = User::newFromName( '172.0.2.77', false );
370
371 $this->doStashOld( $user );
372
373 // Should still be good, because no intervening edits
374 $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
375 }
376
377 public function testCheckCacheInterveningEdits() {
378 $user = $this->getTestSysop()->getUser();
379
380 $this->doStashOld( $user );
381
382 // Now let's also increment our editcount
383 $this->editPage( ucfirst( __FUNCTION__ ), '' );
384
385 $user->clearInstanceCache();
386 $this->assertFalse( $this->doCheckCache( $user ),
387 "Cache should be invalidated when it's old and the user has an intervening edit" );
388 }
389
390 /**
391 * @dataProvider signatureProvider
392 * @param string $text Which signature to test (~~~, ~~~~, or ~~~~~)
393 * @param int $ttl Expected TTL in seconds
394 */
395 public function testSignatureTtl( $text, $ttl ) {
396 $this->doStash( [ 'text' => $text ] );
397
398 $editStash = TestingAccessWrapper::newFromObject(
399 MediaWikiServices::getInstance()->getPageEditStash() );
400 $cache = $editStash->cache;
401 $key = $this->getStashKey( __CLASS__, $text );
402
403 $wrapper = TestingAccessWrapper::newFromObject( $cache );
404
405 $this->assertEquals( $ttl, $wrapper->bag[$key][HashBagOStuff::KEY_EXP] - time(), '', 1 );
406 }
407
408 public function signatureProvider() {
409 return [
410 '~~~' => [ '~~~', PageEditStash::MAX_SIGNATURE_TTL ],
411 '~~~~' => [ '~~~~', PageEditStash::MAX_SIGNATURE_TTL ],
412 '~~~~~' => [ '~~~~~', PageEditStash::MAX_SIGNATURE_TTL ],
413 ];
414 }
415
416 public function testIsInternal() {
417 $res = $this->doApiRequest( [
418 'action' => 'paraminfo',
419 'modules' => 'stashedit',
420 ] );
421
422 $this->assertCount( 1, $res[0]['paraminfo']['modules'] );
423 $this->assertSame( true, $res[0]['paraminfo']['modules'][0]['internal'] );
424 }
425
426 public function testBusy() {
427 // @todo This doesn't work because both lock acquisitions are in the same MySQL session, so
428 // they don't conflict. How do I open a different session?
429 $this->markTestSkipped();
430
431 $key = $this->getStashKey();
432 $this->db->lock( $key, __METHOD__, 0 );
433 try {
434 $this->doStash( [], null, 'busy' );
435 } finally {
436 $this->db->unlock( $key, __METHOD__ );
437 }
438 }
439 }