[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 |
---|
[362] | 6 | * |
---|
| 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. |
---|
| 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 | * |
---|
[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] | 33 | class 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. |
---|
| 99 | $_POST['some_field'] = 'a non empty string'; |
---|
| 100 | $this->FormValidator->isempty('some_field', _("Error message")); |
---|
| 101 | $this->assertFalse($this->FormValidator->anyerrors()); |
---|
| 102 | |
---|
| 103 | // This one is an error! |
---|
| 104 | $_POST['some_field'] = ''; |
---|
| 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. |
---|
| 112 | $_POST['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 | $_POST['some_field'] = ''; |
---|
| 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. |
---|
| 125 | $_POST['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 | $_POST['some_field'] = 12.3248; // not a string. |
---|
| 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. |
---|
| 138 | $_POST['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 | $_POST['some_field'] = 'not a number'; |
---|
| 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. |
---|
| 151 | $_POST['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 | $_POST['some_field'] = '1234.1'; |
---|
| 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. |
---|
| 164 | $_POST['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 | $_POST['some_field'] = 'some falsity'; |
---|
| 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. |
---|
| 177 | $_POST['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 | $_POST['some_field'] = 'some falsity'; |
---|
| 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. |
---|
| 190 | $_POST['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 | $_POST['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 | $_POST['some_field'] = 'oops, a d1git'; |
---|
| 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. |
---|
| 208 | $_POST['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 | $_POST['some_field'] = 'some falsity'; |
---|
| 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. |
---|
| 221 | $_POST['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 | $_POST['some_field'] = '12'; |
---|
| 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. |
---|
| 234 | $_POST['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 | $_POST['some_field'] = 'quinn@kook.com.'; |
---|
| 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. |
---|
| 247 | $_POST['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 | $_POST['some_field'] = 'd321/654*9875 '; |
---|
| 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. |
---|
| 260 | $_POST['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 | $_POST['some_field'] = 'in a galaxy far far away'; |
---|
| 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. |
---|
| 273 | $_POST['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 | $_POST['some_field'] = '1234 1234 1234 1234'; |
---|
| 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 | } |
---|