source: trunk/tests/LockTest.php @ 357

Last change on this file since 357 was 357, checked in by quinn, 15 years ago

updated tests to work. updated email validation regex to include quote marks around name part. changed logmsg tmp dir name to use script_filename.

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