source: tags/2.1.5/tests/FormValidatorTest.php @ 377

Last change on this file since 377 was 377, checked in by quinn, 14 years ago

Releasing trunk as stable version 2.1.5

File size: 11.0 KB
Line 
1<?php
2/**
3 * The Strangecode Codebase - a general application development framework for PHP
4 * For details visit the project site: <http://trac.strangecode.com/codebase/>
5 * Copyright 2001-2010 Strangecode, LLC
6 *
7 * This file is part of The Strangecode Codebase.
8 *
9 * The Strangecode Codebase is free software: you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as published by the
11 * Free Software Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * The Strangecode Codebase is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * The Strangecode Codebase. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23/**
24 * PHPUnit test case for codebase/lib/FormValidator.inc.php
25 *
26 * The method skeletons below need to be filled in with
27 * real data so that the tests will run correctly. Replace
28 * all EXPECTED_VAL and PARAM strings with real data.
29 *
30 * Created with PHPUnit_Skeleton on 2005-08-09
31 */
32require_once 'PHPUnit.php';
33class FormValidatorTest extends PHPUnit_TestCase {
34
35    var $FormValidator;
36
37    function FormValidatorTest($name)
38    {
39        $this->PHPUnit_TestCase($name);
40    }
41
42    function setUp()
43    {
44        require dirname(__FILE__) . '/_config.inc.php';
45        require_once '../lib/FormValidator.inc.php';
46        $this->FormValidator =& new FormValidator();
47    }
48
49    function tearDown()
50    {
51        unset($this->FormValidator);
52    }
53
54    function test_geterrorlist()
55    {
56        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
57        $err_list = $this->FormValidator->geterrorlist();
58        $this->assertTrue($err_list[0]['message'] === _("This is an error."));
59    }
60
61    function test_adderror()
62    {
63        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
64        $this->assertTrue($this->FormValidator->errors[0]['message'] === _("This is an error."));
65    }
66
67    function test_anyerrors()
68    {
69        $result = $this->FormValidator->anyerrors();
70        $this->assertFalse($result);
71        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
72        $result = $this->FormValidator->anyerrors();
73        $this->assertTrue($result);
74    }
75
76    function test_reseterrorlist()
77    {
78        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
79        $this->FormValidator->reseterrorlist();
80        $this->assertFalse($this->FormValidator->anyerrors());
81    }
82
83    function test_printerrormessages()
84    {
85        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
86        ob_start();
87        $this->FormValidator->printerrormessages();
88        $result = ob_get_clean();
89        $this->assertContains('This is an error', $result);
90    }
91
92    function test_err()
93    {
94        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
95        ob_start();
96        $this->FormValidator->err('some_field', 'printthis');
97        $result = ob_get_clean();
98        $this->assertContains('printthis', $result);
99    }
100
101    function test_notempty()
102    {
103        // This should not generate any error.
104        $_POST['some_field'] = 'a non empty string';
105        $this->FormValidator->isempty('some_field', _("Error message"));
106        $this->assertFalse($this->FormValidator->anyerrors());
107       
108        // This one is an error!
109        $_POST['some_field'] = '';
110        $this->FormValidator->isempty('some_field', _("Error message"));
111        $this->assertTrue($this->FormValidator->anyerrors());
112    }
113
114    function test_isempty()
115    {
116        // This should not generate any error.
117        $_POST['some_field'] = 'a non empty string';
118        $this->FormValidator->isempty('some_field', _("Error message"));
119        $this->assertFalse($this->FormValidator->anyerrors());
120       
121        // This one is an error!
122        $_POST['some_field'] = '';
123        $this->FormValidator->isempty('some_field', _("Error message"));
124        $this->assertTrue($this->FormValidator->anyerrors());
125    }
126
127    function test_isstring()
128    {
129        // This should not generate any error.
130        $_POST['some_field'] = 'this is a string';
131        $this->FormValidator->isstring('some_field', _("Error message"));
132        $this->assertFalse($this->FormValidator->anyerrors());
133       
134        // This one is an error!
135        $_POST['some_field'] = 12.3248; // not a string.
136        $this->FormValidator->isstring('some_field', _("Error message"));
137        $this->assertTrue($this->FormValidator->anyerrors());
138    }
139
140    function test_isnumber()
141    {
142        // This should not generate any error.
143        $_POST['some_field'] = '1234.453';
144        $this->FormValidator->isnumber('some_field', _("Error message"));
145        $this->assertFalse($this->FormValidator->anyerrors());
146       
147        // This one is an error!
148        $_POST['some_field'] = 'not a number';
149        $this->FormValidator->isnumber('some_field', _("Error message"));
150        $this->assertTrue($this->FormValidator->anyerrors());
151    }
152
153    function test_isinteger()
154    {
155        // This should not generate any error.
156        $_POST['some_field'] = '1234';
157        $this->FormValidator->isinteger('some_field', _("Error message"));
158        $this->assertFalse($this->FormValidator->anyerrors());
159       
160        // This one is an error!
161        $_POST['some_field'] = '1234.1';
162        $this->FormValidator->isinteger('some_field', _("Error message"));
163        $this->assertTrue($this->FormValidator->anyerrors());
164    }
165
166    function test_isfloat()
167    {
168        // This should not generate any error.
169        $_POST['some_field'] = '123.1234';
170        $this->FormValidator->isfloat('some_field', _("Error message"));
171        $this->assertFalse($this->FormValidator->anyerrors());
172       
173        // This one is an error!
174        $_POST['some_field'] = 'some falsity';
175        $this->FormValidator->isfloat('some_field', _("Error message"));
176        $this->assertTrue($this->FormValidator->anyerrors());
177    }
178
179    function test_isarray()
180    {
181        // This should not generate any error.
182        $_POST['some_field'] = array('asdf', 123);
183        $this->FormValidator->isarray('some_field', _("Error message"));
184        $this->assertFalse($this->FormValidator->anyerrors());
185       
186        // This one is an error!
187        $_POST['some_field'] = 'some falsity';
188        $this->FormValidator->isarray('some_field', _("Error message"));
189        $this->assertTrue($this->FormValidator->anyerrors());
190    }
191
192    function test_checkregex()
193    {
194        // This should not generate any error.
195        $_POST['some_field'] = '1234abcd';
196        $this->FormValidator->checkregex('some_field', '/\d{4}[a-d]{4}/', true, _("Error message"));
197        $this->assertFalse($this->FormValidator->anyerrors());
198       
199        // This should not generate any error.
200        $_POST['some_field'] = 'no digits here';
201        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
202        $this->assertFalse($this->FormValidator->anyerrors());
203       
204        // This one is an error!
205        $_POST['some_field'] = 'oops, a d1git';
206        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
207        $this->assertTrue($this->FormValidator->anyerrors());
208    }
209
210    function test_stringlength()
211    {
212        // This should not generate any error.
213        $_POST['some_field'] = 'some truth';
214        $this->FormValidator->stringLength('some_field', 0, 255, _("Error message"));
215        $this->assertFalse($this->FormValidator->anyerrors());
216       
217        // This one is an error!
218        $_POST['some_field'] = 'some falsity';
219        $this->FormValidator->stringLength('some_field', 0, 4, _("Error message"));
220        $this->assertTrue($this->FormValidator->anyerrors());
221    }
222
223    function test_numericrange()
224    {
225        // This should not generate any error.
226        $_POST['some_field'] = '12';
227        $this->FormValidator->numericrange('some_field', 3, 22, _("Error message"));
228        $this->assertFalse($this->FormValidator->anyerrors());
229       
230        // This one is an error!
231        $_POST['some_field'] = '12';
232        $this->FormValidator->numericrange('some_field', 300, 2200, _("Error message"));
233        $this->assertTrue($this->FormValidator->anyerrors());
234    }
235
236    function test_validateemail()
237    {
238        // This should not generate any error.
239        $_POST['some_field'] = 'Quinn the Kook <quinn@strangecode.com>';
240        $this->FormValidator->validateemail('some_field', _("Error message"));
241        $this->assertFalse($this->FormValidator->anyerrors());
242               
243        // This one is an error!
244        $_POST['some_field'] = 'quinn@kook.com.';
245        $this->FormValidator->validateemail('some_field', _("Error message"));
246        $this->assertTrue($this->FormValidator->anyerrors());
247    }
248
249    function test_validatephone()
250    {
251        // This should not generate any error.
252        $_POST['some_field'] = '+1 (530) 555-1212';
253        $this->FormValidator->validatephone('some_field', _("Error message"));
254        $this->assertFalse($this->FormValidator->anyerrors());
255       
256        // This one is an error!
257        $_POST['some_field'] = 'd321/654*9875 ';
258        $this->FormValidator->validatephone('some_field', _("Error message"));
259        $this->assertTrue($this->FormValidator->anyerrors());
260    }
261
262    function test_validatestrdate()
263    {
264        // This should not generate any error.
265        $_POST['some_field'] = 'next tuesday';
266        $this->FormValidator->validatestrdate('some_field', _("Error message"));
267        $this->assertFalse($this->FormValidator->anyerrors());
268       
269        // This one is an error!
270        $_POST['some_field'] = 'in a galaxy far far away';
271        $this->FormValidator->validatestrdate('some_field', _("Error message"));
272        $this->assertTrue($this->FormValidator->anyerrors());
273    }
274
275    function test_validateccnumber()
276    {
277        // This should not generate any error.
278        $_POST['some_field'] = '2323-2005-7766-3554';
279        $this->FormValidator->validateccnumber('some_field', null);
280        $this->assertFalse($this->FormValidator->anyerrors());
281       
282        // This one is an error!
283        $_POST['some_field'] = '1234 1234 1234 1234';
284        $this->FormValidator->validateccnumber('some_field', null);
285        $this->assertTrue($this->FormValidator->anyerrors());
286    }
287
288    function test_fileuploaded()
289    {
290        // This one is an error!
291        $this->FormValidator->fileUploaded('this-file-does-not-exist', _("Error message"));
292        $this->assertTrue($this->FormValidator->anyerrors());
293    }
294
295}
296// Running the test.
297$suite = new PHPUnit_TestSuite('FormValidatorTest');
298$result = PHPUnit::run($suite);
299echo $result->toString();
300?>
Note: See TracBrowser for help on using the repository browser.