1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_admins_model extends CI_Driver {
4 private $ci;
5 private $admin_role_id;
6 private $default_admin;
7 private $default_settings;
8
9 10 11
12 public function __construct() {
13 $this->ci =& get_instance();
14 $this->ci->load->library('Unit_test');
15 $this->ci->load->model('admins_model');
16
17 $this->admin_role_id = $this->ci->db->get_where('ea_roles',
18 array('slug' => DB_SLUG_ADMIN))->row()->id;
19
20 $this->default_admin = array(
21 'first_name' => 'John',
22 'last_name' => 'Doe',
23 'email' => 'john@doe.com',
24 'mobile_number' => '2340982039',
25 'phone_number' => '9098091234',
26 'address' => 'Some Street 80',
27 'city' => 'Some City',
28 'state' => 'Some State',
29 'zip_code' => '12345',
30 'notes' => 'This is a test admin user.',
31 'id_roles' => $this->admin_role_id
32 );
33
34 $this->default_settings = array(
35 'username' => 'test_admin',
36 'password' => 'test_pswd',
37 'working_plan' => NULL,
38 'notifications' => FALSE,
39 'google_sync' => 0,
40 'google_token' => NULL,
41 'sync_past_days' => NULL,
42 'sync_future_days' => NULL
43 );
44 }
45
46 47 48
49 public function run_all() {
50
51
52
53 $class_methods = get_class_methods('Unit_tests_admins_model');
54 foreach ($class_methods as $method_name) {
55 if (substr($method_name, 0, 5) === 'test_') {
56 call_user_func(array($this, $method_name));
57 }
58 }
59 }
60
61
62
63
64
65 private function test_add_insert() {
66 $admin = $this->default_admin;
67 $admin['settings'] = $this->default_settings;
68
69 $admin['id'] = $this->ci->admins_model->add($admin);
70 $this->ci->unit->run($admin['id'], 'is_int', 'Test if add() - insert operation - has '
71 . 'has returned an integer value.');
72
73 $db_record = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->row_array();
74
75 $db_record['settings'] = $this->ci->db->get_where('ea_user_settings',
76 array('id_users' => $admin['id']))->row_array();
77 unset($db_record['settings']['id_users'], $db_record['settings']['salt'],
78 $admin['settings']['password'], $db_record['settings']['password']);
79 $this->ci->unit->run($admin, $db_record, 'Test if add() - insert operation - has '
80 . 'successfully inserted a new admin record.');
81
82 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
83 }
84
85 private function test_add_update() {
86
87 $admin = $this->default_admin;
88 $this->ci->db->insert('ea_users', $admin);
89 $admin['id'] = intval($this->ci->db->insert_id());
90 $admin['settings'] = $this->default_settings;
91 $admin['settings']['id_users'] = $admin['id'];
92 $this->ci->db->insert('ea_user_settings', $admin['settings']);
93 unset($admin['settings']['id_users']);
94
95 $admin['first_name'] = 'First Name Changed';
96 $admin['last_name'] = 'Last Name Changed';
97 $admin['email'] = 'email@changed.com';
98 $admin['mobile_number'] = 'Mobile Number Changed';
99 $admin['phone_number'] = 'Phone Number Changed';
100 $admin['address'] = 'Address Changed';
101 $admin['city'] = 'City Changed';
102 $admin['zip_code'] = 'Zip Code Changed';
103 $admin['notes'] = 'Notes Changed';
104
105 $update_result = $this->ci->admins_model->add($admin);
106 $this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
107 . 'returned a valid integer value.');
108
109 $db_record = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->row_array();
110 $db_record['settings'] = $this->ci->db->get_where('ea_user_settings', array('id_users' => $admin['id']))->row_array();
111 unset($db_record['settings']['id_users'], $db_record['settings']['salt'],
112 $admin['settings']['password'], $db_record['settings']['password']);
113
114 $this->ci->unit->run($admin, $db_record, 'Test if add() - update operation - has '
115 . 'successfully updated an existing admin record.');
116
117 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
118 }
119
120 private function test_add_with_invalid_data() {
121 $admin = $this->default_admin;
122 $admin['email'] = 'Invalid Email Value';
123
124 $has_thrown_exc = FALSE;
125 try {
126 $this->ci->admins_model->add($admin);
127 } catch(Exception $exc) {
128 $has_thrown_exc = TRUE;
129 }
130
131 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() has thrown an exception with '
132 . 'invalid data.');
133 }
134
135
136
137 private function test_exists_with_record_that_exists() {
138 $admin = $this->default_admin;
139 $this->ci->db->insert('ea_users', $admin);
140 $admin['id'] = intval($this->ci->db->insert_id());
141
142 $exists = $this->ci->admins_model->exists($admin);
143 $this->ci->unit->run($exists, TRUE, 'Test if exists() has returned TRUE on record '
144 . 'that exists.');
145
146 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
147 }
148
149 private function test_exists_with_invalid_data() {
150 $admin = $this->default_admin;
151 unset($admin['email']);
152
153 $has_thrown_exc = FALSE;
154 try {
155 $this->ci->admins_model->exists($admin);
156 } catch(Exception $exc) {
157 $has_thrown_exc = TRUE;
158 }
159
160 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if exists() has thrown an exception '
161 . 'on invalid admin data.');
162 }
163
164 private function test_exists_with_record_that_does_not_exist() {
165 $admin = $this->default_admin;
166 $exists = $this->ci->admins_model->exists($admin);
167 $this->ci->unit->run($exists, FALSE, 'Test if exists() returned FALSE with record '
168 . 'that does not exist.');
169 }
170
171
172 private function test_delete() {
173 $admin = $this->default_admin;
174 $this->ci->db->insert('ea_users', $admin);
175 $admin['id'] = intval($this->ci->db->insert_id());
176
177 $delete_result = $this->ci->admins_model->delete($admin['id']);
178 $this->ci->unit->run($delete_result, TRUE, 'Test if delete() has returned TRUE on '
179 . 'successfull deletion.');
180
181 $num_rows = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->num_rows();
182 $this->ci->unit->run($num_rows, 0, 'Test if delete() has successfully removed the '
183 . 'admin record from the database.');
184
185 if ($num_rows > 0) {
186 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
187 }
188 }
189
190 private function test_delete_with_record_that_does_not_exist() {
191 $random_id = 2340923234;
192 $has_thrown_exc = FALSE;
193 try {
194 $this->ci->admins_model->delete($random_id);
195 } catch(Exception $exc) {
196 $has_thrown_exc = TRUE;
197 }
198 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
199 . 'on record that does not exist.');
200 }
201
202 private function test_delete_with_invalid_id_argument() {
203 $invalid_id = 'Not Numeric Value';
204 $has_thrown_exc = FALSE;
205 try {
206 $this->ci->admins_model->delete($invalid_id);
207 } catch(Exception $exc) {
208 $has_thrown_exc = TRUE;
209 }
210 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
211 . 'on invalid id argument.');
212 }
213
214 private function test_delete_when_only_one_admin_user_left() {
215
216
217 $admin_id = $this->ci->db->get_where('ea_users', array('id_roles' => $this->admin_role_id))
218 ->row()->id;
219
220 $has_thrown_exc = FALSE;
221 try {
222 $this->ci->admins_model->delete($admin_id);
223 } catch(Exception $exc) {
224 $has_thrown_exc = TRUE;
225 }
226 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception '
227 . 'when trying to delete the only one left admin record.');
228 }
229
230
231 private function test_find_record_id() {
232 $admin = $this->default_admin;
233 $this->ci->db->insert('ea_users', $admin);
234 $admin['id'] = intval($this->ci->db->insert_id());
235
236 $result = $this->ci->admins_model->find_record_id($admin);
237 $this->ci->unit->run($result, $admin['id'], 'Test if find_record_id() has successfully '
238 . 'found the correct record id.');
239
240 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
241 }
242
243 private function test_find_record_id_with_no_email_provided() {
244 $admin = $this->default_admin;
245 unset($admin['email']);
246
247 $has_thrown_exc = FALSE;
248 try {
249 $this->ci->admins_model->find_record_id($admin);
250 } catch(Exception $exc) {
251 $has_thrown_exc = TRUE;
252 }
253 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
254 . 'an exception when trying to find a record id without email provided.');
255 }
256
257 private function test_find_record_id_with_record_that_does_not_exist() {
258 $admin = $this->default_admin;
259 $has_thrown_ecx = FALSE;
260 try {
261 $this->ci->admins_model->find_record_id($admin);
262 } catch (Exception $exc) {
263 $has_thrown_ecx = TRUE;
264 }
265 $this->ci->unit->run($has_thrown_ecx, TRUE, 'Test if find_record_id() has thrown '
266 . 'exception when trying to find record that does not exist.');
267 }
268
269
270
271 private function test_get_batch() {
272 $model_batch = $this->ci->admins_model->get_batch();
273
274 $db_batch = $this->ci->db->get_where('ea_users',
275 array('id_roles' => $this->admin_role_id))->result_array();
276
277 foreach($db_batch as &$admin) {
278 $admin['settings'] = $this->ci->db->get_where('ea_user_settings',
279 array('id_users' => $admin['id']))->row_array();
280 unset($admin['settings']['id_users']);
281 }
282
283
284 $this->ci->unit->run($model_batch, $db_batch, 'Test if get_batch() has successfully '
285 . 'returned an array of admin users.');
286 }
287
288 private function test_get_batch_with_where_clause() {
289 $admin = $this->default_admin;
290 $this->ci->db->insert('ea_users', $admin);
291 $admin['id'] = intval($this->ci->db->insert_id());
292
293 $model_batch = $this->ci->admins_model->get_batch(array('id' => $admin['id']));
294 $db_batch = $this->ci->db->get_where('ea_users', array('id' => $admin['id']))->result_array();
295 foreach($db_batch as &$admin) {
296 $admin['settings'] = array();
297 }
298
299 $this->ci->unit->run($model_batch, $db_batch, 'Test if get_batch() with where clause '
300 . 'has successfully returned the correct results.');
301
302 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
303 }
304
305 private function test_get_batch_with_invalid_where_clause() {
306
307 }
308
309
310 private function test_get_row() {
311 $admin = $this->default_admin;
312 $this->ci->db->insert('ea_users', $admin);
313 $admin['id'] = intval($this->ci->db->insert_id());
314 $admin['settings'] = array();
315
316 $model_admin = $this->ci->admins_model->get_row($admin['id']);
317 $this->ci->unit->run($model_admin, $admin, 'Test if get_row() has successfully '
318 . 'returned the data of the selected row.');
319
320 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
321 }
322
323 private function test_get_row_invalid_argument() {
324 $invalid_id = 'This is not numeric.';
325 $has_thrown_exc = FALSE;
326 try {
327 $this->ci->admins_model->get_row($invalid_id);
328 } catch (Exception $exc) {
329 $has_thrown_exc = TRUE;
330 }
331 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception with '
332 . 'invalid argument.');
333 }
334
335 private function test_get_row_record_does_not_exist() {
336 $random_id = 2390482039;
337 $has_thrown_exc = FALSE;
338 try {
339 $this->ci->admins_model->get_row($random_id);
340 } catch(Exception $exc) {
341 $has_thrown_exc = TRUE;
342 }
343
344 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception '
345 . 'with record that does not exist.');
346 }
347
348
349 private function test_get_value() {
350 $admin = $this->default_admin;
351 $this->ci->db->insert('ea_users', $admin);
352 $admin['id'] = intval($this->ci->db->insert_id());
353
354 $last_name = $this->ci->admins_model->get_value('last_name', $admin['id']);
355 $this->ci->unit->run($last_name, $admin['last_name'], 'Test if get_value() has successfully '
356 . 'returned the correct value.');
357
358 $this->ci->db->delete('ea_users', array('id' => $admin['id']));
359 }
360
361 private function test_get_value_invalid_field_name() {
362 $field_name = 230982039;
363 $admin_id = 23;
364
365 $has_thrown_exc = FALSE;
366 try {
367 $this->ci->admins_model->get_value($field_name, $admin_id);
368 } catch (Exception $exc) {
369 $has_thrown_exc = TRUE;
370 }
371
372 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
373 . 'with invalid field name argument.');
374 }
375
376 private function test_get_value_invalid_admin_id() {
377 $field_name = 'last_name';
378 $admin_id = 'This is invalid';
379
380 $has_thrown_exc = FALSE;
381 try {
382 $this->ci->admins_model->get_value($field_name, $admin_id);
383 } catch (Exception $exc) {
384 $has_thrown_exc = TRUE;
385 }
386
387 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
388 . 'with invalid admin id argument.');
389 }
390
391 private function test_get_value_record_does_not_exist() {
392 $field_name = 'last_name';
393 $admin_id = 239409283092;
394
395
396 $has_thrown_exc = FALSE;
397 try {
398 $this->ci->admins_model->get_value($field_name, $admin_id);
399 } catch (Exception $exc) {
400 $has_thrown_exc = TRUE;
401 }
402
403 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
404 . 'with record that does not exist.');
405 }
406
407 private function tes_get_value_field_name_does_not_exist() {
408 $field_name = 'this does not exist';
409 $admin_id = 23;
410
411
412 $has_thrown_exc = FALSE;
413 try {
414 $this->ci->admins_model->get_value($field_name, $admin_id);
415 } catch (Exception $exc) {
416 $has_thrown_exc = TRUE;
417 }
418
419 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown an exception '
420 . 'with field name that does not exist.');
421 }
422
423
424 private function test_validate() {
425 $admin = $this->default_admin;
426 $validation_result = $this->ci->admins_model->validate($admin);
427 $this->ci->unit->run($validation_result, TRUE, 'Test if validate() has returned TRUE on '
428 . 'valid admin data.');
429 }
430
431 private function test_validate_record_does_not_exist() {
432 $admin = $this->default_admin;
433 $admin['id'] = 234092830;
434
435 $has_thrown_exc = FALSE;
436 try {
437 $this->ci->admins_model->validate($admin);
438 } catch (Exception $exc) {
439 $has_thrown_exc = TRUE;
440 }
441 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
442 . 'with record that does not exist.');
443 }
444
445 private function test_validate_missing_required_fields() {
446
447
448 $admin = $this->default_admin;
449 unset($admin['last_name']);
450 unset($admin['email']);
451 unset($admin['phone_number']);
452 $has_thrown_exc = FALSE;
453 try {
454 $this->ci->admins_model->validate($admin);
455 } catch (Exception $exc) {
456 $has_thrown_exc = TRUE;
457 }
458 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
459 . 'with missing required field values.');
460 }
461
462 private function test_validate_invalid_email() {
463 $admin = $this->default_admin;
464 $admin['email'] = 'This is invalid';
465 $has_thrown_exc = FALSE;
466 try {
467 $this->ci->admins_model->validate($admin);
468 } catch (Exception $exc) {
469 $has_thrown_exc = TRUE;
470 }
471 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
472 . 'with invalid email address.');
473 }
474 }
475
476
477