[1] | 1 | <?php |
---|
| 2 | /** |
---|
| 3 | * PHPUnit test case for RecordLock |
---|
[42] | 4 | * |
---|
| 5 | * The method skeletons below need to be filled in with |
---|
| 6 | * real data so that the tests will run correctly. Replace |
---|
| 7 | * all EXPECTED_VAL and PARAM strings with real data. |
---|
| 8 | * |
---|
[1] | 9 | * Created with PHPUnit_Skeleton on 2005-08-09 |
---|
| 10 | */ |
---|
| 11 | require_once 'PHPUnit.php'; |
---|
| 12 | class RecordLockTest extends PHPUnit_TestCase { |
---|
| 13 | |
---|
| 14 | var $RecordLock; |
---|
| 15 | var $Auth_SQL; |
---|
| 16 | |
---|
| 17 | function RecordLockTest($name) |
---|
| 18 | { |
---|
| 19 | $this->PHPUnit_TestCase($name); |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | function setUp() |
---|
| 23 | { |
---|
| 24 | require dirname(__FILE__) . '/_config.inc.php'; |
---|
| 25 | require_once '../lib/RecordLock.inc.php'; |
---|
| 26 | require_once '../lib/Auth_SQL.inc.php'; |
---|
| 27 | |
---|
| 28 | $this->Auth_SQL =& new Auth_SQL('testauth'); |
---|
| 29 | $this->Auth_SQL->setParam(array( |
---|
| 30 | 'db_table' => 'test_user_tbl', |
---|
| 31 | 'db_primary_key' => 'user_id', |
---|
| 32 | 'db_login_table' => 'test_login_tbl', |
---|
| 33 | 'login_url' => '/login.php', |
---|
| 34 | 'blocking' => true |
---|
| 35 | )); |
---|
| 36 | |
---|
| 37 | // Use fresh user table. |
---|
| 38 | $this->Auth_SQL->initDB(true); |
---|
| 39 | |
---|
| 40 | // Insert test data. |
---|
| 41 | DB::query(" |
---|
| 42 | INSERT INTO test_user_tbl ( |
---|
| 43 | username, |
---|
| 44 | userpass, |
---|
| 45 | first_name, |
---|
| 46 | last_name, |
---|
| 47 | email, |
---|
| 48 | user_type |
---|
| 49 | ) VALUES ( |
---|
| 50 | 'testuser', |
---|
[126] | 51 | '" . $this->Auth_SQL->encryptpassword('testpass') . "', |
---|
[1] | 52 | 'John', |
---|
| 53 | 'Doe', |
---|
| 54 | 'john@example.com', |
---|
| 55 | 'admin' |
---|
| 56 | ) |
---|
| 57 | "); |
---|
[126] | 58 | if (!$this->Auth_SQL->login('testuser', 'testpass')) { |
---|
| 59 | trigger_error('Test user not logged in.', E_USER_WARNING); |
---|
| 60 | } |
---|
[1] | 61 | |
---|
| 62 | $this->RecordLock =& RecordLock::getInstance($this->Auth_SQL); |
---|
| 63 | $this->RecordLock->setParam(array('db_table' => 'test_lock_tbl')); |
---|
| 64 | |
---|
| 65 | // Use fresh lock table. |
---|
| 66 | $this->RecordLock->initDB(true); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | function tearDown() |
---|
| 70 | { |
---|
| 71 | unset($this->RecordLock); |
---|
| 72 | unset($this->Auth_SQL); |
---|
| 73 | DB::query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
| 74 | DB::query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
| 75 | DB::query("DROP TABLE IF EXISTS test_lock_tbl"); |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | function test_getinstance() |
---|
| 79 | { |
---|
| 80 | $result = RecordLock::getinstance($this->Auth_SQL); |
---|
| 81 | $this->assertTrue($result && is_a($result, 'RecordLock')); |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | function test_setparam() |
---|
| 85 | { |
---|
| 86 | $this->RecordLock->setparam(array('var'=>'val')); |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | function test_getparam() |
---|
| 90 | { |
---|
| 91 | $this->RecordLock->getparam('var'); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | function test_select() |
---|
| 95 | { |
---|
| 96 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 97 | $result = $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 98 | $this->assertTrue($result); |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | function test_islocked() |
---|
| 102 | { |
---|
| 103 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 104 | |
---|
| 105 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 106 | $result = $this->RecordLock->islocked(); |
---|
| 107 | $this->assertTrue($result, 'Lock was not set.'); |
---|
| 108 | |
---|
| 109 | $this->RecordLock->remove(); |
---|
| 110 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 111 | $result = $this->RecordLock->islocked(); |
---|
| 112 | $this->assertFalse($result, 'Lock was not removed.'); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | function test_ismine() |
---|
| 116 | { |
---|
| 117 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 118 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 119 | $result = $this->RecordLock->ismine(); |
---|
| 120 | $this->assertTrue($result); |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | function test_set() |
---|
| 124 | { |
---|
| 125 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 126 | $this->RecordLock->remove(); |
---|
| 127 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 128 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 129 | $result = $this->RecordLock->islocked(); |
---|
| 130 | $this->assertTrue($result); |
---|
| 131 | } |
---|
| 132 | |
---|
| 133 | function test_remove() |
---|
| 134 | { |
---|
| 135 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 136 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 137 | $this->RecordLock->remove(); |
---|
| 138 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 139 | $result = $this->RecordLock->islocked(); |
---|
| 140 | $this->assertFalse($result); |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | function test_removeall() |
---|
| 144 | { |
---|
| 145 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 146 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 147 | $this->RecordLock->removeall(); |
---|
| 148 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 149 | $result = $this->RecordLock->islocked(); |
---|
| 150 | $this->assertFalse($result); |
---|
| 151 | } |
---|
| 152 | |
---|
| 153 | function test__auto_timeout() |
---|
| 154 | { |
---|
| 155 | $this->RecordLock->_auto_timeout(); |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | function test_getid() |
---|
| 159 | { |
---|
| 160 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 161 | $this->RecordLock->set('test_user_tbl', 'user_id', '1'); |
---|
| 162 | $result = $this->RecordLock->getid(); |
---|
| 163 | $this->assertTrue('' != $result); |
---|
| 164 | $this->assertTrue(is_numeric($result)); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | function test_gettitle() |
---|
| 168 | { |
---|
| 169 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 170 | $this->RecordLock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 171 | $result = $this->RecordLock->gettitle(); |
---|
| 172 | $this->assertEquals('userlock', $result); |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | function test_geteditor() |
---|
| 176 | { |
---|
| 177 | $this->RecordLock->select('test_user_tbl', 'user_id', '1'); |
---|
| 178 | $this->RecordLock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 179 | $result = $this->RecordLock->geteditor(); |
---|
| 180 | $this->assertEquals('testuser', $result); |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | function test_getsecondselapsed() |
---|
| 184 | { |
---|
| 185 | $result = $this->RecordLock->getsecondselapsed(); |
---|
| 186 | $this->assertType('integer', $result); |
---|
| 187 | } |
---|
| 188 | |
---|
| 189 | } |
---|
| 190 | // Running the test. |
---|
| 191 | $suite = new PHPUnit_TestSuite('RecordLockTest'); |
---|
| 192 | $result = PHPUnit::run($suite); |
---|
| 193 | echo $result->toString(); |
---|
| 194 | ?> |
---|