From 1ecb26ec1f88d9f68cad357985de299ab9e3dba6 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Fri, 15 Aug 2014 17:00:13 -0400 Subject: [PATCH] Convert part of task validation to use CodeIgniter's validation library --- application/config/form_validation.php | 4 +- .../libraries/Validation_callbacks.php | 18 +++ application/models/task_model.php | 58 ++----- tests/models/TaskModelNoFixturesTest.php | 147 ++++++++++++++++++ tests/models/TaskModelTest.php | 130 ---------------- 5 files changed, 180 insertions(+), 177 deletions(-) create mode 100644 tests/models/TaskModelNoFixturesTest.php diff --git a/application/config/form_validation.php b/application/config/form_validation.php index 69807c6..792431f 100755 --- a/application/config/form_validation.php +++ b/application/config/form_validation.php @@ -24,12 +24,12 @@ $config = [ [ 'field' => 'due_hour', 'label' => 'Due Hour', - 'rules' => 'min_length[1]|less_than[24]|is_natural' + 'rules' => 'less_than[24]|is_natural' ], [ 'field' => 'due_minute', 'label' => 'Due Minute', - 'rules' => 'min_length[1]|less_than[61]|is_natural' + 'rules' => 'less_than[61]|is_natural' ], [ 'field' => 'due', diff --git a/application/libraries/Validation_callbacks.php b/application/libraries/Validation_callbacks.php index 33a40eb..93b1feb 100755 --- a/application/libraries/Validation_callbacks.php +++ b/application/libraries/Validation_callbacks.php @@ -57,5 +57,23 @@ class Validation_callbacks { return $valid; } + + /** + * Verify that a reminder has a valid due date + * + * @param string $date + * @return bool + */ + public function reminder_due($date) + { + $has_date = ($date != '0'); + + if ( ! $has_date) + { + $this->CI->form_validation->set_message('validate', 'You must set a due date in order to get a reminder.'); + } + + return $has_date; + } } // End of libraries/Validation_callbacks.php \ No newline at end of file diff --git a/application/models/task_model.php b/application/models/task_model.php index ac818f8..2e43c47 100755 --- a/application/models/task_model.php +++ b/application/models/task_model.php @@ -401,49 +401,18 @@ class Task_model extends CI_Model { // Clear previous validations $this->form_vals = NULL; - $title = $this->input->post('title', TRUE); - $desc = $this->input->post('desc', TRUE); - $category = (int) $this->input->post('category'); - $priority = (int) $this->input->post('priority'); $due = $this->input->post('due', TRUE); $due_hour = $this->input->post('due_hour', TRUE); $due_minute = $this->input->post('due_minute', TRUE); - $status = ($this->input->post('status') == FALSE) ? 1 : $this->input->post('status'); - $created = time(); $err = array(); - //Check title - if(strlen($title) < 1) - { - $err[] = "You must give the task a title"; - } - else - { - //Return form values - $this->form_vals['title'] = $title; - } + // Basic validation + $valid = $this->form_validation->run('task'); - //Check description - if(strlen($desc) < 1) + if ( ! $valid) { - $err[] = "The task must have a description"; - } - else - { - //Return form values - $this->form_vals['description'] = $desc; - } - - //Check task category - if((int)$category < 1) - { - $err[] = "Select a task category"; - } - else - { - //Return form values - $this->form_vals['category'] = $category; + $err = array_merge($err, $this->form_validation->get_error_array()); } //Check due date @@ -452,10 +421,8 @@ class Task_model extends CI_Model { //Verify date format $valid = $this->validation_callbacks->due_date($due); - if ( ! $valid) { - $err[] = "You must enter a due date in YYYY-MM-DD format."; return $err; } @@ -537,12 +504,6 @@ class Task_model extends CI_Model { $this->groups = ( ! empty($groups)) ? $groups : FALSE; $this->friends = ( ! empty($friends)) ? $friends : FALSE; $this->share_type = $share_type; - $this->title = $title; - $this->description = $desc; - $this->category = $category; - $this->priority = $priority; - $this->status = $status; - $this->created = $created; $this->due = $due_timestamp; $this->friend_perms = (isset($friend_perms)) ? $friend_perms : FALSE; $this->group_perms = (isset($group_perms)) ? $group_perms : FALSE; @@ -569,12 +530,19 @@ class Task_model extends CI_Model { */ public function add_task() { - $title = $this->title; + $title = $this->input->post('title', TRUE); + $desc = $this->input->post('desc', TRUE); + $category = (int) $this->input->post('category'); + $priority = (int) $this->input->post('priority'); + $status = ($this->input->post('status') == FALSE) ? 1 : $this->input->post('status'); + $created = time(); + + /*$title = $this->title; $desc = $this->description; $category = $this->category; $priority = $this->priority; $status = $this->status; - $created = $this->created; + $created = $this->created;*/ $due = $this->due; $uid = $this->user_id; diff --git a/tests/models/TaskModelNoFixturesTest.php b/tests/models/TaskModelNoFixturesTest.php new file mode 100644 index 0000000..59cd5e1 --- /dev/null +++ b/tests/models/TaskModelNoFixturesTest.php @@ -0,0 +1,147 @@ +CI->load->model('task_model'); + $this->CI->form_validation->reset(); + } + + public function dataValidateTask() + { + return [ + 'Empty task with reminder validation' => [ + 'post' => [ + 'due' => 'April 27, 2014', + 'reminder' => 'rem_true' + ], + 'expected' => [ + 'The Title field is required.', + 'The Description field is required.', + 'The Category field is required.', + 'The Priority field is required.', + 'You must enter a due date in YYYY-MM-DD format.', + 'You must set a due date in order to get a reminder.', + 'You must put numeric hours and minutes for a reminder time.' + ] + ], + 'Empty task with bad due date' => [ + 'post' => [ + 'due' => '165743248576543152', + ], + 'expected' => [ + 'The Title field is required.', + 'The Description field is required.', + 'The Category field is required.', + 'The Priority field is required.', + 'You must enter a due date in YYYY-MM-DD format.' + ] + ], + 'Simple task validation' => [ + 'post' => [ + 'title' => 'A Test Task', + 'desc' => 'A test task to validate with', + 'category' => 7, + 'priority' => 5, + 'due' => '2015-03-09', + ], + 'expected' => TRUE, + /*'form_vals' => [ + 'title' => 'A Test Task', + 'description' => 'A test task to validate with', + 'category' => 7, + 'due' => 1425873600, + 'due_minute' => FALSE + ],*/ + ], + 'task validation with reminder' => [ + 'post' => [ + 'title' => 'A Test Task', + 'desc' => 'A test task to validate with', + 'category' => 7, + 'priority' => 5, + 'due' => '2015-03-09', + 'reminder' => 'rem_true', + 'rem_minutes' => 30, + 'rem_hours' => 4 + ], + 'expected' => TRUE, + /*'form_vals' => [ + 'title' => 'A Test Task', + 'description' => 'A test task to validate with', + 'category' => 7, + 'due' => 1425873600, + 'due_minute' => FALSE, + 'reminder' => TRUE, + 'rem_hours' => 4, + 'rem_minutes' => 30 + ],*/ + ], + 'task validation group shared task' => [ + 'post' => [ + 'title' => 'A Test Task', + 'desc' => 'A test task to validate with', + 'category' => 7, + 'priority' => 5, + 'due' => '2015-03-09', + 'share' => TRUE, + 'group' => [ + '62' + ], + 'group_perms' => 2, + 'friend_perms' => -1 + ], + 'expected' => TRUE, + /*'form_vals' => [ + 'title' => 'A Test Task', + 'description' => 'A test task to validate with', + 'category' => 7, + 'due' => 1425873600, + 'due_minute' => FALSE, + ],*/ + ], + 'task validation user shared task' => [ + 'post' => [ + 'title' => 'A Test Task', + 'desc' => 'A test task to validate with', + 'category' => 7, + 'priority' => 5, + 'due' => '2015-03-09', + 'share' => TRUE, + 'friend' => [3,7], + 'friend_perms' => 2, + 'group_perms' => -1 + ], + 'expected' => TRUE, + /*'form_vals' => [ + 'title' => 'A Test Task', + 'description' => 'A test task to validate with', + 'category' => 7, + 'due' => 1425873600, + 'due_minute' => FALSE, + ],*/ + ], + ]; + } + + /** + * @dataProvider dataValidateTask + */ + public function testValidateTask($post, $expected) + { + $_POST = []; + $_POST = $post; + + $actual = $this->CI->task_model->validate_task(); + + // Verify the form validation data + //$this->assertEquals($form_vals, $this->CI->task_model->form_vals); + + + // Verify the function data + $this->assertEquals($expected, $actual); + } +} +// End of TaskModelNoFixturesTest \ No newline at end of file diff --git a/tests/models/TaskModelTest.php b/tests/models/TaskModelTest.php index c933676..ef09634 100755 --- a/tests/models/TaskModelTest.php +++ b/tests/models/TaskModelTest.php @@ -27,136 +27,6 @@ class TaskModelTest extends Todo_TestCase { parent::setUp(); $this->CI->load->model('task_model'); } - - public function dataValidateTask() - { - return [ - 'Empty task with reminder validation' => [ - 'post' => [ - 'due' => 'April 27, 2014', - 'reminder' => 'rem_true' - ], - 'expected' => [ - 'You must give the task a title', - 'The task must have a description', - 'Select a task category', - 'You must set a due date in order to get a reminder.', - 'You must put numeric hours and minutes for a reminder time.', - ] - ], - 'Empty task with bad due date' => [ - 'post' => [ - 'due' => '165743248576543152', - ], - 'expected' => [ - 'You must give the task a title', - 'The task must have a description', - 'Select a task category', - 'You must enter a due date in YYYY-MM-DD format.' - ] - ], - 'Simple task validation' => [ - 'post' => [ - 'title' => 'A Test Task', - 'desc' => 'A test task to validate with', - 'category' => 7, - 'priority' => 5, - 'due' => '2015-03-09', - ], - 'expected' => TRUE, - 'form_vals' => [ - 'title' => 'A Test Task', - 'description' => 'A test task to validate with', - 'category' => 7, - 'due' => 1425873600, - 'due_minute' => FALSE - ], - ], - 'task validation with reminder' => [ - 'post' => [ - 'title' => 'A Test Task', - 'desc' => 'A test task to validate with', - 'category' => 7, - 'priority' => 5, - 'due' => '2015-03-09', - 'reminder' => 'rem_true', - 'rem_minutes' => 30, - 'rem_hours' => 4 - ], - 'expected' => TRUE, - 'form_vals' => [ - 'title' => 'A Test Task', - 'description' => 'A test task to validate with', - 'category' => 7, - 'due' => 1425873600, - 'due_minute' => FALSE, - 'reminder' => TRUE, - 'rem_hours' => 4, - 'rem_minutes' => 30 - ], - ], - 'task validation group shared task' => [ - 'post' => [ - 'title' => 'A Test Task', - 'desc' => 'A test task to validate with', - 'category' => 7, - 'priority' => 5, - 'due' => '2015-03-09', - 'share' => TRUE, - 'group' => [ - '62' - ], - 'group_perms' => 2, - 'friend_perms' => -1 - ], - 'expected' => TRUE, - 'form_vals' => [ - 'title' => 'A Test Task', - 'description' => 'A test task to validate with', - 'category' => 7, - 'due' => 1425873600, - 'due_minute' => FALSE, - ], - ], - 'task validation user shared task' => [ - 'post' => [ - 'title' => 'A Test Task', - 'desc' => 'A test task to validate with', - 'category' => 7, - 'priority' => 5, - 'due' => '2015-03-09', - 'share' => TRUE, - 'friend' => [3,7], - 'friend_perms' => 2, - 'group_perms' => -1 - ], - 'expected' => TRUE, - 'form_vals' => [ - 'title' => 'A Test Task', - 'description' => 'A test task to validate with', - 'category' => 7, - 'due' => 1425873600, - 'due_minute' => FALSE, - ], - ], - ]; - } - - /** - * @dataProvider dataValidateTask - */ - public function testValidateTask($post, $expected, $form_vals = NULL) - { - $_POST = $post; - - $actual = $this->CI->task_model->validate_task(); - - // Verify the form validation data - $this->assertEquals($form_vals, $this->CI->task_model->form_vals); - - // Verify the function data - $this->assertEquals($expected, $actual); - } public function testGetTaskList() {