diff options
-rw-r--r-- | phpBB/adm/index.php | 542 | ||||
-rw-r--r-- | phpBB/includes/functions_acp.php | 550 | ||||
-rw-r--r-- | tests/functions_acp/build_cfg_template_test.php | 192 | ||||
-rw-r--r-- | tests/functions_acp/build_select_test.php | 56 | ||||
-rw-r--r-- | tests/functions_acp/h_radio_test.php | 121 | ||||
-rw-r--r-- | tests/functions_acp/validate_config_vars_test.php | 151 | ||||
-rw-r--r-- | tests/functions_acp/validate_range_test.php | 179 | ||||
-rw-r--r-- | tests/mock/lang.php | 33 |
8 files changed, 1283 insertions, 541 deletions
diff --git a/phpBB/adm/index.php b/phpBB/adm/index.php index ce5012ba29..726cb1644c 100644 --- a/phpBB/adm/index.php +++ b/phpBB/adm/index.php @@ -18,6 +18,7 @@ define('NEED_SID', true); $phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './../'; $phpEx = substr(strrchr(__FILE__, '.'), 1); require($phpbb_root_path . 'common.' . $phpEx); +require($phpbb_root_path . 'includes/functions_acp.' . $phpEx); require($phpbb_root_path . 'includes/functions_admin.' . $phpEx); require($phpbb_root_path . 'includes/functions_module.' . $phpEx); @@ -81,544 +82,3 @@ $template->set_filenames(array( )); adm_page_footer(); - -/** -* Header for acp pages -*/ -function adm_page_header($page_title) -{ - global $config, $db, $user, $template; - global $phpbb_root_path, $phpbb_admin_path, $phpEx, $SID, $_SID; - - if (defined('HEADER_INC')) - { - return; - } - - define('HEADER_INC', true); - - // gzip_compression - if ($config['gzip_compress']) - { - if (@extension_loaded('zlib') && !headers_sent()) - { - ob_start('ob_gzhandler'); - } - } - - $template->assign_vars(array( - 'PAGE_TITLE' => $page_title, - 'USERNAME' => $user->data['username'], - - 'SID' => $SID, - '_SID' => $_SID, - 'SESSION_ID' => $user->session_id, - 'ROOT_PATH' => $phpbb_admin_path, - - 'U_LOGOUT' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=logout'), - 'U_ADM_LOGOUT' => append_sid("{$phpbb_admin_path}index.$phpEx", 'action=admlogout'), - 'U_ADM_INDEX' => append_sid("{$phpbb_admin_path}index.$phpEx"), - 'U_INDEX' => append_sid("{$phpbb_root_path}index.$phpEx"), - - 'T_IMAGES_PATH' => "{$phpbb_root_path}images/", - 'T_SMILIES_PATH' => "{$phpbb_root_path}{$config['smilies_path']}/", - 'T_AVATAR_PATH' => "{$phpbb_root_path}{$config['avatar_path']}/", - 'T_AVATAR_GALLERY_PATH' => "{$phpbb_root_path}{$config['avatar_gallery_path']}/", - 'T_ICONS_PATH' => "{$phpbb_root_path}{$config['icons_path']}/", - 'T_RANKS_PATH' => "{$phpbb_root_path}{$config['ranks_path']}/", - 'T_UPLOAD_PATH' => "{$phpbb_root_path}{$config['upload_path']}/", - - 'ICON_MOVE_UP' => '<img src="' . $phpbb_admin_path . 'images/icon_up.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />', - 'ICON_MOVE_UP_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_up_disabled.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />', - 'ICON_MOVE_DOWN' => '<img src="' . $phpbb_admin_path . 'images/icon_down.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />', - 'ICON_MOVE_DOWN_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_down_disabled.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />', - 'ICON_EDIT' => '<img src="' . $phpbb_admin_path . 'images/icon_edit.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />', - 'ICON_EDIT_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_edit_disabled.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />', - 'ICON_DELETE' => '<img src="' . $phpbb_admin_path . 'images/icon_delete.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />', - 'ICON_DELETE_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_delete_disabled.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />', - 'ICON_SYNC' => '<img src="' . $phpbb_admin_path . 'images/icon_sync.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />', - 'ICON_SYNC_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_sync_disabled.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />', - - 'S_USER_LANG' => $user->lang['USER_LANG'], - 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'], - 'S_CONTENT_ENCODING' => 'UTF-8', - 'S_CONTENT_FLOW_BEGIN' => ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right', - 'S_CONTENT_FLOW_END' => ($user->lang['DIRECTION'] == 'ltr') ? 'right' : 'left', - )); - - // application/xhtml+xml not used because of IE - header('Content-type: text/html; charset=UTF-8'); - - header('Cache-Control: private, no-cache="set-cookie"'); - header('Expires: 0'); - header('Pragma: no-cache'); - - return; -} - -/** -* Page footer for acp pages -*/ -function adm_page_footer($copyright_html = true) -{ - global $db, $config, $template, $user, $auth, $cache; - global $starttime, $phpbb_root_path, $phpbb_admin_path, $phpEx; - global $request; - - // Output page creation time - if (defined('DEBUG')) - { - $mtime = explode(' ', microtime()); - $totaltime = $mtime[0] + $mtime[1] - $starttime; - - if ($request->variable('explain', false) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report')) - { - $db->sql_report('display'); - } - - $debug_output = sprintf('Time : %.3fs | ' . $db->sql_num_queries() . ' Queries | GZIP : ' . (($config['gzip_compress']) ? 'On' : 'Off') . (($user->load) ? ' | Load : ' . $user->load : ''), $totaltime); - - if ($auth->acl_get('a_') && defined('DEBUG_EXTRA')) - { - if (function_exists('memory_get_usage')) - { - if ($memory_usage = memory_get_usage()) - { - global $base_memory_usage; - $memory_usage -= $base_memory_usage; - $memory_usage = get_formatted_filesize($memory_usage); - - $debug_output .= ' | Memory Usage: ' . $memory_usage; - } - } - - $debug_output .= ' | <a href="' . build_url() . '&explain=1">Explain</a>'; - } - } - - $template->assign_vars(array( - 'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : '', - 'TRANSLATION_INFO' => (!empty($user->lang['TRANSLATION_INFO'])) ? $user->lang['TRANSLATION_INFO'] : '', - 'S_COPYRIGHT_HTML' => $copyright_html, - 'VERSION' => $config['version']) - ); - - $template->display('body'); - - garbage_collection(); - exit_handler(); -} - -/** -* Generate back link for acp pages -*/ -function adm_back_link($u_action) -{ - global $user; - return '<br /><br /><a href="' . $u_action . '">« ' . $user->lang['BACK_TO_PREV'] . '</a>'; -} - -/** -* Build select field options in acp pages -*/ -function build_select($option_ary, $option_default = false) -{ - global $user; - - $html = ''; - foreach ($option_ary as $value => $title) - { - $selected = ($option_default !== false && $value == $option_default) ? ' selected="selected"' : ''; - $html .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$title] . '</option>'; - } - - return $html; -} - -/** -* Build radio fields in acp pages -*/ -function h_radio($name, &$input_ary, $input_default = false, $id = false, $key = false) -{ - global $user; - - $html = ''; - $id_assigned = false; - foreach ($input_ary as $value => $title) - { - $selected = ($input_default !== false && $value == $input_default) ? ' checked="checked"' : ''; - $html .= '<label><input type="radio" name="' . $name . '"' . (($id && !$id_assigned) ? ' id="' . $id . '"' : '') . ' value="' . $value . '"' . $selected . (($key) ? ' accesskey="' . $key . '"' : '') . ' class="radio" /> ' . $user->lang[$title] . '</label>'; - $id_assigned = true; - } - - return $html; -} - -/** -* Build configuration template for acp configuration pages -*/ -function build_cfg_template($tpl_type, $key, &$new, $config_key, $vars) -{ - global $user, $module; - - $tpl = ''; - $name = 'config[' . $config_key . ']'; - - // Make sure there is no notice printed out for non-existent config options (we simply set them) - if (!isset($new[$config_key])) - { - $new[$config_key] = ''; - } - - switch ($tpl_type[0]) - { - case 'text': - case 'password': - $size = (int) $tpl_type[1]; - $maxlength = (int) $tpl_type[2]; - - $tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new[$config_key] . '" />'; - break; - - case 'dimension': - $size = (int) $tpl_type[1]; - $maxlength = (int) $tpl_type[2]; - - $tpl = '<input id="' . $key . '" type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_width]" value="' . $new[$config_key . '_width'] . '" /> x <input type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_height]" value="' . $new[$config_key . '_height'] . '" />'; - break; - - case 'textarea': - $rows = (int) $tpl_type[1]; - $cols = (int) $tpl_type[2]; - - $tpl = '<textarea id="' . $key . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $new[$config_key] . '</textarea>'; - break; - - case 'radio': - $key_yes = ($new[$config_key]) ? ' checked="checked"' : ''; - $key_no = (!$new[$config_key]) ? ' checked="checked"' : ''; - - $tpl_type_cond = explode('_', $tpl_type[1]); - $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true; - - $tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $user->lang['NO'] : $user->lang['DISABLED']) . '</label>'; - $tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $user->lang['YES'] : $user->lang['ENABLED']) . '</label>'; - - $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes; - break; - - case 'select': - case 'custom': - - $return = ''; - - if (isset($vars['method'])) - { - $call = array($module->module, $vars['method']); - } - else if (isset($vars['function'])) - { - $call = $vars['function']; - } - else - { - break; - } - - if (isset($vars['params'])) - { - $args = array(); - foreach ($vars['params'] as $value) - { - switch ($value) - { - case '{CONFIG_VALUE}': - $value = $new[$config_key]; - break; - - case '{KEY}': - $value = $key; - break; - } - - $args[] = $value; - } - } - else - { - $args = array($new[$config_key], $key); - } - - $return = call_user_func_array($call, $args); - - if ($tpl_type[0] == 'select') - { - $tpl = '<select id="' . $key . '" name="' . $name . '">' . $return . '</select>'; - } - else - { - $tpl = $return; - } - - break; - - default: - break; - } - - if (isset($vars['append'])) - { - $tpl .= $vars['append']; - } - - return $tpl; -} - -/** -* 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. -*/ -function validate_config_vars($config_vars, &$cfg_array, &$error) -{ - global $phpbb_root_path, $user; - $type = 0; - $min = 1; - $max = 2; - - foreach ($config_vars as $config_name => $config_definition) - { - if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false) - { - continue; - } - - if (!isset($config_definition['validate'])) - { - continue; - } - - $validator = explode(':', $config_definition['validate']); - - // Validate a bit. ;) (0 = type, 1 = min, 2= max) - switch ($validator[$type]) - { - case 'string': - $length = strlen($cfg_array[$config_name]); - - // the column is a VARCHAR - $validator[$max] = (isset($validator[$max])) ? min(255, $validator[$max]) : 255; - - if (isset($validator[$min]) && $length < $validator[$min]) - { - $error[] = sprintf($user->lang['SETTING_TOO_SHORT'], $user->lang[$config_definition['lang']], $validator[$min]); - } - else if (isset($validator[$max]) && $length > $validator[2]) - { - $error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$config_definition['lang']], $validator[$max]); - } - break; - - case 'bool': - $cfg_array[$config_name] = ($cfg_array[$config_name]) ? 1 : 0; - break; - - case 'int': - $cfg_array[$config_name] = (int) $cfg_array[$config_name]; - - if (isset($validator[$min]) && $cfg_array[$config_name] < $validator[$min]) - { - $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], $validator[$min]); - } - else if (isset($validator[$max]) && $cfg_array[$config_name] > $validator[$max]) - { - $error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$config_definition['lang']], $validator[$max]); - } - - if (strpos($config_name, '_max') !== false) - { - // Min/max pairs of settings should ensure that min <= max - // Replace _max with _min to find the name of the minimum - // corresponding configuration variable - $min_name = str_replace('_max', '_min', $config_name); - - if (isset($cfg_array[$min_name]) && is_numeric($cfg_array[$min_name]) && $cfg_array[$config_name] < $cfg_array[$min_name]) - { - // A minimum value exists and the maximum value is less than it - $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], (int) $cfg_array[$min_name]); - } - } - break; - - // Absolute path - case 'script_path': - if (!$cfg_array[$config_name]) - { - break; - } - - $destination = str_replace('\\', '/', $cfg_array[$config_name]); - - if ($destination !== '/') - { - // Adjust destination path (no trailing slash) - if (substr($destination, -1, 1) == '/') - { - $destination = substr($destination, 0, -1); - } - - $destination = str_replace(array('../', './'), '', $destination); - - if ($destination[0] != '/') - { - $destination = '/' . $destination; - } - } - - $cfg_array[$config_name] = trim($destination); - - break; - - // Absolute path - case 'lang': - if (!$cfg_array[$config_name]) - { - break; - } - - $cfg_array[$config_name] = basename($cfg_array[$config_name]); - - if (!file_exists($phpbb_root_path . 'language/' . $cfg_array[$config_name] . '/')) - { - $error[] = $user->lang['WRONG_DATA_LANG']; - } - break; - - // Relative path (appended $phpbb_root_path) - case 'rpath': - case 'rwpath': - if (!$cfg_array[$config_name]) - { - break; - } - - $destination = $cfg_array[$config_name]; - - // Adjust destination path (no trailing slash) - if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\') - { - $destination = substr($destination, 0, -1); - } - - $destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination); - if ($destination && ($destination[0] == '/' || $destination[0] == "\\")) - { - $destination = ''; - } - - $cfg_array[$config_name] = trim($destination); - - // Path being relative (still prefixed by phpbb_root_path), but with the ability to escape the root dir... - case 'path': - case 'wpath': - - if (!$cfg_array[$config_name]) - { - break; - } - - $cfg_array[$config_name] = trim($cfg_array[$config_name]); - - // Make sure no NUL byte is present... - if (strpos($cfg_array[$config_name], "\0") !== false || strpos($cfg_array[$config_name], '%00') !== false) - { - $cfg_array[$config_name] = ''; - break; - } - - if (!file_exists($phpbb_root_path . $cfg_array[$config_name])) - { - $error[] = sprintf($user->lang['DIRECTORY_DOES_NOT_EXIST'], $cfg_array[$config_name]); - } - - if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !is_dir($phpbb_root_path . $cfg_array[$config_name])) - { - $error[] = sprintf($user->lang['DIRECTORY_NOT_DIR'], $cfg_array[$config_name]); - } - - // Check if the path is writable - if ($config_definition['validate'] == 'wpath' || $config_definition['validate'] == 'rwpath') - { - if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !phpbb_is_writable($phpbb_root_path . $cfg_array[$config_name])) - { - $error[] = sprintf($user->lang['DIRECTORY_NOT_WRITABLE'], $cfg_array[$config_name]); - } - } - - break; - } - } - - return; -} - -/** -* Checks whatever or not a variable is OK for use in the Database -* param mixed $value_ary An array of the form array(array('lang' => ..., 'value' => ..., 'column_type' =>))' -* param mixed $error The error array -*/ -function validate_range($value_ary, &$error) -{ - global $user; - - $column_types = array( - 'BOOL' => array('php_type' => 'int', 'min' => 0, 'max' => 1), - 'USINT' => array('php_type' => 'int', 'min' => 0, 'max' => 65535), - 'UINT' => array('php_type' => 'int', 'min' => 0, 'max' => (int) 0x7fffffff), - // Do not use (int) 0x80000000 - it evaluates to different - // values on 32-bit and 64-bit systems. - // Apparently -2147483648 is a float on 32-bit systems, - // despite fitting in an int, thus explicit cast is needed. - 'INT' => array('php_type' => 'int', 'min' => (int) -2147483648, 'max' => (int) 0x7fffffff), - 'TINT' => array('php_type' => 'int', 'min' => -128, 'max' => 127), - - 'VCHAR' => array('php_type' => 'string', 'min' => 0, 'max' => 255), - ); - foreach ($value_ary as $value) - { - $column = explode(':', $value['column_type']); - $max = $min = 0; - $type = 0; - if (!isset($column_types[$column[0]])) - { - continue; - } - else - { - $type = $column_types[$column[0]]; - } - - switch ($type['php_type']) - { - case 'string' : - $max = (isset($column[1])) ? min($column[1],$type['max']) : $type['max']; - if (strlen($value['value']) > $max) - { - $error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$value['lang']], $max); - } - break; - - case 'int': - $min = (isset($column[1])) ? max($column[1],$type['min']) : $type['min']; - $max = (isset($column[2])) ? min($column[2],$type['max']) : $type['max']; - if ($value['value'] < $min) - { - $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$value['lang']], $min); - } - else if ($value['value'] > $max) - { - $error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$value['lang']], $max); - } - break; - } - } -} diff --git a/phpBB/includes/functions_acp.php b/phpBB/includes/functions_acp.php new file mode 100644 index 0000000000..d944d9bd62 --- /dev/null +++ b/phpBB/includes/functions_acp.php @@ -0,0 +1,550 @@ +<?php
+/**
+*
+* @package acp
+* @version $Id$
+* @copyright (c) 2005 phpBB Group
+* @license http://opensource.org/licenses/gpl-license.php GNU Public License
+*
+*/
+
+/**
+* Header for acp pages
+*/
+function adm_page_header($page_title)
+{
+ global $config, $db, $user, $template;
+ global $phpbb_root_path, $phpbb_admin_path, $phpEx, $SID, $_SID;
+
+ if (defined('HEADER_INC'))
+ {
+ return;
+ }
+
+ define('HEADER_INC', true);
+
+ // gzip_compression
+ if ($config['gzip_compress'])
+ {
+ if (@extension_loaded('zlib') && !headers_sent())
+ {
+ ob_start('ob_gzhandler');
+ }
+ }
+
+ $template->assign_vars(array(
+ 'PAGE_TITLE' => $page_title,
+ 'USERNAME' => $user->data['username'],
+
+ 'SID' => $SID,
+ '_SID' => $_SID,
+ 'SESSION_ID' => $user->session_id,
+ 'ROOT_PATH' => $phpbb_admin_path,
+
+ 'U_LOGOUT' => append_sid("{$phpbb_root_path}ucp.$phpEx", 'mode=logout'),
+ 'U_ADM_LOGOUT' => append_sid("{$phpbb_admin_path}index.$phpEx", 'action=admlogout'),
+ 'U_ADM_INDEX' => append_sid("{$phpbb_admin_path}index.$phpEx"),
+ 'U_INDEX' => append_sid("{$phpbb_root_path}index.$phpEx"),
+
+ 'T_IMAGES_PATH' => "{$phpbb_root_path}images/",
+ 'T_SMILIES_PATH' => "{$phpbb_root_path}{$config['smilies_path']}/",
+ 'T_AVATAR_PATH' => "{$phpbb_root_path}{$config['avatar_path']}/",
+ 'T_AVATAR_GALLERY_PATH' => "{$phpbb_root_path}{$config['avatar_gallery_path']}/",
+ 'T_ICONS_PATH' => "{$phpbb_root_path}{$config['icons_path']}/",
+ 'T_RANKS_PATH' => "{$phpbb_root_path}{$config['ranks_path']}/",
+ 'T_UPLOAD_PATH' => "{$phpbb_root_path}{$config['upload_path']}/",
+
+ 'ICON_MOVE_UP' => '<img src="' . $phpbb_admin_path . 'images/icon_up.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
+ 'ICON_MOVE_UP_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_up_disabled.gif" alt="' . $user->lang['MOVE_UP'] . '" title="' . $user->lang['MOVE_UP'] . '" />',
+ 'ICON_MOVE_DOWN' => '<img src="' . $phpbb_admin_path . 'images/icon_down.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
+ 'ICON_MOVE_DOWN_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_down_disabled.gif" alt="' . $user->lang['MOVE_DOWN'] . '" title="' . $user->lang['MOVE_DOWN'] . '" />',
+ 'ICON_EDIT' => '<img src="' . $phpbb_admin_path . 'images/icon_edit.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
+ 'ICON_EDIT_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_edit_disabled.gif" alt="' . $user->lang['EDIT'] . '" title="' . $user->lang['EDIT'] . '" />',
+ 'ICON_DELETE' => '<img src="' . $phpbb_admin_path . 'images/icon_delete.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
+ 'ICON_DELETE_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_delete_disabled.gif" alt="' . $user->lang['DELETE'] . '" title="' . $user->lang['DELETE'] . '" />',
+ 'ICON_SYNC' => '<img src="' . $phpbb_admin_path . 'images/icon_sync.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
+ 'ICON_SYNC_DISABLED' => '<img src="' . $phpbb_admin_path . 'images/icon_sync_disabled.gif" alt="' . $user->lang['RESYNC'] . '" title="' . $user->lang['RESYNC'] . '" />',
+
+ 'S_USER_LANG' => $user->lang['USER_LANG'],
+ 'S_CONTENT_DIRECTION' => $user->lang['DIRECTION'],
+ 'S_CONTENT_ENCODING' => 'UTF-8',
+ 'S_CONTENT_FLOW_BEGIN' => ($user->lang['DIRECTION'] == 'ltr') ? 'left' : 'right',
+ 'S_CONTENT_FLOW_END' => ($user->lang['DIRECTION'] == 'ltr') ? 'right' : 'left',
+ ));
+
+ // application/xhtml+xml not used because of IE
+ header('Content-type: text/html; charset=UTF-8');
+
+ header('Cache-Control: private, no-cache="set-cookie"');
+ header('Expires: 0');
+ header('Pragma: no-cache');
+
+ return;
+}
+
+/**
+* Page footer for acp pages
+*/
+function adm_page_footer($copyright_html = true)
+{
+ global $db, $config, $template, $user, $auth, $cache;
+ global $starttime, $phpbb_root_path, $phpbb_admin_path, $phpEx;
+ global $request;
+
+ // Output page creation time
+ if (defined('DEBUG'))
+ {
+ $mtime = explode(' ', microtime());
+ $totaltime = $mtime[0] + $mtime[1] - $starttime;
+
+ if ($request->variable('explain', false) && $auth->acl_get('a_') && defined('DEBUG_EXTRA') && method_exists($db, 'sql_report'))
+ {
+ $db->sql_report('display');
+ }
+
+ $debug_output = sprintf('Time : %.3fs | ' . $db->sql_num_queries() . ' Queries | GZIP : ' . (($config['gzip_compress']) ? 'On' : 'Off') . (($user->load) ? ' | Load : ' . $user->load : ''), $totaltime);
+
+ if ($auth->acl_get('a_') && defined('DEBUG_EXTRA'))
+ {
+ if (function_exists('memory_get_usage'))
+ {
+ if ($memory_usage = memory_get_usage())
+ {
+ global $base_memory_usage;
+ $memory_usage -= $base_memory_usage;
+ $memory_usage = get_formatted_filesize($memory_usage);
+
+ $debug_output .= ' | Memory Usage: ' . $memory_usage;
+ }
+ }
+
+ $debug_output .= ' | <a href="' . build_url() . '&explain=1">Explain</a>';
+ }
+ }
+
+ $template->assign_vars(array(
+ 'DEBUG_OUTPUT' => (defined('DEBUG')) ? $debug_output : '',
+ 'TRANSLATION_INFO' => (!empty($user->lang['TRANSLATION_INFO'])) ? $user->lang['TRANSLATION_INFO'] : '',
+ 'S_COPYRIGHT_HTML' => $copyright_html,
+ 'VERSION' => $config['version'])
+ );
+
+ $template->display('body');
+
+ garbage_collection();
+ exit_handler();
+}
+
+/**
+* Generate back link for acp pages
+*/
+function adm_back_link($u_action)
+{
+ global $user;
+ return '<br /><br /><a href="' . $u_action . '">« ' . $user->lang['BACK_TO_PREV'] . '</a>';
+}
+
+/**
+* Build select field options in acp pages
+*/
+function build_select($option_ary, $option_default = false)
+{
+ global $user;
+
+ $html = '';
+ foreach ($option_ary as $value => $title)
+ {
+ $selected = ($option_default !== false && $value == $option_default) ? ' selected="selected"' : '';
+ $html .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$title] . '</option>';
+ }
+
+ return $html;
+}
+
+/**
+* Build radio fields in acp pages
+*/
+function h_radio($name, &$input_ary, $input_default = false, $id = false, $key = false)
+{
+ global $user;
+
+ $html = '';
+ $id_assigned = false;
+ foreach ($input_ary as $value => $title)
+ {
+ $selected = ($input_default !== false && $value == $input_default) ? ' checked="checked"' : '';
+ $html .= '<label><input type="radio" name="' . $name . '"' . (($id && !$id_assigned) ? ' id="' . $id . '"' : '') . ' value="' . $value . '"' . $selected . (($key) ? ' accesskey="' . $key . '"' : '') . ' class="radio" /> ' . $user->lang[$title] . '</label>';
+ $id_assigned = true;
+ }
+
+ return $html;
+}
+
+/**
+* Build configuration template for acp configuration pages
+*/
+function build_cfg_template($tpl_type, $key, &$new, $config_key, $vars)
+{
+ global $user, $module;
+
+ $tpl = '';
+ $name = 'config[' . $config_key . ']';
+
+ // Make sure there is no notice printed out for non-existent config options (we simply set them)
+ if (!isset($new[$config_key]))
+ {
+ $new[$config_key] = '';
+ }
+
+ switch ($tpl_type[0])
+ {
+ case 'text':
+ case 'password':
+ $size = (int) $tpl_type[1];
+ $maxlength = (int) $tpl_type[2];
+
+ $tpl = '<input id="' . $key . '" type="' . $tpl_type[0] . '"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="' . $name . '" value="' . $new[$config_key] . '" />';
+ break;
+
+ case 'dimension':
+ $size = (int) $tpl_type[1];
+ $maxlength = (int) $tpl_type[2];
+
+ $tpl = '<input id="' . $key . '" type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_width]" value="' . $new[$config_key . '_width'] . '" /> x <input type="text"' . (($size) ? ' size="' . $size . '"' : '') . ' maxlength="' . (($maxlength) ? $maxlength : 255) . '" name="config[' . $config_key . '_height]" value="' . $new[$config_key . '_height'] . '" />';
+ break;
+
+ case 'textarea':
+ $rows = (int) $tpl_type[1];
+ $cols = (int) $tpl_type[2];
+
+ $tpl = '<textarea id="' . $key . '" name="' . $name . '" rows="' . $rows . '" cols="' . $cols . '">' . $new[$config_key] . '</textarea>';
+ break;
+
+ case 'radio':
+ $key_yes = ($new[$config_key]) ? ' checked="checked"' : '';
+ $key_no = (!$new[$config_key]) ? ' checked="checked"' : '';
+
+ $tpl_type_cond = explode('_', $tpl_type[1]);
+ $type_no = ($tpl_type_cond[0] == 'disabled' || $tpl_type_cond[0] == 'enabled') ? false : true;
+
+ $tpl_no = '<label><input type="radio" name="' . $name . '" value="0"' . $key_no . ' class="radio" /> ' . (($type_no) ? $user->lang['NO'] : $user->lang['DISABLED']) . '</label>';
+ $tpl_yes = '<label><input type="radio" id="' . $key . '" name="' . $name . '" value="1"' . $key_yes . ' class="radio" /> ' . (($type_no) ? $user->lang['YES'] : $user->lang['ENABLED']) . '</label>';
+
+ $tpl = ($tpl_type_cond[0] == 'yes' || $tpl_type_cond[0] == 'enabled') ? $tpl_yes . $tpl_no : $tpl_no . $tpl_yes;
+ break;
+
+ case 'select':
+ case 'custom':
+
+ $return = '';
+
+ if (isset($vars['method']))
+ {
+ $call = array($module->module, $vars['method']);
+ }
+ else if (isset($vars['function']))
+ {
+ $call = $vars['function'];
+ }
+ else
+ {
+ break;
+ }
+
+ if (isset($vars['params']))
+ {
+ $args = array();
+ foreach ($vars['params'] as $value)
+ {
+ switch ($value)
+ {
+ case '{CONFIG_VALUE}':
+ $value = $new[$config_key];
+ break;
+
+ case '{KEY}':
+ $value = $key;
+ break;
+ }
+
+ $args[] = $value;
+ }
+ }
+ else
+ {
+ $args = array($new[$config_key], $key);
+ }
+
+ $return = call_user_func_array($call, $args);
+
+ if ($tpl_type[0] == 'select')
+ {
+ $tpl = '<select id="' . $key . '" name="' . $name . '">' . $return . '</select>';
+ }
+ else
+ {
+ $tpl = $return;
+ }
+
+ break;
+
+ default:
+ break;
+ }
+
+ if (isset($vars['append']))
+ {
+ $tpl .= $vars['append'];
+ }
+
+ return $tpl;
+}
+
+/**
+* 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.
+*/
+function validate_config_vars($config_vars, &$cfg_array, &$error)
+{
+ global $phpbb_root_path, $user;
+ $type = 0;
+ $min = 1;
+ $max = 2;
+
+ foreach ($config_vars as $config_name => $config_definition)
+ {
+ if (!isset($cfg_array[$config_name]) || strpos($config_name, 'legend') !== false)
+ {
+ continue;
+ }
+
+ if (!isset($config_definition['validate']))
+ {
+ continue;
+ }
+
+ $validator = explode(':', $config_definition['validate']);
+
+ // Validate a bit. ;) (0 = type, 1 = min, 2= max)
+ switch ($validator[$type])
+ {
+ case 'string':
+ $length = strlen($cfg_array[$config_name]);
+
+ // the column is a VARCHAR
+ $validator[$max] = (isset($validator[$max])) ? min(255, $validator[$max]) : 255;
+
+ if (isset($validator[$min]) && $length < $validator[$min])
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_SHORT'], $user->lang[$config_definition['lang']], $validator[$min]);
+ }
+ else if (isset($validator[$max]) && $length > $validator[2])
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$config_definition['lang']], $validator[$max]);
+ }
+ break;
+
+ case 'bool':
+ $cfg_array[$config_name] = ($cfg_array[$config_name]) ? 1 : 0;
+ break;
+
+ case 'int':
+ $cfg_array[$config_name] = (int) $cfg_array[$config_name];
+
+ if (isset($validator[$min]) && $cfg_array[$config_name] < $validator[$min])
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], $validator[$min]);
+ }
+ else if (isset($validator[$max]) && $cfg_array[$config_name] > $validator[$max])
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$config_definition['lang']], $validator[$max]);
+ }
+
+ if (strpos($config_name, '_max') !== false)
+ {
+ // Min/max pairs of settings should ensure that min <= max
+ // Replace _max with _min to find the name of the minimum
+ // corresponding configuration variable
+ $min_name = str_replace('_max', '_min', $config_name);
+
+ if (isset($cfg_array[$min_name]) && is_numeric($cfg_array[$min_name]) && $cfg_array[$config_name] < $cfg_array[$min_name])
+ {
+ // A minimum value exists and the maximum value is less than it
+ $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$config_definition['lang']], (int) $cfg_array[$min_name]);
+ }
+ }
+ break;
+
+ // Absolute path
+ case 'script_path':
+ if (!$cfg_array[$config_name])
+ {
+ break;
+ }
+
+ $destination = str_replace('\\', '/', $cfg_array[$config_name]);
+
+ if ($destination !== '/')
+ {
+ // Adjust destination path (no trailing slash)
+ if (substr($destination, -1, 1) == '/')
+ {
+ $destination = substr($destination, 0, -1);
+ }
+
+ $destination = str_replace(array('../', './'), '', $destination);
+
+ if ($destination[0] != '/')
+ {
+ $destination = '/' . $destination;
+ }
+ }
+
+ $cfg_array[$config_name] = trim($destination);
+
+ break;
+
+ // Absolute path
+ case 'lang':
+ if (!$cfg_array[$config_name])
+ {
+ break;
+ }
+
+ $cfg_array[$config_name] = basename($cfg_array[$config_name]);
+
+ if (!file_exists($phpbb_root_path . 'language/' . $cfg_array[$config_name] . '/'))
+ {
+ $error[] = $user->lang['WRONG_DATA_LANG'];
+ }
+ break;
+
+ // Relative path (appended $phpbb_root_path)
+ case 'rpath':
+ case 'rwpath':
+ if (!$cfg_array[$config_name])
+ {
+ break;
+ }
+
+ $destination = $cfg_array[$config_name];
+
+ // Adjust destination path (no trailing slash)
+ if (substr($destination, -1, 1) == '/' || substr($destination, -1, 1) == '\\')
+ {
+ $destination = substr($destination, 0, -1);
+ }
+
+ $destination = str_replace(array('../', '..\\', './', '.\\'), '', $destination);
+ if ($destination && ($destination[0] == '/' || $destination[0] == "\\"))
+ {
+ $destination = '';
+ }
+
+ $cfg_array[$config_name] = trim($destination);
+
+ // Path being relative (still prefixed by phpbb_root_path), but with the ability to escape the root dir...
+ case 'path':
+ case 'wpath':
+
+ if (!$cfg_array[$config_name])
+ {
+ break;
+ }
+
+ $cfg_array[$config_name] = trim($cfg_array[$config_name]);
+
+ // Make sure no NUL byte is present...
+ if (strpos($cfg_array[$config_name], "\0") !== false || strpos($cfg_array[$config_name], '%00') !== false)
+ {
+ $cfg_array[$config_name] = '';
+ break;
+ }
+
+ if (!file_exists($phpbb_root_path . $cfg_array[$config_name]))
+ {
+ $error[] = sprintf($user->lang['DIRECTORY_DOES_NOT_EXIST'], $cfg_array[$config_name]);
+ }
+
+ if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !is_dir($phpbb_root_path . $cfg_array[$config_name]))
+ {
+ $error[] = sprintf($user->lang['DIRECTORY_NOT_DIR'], $cfg_array[$config_name]);
+ }
+
+ // Check if the path is writable
+ if ($config_definition['validate'] == 'wpath' || $config_definition['validate'] == 'rwpath')
+ {
+ if (file_exists($phpbb_root_path . $cfg_array[$config_name]) && !phpbb_is_writable($phpbb_root_path . $cfg_array[$config_name]))
+ {
+ $error[] = sprintf($user->lang['DIRECTORY_NOT_WRITABLE'], $cfg_array[$config_name]);
+ }
+ }
+
+ break;
+ }
+ }
+
+ return;
+}
+
+/**
+* Checks whatever or not a variable is OK for use in the Database
+* param mixed $value_ary An array of the form array(array('lang' => ..., 'value' => ..., 'column_type' =>))'
+* param mixed $error The error array
+*/
+function validate_range($value_ary, &$error)
+{
+ global $user;
+
+ $column_types = array(
+ 'BOOL' => array('php_type' => 'int', 'min' => 0, 'max' => 1),
+ 'USINT' => array('php_type' => 'int', 'min' => 0, 'max' => 65535),
+ 'UINT' => array('php_type' => 'int', 'min' => 0, 'max' => (int) 0x7fffffff),
+ // Do not use (int) 0x80000000 - it evaluates to different
+ // values on 32-bit and 64-bit systems.
+ // Apparently -2147483648 is a float on 32-bit systems,
+ // despite fitting in an int, thus explicit cast is needed.
+ 'INT' => array('php_type' => 'int', 'min' => (int) -2147483648, 'max' => (int) 0x7fffffff),
+ 'TINT' => array('php_type' => 'int', 'min' => -128, 'max' => 127),
+
+ 'VCHAR' => array('php_type' => 'string', 'min' => 0, 'max' => 255),
+ );
+ foreach ($value_ary as $value)
+ {
+ $column = explode(':', $value['column_type']);
+ $max = $min = 0;
+ $type = 0;
+ if (!isset($column_types[$column[0]]))
+ {
+ continue;
+ }
+ else
+ {
+ $type = $column_types[$column[0]];
+ }
+
+ switch ($type['php_type'])
+ {
+ case 'string' :
+ $max = (isset($column[1])) ? min($column[1],$type['max']) : $type['max'];
+ if (strlen($value['value']) > $max)
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_LONG'], $user->lang[$value['lang']], $max);
+ }
+ break;
+
+ case 'int':
+ $min = (isset($column[1])) ? max($column[1],$type['min']) : $type['min'];
+ $max = (isset($column[2])) ? min($column[2],$type['max']) : $type['max'];
+ if ($value['value'] < $min)
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_LOW'], $user->lang[$value['lang']], $min);
+ }
+ else if ($value['value'] > $max)
+ {
+ $error[] = sprintf($user->lang['SETTING_TOO_BIG'], $user->lang[$value['lang']], $max);
+ }
+ break;
+ }
+ }
+}
diff --git a/tests/functions_acp/build_cfg_template_test.php b/tests/functions_acp/build_cfg_template_test.php new file mode 100644 index 0000000000..7bf85a3532 --- /dev/null +++ b/tests/functions_acp/build_cfg_template_test.php @@ -0,0 +1,192 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../mock/lang.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_build_cfg_template_test extends phpbb_test_case +{ + public function build_cfg_template_text_data() + { + return array( + array( + array('text', 20, 255), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<input id="key_name" type="text" size="20" maxlength="255" name="config[config_key_name]" value="1" />', + ), + array( + array('password', 20, 128), + 'key_name', + array('config_key_name' => '2'), + 'config_key_name', + array(), + '<input id="key_name" type="password" size="20" maxlength="128" name="config[config_key_name]" value="2" />', + ), + array( + array('text', 0, 255), + 'key_name', + array('config_key_name' => '3'), + 'config_key_name', + array(), + '<input id="key_name" type="text" maxlength="255" name="config[config_key_name]" value="3" />', + ), + ); + } + + /** + * @dataProvider build_cfg_template_text_data + */ + public function test_build_cfg_template_text($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_dimension_data() + { + return array( + array( + array('dimension', 20, 255), + 'key_name', + array('config_key_name_width' => 10, 'config_key_name_height' => 20), + 'config_key_name', + array(), + '<input id="key_name" type="text" size="20" maxlength="255" name="config[config_key_name_width]" value="10" /> x <input type="text" size="20" maxlength="255" name="config[config_key_name_height]" value="20" />', + ), + array( + array('dimension', 0, 255), + 'key_name', + array('config_key_name_width' => 10, 'config_key_name_height' => 20), + 'config_key_name', + array(), + '<input id="key_name" type="text" maxlength="255" name="config[config_key_name_width]" value="10" /> x <input type="text" maxlength="255" name="config[config_key_name_height]" value="20" />', + ), + ); + } + + /** + * @dataProvider build_cfg_template_dimension_data + */ + public function test_build_cfg_template_dimension($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_textarea_data() + { + return array( + array( + array('textarea', 5, 30), + 'key_name', + array('config_key_name' => 'phpBB'), + 'config_key_name', + array(), + '<textarea id="key_name" name="config[config_key_name]" rows="5" cols="30">phpBB</textarea>', + ), + ); + } + + /** + * @dataProvider build_cfg_template_textarea_data + */ + public function test_build_cfg_template_textarea($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_radio_data() + { + return array( + array( + array('radio', 'enabled_disabled'), + 'key_name', + array('config_key_name' => '0'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" class="radio" /> ENABLED</label><label><input type="radio" name="config[config_key_name]" value="0" checked="checked" class="radio" /> DISABLED</label>', + ), + array( + array('radio', 'enabled_disabled'), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" checked="checked" class="radio" /> ENABLED</label><label><input type="radio" name="config[config_key_name]" value="0" class="radio" /> DISABLED</label>', + ), + array( + array('radio', 'yes_no'), + 'key_name', + array('config_key_name' => '0'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" class="radio" /> YES</label><label><input type="radio" name="config[config_key_name]" value="0" checked="checked" class="radio" /> NO</label>', + ), + array( + array('radio', 'yes_no'), + 'key_name', + array('config_key_name' => '1'), + 'config_key_name', + array(), + '<label><input type="radio" id="key_name" name="config[config_key_name]" value="1" checked="checked" class="radio" /> YES</label><label><input type="radio" name="config[config_key_name]" value="0" class="radio" /> NO</label>', + ), + ); + } + + /** + * @dataProvider build_cfg_template_radio_data + */ + public function test_build_cfg_template_radio($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } + + public function build_cfg_template_append_data() + { + return array( + array( + array('textarea', 5, 30), + 'key_name', + array('config_key_name' => 'phpBB'), + 'config_key_name', + array('append' => 'Bertie is cool!'), + '<textarea id="key_name" name="config[config_key_name]" rows="5" cols="30">phpBB</textarea>Bertie is cool!', + ), + ); + } + + /** + * @dataProvider build_cfg_template_append_data + */ + public function test_build_cfg_template_append($tpl_type, $key, $new, $config_key, $vars, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_cfg_template($tpl_type, $key, $new, $config_key, $vars)); + } +} diff --git a/tests/functions_acp/build_select_test.php b/tests/functions_acp/build_select_test.php new file mode 100644 index 0000000000..7079e69f12 --- /dev/null +++ b/tests/functions_acp/build_select_test.php @@ -0,0 +1,56 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../mock/lang.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_built_select_test extends phpbb_test_case +{ + public function build_select_data() + { + return array( + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + '<option value="test">TEST</option><option value="second">SEC_OPTION</option>', + ), + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + '<option value="test" selected="selected">TEST</option><option value="second">SEC_OPTION</option>', + ), + array( + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'second', + '<option value="test">TEST</option><option value="second" selected="selected">SEC_OPTION</option>', + ), + ); + } + + /** + * @dataProvider build_select_data + */ + public function test_build_select($option_ary, $option_default, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, build_select($option_ary, $option_default)); + } +} diff --git a/tests/functions_acp/h_radio_test.php b/tests/functions_acp/h_radio_test.php new file mode 100644 index 0000000000..18cb5d031e --- /dev/null +++ b/tests/functions_acp/h_radio_test.php @@ -0,0 +1,121 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../mock/lang.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_h_radio_test extends phpbb_test_case +{ + public function h_radio_data() + { + return array( + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + false, + false, + '<label><input type="radio" name="test_name" value="test" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + false, + false, + '<label><input type="radio" name="test_name" value="test" checked="checked" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + 'test_id', + false, + '<label><input type="radio" name="test_name" id="test_id" value="test" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + 'test_id', + false, + '<label><input type="radio" name="test_name" id="test_id" value="test" checked="checked" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" class="radio" /> SEC_OPTION</label>', + ), + + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + false, + 'k', + '<label><input type="radio" name="test_name" value="test" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + false, + 'k', + '<label><input type="radio" name="test_name" value="test" checked="checked" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + false, + 'test_id', + 'k', + '<label><input type="radio" name="test_name" id="test_id" value="test" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + array( + 'test_name', + array( + 'test' => 'TEST', + 'second' => 'SEC_OPTION', + ), + 'test', + 'test_id', + 'k', + '<label><input type="radio" name="test_name" id="test_id" value="test" checked="checked" accesskey="k" class="radio" /> TEST</label><label><input type="radio" name="test_name" value="second" accesskey="k" class="radio" /> SEC_OPTION</label>', + ), + ); + } + + /** + * @dataProvider h_radio_data + */ + public function test_h_radio($name, $input_ary, $input_default, $id, $key, $expected) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $this->assertEquals($expected, h_radio($name, $input_ary, $input_default, $id, $key)); + } +} diff --git a/tests/functions_acp/validate_config_vars_test.php b/tests/functions_acp/validate_config_vars_test.php new file mode 100644 index 0000000000..aa63bc38df --- /dev/null +++ b/tests/functions_acp/validate_config_vars_test.php @@ -0,0 +1,151 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../mock/lang.php'; +require_once dirname(__FILE__) . '/../../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' => self::return_string(255), + 'test_string_128' => self::return_string(128), + 'test_string_32_64' => self::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' => self::return_string(20)), + array('SETTING_TOO_SHORT'), + ), + array( + array('test_string' => array('lang' => 'TEST_STRING', 'validate' => 'string')), + array('test_string' => self::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' => self::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); + } +} diff --git a/tests/functions_acp/validate_range_test.php b/tests/functions_acp/validate_range_test.php new file mode 100644 index 0000000000..a9c9612ad7 --- /dev/null +++ b/tests/functions_acp/validate_range_test.php @@ -0,0 +1,179 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once dirname(__FILE__) . '/../mock/lang.php'; +require_once dirname(__FILE__) . '/../../phpBB/includes/functions_acp.php'; + +class phpbb_functions_acp_validate_range_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_range_data_fit() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 1))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 65535))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => 0))), + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => (int) 0x7fffffff))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => (int) -2147483648))), + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => (int) 0x7fffffff))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => -28))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => 35))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => -128))), + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => 127))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => -16))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => 16))), + + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => ''))), + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => self::return_string(255)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => self::return_string(128)))), + ); + } + + /** + * @dataProvider validate_range_data_fit + */ + public function test_validate_range_fit($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array(), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_LOW-error. + */ + public function validate_range_data_too_low() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => -1))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => -1))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 31))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => -1))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 31))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => ((int) -2147483648) - 1))), + array(array(array('column_type' => 'INT:32:128', 'lang' => 'TEST', 'value' => 31))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => -33))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => -129))), + array(array(array('column_type' => 'TINT:32:64', 'lang' => 'TEST', 'value' => 31))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => -33))), + ); + } + + /** + * @dataProvider validate_range_data_too_low + */ + public function test_validate_range_too_low($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_LOW'), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_BIG-error. + */ + public function validate_range_data_too_big() + { + return array( + array(array(array('column_type' => 'BOOL', 'lang' => 'TEST', 'value' => 2))), + + array(array(array('column_type' => 'USINT', 'lang' => 'TEST', 'value' => 65536))), + array(array(array('column_type' => 'USINT:32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'UINT', 'lang' => 'TEST', 'value' => ((int) 0x7fffffff) + 1))), + array(array(array('column_type' => 'UINT:32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'INT', 'lang' => 'TEST', 'value' => ((int) 0x7fffffff) + 1))), + array(array(array('column_type' => 'INT:-32:-16', 'lang' => 'TEST', 'value' => -15))), + array(array(array('column_type' => 'INT:-32:128', 'lang' => 'TEST', 'value' => 129))), + + array(array(array('column_type' => 'TINT', 'lang' => 'TEST', 'value' => 128))), + array(array(array('column_type' => 'TINT:-32:-16', 'lang' => 'TEST', 'value' => -15))), + array(array(array('column_type' => 'TINT:-32:64', 'lang' => 'TEST', 'value' => 65))), + ); + } + + /** + * @dataProvider validate_range_data_too_big + */ + public function test_validate_range_too_big($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_BIG'), $phpbb_error); + } + + /** + * Data sets that throw the SETTING_TOO_LONG-error. + */ + public function validate_range_data_too_long() + { + return array( + array(array(array('column_type' => 'VCHAR', 'lang' => 'TEST', 'value' => self::return_string(256)))), + array(array(array('column_type' => 'VCHAR:128', 'lang' => 'TEST', 'value' => self::return_string(129)))), + ); + } + + /** + * @dataProvider validate_range_data_too_long + */ + public function test_validate_range_too_long($test_data) + { + global $user; + + $user->lang = new phpbb_mock_lang(); + + $phpbb_error = array(); + validate_range($test_data, $phpbb_error); + + $this->assertEquals(array('SETTING_TOO_LONG'), $phpbb_error); + } +} diff --git a/tests/mock/lang.php b/tests/mock/lang.php new file mode 100644 index 0000000000..17a39629c1 --- /dev/null +++ b/tests/mock/lang.php @@ -0,0 +1,33 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* phpbb_mock_lang +* mock a user with some language-keys specified +*/ +class phpbb_mock_lang implements ArrayAccess +{ + public function offsetExists($offset) + { + return true; + } + + public function offsetGet($offset) + { + return $offset; + } + + public function offsetSet($offset, $value) + { + } + + public function offsetUnset($offset) + { + } +} |