[ticket/9823] Unit tests for validate_config_vars.

Fix some documentations and use the $max-var instead of the magic-number
in validate_config_vars.

PHPBB3-9823
This commit is contained in:
Joas Schilling 2010-09-16 13:04:12 +02:00
parent 11e3804f97
commit 737849bc23
4 changed files with 159 additions and 10 deletions

View file

@ -300,7 +300,7 @@ function build_cfg_template($tpl_type, $key, &$new, $config_key, $vars)
} }
/** /**
* Going through a config array and validate values, writing errors to $error. The validation method accepts parameters separated by ':' for string and int. * Going through a config array and validate values, writing errors to $error. The validation method accepts parameters separated by ':' for string and int.
* The first parameter defines the type to be used, the second the lower bound and the third the upper bound. Only the type is required. * The first parameter defines the type to be used, the second the lower bound and the third the upper bound. Only the type is required.
*/ */
function validate_config_vars($config_vars, &$cfg_array, &$error) function validate_config_vars($config_vars, &$cfg_array, &$error)
@ -337,7 +337,7 @@ function validate_config_vars($config_vars, &$cfg_array, &$error)
{ {
$error[] = sprintf($user->lang['SETTING_TOO_SHORT'], $user->lang[$config_definition['lang']], $validator[$min]); $error[] = sprintf($user->lang['SETTING_TOO_SHORT'], $user->lang[$config_definition['lang']], $validator[$min]);
} }
else if (isset($validator[$max]) && $length > $validator[2]) else if (isset($validator[$max]) && $length > $validator[$max])
{ {
$error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$config_definition['lang']], $validator[$max]); $error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$config_definition['lang']], $validator[$max]);
} }

View file

@ -18,6 +18,7 @@ require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'functions_acp/build_cfg_template.php'; require_once 'functions_acp/build_cfg_template.php';
require_once 'functions_acp/build_select.php'; require_once 'functions_acp/build_select.php';
require_once 'functions_acp/h_radio.php'; require_once 'functions_acp/h_radio.php';
require_once 'functions_acp/validate_config_vars.php';
require_once 'functions_acp/validate_range.php'; require_once 'functions_acp/validate_range.php';
class phpbb_functions_acp_all_tests class phpbb_functions_acp_all_tests
@ -34,6 +35,7 @@ class phpbb_functions_acp_all_tests
$suite->addTestSuite('phpbb_functions_acp_build_cfg_template_test'); $suite->addTestSuite('phpbb_functions_acp_build_cfg_template_test');
$suite->addTestSuite('phpbb_functions_acp_built_select_test'); $suite->addTestSuite('phpbb_functions_acp_built_select_test');
$suite->addTestSuite('phpbb_functions_acp_h_radio_test'); $suite->addTestSuite('phpbb_functions_acp_h_radio_test');
$suite->addTestSuite('phpbb_functions_acp_validate_config_vars_test');
$suite->addTestSuite('phpbb_functions_acp_validate_range_test'); $suite->addTestSuite('phpbb_functions_acp_validate_range_test');
return $suite; return $suite;

View file

