Skip to content
Snippets Groups Projects
Commit f9b08b8e authored by Carsten  Rose's avatar Carsten Rose
Browse files

Dirty.php, DirtyTest.php, TestDirty.sql: further phpunit tests

parent d9a49289
No related branches found
No related tags found
No related merge requests found
......@@ -16,8 +16,6 @@ require_once(__DIR__ . '/../../qfq/form/Dirty.php');
require_once(__DIR__ . '/../../qfq/store/Sip.php');
require_once(__DIR__ . '/../../qfq/store/Session.php');
require_once(__DIR__ . '/../../qfq/database/Database.php');
//require_once(__DIR__ . '/../../qfq/exceptions/UserFormException.php');
/*
* Open to check
......@@ -27,56 +25,51 @@ require_once(__DIR__ . '/../../qfq/database/Database.php');
/*
* 1. DirtyMode: none
* ===============
* OK r=0, Alice lock, Alice releasefirst simple unit tests for recdord locking.-k
* OK r>0, Alice lock, Alice release
* OK Bob lock, Alice lock
* r=0, Alice lock, Alice releasefirst simple unit tests for recdord locking.-k
* r>0, Alice lock, Alice release
* Bob lock, Alice lock
*
* 2. DirtyMode: advisory
* ===================
* OK r=0, Alice lock, Alice release
* OK r>0, Alice lock, Alice release
* OK Alice lock, Alice timeout, Alice release
* OK Alice release
* OK Alice lock, Record modified by someone else, Alice release
* OK Bob lock, Alice lock
* OK Bob lock, Bob timeout, Alice lock
* r=0, Alice lock, Alice release
* r>0, Alice lock, Alice release
* Alice lock, Alice timeout, Alice release
* Alice release
* Alice lock, Record modified by someone else, Alice release
* Bob lock, Alice lock
* Bob lock, Bob timeout, Alice lock
* Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
* Alice lock Form A, Bob lock Form B
* Alice lock 1 Form A, Bob lock 1 Form B
*
* 3. DirtyMode: exclusive
* ====================
* OK r=0, Alice lock, Alice release
* OK r>0, Alice lock, Alice release
* OK Alice lock, Alice timeout, Alice release
* OK Alice release
* OK Alice lock, Record modified by someone else, Alice release
* OK Bob lock, Alice lock
* OK Bob lock, Bob timeout, Alice lock
* r=0, Alice lock, Alice release
* r>0, Alice lock, Alice release
* Alice lock, Alice timeout, Alice release
* Alice release
* Alice lock, Record modified by someone else, Alice release
* Bob lock, Alice lock
* Bob lock, Bob timeout, Alice lock
* Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
* * Alice lock, Alice release, Bob lock, Bob release
* Alice lock Form A, Bob lock Form B
* Alice lock 1 Form A, Bob lock 1 Form B
*
* 4. Mix Mode
* ===========
*
* Form A: none, Form B: exclusive
* Alice lock, Bob lock
*
* Form A: none, Form B: advisory
* Alice lock, Bob lock
*
* Form A: advisory, Form B: none
* Alice lock, Bob lock
* For all: Alice lock 1 Form A, Bob lock 1 Form B
* Form A: none, Form B: exclusive
* Form A: none, Form B: advisory
* Form A: advisory, Form B: none
* Form A: advisory, Form B: exclusive
* Form A: exclusive, Form B: none
* Form A: exclusive, Form B: advisory
*
* Form A: advisory, Form B: exclusive
* Alice lock, Bob lock
*
* Form A: exclusive, Form B: none
* Alice lock, Bob lock
*
* Form A: exclusive, Form B: advisory
* Alice lock, Bob lock
* 5. Special
* ==========
*
* Compute expire time
* Lock non existing record
*/
class DirtyTest extends \AbstractDatabaseTest {
......@@ -349,6 +342,117 @@ class DirtyTest extends \AbstractDatabaseTest {
$this->assertEquals($expected, $result);
}
/**
* * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
*/
public function testAdvisoryAliceLock1AliceLock2BobLock3BobRelease3AliceRelease2AliceRelease1() {
// Alice lock 1
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// Alice lock 2
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockAdvisory", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// Bob becomes logged in user
$_COOKIE[SESSION_NAME] = 'SessionCookieBob';
// Create third Person record
$rc = $this->db->sql("INSERT INTO Person (`name`, `firstName`) VALUES ('no', 'name')", ROW_REGULAR);
// Bob lock 3
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=3&form=lockAdvisory", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// RELEASE
// Bob Release 3
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
$session = \qfq\Session::getInstance(true);
$session::clearAll();
// Alice Release 2
$_COOKIE[SESSION_NAME] = 'SessionCookieAlice';
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
// with phpunit, we only have one sip at a time
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
// Alice Release 1
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
// with phpunit, we only have one sip at a time
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
}
/**
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAdvisoryAliceFormALock1BobFormBLock1() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory2", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT_ALLOW_FORCE, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/**********************************************************************
* 3. DirtyMode: exclusive
*/
......@@ -385,6 +489,9 @@ class DirtyTest extends \AbstractDatabaseTest {
*/
public function testExclusiveR1AliceLockRelease() {
// Create clean environment
$this->clean('SessionCookieAlice');
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
// Alice lock
......@@ -536,8 +643,335 @@ class DirtyTest extends \AbstractDatabaseTest {
$this->assertEquals($expected, $result);
}
/**
* * Alice lock 1, Alice lock 2, Bob lock 3, Bob release 3, Alice release 2, Alice release 1
*/
public function testExclusiveAliceLock1AliceLock2BobLock3BobRelease3AliceRelease2AliceRelease1() {
// Alice lock 1
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// Alice lock 2
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// Bob becomes logged in user
$_COOKIE[SESSION_NAME] = 'SessionCookieBob';
// Create third Person record
$rc = $this->db->sql("INSERT INTO Person (`name`, `firstName`) VALUES ('no', 'name')", ROW_REGULAR);
// Bob lock 3
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=3&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$this->assertEquals($expected, $result);
// RELEASE
// Bob Release 3
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
$session = \qfq\Session::getInstance(true);
$session::clearAll();
// Alice Release 2
$_COOKIE[SESSION_NAME] = 'SessionCookieAlice';
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
// with phpunit, we only have one sip at a time
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=2&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
// Alice Release 1
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_RELEASE;
// with phpunit, we only have one sip at a time
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => ''];
$this->assertEquals($expected, $result);
}
/**
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testExclusiveAliceFormALock1BobFormBLock1() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive2", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/**********************************************************************
* 4. Mix Mode
*/
/**
* Form A: none, Form B: exclusive
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1NoneBobLock1Exclusive() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '' ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
$this->assertEquals($expected, $result);
}
/**
* Form A: none, Form B: advisory
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1NoneBobLock1Advisory() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '' ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900];
$this->assertEquals($expected, $result);
}
/**
* Form A: advisory, Form B: none
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1AdvisoryBobLock1None() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT_ALLOW_FORCE, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/**
* Form A: advisory, Form B: Exclusive
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1AdvisoryBobLock1Exclusive() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/**
* Form A: exclusive, Form B: none
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1ExclusiveBobLock1None() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockNone", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/**
* Form A: exclusive, Form B: Advisory
* Alice lock 1 Form A, Bob lock 1 Form B
*/
public function testAliceLock1ExclusiveBobLock1Advisory() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 1 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockExclusive", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$expected = [API_STATUS => API_ANSWER_STATUS_SUCCESS, API_MESSAGE => '', API_LOCK_TIMEOUT => 900 ];
$this->assertEquals($expected, $result);
// Create clean environment
$this->clean('SessionCookieBob');
// Bob lock 1 Form B
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=1&form=lockAdvisory", RETURN_SIP);
$dirty = new \qfq\Dirty();
$result = $dirty->process();
$msg = 'The record has already';
$expected = [API_STATUS => API_ANSWER_STATUS_CONFLICT, API_MESSAGE => $msg];
// cut IP, User and Timestamp
$result[API_MESSAGE] = substr($result[API_MESSAGE], 0, strlen($msg));
$this->assertEquals($expected, $result);
}
/****************************************************
* Special
* 5. Special
*/
/**
......@@ -560,6 +994,41 @@ class DirtyTest extends \AbstractDatabaseTest {
$this->assertLessThan(2, $secs);
}
/**
* Lock non existing record
* @expectedException \qfq\dBException
*/
public function testLockNonExistingRecord() {
// Create clean environment
$this->clean('SessionCookieAlice');
// Alice lock 9999 Form A
$_GET[CLIENT_SIP] = $this->sip->queryStringToSip("?id=input&r=9999&form=lockExclusive", RETURN_SIP);
$_GET[DIRTY_API_ACTION] = DIRTY_API_ACTION_LOCK;
$dirty = new \qfq\Dirty();
$result = $dirty->process();
}
/**
* Clear Session to have a new clean environment
*
* @param $cookie
*/
private function clean($cookie='') {
$session = \qfq\Session::getInstance(true);
$session::clearAll();
if($cookie!='') {
$_COOKIE[SESSION_NAME] = $cookie;
}
}
/************************************************
* Setup
*/
......
......@@ -6,5 +6,7 @@ DELETE FROM Form WHERE name LIKE 'lock%';
INSERT INTO Form ( name, title, noteInternal, tableName, permitNew, permitEdit, render, multiSql, parameter, dirtyMode) VALUES
( 'lockNone', 'Text None', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'none'),
( 'lockAdvisory', 'Text Advisory', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'advisory'),
( 'lockExclusive', 'Text Exclusive', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'exclusive');
( 'lockExclusive', 'Text Exclusive', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'exclusive'),
( 'lockAdvisory2', 'Text Advisory', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'advisory'),
( 'lockExclusive2', 'Text Exclusive', 'note internal', 'Person', 'always', 'always', 'bootstrap', '', '', 'exclusive');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment