source: trunk/tests/AuthSQLTest.php @ 1

Last change on this file since 1 was 1, checked in by scdev, 19 years ago

Initial import.

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