source: tags/2.1.5/tests/LockTest.php @ 377

Last change on this file since 377 was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 6.5 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
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 *
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 *
30 * Created with PHPUnit_Skeleton on 2005-08-09
31 */
32require_once 'PHPUnit.php';
33class LockTest extends PHPUnit_TestCase {
34
35    var $Lock;
36    var $Auth_SQL;
37
38    function LockTest($name)
39    {
40        $this->PHPUnit_TestCase($name);
41    }
42
43    function setUp()
44    {   
45        require dirname(__FILE__) . '/_config.inc.php';
46        require_once '../lib/Lock.inc.php';
47        require_once '../lib/Auth_SQL.inc.php';
48
49        $this->Auth_SQL =& new Auth_SQL('test');
50        $this->Auth_SQL->setParam(array(
51            'db_table'          => 'test_user_tbl',
52            'db_primary_key'    => 'user_id',
53            'db_login_table'    => 'test_login_tbl',
54            'login_url'         => '/login.php',
55            'blocking'          => true
56        ));
57
58        // Use fresh user table.
59        $this->Auth_SQL->initDB(true);
60
61        // Insert test data.
62        $db =& DB::getInstance();
63        $db->query("
64            INSERT INTO test_user_tbl (
65                username,
66                userpass,
67                first_name,
68                last_name,
69                email
70            ) VALUES (
71                'testuser',
72                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
73                'John',
74                'Doe',
75                'john@example.com'
76            )
77        ");
78        $this->Auth_SQL->login('testuser', 'testpass');
79
80        if (!$this->Auth_SQL->isLoggedIn()) {
81            trigger_error("User login failed...tests canceled.", E_USER_ERROR);
82        }
83
84        $this->Lock =& Lock::getInstance($this->Auth_SQL);
85        $this->Lock->setParam(array('db_table' => 'test_lock_tbl'));
86
87        // Use fresh lock table.
88        $this->Lock->initDB(true);
89    }
90
91    function tearDown()
92    {
93        $db =& DB::getInstance();
94   
95        unset($this->Lock);
96        unset($this->Auth_SQL);
97        $db->query("DROP TABLE IF EXISTS test_user_tbl");
98        $db->query("DROP TABLE IF EXISTS test_login_tbl");
99        $db->query("DROP TABLE IF EXISTS test_lock_tbl");
100    }
101
102    function test_getinstance()
103    {
104        $result = Lock::getinstance($this->Auth_SQL);
105        $this->assertTrue($result && is_a($result, 'Lock'));
106    }
107
108    function test_setparam()
109    {
110        $this->Lock->setparam(array('var'=>'val'));
111    }
112
113    function test_getparam()
114    {
115        $this->Lock->getparam('var');
116    }
117
118    function test_select()
119    {
120        $this->Lock->set('test_user_tbl', 'user_id', '1');
121        $result = $this->Lock->select('test_user_tbl', 'user_id', '1');
122        $this->assertTrue($result);
123    }
124
125    function test_islocked()
126    {
127        $this->Lock->select('test_user_tbl', 'user_id', '1');
128
129        $this->Lock->set('test_user_tbl', 'user_id', '1');
130        $result = $this->Lock->islocked();
131        $this->assertTrue($result, 'Lock was not set.');
132
133        $this->Lock->remove();
134        $this->Lock->select('test_user_tbl', 'user_id', '1');
135        $result = $this->Lock->islocked();
136        $this->assertFalse($result, 'Lock was not removed.');
137    }
138
139    function test_ismine()
140    {
141        $this->Lock->set('test_user_tbl', 'user_id', '1');
142        $this->Lock->select('test_user_tbl', 'user_id', '1');
143        $result = $this->Lock->ismine();
144        $this->assertTrue($result);
145    }
146
147    function test_set()
148    {
149        $this->Lock->select('test_user_tbl', 'user_id', '1');
150        $this->Lock->remove();
151        $this->Lock->set('test_user_tbl', 'user_id', '1');
152        $this->Lock->select('test_user_tbl', 'user_id', '1');
153        $result = $this->Lock->islocked();
154        $this->assertTrue($result);
155    }
156
157    function test_remove()
158    {
159        $this->Lock->select('test_user_tbl', 'user_id', '1');
160        $this->Lock->set('test_user_tbl', 'user_id', '1');
161        $this->Lock->remove();
162        $this->Lock->select('test_user_tbl', 'user_id', '1');
163        $result = $this->Lock->islocked();
164        $this->assertFalse($result);
165    }
166
167    function test_removeall()
168    {
169        $this->Lock->select('test_user_tbl', 'user_id', '1');
170        $this->Lock->set('test_user_tbl', 'user_id', '1');
171        $this->Lock->removeall();
172        $this->Lock->select('test_user_tbl', 'user_id', '1');
173        $result = $this->Lock->islocked();
174        $this->assertFalse($result);
175    }
176
177    function test__auto_timeout()
178    {
179        $this->Lock->_auto_timeout();
180    }
181
182    function test_getid()
183    {
184        $this->Lock->select('test_user_tbl', 'user_id', '1');
185        $this->Lock->set('test_user_tbl', 'user_id', '1');
186        $result = $this->Lock->getid();
187        $this->assertTrue('' != $result);
188        $this->assertTrue(is_numeric($result));
189    }
190
191    function test_gettitle()
192    {
193        $this->Lock->select('test_user_tbl', 'user_id', '1');
194        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
195        $result = $this->Lock->gettitle();
196        $this->assertEquals('userlock', $result);
197    }
198
199    function test_geteditor()
200    {
201        $this->Lock->select('test_user_tbl', 'user_id', '1');
202        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
203        $result = $this->Lock->geteditor();
204        $this->assertEquals('testuser', $result);
205    }
206
207    function test_getsecondselapsed()
208    {
209        $result = $this->Lock->getsecondselapsed();
210        $this->assertType('integer', $result);
211    }
212
213}
214// Running the test.
215$suite = new PHPUnit_TestSuite('LockTest');
216$result = PHPUnit::run($suite);
217echo $result->toString();
218?>
Note: See TracBrowser for help on using the repository browser.