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