@ -0,0 +1,152 @@
<?php
/**
*
* @package testing
* @copyright (c) 2010 phpBB Group
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/
require_once 'test_framework/framework.php';
require_once 'functions_acp/user_mock.php';
require_once '../phpBB/includes/functions_acp.php';
class phpbb_functions_acp_validate_config_vars_test extends phpbb_test_case
{
/**
* Helper function which returns a string in a given length.
*/
static public function return_string($length)
{
$string = '';
for ($i = 0; $i < $length; $i++)
{
$string .= 'a';
}
return $string;
}
/**
* Data sets that don't throw an error.
*/
public function validate_config_vars_fit_data()
{
return array(
array(
array(
'test_bool' => array('lang' => 'TEST_BOOL', 'validate' => 'bool'),
'test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string'),
'test_string_128' => array('lang' => 'TEST_STRING_128', 'validate' => 'string:128'),
'test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64'),
'test_int' => array('lang' => 'TEST_INT', 'validate' => 'int'),
'test_int_32' => array('lang' => 'TEST_INT', 'validate' => 'int:32'),
'test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64'),
'test_lang' => array('lang' => 'TEST_LANG', 'validate' => 'lang'),
/*
'test_sp' => array('lang' => 'TEST_SP', 'validate' => 'script_path'),
'test_rpath' => array('lang' => 'TEST_RPATH', 'validate' => 'rpath'),
'test_rwpath' => array('lang' => 'TEST_RWPATH', 'validate' => 'rwpath'),
'test_path' => array('lang' => 'TEST_PATH', 'validate' => 'path'),
'test_wpath' => array('lang' => 'TEST_WPATH', 'validate' => 'wpath'),
*/
),
array(
'test_bool' => true,
'test_string' => phpbb_functions_acp_validate_config_vars_test::return_string(255),
'test_string_128' => phpbb_functions_acp_validate_config_vars_test::return_string(128),
'test_string_32_64' => phpbb_functions_acp_validate_config_vars_test::return_string(48),
'test_int' => 128,
'test_int_32' => 32,
'test_int_32_64' => 48,
'test_lang' => 'en',
),
),
);
}
/**
* @dataProvider validate_config_vars_fit_data
*/
public function test_validate_config_vars_fit($test_data, $cfg_array)
{
global $user;
$user->lang = new phpbb_mock_lang();
$phpbb_error = array();
validate_config_vars($test_data, $cfg_array, $phpbb_error);
$this->assertEquals(array(), $phpbb_error);
}
/**
* Data sets that throw the error.
*/
public function validate_config_vars_error_data()
{
return array(
array(
array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')),
array('test_string_32_64' => phpbb_functions_acp_validate_config_vars_test::return_string(20)),
array('SETTING_TOO_SHORT'),
),
array(
array('test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string')),
array('test_string' => phpbb_functions_acp_validate_config_vars_test::return_string(256)),
array('SETTING_TOO_LONG'),
),
array(
array('test_string_32_64' => array('lang' => 'TEST_STRING_32_64', 'validate' => 'string:32:64')),
array('test_string_32_64' => phpbb_functions_acp_validate_config_vars_test::return_string(65)),
array('SETTING_TOO_LONG'),
),
array(
array('test_int_32' => array('lang' => 'TEST_INT', 'validate' => 'int:32')),
array('test_int_32' => 31),
array('SETTING_TOO_LOW'),
),
array(
array('test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64')),
array('test_int_32_64' => 31),
array('SETTING_TOO_LOW'),
),
array(
array('test_int_32_64' => array('lang' => 'TEST_INT', 'validate' => 'int:32:64')),
array('test_int_32_64' => 65),
array('SETTING_TOO_BIG'),
),
array(
array(
'test_int_min' => array('lang' => 'TEST_INT_MIN', 'validate' => 'int:32:64'),
'test_int_max' => array('lang' => 'TEST_INT_MAX', 'validate' => 'int:32:64'),
),
array(
'test_int_min' => 52,
'test_int_max' => 48,
),
array('SETTING_TOO_LOW'),
),
array(
array('test_lang' => array('lang' => 'TEST_LANG', 'validate' => 'lang')),
array('test_lang' => 'this_is_no_language'),
array('WRONG_DATA_LANG'),
),
);
}
/**
* @dataProvider validate_config_vars_error_data
*/
public function test_validate_config_vars_error($test_data, $cfg_array, $expected)
{
global $user;
$user->lang = new phpbb_mock_lang();
$phpbb_error = array();
validate_config_vars($test_data, $cfg_array, $phpbb_error);
$this->assertEquals($expected, $phpbb_error);
}
}

View file

@ -13,14 +13,9 @@ require_once '../phpBB/includes/functions_acp.php';
class phpbb_functions_acp_validate_range_test extends phpbb_test_case class phpbb_functions_acp_validate_range_test extends phpbb_test_case
{ {
/* 'BOOL' => array('php_type' => 'int', 'min' => 0, 'max' => 1), /**
'USINT' => array('php_type' => 'int', 'min' => 0, 'max' => 65535), * Helper function which returns a string in a given length.
'UINT' => array('php_type' => 'int', 'min' => 0, 'max' => (int) 0x7fffffff), */
'INT' => array('php_type' => 'int', 'min' => (int) 0x80000000, 'max' => (int) 0x7fffffff),
'TINT' => array('php_type' => 'int', 'min' => -128, 'max' => 127),
'VCHAR' => array('php_type' => 'string', 'min' => 0, 'max' => 255),
*/
static public function return_string($length) static public function return_string($length)
{ {
$string = ''; $string = '';