source: trunk/tests/LockTest.php

Last change on this file was 695, checked in by anonymous, 5 years ago

Change usage of is_a($x, 'X') to $x instance of X

File size: 6.4 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-2012 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 */
32
33class LockTest extends PHPUnit_Framework_TestCase {
34
35    var $Lock;
36    var $Auth_SQL;
37
38    function setUp()
39    {
40        require dirname(__FILE__) . '/_config.inc.php';
41        require_once '../lib/Lock.inc.php';
42        require_once '../lib/Auth_SQL.inc.php';
43
44        $this->Auth_SQL = new Auth_SQL('test');
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.
57        $db =& DB::getInstance();
58        $db->query("
59            INSERT INTO test_user_tbl (
60                username,
61                userpass,
62                first_name,
63                last_name,
64                email
65            ) VALUES (
66                'testuser',
67                '" . $this->Auth_SQL->encryptPassword('testpass') . "',
68                'John',
69                'Doe',
70                'john@example.com'
71            )
72        ");
73
74        // Sessions require client IP addr.
75        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
76
77        $this->Auth_SQL->login('testuser', 'testpass');
78
79        if (!$this->Auth_SQL->isLoggedIn()) {
80            trigger_error("User login failed...tests canceled.", E_USER_ERROR);
81        }
82
83        $this->Lock =& Lock::getInstance($this->Auth_SQL);
84        $this->Lock->setParam(array('db_table' => 'test_lock_tbl'));
85
86        // Use fresh lock table.
87        $this->Lock->initDB(true);
88    }
89
90    function tearDown()
91    {
92        $db =& DB::getInstance();
93
94        unset($this->Lock);
95        unset($this->Auth_SQL);
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");
99    }
100
101    function test_getinstance()
102    {
103        $result = Lock::getinstance($this->Auth_SQL);
104        $this->assertTrue($result && $result instanceof \Lock);
105    }
106
107    function test_setparam()
108    {
109        $this->Lock->setparam(array('var'=>'val'));
110    }
111
112    function test_getparam()
113    {
114        $this->Lock->getparam('var');
115    }
116
117    function test_select()
118    {
119        $this->Lock->set('test_user_tbl', 'user_id', '1');
120        $result = $this->Lock->select('test_user_tbl', 'user_id', '1');
121        $this->assertTrue($result);
122    }
123
124    function test_islocked()
125    {
126        $this->Lock->select('test_user_tbl', 'user_id', '1');
127
128        $this->Lock->set('test_user_tbl', 'user_id', '1');
129        $result = $this->Lock->islocked();
130        $this->assertTrue($result, 'Lock was not set.');
131
132        $this->Lock->remove();
133        $this->Lock->select('test_user_tbl', 'user_id', '1');
134        $result = $this->Lock->islocked();
135        $this->assertFalse($result, 'Lock was not removed.');
136    }
137
138    function test_ismine()
139    {
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();
143        $this->assertTrue($result);
144    }
145
146    function test_set()
147    {
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();
153        $this->assertTrue($result);
154    }
155
156    function test_remove()
157    {
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();
163        $this->assertFalse($result);
164    }
165
166    function test_removeall()
167    {
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();
173        $this->assertFalse($result);
174    }
175
176    function test__auto_timeout()
177    {
178        $this->Lock->_auto_timeout();
179    }
180
181    function test_getid()
182    {
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();
186        $this->assertTrue('' != $result);
187        $this->assertTrue(is_numeric($result));
188    }
189
190    function test_gettitle()
191    {
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();
195        $this->assertEquals('userlock', $result);
196    }
197
198    function test_geteditor()
199    {
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();
203        $this->assertEquals('testuser', $result);
204    }
205
206    function test_getsecondselapsed()
207    {
208        $result = $this->Lock->getsecondselapsed();
209        $this->assertInternalType('integer', $result);
210    }
211
212}
Note: See TracBrowser for help on using the repository browser.