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 | */ |
---|
11 | require_once 'PHPUnit.php'; |
---|
12 | class 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 | |
---|
109 | echo "Testing wrong password...\n"; |
---|
110 | $false = $this->Auth_SQL->authenticate('testuser', 'wrongpass'); |
---|
111 | |
---|
112 | $this->assertfalse($false, 'User login succeeded, but should have failed.'); |
---|
113 | } |
---|
114 | |
---|
115 | function test_login_and_isLoggedIn() |
---|
116 | { |
---|
117 | $login = $this->Auth_SQL->login('testuser', 'testpass'); |
---|
118 | $this->assertTrue($login, '1. User login failed, but should have succeeded.'); |
---|
119 | $before_logged_in = $this->Auth_SQL->isloggedin(); |
---|
120 | $this->assertTrue($before_logged_in, '2. User is not logged in, but should be.'); |
---|
121 | $this->Auth_SQL->clearauth(); |
---|
122 | $after_logged_in = $this->Auth_SQL->isloggedin(); |
---|
123 | $this->assertFalse($after_logged_in, '3. User is still logged in but should not be.'); |
---|
124 | |
---|
125 | echo "Testing wrong password...\n"; |
---|
126 | $login2 = $this->Auth_SQL->login('testuser', 'wrongpass'); |
---|
127 | $this->assertFalse($login2, '4. User login succeeded, but should have failed.'); |
---|
128 | $before_logged_in2 = $this->Auth_SQL->isloggedin(); |
---|
129 | $this->assertFalse($before_logged_in2, '5. User is logged in, but should not be.'); |
---|
130 | $this->Auth_SQL->clearauth(); |
---|
131 | $after_logged_in2 = $this->Auth_SQL->isloggedin(); |
---|
132 | $this->assertFalse($after_logged_in2, '6. Wrong user is still logged in but should not be.'); |
---|
133 | } |
---|
134 | |
---|
135 | function test_requirelogin() |
---|
136 | { |
---|
137 | // $this->Auth_SQL->requirelogin('Login is required!'); |
---|
138 | } |
---|
139 | |
---|
140 | function test_blockaccount() |
---|
141 | { |
---|
142 | $this->Auth_SQL->login('testuser', 'testpass'); |
---|
143 | $this->Auth_SQL->blockaccount(null, 'blocktestuser'); |
---|
144 | $qid = DB::query(" |
---|
145 | SELECT blocked_reason |
---|
146 | FROM test_user_tbl |
---|
147 | "); |
---|
148 | list($reason) = mysql_fetch_row($qid); |
---|
149 | $this->assertEquals('blocktestuser', $reason, "Block not found in DB record."); |
---|
150 | } |
---|
151 | |
---|
152 | function test_unblockaccount() |
---|
153 | { |
---|
154 | DB::query(" |
---|
155 | UPDATE test_user_tbl SET blocked_reason = 'blocktestuser' |
---|
156 | "); |
---|
157 | $this->Auth_SQL->unblockaccount(); |
---|
158 | |
---|
159 | $qid = DB::query(" |
---|
160 | SELECT blocked_reason |
---|
161 | FROM test_user_tbl |
---|
162 | "); |
---|
163 | list($reason) = mysql_fetch_row($qid); |
---|
164 | $this->assertTrue('' == $reason, "Block not removed from DB record."); |
---|
165 | } |
---|
166 | |
---|
167 | function test_usernameexists() |
---|
168 | { |
---|
169 | $result = $this->Auth_SQL->usernameexists('testuser'); |
---|
170 | $this->assertTrue($result); |
---|
171 | } |
---|
172 | |
---|
173 | function test_getusername() |
---|
174 | { |
---|
175 | $result = $this->Auth_SQL->getusername(1); |
---|
176 | $this->assertEquals('testuser', $result); |
---|
177 | } |
---|
178 | |
---|
179 | function test_generatepassword() |
---|
180 | { |
---|
181 | $result = $this->Auth_SQL->generatepassword('xCVcvd'); |
---|
182 | $this->assertRegExp('/[bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZaeiouyAEIOUY0123456789][bcdfghjklmnprstvwxzBCDFGHJKLMNPRSTVWXZ][aeiouyAEIOUY][bcdfghjklmnprstvwxz][aeiouy][0123456789]/', $result, 'Generated password does not match intended pattern'); |
---|
183 | } |
---|
184 | |
---|
185 | function test_encryptpassword() |
---|
186 | { |
---|
187 | $result = $this->Auth_SQL->encryptpassword('123'); |
---|
188 | $this->assertEquals('202cb962ac59075b964b07152d234b70', $result); |
---|
189 | } |
---|
190 | |
---|
191 | function test_setpassword() |
---|
192 | { |
---|
193 | $this->Auth_SQL->setpassword(null, '123'); |
---|
194 | $qid = DB::query(" |
---|
195 | SELECT userpass |
---|
196 | FROM test_user_tbl |
---|
197 | "); |
---|
198 | list($pass) = mysql_fetch_row($qid); |
---|
199 | $this->assertEquals('202cb962ac59075b964b07152d234b70', $pass); |
---|
200 | } |
---|
201 | |
---|
202 | function test_resetpassword() |
---|
203 | { |
---|
204 | $result = $this->Auth_SQL->resetpassword(1, 'Because this is a test.'); |
---|
205 | $this->assertType('array', $result); |
---|
206 | } |
---|
207 | |
---|
208 | // function test_inclearancezone() |
---|
209 | // { |
---|
210 | // $result = $this->Auth_SQL->inclearancezone(PARAM); |
---|
211 | // $expected = EXPECTED_VAL; |
---|
212 | // $this->assertEquals($expected, $result); |
---|
213 | // } |
---|
214 | // |
---|
215 | // function test_requireaccessclearance() |
---|
216 | // { |
---|
217 | // $result = $this->Auth_SQL->requireaccessclearance(PARAM); |
---|
218 | // $expected = EXPECTED_VAL; |
---|
219 | // $this->assertEquals($expected, $result); |
---|
220 | // } |
---|
221 | |
---|
222 | } |
---|
223 | // Running the test. |
---|
224 | $suite = new PHPUnit_TestSuite('Auth_SQLTest'); |
---|
225 | $result = PHPUnit::run($suite); |
---|
226 | echo $result->toString(); |
---|
227 | ?> |
---|