source: branches/2.0singleton/tests/Auth_SQLTest.php @ 647

Last change on this file since 647 was 130, checked in by scdev, 18 years ago

finished updating DB:: to $db->

File size: 7.4 KB
Line 
1<?php
2/**
3 * PHPUnit test case for Auth_SQL
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 Auth_SQLTest extends PHPUnit_TestCase {
13
14    var $Auth_SQL;
15
16    function Auth_SQLTest($name)
17    {
18        $this->PHPUnit_TestCase($name);
19    }
20
21    function setUp()
22    {
23        $db =& DB::getInstance();
24   
25        require dirname(__FILE__) . '/_config.inc.php';
26        require_once '../lib/Auth_SQL.inc.php';
27        $this->Auth_SQL =& new Auth_SQL('testauth');
28        $this->Auth_SQL->setParam(array(
29            'db_table'          => 'test_user_tbl',
30            'db_primary_key'    => 'user_id',
31            'db_login_table'    => 'test_login_tbl',
32            'login_url'         => '/login.php',
33            'blocking'          => true
34        ));
35
36        // Use fresh user table.
37        $this->Auth_SQL->initDB(true);
38
39        // Insert test data.
40        $db->query("
41            INSERT INTO test_user_tbl (
42                username,
43                userpass,
44                first_name,
45                last_name,
46                email,
47                user_type
48            ) VALUES (
49                'testuser',
50                md5('testpass'),
51                'John',
52                'Doe',
53                'root@localhost',
54                'admin'
55            )
56        ");
57
58    }
59
60    function tearDown()
61    {
62        $db =& DB::getInstance();
63   
64        unset($this->Auth_SQL);
65        $db->query("DROP TABLE IF EXISTS test_user_tbl");
66        $db->query("DROP TABLE IF EXISTS test_login_tbl");
67    }
68
69    function test_setval()
70    {
71        $this->Auth_SQL->setval('testuserkey', 'testuserval');
72        $this->assertEquals('testuserval', $_SESSION[$this->Auth_SQL->_sess]['user_data']['testuserkey']);
73    }
74
75    function test_getval()
76    {
77        $_SESSION[$this->Auth_SQL->_sess]['user_data']['testuserkey'] = 'testuserval';
78        $val = $this->Auth_SQL->getVal('testuserkey');
79        $this->assertEquals('testuserval', $val);
80    }
81
82    function test_setparam()
83    {
84        $this->Auth_SQL->setParam(array(
85            'login_url'         => 'testloginurl.php'
86        ));
87        $this->assertEquals('testloginurl.php', $this->Auth_SQL->_params['login_url']);
88    }
89
90    function test_getparam()
91    {
92        $this->Auth_SQL->_params['login_url'] = 'testloginurl.php';
93        $param = $this->Auth_SQL->getParam('login_url');
94        $this->assertEquals('testloginurl.php', $param);
95    }
96
97    function test_clearauth()
98    {
99        $login = $this->Auth_SQL->login('testuser', 'testpass');
100        $this->assertTrue($login, 'User login failed, but should have succeeded.');
101        $before_logged_in = $this->Auth_SQL->isloggedin();
102        $this->assertTrue($before_logged_in, 'User is not logged in, but should be.');
103        $this->Auth_SQL->clearauth();
104        $after_logged_in = $this->Auth_SQL->isloggedin();
105        $this->assertFalse($after_logged_in, 'User is still logged in but should not be.');
106    }
107
108    function test_authenticate()
109    {
110        $true = $this->Auth_SQL->authenticate('testuser', 'testpass');
111        $this->assertTrue($true, 'User login failed, but should have succeeded.');
112
113        echo "Testing wrong password...\n";
114        $false = $this->Auth_SQL->authenticate('testuser', 'wrongpass');
115
116        $this->assertfalse($false, 'User login succeeded, but should have failed.');
117    }
118
119    function test_login_and_isLoggedIn()
120    {
121        $login = $this->Auth_SQL->login('testuser', 'testpass');
122        $this->assertTrue($login, '1. User login failed, but should have succeeded.');
123        $before_logged_in = $this->Auth_SQL->isloggedin();
124        $this->assertTrue($before_logged_in, '2. User is not logged in, but should be.');
125        $this->Auth_SQL->clearauth();
126        $after_logged_in = $this->Auth_SQL->isloggedin();
127        $this->assertFalse($after_logged_in, '3. User is still logged in but should not be.');
128
129        echo "Testing wrong password...\n";
130        $login2 = $this->Auth_SQL->login('testuser', 'wrongpass');
131        $this->assertFalse($login2, '4. User login succeeded, but should have failed.');
132        $before_logged_in2 = $this->Auth_SQL->isloggedin();
133        $this->assertFalse($before_logged_in2, '5. User is logged in, but should not be.');
134        $this->Auth_SQL->clearauth();
135        $after_logged_in2 = $this->Auth_SQL->isloggedin();
136        $this->assertFalse($after_logged_in2, '6. Wrong user is still logged in but should not be.');
137    }
138
139    function test_requirelogin()
140    {
141//         $this->Auth_SQL->requirelogin('Login is required!');
142    }
143
144    function test_blockaccount()
145    {
146        $db =& DB::getInstance();
147   
148        $this->Auth_SQL->login('testuser', 'testpass');
149        $this->Auth_SQL->blockaccount(null, 'blocktestuser');
150        $qid = $db->query("
151            SELECT blocked_reason
152            FROM test_user_tbl
153        ");
154        list($reason) = mysql_fetch_row($qid);
155        $this->assertEquals('blocktestuser', $reason, "Block not found in DB record.");
156    }
157
158    function test_unblockaccount()
159    {
160        $db =& DB::getInstance();
161   
162        $db->query("
163            UPDATE test_user_tbl SET blocked_reason = 'blocktestuser'
164        ");
165        $this->Auth_SQL->unblockaccount();
166
167        $qid = $db->query("
168            SELECT blocked_reason
169            FROM test_user_tbl
170        ");
171        list($reason) = mysql_fetch_row($qid);
172        $this->assertTrue('' == $reason, "Block not removed from DB record.");
173    }
174
175    function test_usernameexists()
176    {
177        $result = $this->Auth_SQL->usernameexists('testuser');
178        $this->assertTrue($result);
179    }
180
181    function test_getusername()
182    {
183        $result = $this->Auth_SQL->getusername(1);
184        $this->assertEquals('testuser', $result);
185    }
186
187    function test_generatepassword()
188    {
189        $result = $this->Auth_SQL->generatepassword('xCVcvd');
190        $this->assertRegExp('/[bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZaeiouyAEIOUY0123456789][bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZ][aeiouyAEIOUY][bcdfghjklmnprstvwxz][aeiouy][0123456789]/', $result, 'Generated password does not match intended pattern');
191    }
192
193    function test_encryptpassword()
194    {
195        $result = $this->Auth_SQL->encryptpassword('123');
196        $this->assertEquals('202cb962ac59075b964b07152d234b70', $result);
197    }
198
199    function test_setpassword()
200    {
201        $db =& DB::getInstance();
202   
203        $this->Auth_SQL->setpassword(null, '123');
204        $qid = $db->query("
205            SELECT userpass
206            FROM test_user_tbl
207        ");
208        list($pass) = mysql_fetch_row($qid);
209        $this->assertEquals('202cb962ac59075b964b07152d234b70', $pass);
210    }
211
212    function test_resetpassword()
213    {
214        $result = $this->Auth_SQL->resetpassword(1, 'Because this is a test.');
215        $this->assertType('array', $result);
216    }
217
218//     function test_inclearancezone()
219//     {
220//         $result = $this->Auth_SQL->inclearancezone(PARAM);
221//         $expected = EXPECTED_VAL;
222//         $this->assertEquals($expected, $result);
223//     }
224//
225//     function test_requireaccessclearance()
226//     {
227//         $result = $this->Auth_SQL->requireaccessclearance(PARAM);
228//         $expected = EXPECTED_VAL;
229//         $this->assertEquals($expected, $result);
230//     }
231
232}
233// Running the test.
234$suite = new PHPUnit_TestSuite('Auth_SQLTest');
235$result = PHPUnit::run($suite);
236echo $result->toString();
237?>
Note: See TracBrowser for help on using the repository browser.