Browse Source

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

Timothy J. Warren 3 years ago
parent
commit
1ecb26ec1f

+ 2
- 2
application/config/form_validation.php View File

@@ -24,12 +24,12 @@ $config = [
24 24
 		[
25 25
 			'field' => 'due_hour',
26 26
 			'label' => 'Due Hour',
27
-			'rules' => 'min_length[1]|less_than[24]|is_natural'
27
+			'rules' => 'less_than[24]|is_natural'
28 28
 		],
29 29
 		[
30 30
 			'field' => 'due_minute',
31 31
 			'label' => 'Due Minute',
32
-			'rules' => 'min_length[1]|less_than[61]|is_natural'
32
+			'rules' => 'less_than[61]|is_natural'
33 33
 		],
34 34
 		[
35 35
 			'field' => 'due',

+ 18
- 0
application/libraries/Validation_callbacks.php View File

@@ -57,5 +57,23 @@ class Validation_callbacks {
57 57
 
58 58
 		return $valid;
59 59
 	}
60
+
61
+	/**
62
+	 * Verify that a reminder has a valid due date
63
+	 *
64
+	 * @param string $date
65
+	 * @return bool
66
+	 */
67
+	public function reminder_due($date)
68
+	{
69
+		$has_date = ($date != '0');
70
+
71
+		if ( ! $has_date)
72
+		{
73
+			$this->CI->form_validation->set_message('validate', 'You must set a due date in order to get a reminder.');
74
+		}
75
+
76
+		return $has_date;
77
+	}
60 78
 }
61 79
 // End of libraries/Validation_callbacks.php

+ 13
- 45
application/models/task_model.php View File

@@ -401,49 +401,18 @@ class Task_model extends CI_Model {
401 401
 		// Clear previous validations
402 402
 		$this->form_vals = NULL;
403 403
 
404
-		$title = $this->input->post('title', TRUE);
405
-		$desc = $this->input->post('desc', TRUE);
406
-		$category = (int) $this->input->post('category');
407
-		$priority = (int) $this->input->post('priority');
408 404
 		$due = $this->input->post('due', TRUE);
409 405
 		$due_hour = $this->input->post('due_hour', TRUE);
410 406
 		$due_minute = $this->input->post('due_minute', TRUE);
411
-		$status = ($this->input->post('status') == FALSE) ? 1 : $this->input->post('status');
412
-		$created = time();
413 407
 
414 408
 		$err = array();
415 409
 
416
-		//Check title
417
-		if(strlen($title) < 1)
418
-		{
419
-			$err[] = "You must give the task a title";
420
-		}
421
-		else
422
-		{
423
-			//Return form values
424
-			$this->form_vals['title'] = $title;
425
-		}
426
-
427
-		//Check description
428
-		if(strlen($desc) < 1)
429
-		{
430
-			$err[] = "The task must have a description";
431
-		}
432
-		else
433
-		{
434
-			//Return form values
435
-			$this->form_vals['description'] = $desc;
436
-		}
410
+		// Basic validation
411
+		$valid = $this->form_validation->run('task');
437 412
 
438
-		//Check task category
439
-		if((int)$category < 1)
440
-		{
441
-			$err[] = "Select a task category";
442
-		}
443
-		else
413
+		if ( ! $valid)
444 414
 		{
445
-			//Return form values
446
-			$this->form_vals['category'] = $category;
415
+			$err = array_merge($err, $this->form_validation->get_error_array());
447 416
 		}
448 417
 
449 418
 		//Check due date
@@ -452,10 +421,8 @@ class Task_model extends CI_Model {
452 421
 			//Verify date format
453 422
 			$valid = $this->validation_callbacks->due_date($due);
454 423
 
455
-
456 424
 			if ( ! $valid)
457 425
 			{
458
-				$err[] = "You must enter a due date in YYYY-MM-DD format.";
459 426
 				return $err;
460 427
 			}
461 428
 
@@ -537,12 +504,6 @@ class Task_model extends CI_Model {
537 504
 			$this->groups = ( ! empty($groups)) ? $groups : FALSE;
538 505
 			$this->friends = ( ! empty($friends)) ? $friends : FALSE;
539 506
 			$this->share_type = $share_type;
540
-			$this->title = $title;
541
-			$this->description = $desc;
542
-			$this->category = $category;
543
-			$this->priority = $priority;
544
-			$this->status = $status;
545
-			$this->created = $created;
546 507
 			$this->due = $due_timestamp;
547 508
 			$this->friend_perms = (isset($friend_perms)) ? $friend_perms : FALSE;
548 509
 			$this->group_perms = (isset($group_perms)) ? $group_perms : FALSE;
@@ -569,12 +530,19 @@ class Task_model extends CI_Model {
569 530
 	 */
570 531
 	public function add_task()
571 532
 	{
572
-		$title = $this->title;
533
+		$title = $this->input->post('title', TRUE);
534
+		$desc = $this->input->post('desc', TRUE);
535
+		$category = (int) $this->input->post('category');
536
+		$priority = (int) $this->input->post('priority');
537
+		$status = ($this->input->post('status') == FALSE) ? 1 : $this->input->post('status');
538
+		$created = time();
539
+
540
+		/*$title = $this->title;
573 541
 		$desc = $this->description;
574 542
 		$category = $this->category;
575 543
 		$priority = $this->priority;
576 544
 		$status = $this->status;
577
-		$created = $this->created;
545
+		$created = $this->created;*/
578 546
 		$due = $this->due;
579 547
 		$uid = $this->user_id;
580 548
 

+ 147
- 0
tests/models/TaskModelNoFixturesTest.php View File

@@ -0,0 +1,147 @@
1
+<?php
2
+
3
+class TaskModelNoFixturesTest extends Todo_TestCase {
4
+
5
+	public function setUp()
6
+	{
7
+		parent::setUp();
8
+		$this->CI->load->model('task_model');
9
+		$this->CI->form_validation->reset();
10
+	}
11
+
12
+	public function dataValidateTask()
13
+	{
14
+		return [
15
+			'Empty task with reminder validation' => [
16
+				'post' => [
17
+					'due' => 'April 27, 2014',
18
+					'reminder' => 'rem_true'
19
+				],
20
+				'expected' => [
21
+					'The Title field is required.',
22
+					'The Description field is required.',
23
+					'The Category field is required.',
24
+					'The Priority field is required.',
25
+					'You must enter a due date in YYYY-MM-DD format.',
26
+					'You must set a due date in order to get a reminder.',
27
+					'You must put numeric hours and minutes for a reminder time.'
28
+				]
29
+			],
30
+			'Empty task with bad due date' => [
31
+				'post' => [
32
+					'due' => '165743248576543152',
33
+				],
34
+				'expected' => [
35
+					'The Title field is required.',
36
+					'The Description field is required.',
37
+					'The Category field is required.',
38
+					'The Priority field is required.',
39
+					'You must enter a due date in YYYY-MM-DD format.'
40
+				]
41
+			],
42
+			'Simple task validation' => [
43
+				'post' => [
44
+					'title' => 'A Test Task',
45
+					'desc' => 'A test task to validate with',
46
+					'category' => 7,
47
+					'priority' => 5,
48
+					'due' => '2015-03-09',
49
+				],
50
+				'expected' => TRUE,
51
+				/*'form_vals' => [
52
+					'title' => 'A Test Task',
53
+					'description' => 'A test task to validate with',
54
+					'category' => 7,
55
+					'due' => 1425873600,
56
+					'due_minute' => FALSE
57
+				],*/
58
+			],
59
+			'task validation with reminder' => [
60
+				'post' => [
61
+					'title' => 'A Test Task',
62
+					'desc' => 'A test task to validate with',
63
+					'category' => 7,
64
+					'priority' => 5,
65
+					'due' => '2015-03-09',
66
+					'reminder' => 'rem_true',
67
+					'rem_minutes' => 30,
68
+					'rem_hours' => 4
69
+				],
70
+				'expected' => TRUE,
71
+				/*'form_vals' => [
72
+					'title' => 'A Test Task',
73
+					'description' => 'A test task to validate with',
74
+					'category' => 7,
75
+					'due' => 1425873600,
76
+					'due_minute' => FALSE,
77
+					'reminder' => TRUE,
78
+					'rem_hours' => 4,
79
+					'rem_minutes' => 30
80
+				],*/
81
+			],
82
+			'task validation group shared task' => [
83
+				'post' => [
84
+					'title' => 'A Test Task',
85
+					'desc' => 'A test task to validate with',
86
+					'category' => 7,
87
+					'priority' => 5,
88
+					'due' => '2015-03-09',
89
+					'share' => TRUE,
90
+					'group' => [
91
+						'62'
92
+					],
93
+					'group_perms' => 2,
94
+					'friend_perms' => -1
95
+				],
96
+				'expected' => TRUE,
97
+				/*'form_vals' => [
98
+					'title' => 'A Test Task',
99
+					'description' => 'A test task to validate with',
100
+					'category' => 7,
101
+					'due' => 1425873600,
102
+					'due_minute' => FALSE,
103
+				],*/
104
+			],
105
+			'task validation user shared task' => [
106
+				'post' => [
107
+					'title' => 'A Test Task',
108
+					'desc' => 'A test task to validate with',
109
+					'category' => 7,
110
+					'priority' => 5,
111
+					'due' => '2015-03-09',
112
+					'share' => TRUE,
113
+					'friend' => [3,7],
114
+					'friend_perms' => 2,
115
+					'group_perms' => -1
116
+				],
117
+				'expected' => TRUE,
118
+				/*'form_vals' => [
119
+					'title' => 'A Test Task',
120
+					'description' => 'A test task to validate with',
121
+					'category' => 7,
122
+					'due' => 1425873600,
123
+					'due_minute' => FALSE,
124
+				],*/
125
+			],
126
+		];
127
+	}
128
+
129
+	/**
130
+	 * @dataProvider dataValidateTask
131
+	 */
132
+	public function testValidateTask($post, $expected)
133
+	{
134
+		$_POST = [];
135
+		$_POST = $post;
136
+
137
+		$actual = $this->CI->task_model->validate_task();
138
+
139
+		// Verify the form validation data
140
+		//$this->assertEquals($form_vals, $this->CI->task_model->form_vals);
141
+
142
+
143
+		// Verify the function data
144
+		$this->assertEquals($expected, $actual);
145
+	}
146
+}
147
+// End of TaskModelNoFixturesTest

+ 0
- 130
tests/models/TaskModelTest.php View File

@@ -27,136 +27,6 @@ class TaskModelTest extends Todo_TestCase {
27 27
 		parent::setUp();
28 28
 		$this->CI->load->model('task_model');
29 29
 	}
30
-	
31
-	public function dataValidateTask()
32
-	{
33
-		return [
34
-			'Empty task with reminder validation' => [
35
-				'post' => [
36
-					'due' => 'April 27, 2014',
37
-					'reminder' => 'rem_true'
38
-				],
39
-				'expected' => [
40
-					'You must give the task a title',
41
-					'The task must have a description',
42
-					'Select a task category',
43
-					'You must set a due date in order to get a reminder.',
44
-					'You must put numeric hours and minutes for a reminder time.',
45
-				]
46
-			],
47
-			'Empty task with bad due date' => [
48
-				'post' => [
49
-					'due' => '165743248576543152',
50
-				],
51
-				'expected' => [
52
-					'You must give the task a title',
53
-					'The task must have a description',
54
-					'Select a task category',
55
-					'You must enter a due date in YYYY-MM-DD format.'
56
-				]
57
-			],
58
-			'Simple task validation' => [
59
-				'post' => [
60
-					'title' => 'A Test Task',
61
-					'desc' => 'A test task to validate with',
62
-					'category' => 7,
63
-					'priority' => 5,
64
-					'due' => '2015-03-09',
65
-				],
66
-				'expected' => TRUE,
67
-				'form_vals' => [
68
-					'title' => 'A Test Task',
69
-					'description' => 'A test task to validate with',
70
-					'category' => 7,
71
-					'due' => 1425873600,
72
-					'due_minute' => FALSE
73
-				],
74
-			],
75
-			'task validation with reminder' => [
76
-				'post' => [
77
-					'title' => 'A Test Task',
78
-					'desc' => 'A test task to validate with',
79
-					'category' => 7,
80
-					'priority' => 5,
81
-					'due' => '2015-03-09',
82
-					'reminder' => 'rem_true',
83
-					'rem_minutes' => 30,
84
-					'rem_hours' => 4
85
-				],
86
-				'expected' => TRUE,
87
-				'form_vals' => [
88
-					'title' => 'A Test Task',
89
-					'description' => 'A test task to validate with',
90
-					'category' => 7,
91
-					'due' => 1425873600,
92
-					'due_minute' => FALSE,
93
-					'reminder' => TRUE,
94
-					'rem_hours' => 4,
95
-					'rem_minutes' => 30
96
-				],
97
-			],
98
-			'task validation group shared task' => [
99
-				'post' => [
100
-					'title' => 'A Test Task',
101
-					'desc' => 'A test task to validate with',
102
-					'category' => 7,
103
-					'priority' => 5,
104
-					'due' => '2015-03-09',
105
-					'share' => TRUE,
106
-					'group' => [
107
-						'62'
108
-					],
109
-					'group_perms' => 2,
110
-					'friend_perms' => -1
111
-				],
112
-				'expected' => TRUE,
113
-				'form_vals' => [
114
-					'title' => 'A Test Task',
115
-					'description' => 'A test task to validate with',
116
-					'category' => 7,
117
-					'due' => 1425873600,
118
-					'due_minute' => FALSE,
119
-				],
120
-			],
121
-			'task validation user shared task' => [
122
-				'post' => [
123
-					'title' => 'A Test Task',
124
-					'desc' => 'A test task to validate with',
125
-					'category' => 7,
126
-					'priority' => 5,
127
-					'due' => '2015-03-09',
128
-					'share' => TRUE,
129
-					'friend' => [3,7],
130
-					'friend_perms' => 2,
131
-					'group_perms' => -1
132
-				],
133
-				'expected' => TRUE,
134
-				'form_vals' => [
135
-					'title' => 'A Test Task',
136
-					'description' => 'A test task to validate with',
137
-					'category' => 7,
138
-					'due' => 1425873600,
139
-					'due_minute' => FALSE,
140
-				],
141
-			],
142
-		];
143
-	}
144
-
145
-	/**
146
-	 * @dataProvider dataValidateTask
147
-	 */
148
-	public function testValidateTask($post, $expected, $form_vals = NULL)
149
-	{
150
-		$_POST = $post;
151
-
152
-		$actual = $this->CI->task_model->validate_task();
153
-
154
-		// Verify the form validation data
155
-		$this->assertEquals($form_vals, $this->CI->task_model->form_vals);
156
-
157
-		// Verify the function data
158
-		$this->assertEquals($expected, $actual);
159
-	}
160 30
 
161 31
 	public function testGetTaskList()
162 32
 	{