1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Unit_tests_secretaries_model extends CI_Driver {
4 private $ci;
5 private $secretary_role_id;
6 private $default_secretary;
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('secretaries_model');
16
17 $this->secretary_role_id = $this->ci->db->get_where('ea_roles',
18 array('slug' => DB_SLUG_SECRETARY))->row()->id;
19
20 $this->default_secretary = 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 secretary user.',
31 'id_roles' => $this->secretary_role_id
32 );
33
34 $this->default_settings = array(
35 'username' => 'test_secretary',
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_secretaries_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 $secretary = $this->default_secretary;
67 $secretary['providers'] = array();
68 $secretary['settings'] = $this->default_settings;
69 $secretary['id'] = $this->ci->secretaries_model->add($secretary);
70
71 $this->ci->unit->run($secretary['id'], 'is_int', 'Test if add() - insert operation - '
72 . 'has returned and integer value.');
73
74 $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array();
75
76 $db_secretary['providers'] = array();
77 $db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
78 array('id_users' => $secretary['id']))->row_array();
79 unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
80 $secretary['settings']['password'], $db_secretary['settings']['password']);
81
82 $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - insert operation - '
83 . 'has successfully inserted a new record.');
84
85 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
86 }
87
88 private function test_add_update() {
89 $secretary = $this->default_secretary;
90 $this->ci->db->insert('ea_users', $secretary);
91 $secretary['id'] = intval($this->ci->db->insert_id());
92
93 $secretary['settings'] = $this->default_settings;
94 $secretary['settings']['id_users'] = $secretary['id'];
95 $this->ci->db->insert('ea_user_settings', $secretary['settings']);
96 unset($secretary['settings']['id_users']);
97
98 $secretary['first_name'] = 'value changed';
99 $secretary['last_name'] = 'value changed';
100 $secretary['email'] = 'value@changed.com';
101 $secretary['mobile_number'] = 'value changed';
102 $secretary['phone_number'] = 'value changed';
103 $secretary['address'] = 'value changed';
104 $secretary['city'] = 'value changed';
105 $secretary['state'] = 'value changed';
106 $secretary['zip_code'] = 'value changed';
107 $secretary['notes'] = 'value changed';
108
109 $secretary['providers'] = array();
110
111 $update_result = $this->ci->secretaries_model->add($secretary);
112 $this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
113 . 'returned an integer value.');
114
115 $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->row_array();
116 $db_secretary['providers'] = array();
117 $db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
118 array('id_users' => $secretary['id']))->row_array();
119 unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
120 $secretary['settings']['password'], $db_secretary['settings']['password']);
121
122 $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has '
123 . 'successfully updated the secretary record.');
124
125 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
126 }
127
128 private function test_add_invalid_data() {
129 $secretary = $this->default_secretary;
130 $secretary['email'] = 'this value is invalid';
131
132 $has_thrown_exc = FALSE;
133 try {
134 $this->ci->secretaries_model->add($secretary);
135 } catch (Exception $exc) {
136 $has_thrown_exc = TRUE;
137 }
138
139 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if add() has thrown exception '
140 . 'on invalid data.');
141 }
142
143 private function disabled_test_add_using_find_record_id() {
144 $secretary = $this->default_secretary;
145 $this->ci->db->insert('ea_users', $secretary);
146 $secretary_id = intval($this->ci->db->insert_id());
147
148 $secretary['settings'] = $this->default_settings;
149 $secretary['settings']['id_users'] = $secretary_id;
150 $this->ci->db->insert('ea_user_settings', $secretary['settings']);
151 unset($secretary['settings']['id_users']);
152
153
154
155
156
157 $secretary['last_name'] = 'updated value';
158 $secretary['providers'] = array();
159 $update_result = $this->ci->secretaries_model->add($secretary);
160 $this->ci->unit->run($update_result, 'is_int', 'Test if add() - update operation - has '
161 . 'returned and integer value.');
162
163 $db_secretary = $this->ci->db->get_where('ea_users', array('id' => $secretary_id))->row_array();
164 $db_secretary['providers'] = array();
165 unset($db_secretary['id']);
166
167 $db_secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
168 array('id_users' => $secretary_id))->row_array();
169 unset($db_secretary['settings']['id_users'], $db_secretary['settings']['salt'],
170 $secretary['settings']['password'], $db_secretary['settings']['password']);
171
172 $this->ci->unit->run($secretary, $db_secretary, 'Test if add() - update operation - has '
173 . 'successfully updated the secretary record using find_record_id() method '
174 . 'internally.');
175
176 $this->ci->db->delete('ea_users', array('id' => $secretary_id));
177 }
178
179
180 private function test_exists_record_exists() {
181 $secretary = $this->default_secretary;
182 $this->ci->db->insert('ea_users', $secretary);
183 $secretary['id'] = intval($this->ci->db->insert_id());
184
185 $exists = $this->ci->secretaries_model->exists($secretary);
186 $this->ci->unit->run($exists, TRUE, 'Test if exists() has returned TRUE with record '
187 . 'that exists.');
188
189 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
190 }
191
192 private function test_exists_record_does_not_exist() {
193 $secretary = $this->default_secretary;
194 $exists = $this->ci->secretaries_model->exists($secretary);
195 $this->ci->unit->run($exists, FALSE, 'Test if exists() has returned FALSE with record '
196 . 'that does not exists.');
197 }
198
199 private function test_exists_invalid_argument() {
200 $secretary = $this->default_secretary;
201 unset($secretary['email']);
202
203 $has_thrown_exc = FALSE;
204 try {
205 $this->ci->secretaries_model->exists($secretary);
206 } catch (Exception $exc) {
207 $has_thrown_exc = TRUE;
208 }
209 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if exists() has thrown exception with '
210 . 'invalid argument (missing email).');
211 }
212
213
214 private function test_find_record_id_record_exists() {
215 $secretary = $this->default_secretary;
216 $this->ci->db->insert('ea_users', $secretary);
217 $secretary['id'] = intval($this->ci->db->insert_id());
218
219 $model_id = $this->ci->secretaries_model->find_record_id($secretary);
220
221 $this->ci->unit->run($model_id, 'is_int', 'Test if find_record_id() has returned '
222 . 'an integer valuel.');
223
224 $this->ci->unit->run($secretary['id'], $model_id, 'Test if find_record_id() has '
225 . 'successfully found the selected record id');
226
227 $this->ci->db->delete('ea_users', array('id' => $model_id));
228 }
229
230 private function test_find_record_id_record_does_not_exist() {
231 $secretary = $this->default_secretary;
232 $has_thrown_exc = FALSE;
233 try {
234 $this->ci->secretaries_model->find_record_id($secretary);
235 } catch (Exception $exc) {
236 $has_thrown_exc = TRUE;
237 }
238 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
239 . 'exception on record that does not exist.');
240 }
241
242 private function test_find_record_id_invalid_argument() {
243 $secretary = $this->default_secretary;
244 unset($secretary['email']);
245 $has_thrown_exc = FALSE;
246 try {
247 $this->ci->secretaries_model->find_record_id($secretary);
248 } catch (Exception $exc) {
249 $has_thrown_exc = TRUE;
250 }
251 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if find_record_id() has thrown '
252 . 'exception on invalid argument given (missing email value).');
253 }
254
255
256 private function test_validate() {
257 $secretary = $this->default_secretary;
258 $validation_result = $this->ci->secretaries_model->validate($secretary);
259 $this->ci->unit->run($validation_result, TRUE, 'Test if validate() has returned TRUE '
260 . 'on valid secretary data.');
261
262 }
263
264 private function test_validate_provided_id_does_not_exist() {
265 $secretary = $this->default_secretary;
266 $secretary['id'] = 'This id does not exist in database.';
267
268 $has_thrown_exc = FALSE;
269 try {
270 $this->ci->secretaries_model->validate($secretary);
271 } catch (Exception $exc) {
272 $has_thrown_exc = TRUE;
273 }
274 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
275 . 'with invalid data (provided id does not exists in db).');
276 }
277
278 private function test_validate_invalid_users_value_data_type() {
279 $secretary = $this->default_secretary;
280 $secretary['providers'] = 'This is not an array';
281
282 $has_thrown_exc = FALSE;
283 try {
284 $this->ci->secretaries_model->validate($secretary);
285 } catch (Exception $exc) {
286 $has_thrown_exc = TRUE;
287 }
288 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
289 . 'with invalid data (users value is not an array).');
290 }
291
292 private function test_validate_missing_required_field_values() {
293 $secretary = $this->default_secretary;
294 unset($secretary['last_name']);
295 unset($secretary['email']);
296 unset($secretary['phone_number']);
297
298 $has_thrown_exc = FALSE;
299 try {
300 $this->ci->secretaries_model->validate($secretary);
301 } catch (Exception $exc) {
302 $has_thrown_exc = TRUE;
303 }
304 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
305 . 'with invalid data (missing required field values).');
306 }
307
308 private function test_validate_invalid_email_address() {
309 $secretary = $this->default_secretary;
310 $secretary['email'] = 'This is invalid.';
311
312 $has_thrown_exc = FALSE;
313 try {
314 $this->ci->secretaries_model->validate($secretary);
315 } catch (Exception $exc) {
316 $has_thrown_exc = TRUE;
317 }
318 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if validate() has thrown an exception '
319 . 'with invalid data (invalid email address).');
320 }
321
322
323 private function test_delete() {
324 $secretary = $this->default_secretary;
325 $this->ci->db->insert('ea_users', $secretary);
326 $secretary['id'] = intval($this->ci->db->insert_id());
327
328 $delete_result = $this->ci->secretaries_model->delete($secretary['id']);
329 $this->ci->unit->run($delete_result, TRUE, 'Test if delete() method has returned TRUE '
330 . 'successfull deletion.');
331
332 $num_rows = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->num_rows();
333 $this->ci->unit->run($num_rows, 0, 'Test if delete() method has successfully deleted '
334 . 'the secretary record.');
335
336 if ($num_rows > 0) {
337 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
338 }
339 }
340
341 private function test_delete_invalid_argument() {
342 $invalid_id = 'This is invalid';
343 $has_thrown_exc = FALSE;
344 try {
345 $this->ci->secretaries_model->delete($invalid_id);
346 } catch (Exception $exc) {
347 $has_thrown_exc = TRUE;
348 }
349 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if delete() has thrown exception on '
350 . 'invalid argument given.');
351 }
352
353 private function test_delete_record_does_not_exist() {
354 $random_id = 23420930923;
355 $delete_result = $this->ci->secretaries_model->delete($random_id);
356 $this->ci->unit->run($delete_result, FALSE, 'Test if delete() method has returned FALSE '
357 . 'when trying to delete a record that does not exist.');
358 }
359
360
361 private function test_get_row() {
362 $secretary = $this->default_secretary;
363 $this->ci->db->insert('ea_users', $secretary);
364 $secretary['id'] = intval($this->ci->db->insert_id());
365
366 $secretary['providers'] = array();
367
368 $secretary['settings'] = $this->default_settings;
369 $secretary['settings']['id_users'] = $secretary['id'];
370 $this->ci->db->insert('ea_user_settings', $secretary['settings']);
371
372 unset($secretary['settings']['id_users'], $secretary['settings']['password']);
373
374 $model_secretary = $this->ci->secretaries_model->get_row($secretary['id']);
375 unset($model_secretary['settings']['password']);
376
377 $this->ci->unit->run($secretary, $model_secretary, 'Test if get_row() has successfully '
378 . 'returned the secretary record.');
379
380 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
381 }
382
383 private function test_get_row_invalid_argument() {
384 $invalid_id = 'this is invalid';
385 $has_thrown_exc = FALSE;
386 try {
387 $this->ci->secretaries_model->get_row($invalid_id);
388 } catch (Exception $exc) {
389 $has_thrown_exc = TRUE;
390 }
391 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown exception '
392 . 'on invalid argument given.');
393 }
394
395 private function test_get_row_record_does_not_exist() {
396 $random_id = 2309203923;
397 $has_thrown_exc = FALSE;
398 try {
399 $this->ci->secretaries_model->get_row($random_id);
400 } catch (Exception $exc) {
401 $has_thrown_exc = TRUE;
402 }
403 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_row() has thrown an exception '
404 . 'when trying to get a record that does not exist in the database.');
405 }
406
407
408 private function test_get_value() {
409 $secretary = $this->default_secretary;
410 $this->ci->db->insert('ea_users', $secretary);
411 $secretary['id'] = intval($this->ci->db->insert_id());
412
413 $field_name = 'last_name';
414
415 $last_name = $this->ci->secretaries_model->get_value($field_name, $secretary['id']);
416 $this->ci->unit->run($secretary['last_name'], $last_name, 'Test if get_value() has '
417 . 'successfully returned the correct value.');
418
419 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
420 }
421
422 private function test_get_value_invalid_field_name() {
423 $field_name = 23423452342;
424 $secretary_id = 1;
425
426 $has_thrown_exc = FALSE;
427 try {
428 $this->ci->secretaries_model->get_value($field_name, $secretary_id);
429 } catch (Exception $exc) {
430 $has_thrown_exc = TRUE;
431 }
432 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
433 . 'on invalid field name.');
434 }
435
436 private function test_get_value_invalid_record_id() {
437 $field_name = 'last_name';
438 $secretary_id = 'this is invalid';
439
440 $has_thrown_exc = FALSE;
441 try {
442 $this->ci->secretaries_model->get_value($field_name, $secretary_id);
443 } catch (Exception $exc) {
444 $has_thrown_exc = TRUE;
445 }
446 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
447 . 'on invalid record id.');
448 }
449
450 private function test_get_value_record_does_not_exist() {
451 $field_name = 'last_name';
452 $secretary_id = 23092093233;
453
454 $has_thrown_exc = FALSE;
455 try {
456 $this->ci->secretaries_model->get_value($field_name, $secretary_id);
457 } catch (Exception $exc) {
458 $has_thrown_exc = TRUE;
459 }
460 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
461 . 'on record does not exist.');
462 }
463
464 private function test_get_value_field_does_not_exist() {
465 $field_name = 'this field name does not exist';
466 $secretary_id = 23;
467
468 $has_thrown_exc = FALSE;
469 try {
470 $this->ci->secretaries_model->get_value($field_name, $secretary_id);
471 } catch (Exception $exc) {
472 $has_thrown_exc = TRUE;
473 }
474 $this->ci->unit->run($has_thrown_exc, TRUE, 'Test if get_value() has thrown exception '
475 . 'on field name does not exist.');
476 }
477
478
479 private function test_get_batch() {
480 $model_batch = $this->ci->secretaries_model->get_batch();
481
482 $no_model_batch = $this->ci->db->get_where('ea_users',
483 array('id_roles' => $this->secretary_role_id))->result_array();
484
485 foreach($no_model_batch as &$secretary) {
486 $providers = $this->ci->db->get_where('ea_secretaries_providers',
487 array('id_users_secretary' => $secretary['id']))->result_array();
488 $secretary['providers'] = array();
489 foreach($providers as $provider) {
490 $secretary['providers'][] = $provider['id_users_provider'];
491 }
492
493 $secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
494 array('id_users' => $secretary['id']))->row_array();
495 unset($secretary['settings']['id_users']);
496 }
497
498 $this->ci->unit->run($model_batch, $no_model_batch, 'Test if get_batch() has '
499 . 'returned the correct results.');
500 }
501
502 private function test_get_batch_where_clause() {
503 $secretary = $this->default_secretary;
504 $this->ci->db->insert('ea_users', $secretary);
505 $secretary['id'] = intval($this->ci->db->insert_id());
506
507 $secretary['settings'] = $this->default_settings;
508 $secretary['settings']['id_users'] = $secretary['id'];
509 $this->ci->db->insert('ea_user_settings', $secretary['settings']);
510 unset($secretary['settings']['id_users']);
511
512 $model_batch = $this->ci->secretaries_model->get_batch(array('id' => $secretary['id']));
513
514 $no_model_batch = $this->ci->db->get_where('ea_users', array('id' => $secretary['id']))->result_array();
515 foreach($no_model_batch as &$secretary) {
516 $providers = $this->ci->db->get_where('ea_secretaries_providers',
517 array('id_users_secretary' => $secretary['id']))->result_array();
518 $secretary['providers'] = array();
519 foreach($providers as $provider) {
520 $secretary['providers'][] = $provider['id_users_provider'];
521 }
522
523 $secretary['settings'] = $this->ci->db->get_where('ea_user_settings',
524 array('id_users' => $secretary['id']))->row_array();
525 unset($secretary['settings']['id_users']);
526 }
527
528 $this->ci->unit->run($model_batch, $no_model_batch, 'Test if get_batch() with where clause '
529 . 'has returned the correct results.');
530
531 $this->ci->db->delete('ea_users', array('id' => $secretary['id']));
532 }
533 }
534
535
536
537