[1] | 1 | <?php |
---|
| 2 | /** |
---|
[362] | 3 | * The Strangecode Codebase - a general application development framework for PHP |
---|
| 4 | * For details visit the project site: <http://trac.strangecode.com/codebase/> |
---|
[396] | 5 | * Copyright 2001-2012 Strangecode, LLC |
---|
[362] | 6 | * |
---|
| 7 | * This file is part of The Strangecode Codebase. |
---|
[42] | 8 | * |
---|
[362] | 9 | * The Strangecode Codebase is free software: you can redistribute it and/or |
---|
| 10 | * modify it under the terms of the GNU General Public License as published by the |
---|
| 11 | * Free Software Foundation, either version 3 of the License, or (at your option) |
---|
| 12 | * any later version. |
---|
| 13 | * |
---|
| 14 | * The Strangecode Codebase is distributed in the hope that it will be useful, but |
---|
| 15 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
---|
| 16 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
---|
| 17 | * details. |
---|
| 18 | * |
---|
| 19 | * You should have received a copy of the GNU General Public License along with |
---|
| 20 | * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>. |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | /** |
---|
| 24 | * PHPUnit test case for codebase/lib/Lock.inc.php |
---|
| 25 | * |
---|
[42] | 26 | * The method skeletons below need to be filled in with |
---|
| 27 | * real data so that the tests will run correctly. Replace |
---|
| 28 | * all EXPECTED_VAL and PARAM strings with real data. |
---|
| 29 | * |
---|
[1] | 30 | * Created with PHPUnit_Skeleton on 2005-08-09 |
---|
| 31 | */ |
---|
| 32 | |
---|
[468] | 33 | class LockTest extends PHPUnit_Framework_TestCase { |
---|
| 34 | |
---|
[137] | 35 | var $Lock; |
---|
[1] | 36 | var $Auth_SQL; |
---|
| 37 | |
---|
| 38 | function setUp() |
---|
[136] | 39 | { |
---|
[1] | 40 | require dirname(__FILE__) . '/_config.inc.php'; |
---|
[137] | 41 | require_once '../lib/Lock.inc.php'; |
---|
[1] | 42 | require_once '../lib/Auth_SQL.inc.php'; |
---|
| 43 | |
---|
[468] | 44 | $this->Auth_SQL = new Auth_SQL('test'); |
---|
[1] | 45 | $this->Auth_SQL->setParam(array( |
---|
| 46 | 'db_table' => 'test_user_tbl', |
---|
| 47 | 'db_primary_key' => 'user_id', |
---|
| 48 | 'db_login_table' => 'test_login_tbl', |
---|
| 49 | 'login_url' => '/login.php', |
---|
| 50 | 'blocking' => true |
---|
| 51 | )); |
---|
| 52 | |
---|
| 53 | // Use fresh user table. |
---|
| 54 | $this->Auth_SQL->initDB(true); |
---|
| 55 | |
---|
| 56 | // Insert test data. |
---|
[136] | 57 | $db =& DB::getInstance(); |
---|
| 58 | $db->query(" |
---|
[1] | 59 | INSERT INTO test_user_tbl ( |
---|
| 60 | username, |
---|
| 61 | userpass, |
---|
| 62 | first_name, |
---|
| 63 | last_name, |
---|
[357] | 64 | email |
---|
[1] | 65 | ) VALUES ( |
---|
| 66 | 'testuser', |
---|
[136] | 67 | '" . $this->Auth_SQL->encryptPassword('testpass') . "', |
---|
[1] | 68 | 'John', |
---|
| 69 | 'Doe', |
---|
[357] | 70 | 'john@example.com' |
---|
[1] | 71 | ) |
---|
| 72 | "); |
---|
| 73 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
| 74 | |
---|
[136] | 75 | if (!$this->Auth_SQL->isLoggedIn()) { |
---|
| 76 | trigger_error("User login failed...tests canceled.", E_USER_ERROR); |
---|
| 77 | } |
---|
| 78 | |
---|
[137] | 79 | $this->Lock =& Lock::getInstance($this->Auth_SQL); |
---|
| 80 | $this->Lock->setParam(array('db_table' => 'test_lock_tbl')); |
---|
[1] | 81 | |
---|
| 82 | // Use fresh lock table. |
---|
[137] | 83 | $this->Lock->initDB(true); |
---|
[1] | 84 | } |
---|
| 85 | |
---|
| 86 | function tearDown() |
---|
| 87 | { |
---|
[136] | 88 | $db =& DB::getInstance(); |
---|
| 89 | |
---|
[137] | 90 | unset($this->Lock); |
---|
[1] | 91 | unset($this->Auth_SQL); |
---|
[136] | 92 | $db->query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
| 93 | $db->query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
| 94 | $db->query("DROP TABLE IF EXISTS test_lock_tbl"); |
---|
[1] | 95 | } |
---|
| 96 | |
---|
| 97 | function test_getinstance() |
---|
| 98 | { |
---|
[137] | 99 | $result = Lock::getinstance($this->Auth_SQL); |
---|
| 100 | $this->assertTrue($result && is_a($result, 'Lock')); |
---|
[1] | 101 | } |
---|
| 102 | |
---|
| 103 | function test_setparam() |
---|
| 104 | { |
---|
[137] | 105 | $this->Lock->setparam(array('var'=>'val')); |
---|
[1] | 106 | } |
---|
| 107 | |
---|
| 108 | function test_getparam() |
---|
| 109 | { |
---|
[137] | 110 | $this->Lock->getparam('var'); |
---|
[1] | 111 | } |
---|
| 112 | |
---|
| 113 | function test_select() |
---|
| 114 | { |
---|
[137] | 115 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 116 | $result = $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
[1] | 117 | $this->assertTrue($result); |
---|
| 118 | } |
---|
| 119 | |
---|
| 120 | function test_islocked() |
---|
| 121 | { |
---|
[137] | 122 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
[1] | 123 | |
---|
[137] | 124 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 125 | $result = $this->Lock->islocked(); |
---|
[1] | 126 | $this->assertTrue($result, 'Lock was not set.'); |
---|
| 127 | |
---|
[137] | 128 | $this->Lock->remove(); |
---|
| 129 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 130 | $result = $this->Lock->islocked(); |
---|
[1] | 131 | $this->assertFalse($result, 'Lock was not removed.'); |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | function test_ismine() |
---|
| 135 | { |
---|
[137] | 136 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 137 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 138 | $result = $this->Lock->ismine(); |
---|
[1] | 139 | $this->assertTrue($result); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | function test_set() |
---|
| 143 | { |
---|
[137] | 144 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 145 | $this->Lock->remove(); |
---|
| 146 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 147 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 148 | $result = $this->Lock->islocked(); |
---|
[1] | 149 | $this->assertTrue($result); |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | function test_remove() |
---|
| 153 | { |
---|
[137] | 154 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 155 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 156 | $this->Lock->remove(); |
---|
| 157 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 158 | $result = $this->Lock->islocked(); |
---|
[1] | 159 | $this->assertFalse($result); |
---|
| 160 | } |
---|
| 161 | |
---|
| 162 | function test_removeall() |
---|
| 163 | { |
---|
[137] | 164 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 165 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 166 | $this->Lock->removeall(); |
---|
| 167 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 168 | $result = $this->Lock->islocked(); |
---|
[1] | 169 | $this->assertFalse($result); |
---|
| 170 | } |
---|
| 171 | |
---|
| 172 | function test__auto_timeout() |
---|
| 173 | { |
---|
[137] | 174 | $this->Lock->_auto_timeout(); |
---|
[1] | 175 | } |
---|
| 176 | |
---|
| 177 | function test_getid() |
---|
| 178 | { |
---|
[137] | 179 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 180 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 181 | $result = $this->Lock->getid(); |
---|
[1] | 182 | $this->assertTrue('' != $result); |
---|
| 183 | $this->assertTrue(is_numeric($result)); |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | function test_gettitle() |
---|
| 187 | { |
---|
[137] | 188 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 189 | $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 190 | $result = $this->Lock->gettitle(); |
---|
[1] | 191 | $this->assertEquals('userlock', $result); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | function test_geteditor() |
---|
| 195 | { |
---|
[137] | 196 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 197 | $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 198 | $result = $this->Lock->geteditor(); |
---|
[1] | 199 | $this->assertEquals('testuser', $result); |
---|
| 200 | } |
---|
| 201 | |
---|
| 202 | function test_getsecondselapsed() |
---|
| 203 | { |
---|
[137] | 204 | $result = $this->Lock->getsecondselapsed(); |
---|
[468] | 205 | $this->assertInternalType('integer', $result); |
---|
[1] | 206 | } |
---|
| 207 | |
---|
| 208 | } |
---|