r108486 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r108485‎ | r108486 | r108487 >
Date:01:49, 10 January 2012
Author:bsitu
Status:deferred
Tags:
Comment:
API and UnitTest Cases for Concurrency functions
Modified paths:
  • /branches/concurrency/includes/AutoLoader.php (modified) (history)
  • /branches/concurrency/includes/api/ApiConcurrency.php (added) (history)
  • /branches/concurrency/includes/api/ApiMain.php (modified) (history)
  • /branches/concurrency/tests/phpunit/includes/api/ApiConcurrencyTest.php (added) (history)

Diff [purge]

Index: branches/concurrency/tests/phpunit/includes/api/ApiConcurrencyTest.php
@@ -0,0 +1,171 @@
 2+<?php
 3+
 4+class ApiConcurrencyTest extends ApiTestCase {
 5+ /**
 6+ * @var Array of test users
 7+ */
 8+ public static $users;
 9+
 10+ // Prepare test environment
 11+
 12+ function setUp() {
 13+ parent::setUp();
 14+
 15+ self::$users['one'] = new ApiTestUser(
 16+ 'ApitestuserA',
 17+ 'Api Test UserA',
 18+ 'api_test_userA@example.com',
 19+ array()
 20+ );
 21+
 22+ self::$users['two'] = new ApiTestUser(
 23+ 'ApitestuserB',
 24+ 'Api Test UserB',
 25+ 'api_test_userB@example.com',
 26+ array()
 27+ );
 28+ }
 29+
 30+ public function tearDown() {
 31+ parent::tearDown();
 32+ }
 33+
 34+ function testLogin() {
 35+
 36+ $sessionArray = array();
 37+
 38+ foreach ( self::$users as $key => $user ) {
 39+
 40+ $params = array(
 41+ 'action' => 'login',
 42+ 'lgname' => $user->username,
 43+ 'lgpassword' => $user->password
 44+ );
 45+ list( $result, , $session ) = $this->doApiRequest( $params );
 46+ $this->assertArrayHasKey( "login", $result );
 47+ $this->assertArrayHasKey( "result", $result['login'] );
 48+ $this->assertEquals( "NeedToken", $result['login']['result'] );
 49+ $token = $result['login']['token'];
 50+
 51+ $params = array(
 52+ 'action' => 'login',
 53+ 'lgtoken' => $token,
 54+ 'lgname' => $user->username,
 55+ 'lgpassword' => $user->password
 56+ );
 57+ list( $result, , $session ) = $this->doApiRequest( $params, $session );
 58+ $this->assertArrayHasKey( "login", $result );
 59+ $this->assertArrayHasKey( "result", $result['login'] );
 60+ $this->assertEquals( "Success", $result['login']['result'] );
 61+ $this->assertArrayHasKey( 'lgtoken', $result['login'] );
 62+
 63+ $this->assertNotEmpty( $session, 'API Login must return a session' );
 64+
 65+ $sessionArray[$key] = $session;
 66+
 67+ }
 68+
 69+ return $sessionArray;
 70+
 71+ }
 72+
 73+ /**
 74+ * @depends testLogin
 75+ */
 76+ function testCheckOut( $sessionArray ) {
 77+
 78+ global $wgUser;
 79+
 80+ $wgUser = self::$users['one']->user;
 81+
 82+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 83+ 'action' => 'concurrency',
 84+ 'ccaction' => 'checkout',
 85+ 'record' => 1,
 86+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['one'], self::$users['one']->user );
 87+
 88+ $this->assertEquals( "success", $result['concurrency']['status'] );
 89+
 90+ $wgUser = self::$users['two']->user;
 91+
 92+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 93+ 'action' => 'concurrency',
 94+ 'ccaction' => 'checkout',
 95+ 'record' => 1,
 96+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['two'], self::$users['two']->user );
 97+
 98+ $this->assertEquals( "failure", $result['concurrency']['status'] );
 99+
 100+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 101+ 'action' => 'concurrency',
 102+ 'ccaction' => 'checkout',
 103+ 'record' => 2,
 104+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['two'], self::$users['two']->user );
 105+
 106+ $this->assertEquals( "success", $result['concurrency']['status'] );
 107+
 108+ }
 109+
 110+
 111+ /**
 112+ * @depends testLogin
 113+ */
 114+ function testCheckIn( $sessionArray ) {
 115+
 116+ global $wgUser;
 117+
 118+ $wgUser = self::$users['one']->user;
 119+
 120+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 121+ 'action' => 'concurrency',
 122+ 'ccaction' => 'checkin',
 123+ 'record' => 1,
 124+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['one'], self::$users['one']->user );
 125+
 126+ $this->assertEquals( "success", $result['concurrency']['status'] );
 127+
 128+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 129+ 'action' => 'concurrency',
 130+ 'ccaction' => 'checkin',
 131+ 'record' => 2,
 132+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['one'], self::$users['one']->user );
 133+
 134+ $this->assertEquals( "failure", $result['concurrency']['status'] );
 135+
 136+ $wgUser = self::$users['two']->user;
 137+
 138+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 139+ 'action' => 'concurrency',
 140+ 'ccaction' => 'checkin',
 141+ 'record' => 2,
 142+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['two'], self::$users['two']->user );
 143+
 144+ $this->assertEquals( "success", $result['concurrency']['status'] );
 145+
 146+ }
 147+
 148+ /**
 149+ * @depends testLogin
 150+ */
 151+ function testInvalidCcacton( $sessionArray ) {
 152+ $exception = false;
 153+ try {
 154+ global $wgUser;
 155+
 156+ $wgUser = self::$users['one']->user;
 157+
 158+ list( $result, , $session ) = $this->doApiRequestWithToken( array(
 159+ 'action' => 'concurrency',
 160+ 'ccaction' => 'checkinX',
 161+ 'record' => 1,
 162+ 'resourcetype' => 'responding-to-moodbar-feedback'), $sessionArray['one'], self::$users['one']->user );
 163+ } catch ( UsageException $e ) {
 164+ $exception = true;
 165+ $this->assertEquals("Unrecognized value for parameter 'ccaction': checkinX",
 166+ $e->getMessage() );
 167+ }
 168+ $this->assertTrue( $exception, "Got exception" );
 169+
 170+ }
 171+
 172+}
