[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 |
---|
[541] | 6 | * |
---|
[362] | 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. |
---|
[541] | 13 | * |
---|
[362] | 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. |
---|
[541] | 18 | * |
---|
[362] | 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() |
---|
[541] | 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 | "); |
---|
[541] | 73 | |
---|
| 74 | // Sessions require client IP addr. |
---|
| 75 | $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
---|
| 76 | |
---|
[1] | 77 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
| 78 | |
---|
[136] | 79 | if (!$this->Auth_SQL->isLoggedIn()) { |
---|
| 80 | trigger_error("User login failed...tests canceled.", E_USER_ERROR); |
---|
| 81 | } |
---|
| 82 | |
---|
[137] | 83 | $this->Lock =& Lock::getInstance($this->Auth_SQL); |
---|
| 84 | $this->Lock->setParam(array('db_table' => 'test_lock_tbl')); |
---|
[1] | 85 | |
---|
| 86 | // Use fresh lock table. |
---|
[137] | 87 | $this->Lock->initDB(true); |
---|
[1] | 88 | } |
---|
| 89 | |
---|
| 90 | function tearDown() |
---|
| 91 | { |
---|
[479] | 92 | $db =& DB::getInstance(); |
---|
[541] | 93 | |
---|
[137] | 94 | unset($this->Lock); |
---|
[1] | 95 | unset($this->Auth_SQL); |
---|
[136] | 96 | $db->query("DROP TABLE IF EXISTS test_user_tbl"); |
---|
| 97 | $db->query("DROP TABLE IF EXISTS test_login_tbl"); |
---|
| 98 | $db->query("DROP TABLE IF EXISTS test_lock_tbl"); |
---|
[1] | 99 | } |
---|
| 100 | |
---|
| 101 | function test_getinstance() |
---|
| 102 | { |
---|
[137] | 103 | $result = Lock::getinstance($this->Auth_SQL); |
---|
| 104 | $this->assertTrue($result && is_a($result, 'Lock')); |
---|
[1] | 105 | } |
---|
| 106 | |
---|
| 107 | function test_setparam() |
---|
| 108 | { |
---|
[137] | 109 | $this->Lock->setparam(array('var'=>'val')); |
---|
[1] | 110 | } |
---|
| 111 | |
---|
| 112 | function test_getparam() |
---|
| 113 | { |
---|
[137] | 114 | $this->Lock->getparam('var'); |
---|
[1] | 115 | } |
---|
| 116 | |
---|
| 117 | function test_select() |
---|
| 118 | { |
---|
[137] | 119 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 120 | $result = $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
[1] | 121 | $this->assertTrue($result); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | function test_islocked() |
---|
| 125 | { |
---|
[137] | 126 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
[1] | 127 | |
---|
[137] | 128 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 129 | $result = $this->Lock->islocked(); |
---|
[1] | 130 | $this->assertTrue($result, 'Lock was not set.'); |
---|
| 131 | |
---|
[137] | 132 | $this->Lock->remove(); |
---|
| 133 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 134 | $result = $this->Lock->islocked(); |
---|
[1] | 135 | $this->assertFalse($result, 'Lock was not removed.'); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | function test_ismine() |
---|
| 139 | { |
---|
[137] | 140 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 141 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 142 | $result = $this->Lock->ismine(); |
---|
[1] | 143 | $this->assertTrue($result); |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | function test_set() |
---|
| 147 | { |
---|
[137] | 148 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 149 | $this->Lock->remove(); |
---|
| 150 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 151 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 152 | $result = $this->Lock->islocked(); |
---|
[1] | 153 | $this->assertTrue($result); |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | function test_remove() |
---|
| 157 | { |
---|
[137] | 158 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 159 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 160 | $this->Lock->remove(); |
---|
| 161 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 162 | $result = $this->Lock->islocked(); |
---|
[1] | 163 | $this->assertFalse($result); |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | function test_removeall() |
---|
| 167 | { |
---|
[137] | 168 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 169 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 170 | $this->Lock->removeall(); |
---|
| 171 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 172 | $result = $this->Lock->islocked(); |
---|
[1] | 173 | $this->assertFalse($result); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | function test__auto_timeout() |
---|
| 177 | { |
---|
[137] | 178 | $this->Lock->_auto_timeout(); |
---|
[1] | 179 | } |
---|
| 180 | |
---|
| 181 | function test_getid() |
---|
| 182 | { |
---|
[137] | 183 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 184 | $this->Lock->set('test_user_tbl', 'user_id', '1'); |
---|
| 185 | $result = $this->Lock->getid(); |
---|
[1] | 186 | $this->assertTrue('' != $result); |
---|
| 187 | $this->assertTrue(is_numeric($result)); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | function test_gettitle() |
---|
| 191 | { |
---|
[137] | 192 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 193 | $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 194 | $result = $this->Lock->gettitle(); |
---|
[1] | 195 | $this->assertEquals('userlock', $result); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | function test_geteditor() |
---|
| 199 | { |
---|
[137] | 200 | $this->Lock->select('test_user_tbl', 'user_id', '1'); |
---|
| 201 | $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock'); |
---|
| 202 | $result = $this->Lock->geteditor(); |
---|
[1] | 203 | $this->assertEquals('testuser', $result); |
---|
| 204 | } |
---|
| 205 | |
---|
| 206 | function test_getsecondselapsed() |
---|
| 207 | { |
---|
[137] | 208 | $result = $this->Lock->getsecondselapsed(); |
---|
[468] | 209 | $this->assertInternalType('integer', $result); |
---|
[1] | 210 | } |
---|
| 211 | |
---|
| 212 | } |
---|