+ public function testDeletedRevisionSection() {
+ $name = ucfirst( __FUNCTION__ );
+ $oldRev = $this->editPage( $name, 'A' )->value['revision'];
+ $this->editPage( $name, 'B' );
+
+ $this->setExpectedApiException( 'apierror-sectionreplacefailed' );
+
+ $this->revisionDelete( $oldRev );
+
+ $this->doStash( [
+ 'title' => $name,
+ 'baserevid' => $oldRev->getId(),
+ 'text' => 'C',
+ 'section' => '1',
+ ] );
+ }
+
+ public function testPingLimiter() {
+ global $wgRateLimits;
+
+ $this->stashMwGlobals( 'wgRateLimits' );
+ $wgRateLimits['stashedit'] = [ '&can-bypass' => false, 'user' => [ 1, 60 ] ];
+
+ $this->doStash( [ 'text' => 'A' ] );
+
+ $this->doStash( [ 'text' => 'B' ], null, 'ratelimited' );
+ }
+
+ /**
+ * Shortcut for calling ApiStashEdit::checkCache() without having to create Titles and Contents
+ * in every test.
+ *
+ * @param User $user
+ * @param string $text The text of the article
+ * @return stdClass|bool Return value of ApiStashEdit::checkCache(), false if not in cache
+ */
+ protected function doCheckCache( User $user, $text = 'Content' ) {
+ return ApiStashEdit::checkCache(
+ Title::newFromText( __CLASS__ ),
+ new WikitextContent( $text ),
+ $user
+ );
+ }
+
+ public function testCheckCache() {
+ $user = $this->getMutableTestUser()->getUser();
+
+ $this->doStash( [], $user );
+
+ $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
+
+ // Another user doesn't see the cache
+ $this->assertFalse(
+ $this->doCheckCache( $this->getTestUser()->getUser() ),
+ 'Cache is user-specific'
+ );
+
+ // Nor does the original one if they become a bot
+ $user->addGroup( 'bot' );
+ $this->assertFalse(
+ $this->doCheckCache( $user ),
+ "We assume bots don't have cache entries"
+ );
+
+ // But other groups are okay
+ $user->removeGroup( 'bot' );
+ $user->addGroup( 'sysop' );
+ $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
+ }
+
+ public function testCheckCacheAnon() {
+ $user = new User();
+
+ $this->doStash( [], $user );
+
+ $this->assertInstanceOf( stdClass::class, $this->docheckCache( $user ) );
+ }
+
+ /**
+ * Stash an edit some time in the past, for testing expiry and freshness logic.
+ *
+ * @param User $user Who's doing the editing
+ * @param string $text What text should be cached
+ * @param int $howOld How many seconds is "old" (we actually set it one second before this)
+ */
+ protected function doStashOld(
+ User $user, $text = 'Content', $howOld = ApiStashEdit::PRESUME_FRESH_TTL_SEC
+ ) {
+ $this->doStash( [ 'text' => $text ], $user );
+
+ // Monkey with the cache to make the edit look old. @todo Is there a less fragile way to
+ // fake the time?
+ $key = $this->getStashKey( __CLASS__, $text, $user );
+
+ $cache = ObjectCache::getLocalClusterInstance();
+
+ $editInfo = $cache->get( $key );
+ $editInfo->output->setCacheTime( wfTimestamp( TS_MW,
+ wfTimestamp( TS_UNIX, $editInfo->output->getCacheTime() ) - $howOld - 1 ) );
+
+ $cache->set( $key, $editInfo );
+ }
+
+ public function testCheckCacheOldNoEdits() {
+ $user = $this->getTestSysop()->getUser();
+
+ $this->doStashOld( $user );
+
+ // Should still be good, because no intervening edits
+ $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
+ }
+
+ public function testCheckCacheOldNoEditsAnon() {
+ // Specify a made-up IP address to make sure no edits are lying around
+ $user = User::newFromName( '192.0.2.77', false );
+
+ $this->doStashOld( $user );
+
+ // Should still be good, because no intervening edits
+ $this->assertInstanceOf( stdClass::class, $this->doCheckCache( $user ) );
+ }
+
+ public function testCheckCacheInterveningEdits() {
+ $user = $this->getTestSysop()->getUser();
+
+ $this->doStashOld( $user );
+
+ // Now let's also increment our editcount
+ $this->editPage( ucfirst( __FUNCTION__ ), '' );
+
+ $this->assertFalse( $this->doCheckCache( $user ),
+ "Cache should be invalidated when it's old and the user has an intervening edit" );
+ }
+
+ /**
+ * @dataProvider signatureProvider
+ * @param string $text Which signature to test (~~~, ~~~~, or ~~~~~)
+ * @param int $ttl Expected TTL in seconds
+ */
+ public function testSignatureTtl( $text, $ttl ) {
+ $this->doStash( [ 'text' => $text ] );
+
+ $cache = ObjectCache::getLocalClusterInstance();
+ $key = $this->getStashKey( __CLASS__, $text );
+
+ $wrapper = TestingAccessWrapper::newFromObject( $cache );
+
+ $this->assertEquals( $ttl, $wrapper->bag[$key][HashBagOStuff::KEY_EXP] - time(), '', 1 );
+ }
+
+ public function signatureProvider() {
+ return [
+ '~~~' => [ '~~~', ApiStashEdit::MAX_SIGNATURE_TTL ],
+ '~~~~' => [ '~~~~', ApiStashEdit::MAX_SIGNATURE_TTL ],
+ '~~~~~' => [ '~~~~~', ApiStashEdit::MAX_SIGNATURE_TTL ],
+ ];
+ }
+
+ public function testIsInternal() {
+ $res = $this->doApiRequest( [
+ 'action' => 'paraminfo',
+ 'modules' => 'stashedit',
+ ] );
+
+ $this->assertCount( 1, $res[0]['paraminfo']['modules'] );
+ $this->assertSame( true, $res[0]['paraminfo']['modules'][0]['internal'] );
+ }
+
+ public function testBusy() {
+ // @todo This doesn't work because both lock acquisitions are in the same MySQL session, so
+ // they don't conflict. How do I open a different session?
+ $this->markTestSkipped();
+
+ $key = $this->getStashKey();
+ $this->db->lock( $key, __METHOD__, 0 );
+ try {
+ $this->doStash( [], null, 'busy' );
+ } finally {
+ $this->db->unlock( $key, __METHOD__ );
+ }
+ }