\ No newline at end of file
Property changes on: branches/concurrency/tests/phpunit/includes/api/ApiConcurrencyTest.php
___________________________________________________________________
Added: svn:eol-style
1173 + native
Index: branches/concurrency/includes/api/ApiMain.php
@@ -79,6 +79,7 @@
8080 'patrol' => 'ApiPatrol',
8181 'import' => 'ApiImport',
8282 'userrights' => 'ApiUserrights',
 83+ 'concurrency' => 'ApiConcurrency',
8384 );
8485
8586 /**
Index: branches/concurrency/includes/api/ApiConcurrency.php
@@ -0,0 +1,107 @@
 2+<?php
 3+
 4+/**
 5+ * API module that handles cooperative locking of web resources
 6+ */
 7+class ApiConcurrency extends ApiBase {
 8+
 9+ public function __construct( $main, $action ) {
 10+ parent::__construct( $main, $action );
 11+ }
 12+
 13+ public function execute() {
 14+ global $wgUser;
 15+
 16+ $this->checkPermission( $wgUser );
 17+
 18+ $params = $this->extractRequestParams();
 19+
 20+ $res = array();
 21+
 22+ $concurrencyCheck = new ConcurrencyCheck( $params['resourcetype'], $wgUser );
 23+
 24+ switch ( $params['ccaction'] ) {
 25+ case 'checkout':
 26+ case 'checkin':
 27+ if ( $concurrencyCheck->$params['ccaction']( $params['record'] ) ) {
 28+ $res['status'] = 'success';
 29+ }
 30+ else {
 31+ $res['status'] = 'failure';
 32+ }
 33+ break;
 34+
 35+ default:
 36+ ApiBase::dieDebug( __METHOD__, "Unhandled concurrency action: {$params['ccaction']}" );
 37+ }
 38+
 39+ $this->getResult()->addValue( null, $this->getModuleName(), $res );
 40+ }
 41+
 42+ public function mustBePosted() {
 43+ return true;
 44+ }
 45+
 46+ public function isWriteMode() {
 47+ return true;
 48+ }
 49+
 50+ public function getAllowedParams() {
 51+ return array(
 52+ 'resourcetype' => array(
 53+ ApiBase::PARAM_TYPE => 'string',
 54+ ApiBase::PARAM_REQUIRED => true
 55+ ),
 56+ 'record' => array(
 57+ ApiBase::PARAM_TYPE => 'integer',
 58+ ApiBase::PARAM_REQUIRED => true
 59+ ),
 60+ 'token' => null,
 61+ 'expiry' => array(
 62+ ApiBase::PARAM_TYPE => 'integer'
 63+ ),
 64+ 'ccaction' => array(
 65+ ApiBase::PARAM_REQUIRED => true,
 66+ ApiBase::PARAM_TYPE => array(
 67+ 'checkout',
 68+ 'checkin',
 69+ ),
 70+ ),
 71+ );
 72+ }
 73+
 74+ public function getParamDescription() {
 75+ return array(
 76+ 'resourcetype' => 'the resource type for concurrency check',
 77+ 'record' => 'an unique identifier for a record of the defined resource type',
 78+ 'expiry' => 'the time interval for expiration',
 79+ 'ccaction' => 'the action for concurrency check',
 80+ );
 81+ }
 82+
 83+ public function getDescription() {
 84+ return 'Get/Set a concurrency check for a web resource type';
 85+ }
 86+
 87+ public function needsToken() {
 88+ return true;
 89+ }
 90+
 91+ public function getTokenSalt() {
 92+ return '';
 93+ }
 94+
 95+ public function getVersion() {
 96+ return __CLASS__ . ': $Id: ApiConcurrency.php $';
 97+ }
 98+
 99+ private function checkPermission( $user ) {
 100+ if ( $user->isAnon() ) {
 101+ $this->dieUsage( "You don't have permission to do that", 'permission-denied' );
 102+ }
 103+ if ( $user->isBlocked( false ) ) {
 104+ $this->dieUsageMsg( array( 'blockedtext' ) );
 105+ }
 106+ }
 107+
 108+}
\ No newline at end of file
Property changes on: branches/concurrency/includes/api/ApiConcurrency.php
___________________________________________________________________
Added: svn:eol-style
1109 + native
Index: branches/concurrency/includes/AutoLoader.php
@@ -272,6 +272,7 @@
273273 'ApiBase' => 'includes/api/ApiBase.php',
274274 'ApiBlock' => 'includes/api/ApiBlock.php',
275275 'ApiComparePages' => 'includes/api/ApiComparePages.php',
 276+ 'ApiConcurrency' => 'includes/api/ApiConcurrency.php',
276277 'ApiDelete' => 'includes/api/ApiDelete.php',
277278 'ApiDisabled' => 'includes/api/ApiDisabled.php',
278279 'ApiEditPage' => 'includes/api/ApiEditPage.php',

Follow-up revisions

RevisionCommit summaryAuthorDate
r108525followup to -r108486 - update API return key from "status" to "result"bsitu17:56, 10 January 2012

Status & tagging log