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