OSDN Git Service

298134bae5382048635ecacd2bd02351752680cc
[ethna/ethna.git] / test / Ethna_ActionForm_Validator_Custom_Test.php
1 <?php
2 // vim: foldmethod=marker
3 /**
4  *  Ethna_ActionForm_Validator_Custom_Test.php
5  *
6  *  @author     Yoshinari Takaoka <takaoka@beatcraft.com>
7  *  @version    $Id$
8  */
9
10 // {{{    Ethna_ActionForm_Validator_Custom_Test
11 /**
12  *  Test Case For Ethna_ActionForm(Custom Validator)
13  *
14  *  @access public
15  */
16 class Ethna_ActionForm_Validator_Custom_Test extends Ethna_UnitTestBase
17 {
18     function setUp()
19     {
20         $this->af->use_validator_plugin = false;
21         $this->af->clearFormVars();
22         $this->af->form = array();
23         $this->ae->clear();
24     }
25
26     // {{{ checkMailAddress
27     function test_checkMailAddress()
28     {
29         //    'required' => true とすると
30         //    Ethna_Plugin_Validator_Required の時点で
31         //    エラーになる入力があるためここではfalseに
32         //    設定
33         $form_string = array(
34                            'type' => VAR_TYPE_STRING,
35                            'required' => false,
36                            'custom' => 'checkMailaddress',
37                        );
38         $this->af->setDef('input', $form_string);
39
40         $this->af->set('input', 'hoge@fuga.net');
41         $this->af->validate();
42         $this->assertFalse($this->ae->isError('input'));
43         $this->ae->clear(); 
44
45         $this->af->set('input', '-hoge@fuga.net');
46         $this->af->validate();
47         $this->assertFalse($this->ae->isError('input'));
48         $this->ae->clear(); 
49
50         $this->af->set('input', '.hoge@fuga.net');
51         $this->af->validate();
52         $this->assertFalse($this->ae->isError('input'));
53         $this->ae->clear(); 
54
55         $this->af->set('input', '+hoge@fuga.net');
56         $this->af->validate();
57         $this->assertFalse($this->ae->isError('input'));
58         $this->ae->clear(); 
59
60         // @がない
61         $this->af->set('input', 'hogefuga.et');
62         $this->af->validate();
63         $this->assertTrue($this->ae->isError('input'));
64         $this->ae->clear(); 
65
66         // @の前に文字がない
67         $this->af->set('input', '@hogefuga.et');
68         $this->af->validate();
69         $this->assertTrue($this->ae->isError('input'));
70         $this->ae->clear(); 
71
72         // @の後に文字がない
73         $this->af->set('input', 'hogefuga.net@');
74         $this->af->validate();
75         $this->assertTrue($this->ae->isError('input'));
76         $this->ae->clear(); 
77
78         // 先頭文字が許されていない
79         $this->af->set('input', '%hoge@fuga.net');
80         $this->af->validate();
81         $this->assertTrue($this->ae->isError('input'));
82         $this->ae->clear(); 
83
84         // 末尾文字が許されていない
85         $this->af->set('input', 'hoge@fuga.net.');
86         $this->af->validate();
87         $this->assertTrue($this->ae->isError('input'));
88     }
89     // }}}
90
91     // {{{ checkBoolean
92     function test_checkBoolean()
93     {
94         //    'required' => true とすると
95         //    Ethna_Plugin_Validator_Required の時点で
96         //    エラーになる入力があるためここではfalseに
97         //    設定
98         $form_boolean = array(
99                             'type' => VAR_TYPE_BOOLEAN,
100                             'required' => false,
101                             'custom' => 'checkBoolean',
102                         );
103         $this->af->setDef('input', $form_boolean);
104
105         //   HTML フォームから入ってくる値は
106         //   文字列型である。
107         //   @see http://www.php.net/manual/en/types.comparisons.php  
108         $this->af->set('input', '0');
109         $this->af->validate();
110         $this->assertFalse($this->ae->isError('input'));
111         $this->ae->clear(); 
112
113         $this->af->set('input', '1');
114         $this->af->validate();
115         $this->assertFalse($this->ae->isError('input'));
116         $this->ae->clear(); 
117
118         //   空文字列は false と見做すのが仕様
119         $this->af->set('input', '');
120         $this->af->validate();
121         $this->assertFalse($this->ae->isError('input'));
122         $this->ae->clear(); 
123
124         // 0,1, 空文字列以外の値は全てエラー
125         $this->af->set('input', 3);
126         $this->af->validate();
127         $this->assertTrue($this->ae->isError('input'));
128         $this->ae->clear(); 
129
130         $this->af->set('input', "true");
131         $this->af->validate();
132         $this->assertTrue($this->ae->isError('input'));
133         $this->ae->clear(); 
134
135         $this->af->set('input', "false");
136         $this->af->validate();
137         $this->assertTrue($this->ae->isError('input'));
138     }
139     // }}}
140
141     // {{{ checkURL
142     function test_checkURL()
143     {
144         //    'required' => true とすると
145         //    Ethna_Plugin_Validator_Required の時点で
146         //    エラーになる入力があるためここではfalseに
147         //    設定
148         $form_url = array(
149                         'type' => VAR_TYPE_STRING,
150                         'required' => false,
151                         'custom' => 'checkURL',
152                     );
153         $this->af->setDef('input', $form_url);
154
155         $this->af->set('input', 'http://uga.net');
156         $this->af->validate();
157         $this->assertFalse($this->ae->isError('input'));
158         $this->ae->clear(); 
159
160         $this->af->set('input', 'https://uga.net');
161         $this->af->validate();
162         $this->assertFalse($this->ae->isError('input'));
163         $this->ae->clear(); 
164
165         $this->af->set('input', 'ftp://uga.net');
166         $this->af->validate();
167         $this->assertFalse($this->ae->isError('input'));
168         $this->ae->clear(); 
169
170         $this->af->set('input', 'http://');
171         $this->af->validate();
172         $this->assertFalse($this->ae->isError('input'));
173         $this->ae->clear(); 
174
175         //    空文字列はエラーにしないのが仕様
176         $this->af->set('input', '');
177         $this->af->validate();
178         $this->assertFalse($this->ae->isError('input'));
179         $this->ae->clear(); 
180
181         // '/'が足りない
182         $this->af->set('input', 'http:/');
183         $this->af->validate();
184         $this->assertTrue($this->ae->isError('input'));
185         $this->ae->clear(); 
186
187         // 接頭辞がない
188         $this->af->set('input', 'hoge@fuga.net');
189         $this->af->validate();
190         $this->assertTrue($this->ae->isError('input'));
191     }
192     // }}}
193
194     // {{{ checkVendorChar
195     function test_checkVendorChar()
196     {
197         //    'required' => true とすると
198         //    Ethna_Plugin_Validator_Required の時点で
199         //    エラーになる入力があるためここではfalseに
200         //    設定
201         $form_string = array(
202                            'type' => VAR_TYPE_STRING,
203                            'required' => false,
204                            'custom' => 'checkVendorChar',
205                        );
206         $this->af->setDef('input', $form_string);
207
208         $this->af->set('input', 'http://uga.net');
209         $this->af->validate();
210         $this->assertFalse($this->ae->isError('input'));
211         $this->ae->clear(); 
212
213         $this->af->set('input', chr(0x00));
214         $this->af->validate();
215         $this->assertFalse($this->ae->isError('input'));
216         $this->ae->clear(); 
217
218         $this->af->set('input', chr(0x79));
219         $this->af->validate();
220         $this->assertFalse($this->ae->isError('input'));
221         $this->ae->clear(); 
222
223         $this->af->set('input', chr(0x80));
224         $this->af->validate();
225         $this->assertFalse($this->ae->isError('input'));
226         $this->ae->clear(); 
227
228         $this->af->set('input', chr(0x8e));
229         $this->af->validate();
230         $this->assertFalse($this->ae->isError('input'));
231         $this->ae->clear(); 
232
233         $this->af->set('input', chr(0x8f));
234         $this->af->validate();
235         $this->assertFalse($this->ae->isError('input'));
236         $this->ae->clear(); 
237
238         $this->af->set('input', chr(0xae));
239         $this->af->validate();
240         $this->assertFalse($this->ae->isError('input'));
241         $this->ae->clear(); 
242
243         $this->af->set('input', chr(0xf8));
244         $this->af->validate();
245         $this->assertFalse($this->ae->isError('input'));
246         $this->ae->clear(); 
247
248         $this->af->set('input', chr(0xfd));
249         $this->af->validate();
250         $this->assertFalse($this->ae->isError('input'));
251         $this->ae->clear(); 
252
253         /* IBM拡張文字 / NEC選定IBM拡張文字 */
254         //$c == 0xad || ($c >= 0xf9 && $c <= 0xfc)
255         $this->af->set('input', chr(0xad));
256         $this->af->validate();
257         $this->assertTrue($this->ae->isError('input'));
258         $this->ae->clear(); 
259
260         $this->af->set('input', chr(0xf9));
261         $this->af->validate();
262         $this->assertTrue($this->ae->isError('input'));
263         $this->ae->clear(); 
264
265         $this->af->set('input', chr(0xfa));
266         $this->af->validate();
267         $this->assertTrue($this->ae->isError('input'));
268         $this->ae->clear(); 
269
270         $this->af->set('input', chr(0xfc));
271         $this->af->validate();
272         $this->assertTrue($this->ae->isError('input'));
273     }
274     // }}}
275
276 }
277 // }}}
278
279 ?>