source: trunk/tests/LockTest.php @ 468

Last change on this file since 468 was 468, checked in by anonymous, 10 years ago

Completed integrating /branches/eli_branch into /trunk. Changes include:

  • Removed closing ?> from end of files
  • Upgrade old-style contructor methods to use construct() instead.
  • Class properties and methods defined as public, private, static or protected
  • Ensure code runs under E_ALL with only mysql_* deprecated warnings
  • Search for the '@' symbol anywhere it might be used to supress runtime errors, then replace with proper error recovery.
  • Run the php cli -l option to check files for syntax errors.
  • Bring tests up-to-date with latest version and methods of PHPUnit
File size: 6.3 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        $this->Auth_SQL->login('testuser', 'testpass');
74
75        if (!$this->Auth_SQL->isLoggedIn()) {
76            trigger_error("User login failed...tests canceled.", E_USER_ERROR);
77        }
78
79        $this->Lock =& Lock::getInstance($this->Auth_SQL);
80        $this->Lock->setParam(array('db_table' => 'test_lock_tbl'));
81
82        // Use fresh lock table.
83        $this->Lock->initDB(true);
84    }
85
86    function tearDown()
87    {
88        $db =& DB::getInstance();
89   
90        unset($this->Lock);
91        unset($this->Auth_SQL);
92        $db->query("DROP TABLE IF EXISTS test_user_tbl");
93        $db->query("DROP TABLE IF EXISTS test_login_tbl");
94        $db->query("DROP TABLE IF EXISTS test_lock_tbl");
95    }
96
97    function test_getinstance()
98    {
99        $result = Lock::getinstance($this->Auth_SQL);
100        $this->assertTrue($result && is_a($result, 'Lock'));
101    }
102
103    function test_setparam()
104    {
105        $this->Lock->setparam(array('var'=>'val'));
106    }
107
108    function test_getparam()
109    {
110        $this->Lock->getparam('var');
111    }
112
113    function test_select()
114    {
115        $this->Lock->set('test_user_tbl', 'user_id', '1');
116        $result = $this->Lock->select('test_user_tbl', 'user_id', '1');
117        $this->assertTrue($result);
118    }
119
120    function test_islocked()
121    {
122        $this->Lock->select('test_user_tbl', 'user_id', '1');
123
124        $this->Lock->set('test_user_tbl', 'user_id', '1');
125        $result = $this->Lock->islocked();
126        $this->assertTrue($result, 'Lock was not set.');
127
128        $this->Lock->remove();
129        $this->Lock->select('test_user_tbl', 'user_id', '1');
130        $result = $this->Lock->islocked();
131        $this->assertFalse($result, 'Lock was not removed.');
132    }
133
134    function test_ismine()
135    {
136        $this->Lock->set('test_user_tbl', 'user_id', '1');
137        $this->Lock->select('test_user_tbl', 'user_id', '1');
138        $result = $this->Lock->ismine();
139        $this->assertTrue($result);
140    }
141
142    function test_set()
143    {
144        $this->Lock->select('test_user_tbl', 'user_id', '1');
145        $this->Lock->remove();
146        $this->Lock->set('test_user_tbl', 'user_id', '1');
147        $this->Lock->select('test_user_tbl', 'user_id', '1');
148        $result = $this->Lock->islocked();
149        $this->assertTrue($result);
150    }
151
152    function test_remove()
153    {
154        $this->Lock->select('test_user_tbl', 'user_id', '1');
155        $this->Lock->set('test_user_tbl', 'user_id', '1');
156        $this->Lock->remove();
157        $this->Lock->select('test_user_tbl', 'user_id', '1');
158        $result = $this->Lock->islocked();
159        $this->assertFalse($result);
160    }
161
162    function test_removeall()
163    {
164        $this->Lock->select('test_user_tbl', 'user_id', '1');
165        $this->Lock->set('test_user_tbl', 'user_id', '1');
166        $this->Lock->removeall();
167        $this->Lock->select('test_user_tbl', 'user_id', '1');
168        $result = $this->Lock->islocked();
169        $this->assertFalse($result);
170    }
171
172    function test__auto_timeout()
173    {
174        $this->Lock->_auto_timeout();
175    }
176
177    function test_getid()
178    {
179        $this->Lock->select('test_user_tbl', 'user_id', '1');
180        $this->Lock->set('test_user_tbl', 'user_id', '1');
181        $result = $this->Lock->getid();
182        $this->assertTrue('' != $result);
183        $this->assertTrue(is_numeric($result));
184    }
185
186    function test_gettitle()
187    {
188        $this->Lock->select('test_user_tbl', 'user_id', '1');
189        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
190        $result = $this->Lock->gettitle();
191        $this->assertEquals('userlock', $result);
192    }
193
194    function test_geteditor()
195    {
196        $this->Lock->select('test_user_tbl', 'user_id', '1');
197        $this->Lock->set('test_user_tbl', 'user_id', '1', 'userlock');
198        $result = $this->Lock->geteditor();
199        $this->assertEquals('testuser', $result);
200    }
201
202    function test_getsecondselapsed()
203    {
204        $result = $this->Lock->getsecondselapsed();
205        $this->assertInternalType('integer', $result);
206    }
207
208}
Note: See TracBrowser for help on using the repository browser.