* Copyright 2001-2012 Strangecode, LLC
*
* This file is part of The Strangecode Codebase.
*
* The Strangecode Codebase is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* The Strangecode Codebase is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* The Strangecode Codebase. If not, see .
*/
/**
* PHPUnit test case for codebase/lib/FormValidator.inc.php
*
* The method skeletons below need to be filled in with
* real data so that the tests will run correctly. Replace
* all EXPECTED_VAL and PARAM strings with real data.
*
* Created with PHPUnit_Skeleton on 2005-08-09
*/
class FormValidatorTest extends PHPUnit_Framework_TestCase {
var $FormValidator;
function setUp()
{
require dirname(__FILE__) . '/_config.inc.php';
require_once '../lib/FormValidator.inc.php';
$this->FormValidator = new FormValidator();
}
function tearDown()
{
unset($this->FormValidator);
}
function test_geterrorlist()
{
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
$err_list = $this->FormValidator->geterrorlist();
$this->assertTrue($err_list[0]['message'] === _("This is an error."));
}
function test_adderror()
{
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
$this->assertTrue($this->FormValidator->errors[0]['message'] === _("This is an error."));
}
function test_anyerrors()
{
$result = $this->FormValidator->anyerrors();
$this->assertFalse($result);
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
$result = $this->FormValidator->anyerrors();
$this->assertTrue($result);
}
function test_reseterrorlist()
{
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
$this->FormValidator->reseterrorlist();
$this->assertFalse($this->FormValidator->anyerrors());
}
function test_printerrormessages()
{
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
ob_start();
$this->FormValidator->printerrormessages();
$result = ob_get_clean();
$this->assertContains('This is an error', $result);
}
function test_err()
{
$this->FormValidator->addError('some_field', _("This is an error."), MSG_ERR, __FILE__, __LINE__);
ob_start();
$this->FormValidator->err('some_field', 'printthis');
$result = ob_get_clean();
$this->assertContains('printthis', $result);
}
function test_notempty()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'a non empty string';
$this->FormValidator->notEmpty('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = '';
$this->FormValidator->isempty('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isempty()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'a non empty string';
$this->FormValidator->isempty('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = '';
$this->FormValidator->isempty('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isstring()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'this is a string';
$this->FormValidator->isstring('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 12.3248; // not a string.
$this->FormValidator->isstring('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isnumber()
{
// This should not generate any error.
$_REQUEST['some_field'] = '1234.453';
$this->FormValidator->isnumber('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'not a number';
$this->FormValidator->isnumber('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isinteger()
{
// This should not generate any error.
$_REQUEST['some_field'] = '1234';
$this->FormValidator->isinteger('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = '1234.1';
$this->FormValidator->isinteger('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isfloat()
{
// This should not generate any error.
$_REQUEST['some_field'] = '123.1234';
$this->FormValidator->isfloat('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'some falsity';
$this->FormValidator->isfloat('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_isarray()
{
// This should not generate any error.
$_REQUEST['some_field'] = array('asdf', 123);
$this->FormValidator->isarray('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'some falsity';
$this->FormValidator->isarray('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_checkregex()
{
// This should not generate any error.
$_REQUEST['some_field'] = '1234abcd';
$this->FormValidator->checkregex('some_field', '/\d{4}[a-d]{4}/', true, _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This should not generate any error.
$_REQUEST['some_field'] = 'no digits here';
$this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'oops, a d1git';
$this->FormValidator->checkregex('some_field', '/\d/', false, _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_stringlength()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'some truth';
$this->FormValidator->stringLength('some_field', 0, 255, _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'some falsity';
$this->FormValidator->stringLength('some_field', 0, 4, _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_numericrange()
{
// This should not generate any error.
$_REQUEST['some_field'] = '12';
$this->FormValidator->numericrange('some_field', 3, 22, _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = '12';
$this->FormValidator->numericrange('some_field', 300, 2200, _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_validateemail()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'Quinn the Kook ';
$this->FormValidator->validateemail('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'quinn@kook.com.';
$this->FormValidator->validateemail('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_validatephone()
{
// This should not generate any error.
$_REQUEST['some_field'] = '+1 (530) 555-1212';
$this->FormValidator->validatephone('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'd321/654*9875 ';
$this->FormValidator->validatephone('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_validatestrdate()
{
// This should not generate any error.
$_REQUEST['some_field'] = 'next tuesday';
$this->FormValidator->validatestrdate('some_field', _("Error message"));
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = 'in a galaxy far far away';
$this->FormValidator->validatestrdate('some_field', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_validateccnumber()
{
// This should not generate any error.
$_REQUEST['some_field'] = '2323-2005-7766-3554';
$this->FormValidator->validateccnumber('some_field', null);
$this->assertFalse($this->FormValidator->anyerrors());
// This one is an error!
$_REQUEST['some_field'] = '1234 1234 1234 1234';
$this->FormValidator->validateccnumber('some_field', null);
$this->assertTrue($this->FormValidator->anyerrors());
}
function test_fileuploaded()
{
// This one is an error!
$this->FormValidator->fileUploaded('this-file-does-not-exist', _("Error message"));
$this->assertTrue($this->FormValidator->anyerrors());
}
}