source: trunk/tests/FormValidatorTest.php

Last change on this file was 720, checked in by anonymous, 4 years ago

Update CSS reset with inspiration from https://github.com/hankchizljaw/modern-css-reset

File size: 10.7 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-2012 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 */
32
33class FormValidatorTest extends PHPUnit_Framework_TestCase {
34
35    var $FormValidator;
36
37    function setUp()
38    {
39        require dirname(__FILE__) . '/_config.inc.php';
40        require_once '../lib/FormValidator.inc.php';
41        $this->FormValidator = new FormValidator();
42    }
43
44    function tearDown()
45    {
46        unset($this->FormValidator);
47    }
48
49    function test_geterrorlist()
50    {
51        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
52        $err_list = $this->FormValidator->geterrorlist();
53        $this->assertTrue($err_list[0]['message'] === _("This is an error."));
54    }
55
56    function test_adderror()
57    {
58        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
59        $this->assertTrue($this->FormValidator->errors[0]['message'] === _("This is an error."));
60    }
61
62    function test_anyerrors()
63    {
64        $result = $this->FormValidator->anyerrors();
65        $this->assertFalse($result);
66        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
67        $result = $this->FormValidator->anyerrors();
68        $this->assertTrue($result);
69    }
70
71    function test_reseterrorlist()
72    {
73        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
74        $this->FormValidator->reseterrorlist();
75        $this->assertFalse($this->FormValidator->anyerrors());
76    }
77
78    function test_printerrormessages()
79    {
80        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
81        ob_start();
82        $this->FormValidator->printerrormessages();
83        $result = ob_get_clean();
84        $this->assertContains('This is an error', $result);
85    }
86
87    function test_err()
88    {
89        $this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
90        ob_start();
91        $this->FormValidator->err('some_field', 'printthis');
92        $result = ob_get_clean();
93        $this->assertContains('printthis', $result);
94    }
95
96    function test_notempty()
97    {
98        // This should not generate any error.
99        $_REQUEST['some_field'] = 'a non empty string';
100        $this->FormValidator->notEmpty('some_field', _("Error message"));
101        $this->assertFalse($this->FormValidator->anyerrors());
102
103        // This one is an error!
104        $_REQUEST['some_field'] = '';
105        $this->FormValidator->isempty('some_field', _("Error message"));
106        $this->assertTrue($this->FormValidator->anyerrors());
107    }
108
109    function test_isempty()
110    {
111        // This should not generate any error.
112        $_REQUEST['some_field'] = 'a non empty string';
113        $this->FormValidator->isempty('some_field', _("Error message"));
114        $this->assertFalse($this->FormValidator->anyerrors());
115
116        // This one is an error!
117        $_REQUEST['some_field'] = '';
118        $this->FormValidator->isempty('some_field', _("Error message"));
119        $this->assertTrue($this->FormValidator->anyerrors());
120    }
121
122    function test_isstring()
123    {
124        // This should not generate any error.
125        $_REQUEST['some_field'] = 'this is a string';
126        $this->FormValidator->isstring('some_field', _("Error message"));
127        $this->assertFalse($this->FormValidator->anyerrors());
128
129        // This one is an error!
130        $_REQUEST['some_field'] = 12.3248; // not a string.
131        $this->FormValidator->isstring('some_field', _("Error message"));
132        $this->assertTrue($this->FormValidator->anyerrors());
133    }
134
135    function test_isnumber()
136    {
137        // This should not generate any error.
138        $_REQUEST['some_field'] = '1234.453';
139        $this->FormValidator->isnumber('some_field', _("Error message"));
140        $this->assertFalse($this->FormValidator->anyerrors());
141
142        // This one is an error!
143        $_REQUEST['some_field'] = 'not a number';
144        $this->FormValidator->isnumber('some_field', _("Error message"));
145        $this->assertTrue($this->FormValidator->anyerrors());
146    }
147
148    function test_isinteger()
149    {
150        // This should not generate any error.
151        $_REQUEST['some_field'] = '1234';
152        $this->FormValidator->isinteger('some_field', _("Error message"));
153        $this->assertFalse($this->FormValidator->anyerrors());
154
155        // This one is an error!
156        $_REQUEST['some_field'] = '1234.1';
157        $this->FormValidator->isinteger('some_field', _("Error message"));
158        $this->assertTrue($this->FormValidator->anyerrors());
159    }
160
161    function test_isfloat()
162    {
163        // This should not generate any error.
164        $_REQUEST['some_field'] = '123.1234';
165        $this->FormValidator->isfloat('some_field', _("Error message"));
166        $this->assertFalse($this->FormValidator->anyerrors());
167
168        // This one is an error!
169        $_REQUEST['some_field'] = 'some falsity';
170        $this->FormValidator->isfloat('some_field', _("Error message"));
171        $this->assertTrue($this->FormValidator->anyerrors());
172    }
173
174    function test_isarray()
175    {
176        // This should not generate any error.
177        $_REQUEST['some_field'] = array('asdf', 123);
178        $this->FormValidator->isarray('some_field', _("Error message"));
179        $this->assertFalse($this->FormValidator->anyerrors());
180
181        // This one is an error!
182        $_REQUEST['some_field'] = 'some falsity';
183        $this->FormValidator->isarray('some_field', _("Error message"));
184        $this->assertTrue($this->FormValidator->anyerrors());
185    }
186
187    function test_checkregex()
188    {
189        // This should not generate any error.
190        $_REQUEST['some_field'] = '1234abcd';
191        $this->FormValidator->checkregex('some_field', '/\d{4}[a-d]{4}/', true, _("Error message"));
192        $this->assertFalse($this->FormValidator->anyerrors());
193
194        // This should not generate any error.
195        $_REQUEST['some_field'] = 'no digits here';
196        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
197        $this->assertFalse($this->FormValidator->anyerrors());
198
199        // This one is an error!
200        $_REQUEST['some_field'] = 'oops, a d1git';
201        $this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
202        $this->assertTrue($this->FormValidator->anyerrors());
203    }
204
205    function test_stringlength()
206    {
207        // This should not generate any error.
208        $_REQUEST['some_field'] = 'some truth';
209        $this->FormValidator->stringLength('some_field', 0, 255, _("Error message"));
210        $this->assertFalse($this->FormValidator->anyerrors());
211
212        // This one is an error!
213        $_REQUEST['some_field'] = 'some falsity';
214        $this->FormValidator->stringLength('some_field', 0, 4, _("Error message"));
215        $this->assertTrue($this->FormValidator->anyerrors());
216    }
217
218    function test_numericrange()
219    {
220        // This should not generate any error.
221        $_REQUEST['some_field'] = '12';
222        $this->FormValidator->numericrange('some_field', 3, 22, _("Error message"));
223        $this->assertFalse($this->FormValidator->anyerrors());
224
225        // This one is an error!
226        $_REQUEST['some_field'] = '12';
227        $this->FormValidator->numericrange('some_field', 300, 2200, _("Error message"));
228        $this->assertTrue($this->FormValidator->anyerrors());
229    }
230
231    function test_validateemail()
232    {
233        // This should not generate any error.
234        $_REQUEST['some_field'] = 'Quinn the Kook <quinn@strangecode.com>';
235        $this->FormValidator->validateemail('some_field', _("Error message"));
236        $this->assertFalse($this->FormValidator->anyerrors());
237
238        // This one is an error!
239        $_REQUEST['some_field'] = 'quinn@kook.com.';
240        $this->FormValidator->validateemail('some_field', _("Error message"));
241        $this->assertTrue($this->FormValidator->anyerrors());
242    }
243
244    function test_validatephone()
245    {
246        // This should not generate any error.
247        $_REQUEST['some_field'] = '+1 (530) 555-1212';
248        $this->FormValidator->validatephone('some_field', _("Error message"));
249        $this->assertFalse($this->FormValidator->anyerrors());
250
251        // This one is an error!
252        $_REQUEST['some_field'] = 'd321/654*9875 ';
253        $this->FormValidator->validatephone('some_field', _("Error message"));
254        $this->assertTrue($this->FormValidator->anyerrors());
255    }
256
257    function test_validatestrdate()
258    {
259        // This should not generate any error.
260        $_REQUEST['some_field'] = 'next tuesday';
261        $this->FormValidator->validatestrdate('some_field', _("Error message"));
262        $this->assertFalse($this->FormValidator->anyerrors());
263
264        // This one is an error!
265        $_REQUEST['some_field'] = 'in a galaxy far far away';
266        $this->FormValidator->validatestrdate('some_field', _("Error message"));
267        $this->assertTrue($this->FormValidator->anyerrors());
268    }
269
270    function test_validateccnumber()
271    {
272        // This should not generate any error.
273        $_REQUEST['some_field'] = '2323-2005-7766-3554';
274        $this->FormValidator->validateccnumber('some_field', null);
275        $this->assertFalse($this->FormValidator->anyerrors());
276
277        // This one is an error!
278        $_REQUEST['some_field'] = '1234 1234 1234 1234';
279        $this->FormValidator->validateccnumber('some_field', null);
280        $this->assertTrue($this->FormValidator->anyerrors());
281    }
282
283    function test_fileuploaded()
284    {
285        // This one is an error!
286        $this->FormValidator->fileUploaded('this-file-does-not-exist', _("Error message"));
287        $this->assertTrue($this->FormValidator->anyerrors());
288    }
289
290}
Note: See TracBrowser for help on using the repository browser.