|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class MY_Form_validation_test extends TestCase { |
|
4
|
|
|
private $form_validation; |
|
5
|
|
|
|
|
6
|
|
|
public function __construct() { |
|
7
|
|
|
parent::__construct(); |
|
|
|
|
|
|
8
|
|
|
} |
|
9
|
|
|
|
|
10
|
|
|
public function setUp() { |
|
11
|
|
|
$this->resetInstance(); |
|
|
|
|
|
|
12
|
|
|
$this->CI->load->library('form_validation'); |
|
|
|
|
|
|
13
|
|
|
$this->form_validation = $this->CI->form_validation; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
// A valid username is between 4 & 15 characters, and contains only [a-zA-Z0-9_-] |
|
17
|
|
|
public function test_valid_username_pass() { |
|
18
|
|
|
//username is valid, return true |
|
19
|
|
|
$result = $this->form_validation->valid_username('FooBar'); |
|
20
|
|
|
$this->assertTrue($result); |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
public function test_valid_username_fail_case_1() { |
|
23
|
|
|
//username is invalid (too short), return false |
|
24
|
|
|
$result = $this->form_validation->valid_username('Foo'); |
|
25
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
26
|
|
|
} |
|
27
|
|
|
public function test_valid_username_fail_case_2() { |
|
28
|
|
|
//username is invalid (too long), return false |
|
29
|
|
|
$result = $this->form_validation->valid_username('FooBarFooBarFooBar'); |
|
30
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
31
|
|
|
} |
|
32
|
|
|
public function test_valid_username_fail_case_3() { |
|
33
|
|
|
//username is invalid (invalid characters), return false |
|
34
|
|
|
$result = $this->form_validation->valid_username('フーバル'); |
|
35
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
//A valid password is between 6 & 64 characters, any characters are allowed. |
|
39
|
|
|
public function test_valid_password_pass() { |
|
40
|
|
|
//password is valid, return true |
|
41
|
|
|
$result = $this->form_validation->valid_password('FooBar2'); |
|
42
|
|
|
$this->assertTrue($result); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
public function test_valid_password_fail_1() { |
|
45
|
|
|
//password is invalid (too short), return false |
|
46
|
|
|
$result = $this->form_validation->valid_password('Foo'); |
|
47
|
|
|
$this->assertEquals('The password is too short!', $this->get_error_message('valid_password')); |
|
|
|
|
|
|
48
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
public function test_valid_password_fail_2() { |
|
51
|
|
|
//password is invalid (too long), return false |
|
52
|
|
|
$result = $this->form_validation->valid_password('FooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBarFooBar'); |
|
53
|
|
|
$this->assertEquals('The password is too long!', $this->get_error_message('valid_password')); |
|
|
|
|
|
|
54
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
View Code Duplication |
public function test_is_unique_username_pass() { |
|
58
|
|
|
//username is unique, return true |
|
59
|
|
|
|
|
60
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
61
|
|
|
'MY_Form_validation', |
|
62
|
|
|
['is_unique' => TRUE] |
|
63
|
|
|
); |
|
64
|
|
|
$this->verifyInvokedOnce($validation, 'is_unique'); |
|
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
$result = $validation->is_unique_username('FooBar'); |
|
67
|
|
|
$this->assertTrue($result); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
View Code Duplication |
public function test_is_unique_username_fail() { |
|
70
|
|
|
//username already exists, return false |
|
71
|
|
|
|
|
72
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
73
|
|
|
'MY_Form_validation', |
|
74
|
|
|
['is_unique' => FALSE] |
|
75
|
|
|
); |
|
76
|
|
|
$this->verifyInvokedOnce($validation, 'is_unique'); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
$result = $validation->is_unique_username('FooBar'); |
|
79
|
|
|
$this->assertEquals('The username already exists in our database.', $this->get_error_message('is_unique_username', $validation)); |
|
|
|
|
|
|
80
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
View Code Duplication |
public function test_is_unique_email_pass() { |
|
84
|
|
|
//email is unique, return true |
|
85
|
|
|
|
|
86
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
87
|
|
|
'MY_Form_validation', |
|
88
|
|
|
['is_unique' => TRUE] |
|
89
|
|
|
); |
|
90
|
|
|
$this->verifyInvokedOnce($validation, 'is_unique'); |
|
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
$result = $validation->is_unique_email('[email protected]'); |
|
93
|
|
|
$this->assertTrue($result); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
View Code Duplication |
public function test_is_unique_email_fail() { |
|
96
|
|
|
//email already exists, return false |
|
97
|
|
|
|
|
98
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
99
|
|
|
'MY_Form_validation', |
|
100
|
|
|
['is_unique' => FALSE] |
|
101
|
|
|
); |
|
102
|
|
|
$this->verifyInvokedOnce($validation, 'is_unique'); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
$result = $validation->is_unique_email('[email protected]'); |
|
105
|
|
|
$this->assertEquals('The email already exists in our database.', $this->get_error_message('is_unique_email', $validation)); |
|
|
|
|
|
|
106
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
View Code Duplication |
public function test_isRuleValid_pass() { |
|
110
|
|
|
//rule exists and has no errors, returns true |
|
111
|
|
|
|
|
112
|
|
|
//FIXME: This really isn't the best way to check this... |
|
113
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
114
|
|
|
'MY_Form_validation', |
|
115
|
|
|
['has_rule' => TRUE, 'error_array' => []] |
|
116
|
|
|
); |
|
117
|
|
|
$this->verifyInvokedOnce($validation, 'has_rule'); |
|
|
|
|
|
|
118
|
|
|
|
|
119
|
|
|
$result = $validation->isRuleValid('valid_username'); |
|
120
|
|
|
$this->assertTrue($result); |
|
|
|
|
|
|
121
|
|
|
} |
|
122
|
|
|
public function test_isRuleValid_fail_1() { |
|
123
|
|
|
//rule exists but has errors, returns false |
|
124
|
|
|
|
|
125
|
|
|
//FIXME: This really isn't the best way to check this... |
|
126
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
127
|
|
|
'MY_Form_validation', |
|
128
|
|
|
['has_rule' => TRUE, 'error_array' => ['valid_username' => 'Username is invalid format.']] |
|
129
|
|
|
); |
|
130
|
|
|
$this->verifyInvokedOnce($validation, 'has_rule'); |
|
|
|
|
|
|
131
|
|
|
$this->verifyInvokedOnce($validation, 'error_array'); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
$result = $validation->isRuleValid('valid_username'); |
|
134
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
View Code Duplication |
public function test_isRuleValid_fail_2() { |
|
137
|
|
|
//rule does not exist, returns false |
|
138
|
|
|
|
|
139
|
|
|
//FIXME: This really isn't the best way to check this... |
|
140
|
|
|
$validation = $this->getDouble( |
|
|
|
|
|
|
141
|
|
|
'MY_Form_validation', |
|
142
|
|
|
['has_rule' => FALSE] |
|
143
|
|
|
); |
|
144
|
|
|
$this->verifyInvokedOnce($validation, 'has_rule'); |
|
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
$result = $validation->isRuleValid('valid_username'); |
|
147
|
|
|
$this->assertFalse($result); |
|
|
|
|
|
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
//utility functions |
|
151
|
|
|
function get_error_message(/*str*/$field, /*obj*/ $customObj = FALSE) { |
|
|
|
|
|
|
152
|
|
|
$obj = $customObj ?: $this->form_validation; |
|
|
|
|
|
|
153
|
|
|
$error_messages = ReflectionHelper::getPrivateProperty( |
|
154
|
|
|
$obj, |
|
155
|
|
|
'_error_messages' |
|
156
|
|
|
); |
|
157
|
|
|
|
|
158
|
|
|
return $error_messages[$field]; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: