source: trunk/tests/LockTest.php @ 144

Last change on this file since 144 was 137, checked in by scdev, 18 years ago

Q - Renamed SessionCache? to Cache, RecordVersion? to Version, and RecordLock? to Lock

File size: 5.6 KB
Line 
1<?php
2/**
3 * PHPUnit test case for Lock
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 *
9 * Created with PHPUnit_Skeleton on 2005-08-09
10 */
11require_once 'PHPUnit.php';
12class LockTest extends PHPUnit_TestCase {
13
14    var $Lock;
15    var $Auth_SQL;
16
17    function LockTest($name)
18    {
19        $this->PHPUnit_TestCase($name);
20    }
21
22    function setUp()
23    {   
24        require dirname(__FILE__) . '/_config.inc.php';
25        require_once '../lib/Lock.inc.php';
26        require_once '../lib/Auth_SQL.inc.php';
27
28        $this->Auth_SQL =& new Auth_SQL('test');
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 =& DB::getInstance();
42        $db->query("
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',
52                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
53                'John',
54                'Doe',
55                'john@example.com',
56                'admin'
57            )
58        ");
59        $this->Auth_SQL->login('testuser', 'testpass');
60
61        if (!$this->Auth_SQL->isLoggedIn()) {
62            trigger_error("User login failed...tests canceled.", E_USER_ERROR);
63        }
64
65        $this->Lock =& Lock::getInstance($this->Auth_SQL);
66        $this->Lock->setParam(array('db_table' => 'test_lock_tbl'));
67
68        // Use fresh lock table.
69        $this->Lock->initDB(true);
70    }
71
72    function tearDown()
73    {
74        $db =& DB::getInstance();
75   
76        unset($this->Lock);
77        unset($this->Auth_SQL);
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");
81    }
82
83    function test_getinstance()
84    {
85        $result = Lock::getinstance($this->Auth_SQL);
86        $this->assertTrue($result && is_a($result, 'Lock'));
87    }
88
89    function test_setparam()
90    {
91        $this->Lock->setparam(array('var'=>'val'));
92    }
93
94    function test_getparam()
95    {
96        $this->Lock->getparam('var');
97    }
98
99    function test_select()
100    {
101        $this->Lock->set('test_user_tbl', 'user_id', '1');
102        $result = $this->Lock->select('test_user_tbl', 'user_id', '1');
103        $this->assertTrue($result);
104    }
105
106    function test_islocked()
107    {
108        $this->Lock->select('test_user_tbl', 'user_id', '1');
109
110        $this->Lock->set('test_user_tbl', 'user_id', '1');
111        $result = $this->Lock->islocked();
112        $this->assertTrue($result, 'Lock was not set.');
113
114        $this->Lock->remove();
115        $this->Lock->select('test_user_tbl', 'user_id', '1');
116        $result = $this->Lock->islocked();
117        $this->assertFalse($result, 'Lock was not removed.');
118    }
119
120    function test_ismine()
121    {
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();
125        $this->assertTrue($result);
126    }
127
128    function test_set()
129    {
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();
135        $this->assertTrue($result);
136    }
137
138    function test_remove()
139    {
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();
145        $this->assertFalse($result);
146    }
147
148    function test_removeall()
149    {
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();
155        $this->assertFalse($result);
156    }
157
158    function test__auto_timeout()
159    {
160        $this->Lock->_auto_timeout();
161    }
162
163    function test_getid()
164    {
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();
168        $this->assertTrue('' != $result);
169        $this->assertTrue(is_numeric($result));
170    }
171
172    function test_gettitle()
173    {
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();
177        $this->assertEquals('userlock', $result);
178    }
179
180    function test_geteditor()
181    {
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();
185        $this->assertEquals('testuser', $result);
186    }
187
188    function test_getsecondselapsed()
189    {
190        $result = $this->Lock->getsecondselapsed();
191        $this->assertType('integer', $result);
192    }
193
194}
195// Running the test.
196$suite = new PHPUnit_TestSuite('LockTest');
197$result = PHPUnit::run($suite);
198echo $result->toString();
199?>
Note: See TracBrowser for help on using the repository browser.