Convert part of task validation to use CodeIgniter's validation library

This commit is contained in:
Timothy Warren 2014-08-15 17:00:13 -04:00
parent 4334d40ecb
commit 1ecb26ec1f
5 changed files with 180 additions and 177 deletions

View File

@ -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',

View File

@ -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

View File

@ -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;

View File

@ -0,0 +1,147 @@
<?php
class TaskModelNoFixturesTest extends Todo_TestCase {
public function setUp()
{
parent::setUp();
$this->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

View File

@ -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()
{