public function execute() { global $wgUser; $this->checkPermission($wgUser); $params = $this->extractRequestParams(); $res = array(); $concurrencyCheck = new ConcurrencyCheck($params['resourcetype'], $wgUser); switch ($params['ccaction']) { case 'checkout': case 'checkin': if ($concurrencyCheck->{$params}['ccaction']($params['record'])) { $res['result'] = 'success'; } else { $res['result'] = 'failure'; } // data to be utilized by the caller for checkout if ($params['ccaction'] === 'checkout') { $lastCheckout = $concurrencyCheck->checkoutResult(); if ($res['result'] === 'success') { $user = $wgUser; } else { $user = User::newFromId(intval($lastCheckout['userId'])); } if (!$user->isAnon()) { $res['userid'] = $user->getId(); $res['username'] = $user->getName(); } $res['expiration'] = $lastCheckout['expiration']; } break; default: ApiBase::dieDebug(__METHOD__, "Unhandled concurrency action: {$params['ccaction']}"); } $this->getResult()->addValue(null, $this->getModuleName(), $res); }
public function testCheckoutCheckin() { $first = new ConcurrencyCheck('CCUnitTest', self::$users['user1']->user); $second = new ConcurrencyCheck('CCUnitTest', self::$users['user2']->user); $firstId = self::$users['user1']->user->getId(); $secondId = self::$users['user2']->user->getId(); $testKey = 1337; // clean up after any previously failed tests $first->checkin($testKey); $second->checkin($testKey); // tests $this->assertTrue($first->checkout($testKey), "Initial checkout"); $res = $first->checkoutResult(); $this->assertEquals($firstId, $res['userId'], "User matches on success"); $this->assertTrue(array_key_exists('expiration', $res), "Expiration is present"); $this->assertTrue($res['expiration'] > 0, "Expiration is a positive integer"); $this->assertTrue($first->checkout($testKey), "Cache hit"); global $wgMemc; $cacheKey = wfMemcKey('concurrencycheck', 'CCUnitTest', $testKey); $wgMemc->delete($cacheKey); $first->lastCheckout = array(); $this->assertTrue($first->checkout($testKey), "Cache miss"); $res = $first->checkoutResult(); $this->assertEquals($firstId, $res['userId'], "User matches on success (nocache)"); $this->assertTrue(array_key_exists('expiration', $res), "Expiration is present (nocache)"); $this->assertTrue($res['expiration'] > 0, "Expiration is a positive integer (nocache)"); $this->assertFalse($second->checkout($testKey), "Checkout of locked resource fails as different user"); $res = $second->checkoutResult(); $this->assertEquals($firstId, $res['userId'], "Actual owner matches on failure"); $this->assertTrue($res['expiration'] > 0, "Expiration is a positive integer"); $this->assertTrue($first->checkout($testKey), "Checkout of locked resource succeeds as original user"); $this->assertFalse($second->checkin($testKey), "Checkin of locked resource fails as different user"); $this->assertTrue($first->checkin($testKey), "Checkin of locked resource succeeds as original user"); $second->setExpirationTime(-5); $this->assertTrue($second->checkout($testKey), "Checked-in resource is now available to second user"); $second->setExpirationTime(); $this->assertTrue($first->checkout($testKey), "Checkout of expired resource succeeds as first user"); $this->assertTrue($second->checkout($testKey, true), "Checkout override"); $this->assertFalse($first->checkout($testKey), "Checkout of overriden resource fails as different user"); // cleanup $this->assertTrue($second->checkin($testKey), "Checkin of record with changed ownership"); }