diff options
-rw-r--r-- | phpBB/common.php | 4 | ||||
-rw-r--r-- | phpBB/download/file.php | 5 | ||||
-rw-r--r-- | phpBB/includes/cache/service.php | 45 | ||||
-rw-r--r-- | phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php | 9 | ||||
-rw-r--r-- | phpBB/includes/config/config.php | 157 | ||||
-rw-r--r-- | phpBB/includes/config/db.php | 175 | ||||
-rw-r--r-- | phpBB/includes/db/dbal.php | 22 | ||||
-rw-r--r-- | phpBB/includes/db/firebird.php | 16 | ||||
-rw-r--r-- | phpBB/includes/db/postgres.php | 16 | ||||
-rw-r--r-- | phpBB/includes/functions.php | 72 | ||||
-rw-r--r-- | phpBB/install/database_update.php | 14 | ||||
-rw-r--r-- | phpBB/install/index.php | 4 | ||||
-rw-r--r-- | phpBB/install/install_convert.php | 34 | ||||
-rw-r--r-- | phpBB/install/install_install.php | 13 | ||||
-rw-r--r-- | phpBB/install/install_update.php | 13 | ||||
-rw-r--r-- | phpBB/style.php | 5 | ||||
-rw-r--r-- | tests/config/config_test.php | 112 | ||||
-rw-r--r-- | tests/config/db_test.php | 117 | ||||
-rw-r--r-- | tests/config/fixtures/config.xml | 18 | ||||
-rw-r--r-- | tests/mock/cache.php | 6 |
20 files changed, 703 insertions, 154 deletions
diff --git a/phpBB/common.php b/phpBB/common.php index 7b6a407c94..0116516213 100644 --- a/phpBB/common.php +++ b/phpBB/common.php @@ -227,7 +227,9 @@ $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, defined(' unset($dbpasswd); // Grab global variables, re-cache if necessary -$config = $cache->obtain_config(); +$config = new phpbb_config_db($db, $cache_factory->get_driver()); +set_config(null, null, null, $config); +set_config_count(null, null, null, $config); // Add own hook handler require($phpbb_root_path . 'includes/hooks/index.' . $phpEx); diff --git a/phpBB/download/file.php b/phpBB/download/file.php index a7e8b9f06c..985a24e572 100644 --- a/phpBB/download/file.php +++ b/phpBB/download/file.php @@ -70,7 +70,10 @@ if (isset($_GET['avatar'])) // worst-case default $browser = (!empty($_SERVER['HTTP_USER_AGENT'])) ? htmlspecialchars((string) $_SERVER['HTTP_USER_AGENT']) : 'msie 6.0'; - $config = $cache->obtain_config(); + $config = new phpbb_config_db($db, $cache_factory->get_driver()); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + $filename = $_GET['avatar']; $avatar_group = false; $exit = false; diff --git a/phpBB/includes/cache/service.php b/phpBB/includes/cache/service.php index 424606bfc8..bcbb0ce872 100644 --- a/phpBB/includes/cache/service.php +++ b/phpBB/includes/cache/service.php @@ -40,51 +40,6 @@ class phpbb_cache_service } /** - * Get config values - */ - function obtain_config() - { - global $db; - - if (($config = $this->acm->get('config')) !== false) - { - $sql = 'SELECT config_name, config_value - FROM ' . CONFIG_TABLE . ' - WHERE is_dynamic = 1'; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); - } - else - { - $config = $cached_config = array(); - - $sql = 'SELECT config_name, config_value, is_dynamic - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - if (!$row['is_dynamic']) - { - $cached_config[$row['config_name']] = $row['config_value']; - } - - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); - - $this->acm->put('config', $cached_config); - } - - return $config; - } - - /** * Obtain list of naughty words and build preg style replacement arrays for use by the * calling script */ diff --git a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php index e9cc1f57d2..1c82065a90 100644 --- a/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php +++ b/phpBB/includes/captcha/plugins/phpbb_captcha_gd_plugin.php @@ -152,9 +152,16 @@ class phpbb_captcha_gd extends phpbb_default_captcha global $config; $config_old = $config; + + $config = new phpbb_config(array()); + foreach ($old_config as $key => $value) + { + $config->set($key, $value); + } + foreach ($this->captcha_vars as $captcha_var => $template_var) { - $config[$captcha_var] = request_var($captcha_var, (int) $config[$captcha_var]); + $config->set($captcha_var, request_var($captcha_var, (int) $config[$captcha_var])); } parent::execute_demo(); $config = $config_old; diff --git a/phpBB/includes/config/config.php b/phpBB/includes/config/config.php new file mode 100644 index 0000000000..64fef28cfa --- /dev/null +++ b/phpBB/includes/config/config.php @@ -0,0 +1,157 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Configuration container class +* @package phpBB3 +*/ +class phpbb_config implements ArrayAccess, IteratorAggregate, Countable +{ + /** + * The configuration data + * @var array(string => string) + */ + protected $config; + + /** + * Creates a configuration container with a default set of values + * + * @param array(string => string) $config The configuration data. + */ + public function __construct(array $config) + { + $this->config = $config; + } + + /** + * Retrieves an ArrayIterator over the configuration values. + * + * @return ArrayIterator An iterator over all config data + */ + public function getIterator() + { + return new ArrayIterator($this->config); + } + + /** + * Checks if the specified config value exists. + * + * @param string $key The configuration option's name. + * @return bool Whether the configuration option exists. + */ + public function offsetExists($key) + { + return isset($this->config[$key]); + } + + /** + * Retrieves a configuration value. + * + * @param string $key The configuration option's name. + * @return string The configuration value + */ + public function offsetGet($key) + { + return (isset($this->config[$key])) ? $this->config[$key] : ''; + } + + /** + * Temporarily overwrites the value of a configuration variable. + * + * The configuration change will not persist. It will be lost + * after the request. + * + * @param string $key The configuration option's name. + * @param string $value The temporary value. + */ + public function offsetSet($key, $value) + { + $this->config[$key] = $value; + } + + /** + * Called when deleting a configuration value directly, triggers an error. + * + * @param string $key The configuration option's name. + */ + public function offsetUnset($key) + { + trigger_error('Config values have to be deleted explicitly with the phpbb_config::delete($key) method.', E_USER_ERROR); + } + + /** + * Retrieves the number of configuration options currently set. + * + * @return int Number of config options + */ + public function count() + { + return count($this->config); + } + + /** + * Sets a configuration option's value + * + * @param string $key The configuration option's name + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + public function set($key, $value, $cache = true) + { + $this->config[$key] = $value; + } + + /** + * Sets a configuration option's value only if the old_value matches the + * current configuration value or the configuration value does not exist yet. + * + * @param string $key The configuration option's name + * @param string $old_value Current configuration value + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + * @return bool True if the value was changed, false otherwise. + */ + public function set_atomic($key, $old_value, $value, $cache = true) + { + if (!isset($this->config[$key]) || $this->config[$key] == $old_value) + { + $this->config[$key] = $value; + return true; + } + return false; + } + + /** + * Increments an integer configuration value. + * + * @param string $key The configuration option's name + * @param int $increment Amount to increment by + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + function increment($key, $increment, $cache = true) + { + if (!isset($this->config[$key])) + { + $this->config[$key] = 0; + } + + $this->config[$key] += $increment; + } +} diff --git a/phpBB/includes/config/db.php b/phpBB/includes/config/db.php new file mode 100644 index 0000000000..e6b7bffb73 --- /dev/null +++ b/phpBB/includes/config/db.php @@ -0,0 +1,175 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +/** +* @ignore +*/ +if (!defined('IN_PHPBB')) +{ + exit; +} + +/** +* Configuration container class +* @package phpBB3 +*/ +class phpbb_config_db extends phpbb_config +{ + /** + * Cache instance + * @var phpbb_cache_driver_interface + */ + protected $cache; + + /** + * Database connection + * @var dbal + */ + protected $db; + + /** + * Creates a configuration container with a default set of values + * + * @param phpbb_cache_driver_interface $cache Cache instance + * @param dbal $db Database connection + * @param string $table Configuration table name + */ + public function __construct(phpbb_cache_driver_interface $cache, dbal $db, $table) + { + $this->db = $db; + $this->cache = $cache; + $this->table = $table; + + if (($config = $cache->get('config')) !== false) + { + $sql = 'SELECT config_name, config_value + FROM ' . $this->table . ' + WHERE is_dynamic = 1'; + $result = $this->db->sql_query($sql); + $config += $this->db->sql_fetchrowset($result); + $this->db->sql_freeresult($result); + } + else + { + $config = $cached_config = array(); + + $sql = 'SELECT config_name, config_value, is_dynamic + FROM ' . $this->table; + $result = $this->db->sql_query($sql); + + while ($row = $this->db->sql_fetchrow($result)) + { + if (!$row['is_dynamic']) + { + $cached_config[$row['config_name']] = $row['config_value']; + } + + $config[$row['config_name']] = $row['config_value']; + } + $this->db->sql_freeresult($result); + + $cache->put('config', $cached_config); + } + + parent::__construct($config); + } + + /** + * Sets a configuration option's value + * + * @param string $key The configuration option's name + * @param string $value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + public function set($key, $value, $cache = true) + { + $this->set_atomic($key, false, $value, $cache); + } + + /** + * Sets a configuration option's value only if the old_value matches the + * current configuration value or the configuration value does not exist yet. + * + * @param string $key The configuration option's name + * @param mixed $old_value Current configuration value or false to ignore + * the old value + * @param string $new_value New configuration value + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached + * @return bool True if the value was changed, false otherwise + */ + public function set_atomic($key, $old_value, $new_value, $cache = true) + { + $sql = 'UPDATE ' . $this->table . " + SET config_value = '" . $this->db->sql_escape($new_value) . "' + WHERE config_name = '" . $this->db->sql_escape($key) . "'"; + + if ($old_value !== false) + { + $sql .= " AND config_value = '" . $this->db->sql_escape($old_value) . "'"; + } + + $result = $this->db->sql_query($sql); + + if (!$this->db->sql_affectedrows($result) && isset($this->config[$key])) + { + return false; + } + + if (!isset($this->config[$key])) + { + $sql = 'INSERT INTO ' . $this->table . ' ' . $this->db->sql_build_array('INSERT', array( + 'config_name' => $key, + 'config_value' => $new_value, + 'is_dynamic' => ($cache) ? 0 : 1)); + $this->db->sql_query($sql); + } + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] = $new_value; + return true; + } + + /** + * Increments an integer config value directly in the database. + * + * Using this method instead of setting the new value directly avoids race + * conditions and unlike set_atomic it cannot fail. + * + * @param string $key The configuration option's name + * @param int $increment Amount to increment by + * @param bool $cache Whether this variable should be cached or if it + * changes too frequently to be efficiently cached. + */ + function increment($key, $increment, $cache = true) + { + if (!isset($this->config[$key])) + { + $this->set($key, '0', $cache); + } + + $sql_update = $this->db->cast_expr_to_string($this->db->cast_expr_to_bigint('config_value') . ' + ' . (int) $increment); + + $this->db->sql_query('UPDATE ' . $this->table . ' + SET config_value = ' . $sql_update . " + WHERE config_name = '" . $this->db->sql_escape($key) . "'"); + + if ($cache) + { + $this->cache->destroy('config'); + } + + $this->config[$key] += $increment; + } +} diff --git a/phpBB/includes/db/dbal.php b/phpBB/includes/db/dbal.php index fd0464814f..cfebb61440 100644 --- a/phpBB/includes/db/dbal.php +++ b/phpBB/includes/db/dbal.php @@ -448,6 +448,28 @@ class dbal } /** + * Returns SQL string to cast a string expression to an int. + * + * @param string $expression An expression evaluating to string + * @return string Expression returning an int + */ + function cast_expr_to_bigint($expression) + { + return $expression; + } + + /** + * Returns SQL string to cast an integer expression to a string. + * + * @param string $expression An expression evaluating to int + * @return string Expression returning a string + */ + function cast_expr_to_string($expression) + { + return $expression; + } + + /** * Run more than one insert statement. * * @param string $table table name to run the statements on diff --git a/phpBB/includes/db/firebird.php b/phpBB/includes/db/firebird.php index 68069ab6cf..69476f79f8 100644 --- a/phpBB/includes/db/firebird.php +++ b/phpBB/includes/db/firebird.php @@ -466,6 +466,22 @@ class dbal_firebird extends dbal } /** + * @inheritdoc + */ + function cast_expr_to_bigint($expression) + { + return 'CAST(' . $expression . ' as DECIMAL(255, 0))'; + } + + /** + * @inheritdoc + */ + function cast_expr_to_string($expression) + { + return 'CAST(' . $expression . ' as VARCHAR(255))'; + } + + /** * return sql error array * @access private */ diff --git a/phpBB/includes/db/postgres.php b/phpBB/includes/db/postgres.php index eb297b8db5..424ce12245 100644 --- a/phpBB/includes/db/postgres.php +++ b/phpBB/includes/db/postgres.php @@ -366,6 +366,22 @@ class dbal_postgres extends dbal } /** + * @inheritdoc + */ + function cast_expr_to_bigint($expression) + { + return 'CAST(' . $expression . ' as DECIMAL(255, 0))'; + } + + /** + * @inheritdoc + */ + function cast_expr_to_string($expression) + { + return 'CAST(' . $expression . ' as VARCHAR(255))'; + } + + /** * return sql error array * @access private */ diff --git a/phpBB/includes/functions.php b/phpBB/includes/functions.php index c55931a2fb..12e4ebc597 100644 --- a/phpBB/includes/functions.php +++ b/phpBB/includes/functions.php @@ -17,7 +17,11 @@ if (!defined('IN_PHPBB')) } // Common global functions - +/** +* Casts a variable to the given type. +* +* @deprecated +*/ function set_var(&$result, $var, $type, $multibyte = false) { // no need for dependency injection here, if you have the object, call the method yourself! @@ -30,6 +34,7 @@ function set_var(&$result, $var, $type, $multibyte = false) * See {@link phpbb_request_interface::variable phpbb_request_interface::variable} for * documentation of this function's use. * +* @deprecated * @param mixed $var_name The form variable's name from which data shall be retrieved. * If the value is an array this may be an array of indizes which will give * direct access to a value at any depth. E.g. if the value of "var" is array(1 => "a") @@ -78,59 +83,46 @@ function request_var($var_name, $default, $multibyte = false, $cookie = false, p /** * Set config value. Creates missing config entry. +* +* @deprecated */ -function set_config($config_name, $config_value, $is_dynamic = false) +function set_config($config_name, $config_value, $is_dynamic = false, phpbb_config $set_config = null) { - global $db, $cache, $config; - - $sql = 'UPDATE ' . CONFIG_TABLE . " - SET config_value = '" . $db->sql_escape($config_value) . "' - WHERE config_name = '" . $db->sql_escape($config_name) . "'"; - $db->sql_query($sql); + static $config = null; - if (!$db->sql_affectedrows() && !isset($config[$config_name])) + if ($set_config !== null) { - $sql = 'INSERT INTO ' . CONFIG_TABLE . ' ' . $db->sql_build_array('INSERT', array( - 'config_name' => $config_name, - 'config_value' => $config_value, - 'is_dynamic' => ($is_dynamic) ? 1 : 0)); - $db->sql_query($sql); - } - - $config[$config_name] = $config_value; + $config = $set_config; - if (!$is_dynamic) - { - $cache->destroy('config'); + if (empty($config_name)) + { + return; + } } + + $config->set($config_name, $config_value, !$is_dynamic); } /** * Set dynamic config value with arithmetic operation. +* +* @deprecated */ -function set_config_count($config_name, $increment, $is_dynamic = false) +function set_config_count($config_name, $increment, $is_dynamic = false, phpbb_config $set_config = null) { - global $db, $cache; + static $config = null; - switch ($db->sql_layer) + if ($set_config !== null) { - case 'firebird': - case 'postgres': - $sql_update = 'CAST(CAST(config_value as DECIMAL(255, 0)) + ' . (int) $increment . ' as VARCHAR(255))'; - break; + $config = $set_config; - // MySQL, SQlite, mssql, mssql_odbc, oracle - default: - $sql_update = 'config_value + ' . (int) $increment; - break; + if (empty($config_name)) + { + return; + } } - $db->sql_query('UPDATE ' . CONFIG_TABLE . ' SET config_value = ' . $sql_update . " WHERE config_name = '" . $db->sql_escape($config_name) . "'"); - - if (!$is_dynamic) - { - $cache->destroy('config'); - } + $config->increment($config_name, $increment, !$is_dynamic); } /** @@ -3546,7 +3538,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') // but until 5.3.3 it only works for MX records // See: http://bugs.php.net/bug.php?id=51844 - // Call checkdnsrr() if + // Call checkdnsrr() if // we're looking for an MX record or // we're not on Windows or // we're running a PHP version where #51844 has been fixed @@ -3566,7 +3558,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') // dns_get_record() is available since PHP 5; since PHP 5.3 also on Windows, // but on Windows it does not work reliable for AAAA records before PHP 5.3.1 - // Call dns_get_record() if + // Call dns_get_record() if // we're not looking for an AAAA record or // we're not on Windows or // we're running a PHP version where AAAA lookups work reliable @@ -3596,7 +3588,7 @@ function phpbb_checkdnsrr($host, $type = 'MX') foreach ($resultset as $result) { if ( - isset($result['host']) && $result['host'] == $host && + isset($result['host']) && $result['host'] == $host && isset($result['type']) && $result['type'] == $type ) { diff --git a/phpBB/install/database_update.php b/phpBB/install/database_update.php index f73f7472f0..cecd565fa5 100644 --- a/phpBB/install/database_update.php +++ b/phpBB/install/database_update.php @@ -165,17 +165,9 @@ include($phpbb_root_path . 'language/' . $language . '/install.' . $phpEx); $inline_update = (request_var('type', 0)) ? true : false; // To let set_config() calls succeed, we need to make the config array available globally -$config = array(); - -$sql = 'SELECT * - FROM ' . CONFIG_TABLE; -$result = $db->sql_query($sql); - -while ($row = $db->sql_fetchrow($result)) -{ - $config[$row['config_name']] = $row['config_value']; -} -$db->sql_freeresult($result); +$config = new phpbb_config_db($db, $cache_factory->get_driver()); +set_config(null, null, null, $config); +set_config_count(null, null, null, $config); // We do not include DB Tools here, because we can not be sure the file is up-to-date ;) // Instead, this file defines a clean db_tools version (we are also not able to provide a different file, else the database update will not work standalone) diff --git a/phpBB/install/index.php b/phpBB/install/index.php index 653268ba68..18521e27d0 100644 --- a/phpBB/install/index.php +++ b/phpBB/install/index.php @@ -281,9 +281,9 @@ else } // Set some standard variables we want to force -$config = array( +$config = new phpbb_config(array( 'load_tplcompile' => '1' -); +)); $template->set_custom_template('../adm/style', 'admin'); $template->assign_var('T_TEMPLATE_PATH', '../adm/style'); diff --git a/phpBB/install/install_convert.php b/phpBB/install/install_convert.php index 31c675c201..7aae5bf28c 100644 --- a/phpBB/install/install_convert.php +++ b/phpBB/install/install_convert.php @@ -130,16 +130,7 @@ class install_convert extends module unset($dbpasswd); // We need to fill the config to let internal functions correctly work - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Detect if there is already a conversion in progress at this point and offer to resume // It's quite possible that the user will get disconnected during a large conversion so they need to be able to resume it @@ -350,16 +341,7 @@ class install_convert extends module $this->page_title = $lang['STAGE_SETTINGS']; // We need to fill the config to let internal functions correctly work - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); $convertor_tag = request_var('tag', ''); @@ -597,16 +579,8 @@ class install_convert extends module $db->sql_connect($dbhost, $dbuser, $dbpasswd, $dbname, $dbport, false, true); unset($dbpasswd); - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Override a couple of config variables for the duration $config['max_quote_depth'] = 0; diff --git a/phpBB/install/install_install.php b/phpBB/install/install_install.php index 8143ea7737..e47c8b8c01 100644 --- a/phpBB/install/install_install.php +++ b/phpBB/install/install_install.php @@ -1460,17 +1460,8 @@ class install_install extends module include_once($phpbb_root_path . 'includes/constants.' . $phpEx); include_once($phpbb_root_path . 'includes/search/fulltext_native.' . $phpEx); - // Fill the config array - it is needed by those functions we call - $sql = 'SELECT * - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - $config = array(); - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); $error = false; $search = new fulltext_native($error); diff --git a/phpBB/install/install_update.php b/phpBB/install/install_update.php index 2e78803b74..7c91b112a2 100644 --- a/phpBB/install/install_update.php +++ b/phpBB/install/install_update.php @@ -101,17 +101,8 @@ class install_update extends module // We do not need this any longer, unset for safety purposes unset($dbpasswd); - $config = array(); - - $sql = 'SELECT config_name, config_value - FROM ' . CONFIG_TABLE; - $result = $db->sql_query($sql); - - while ($row = $db->sql_fetchrow($result)) - { - $config[$row['config_name']] = $row['config_value']; - } - $db->sql_freeresult($result); + // We need to fill the config to let internal functions correctly work + $config = new phpbb_config_db($db, new phpbb_cache_driver_null); // Force template recompile $config['load_tplcompile'] = 1; diff --git a/phpBB/style.php b/phpBB/style.php index cff91a2312..09340992c5 100644 --- a/phpBB/style.php +++ b/phpBB/style.php @@ -82,7 +82,10 @@ if ($id) } unset($dbpasswd); - $config = $cache->obtain_config(); + $config = new phpbb_config_db($db, $cache_factory->get_driver()); + set_config(null, null, null, $config); + set_config_count(null, null, null, $config); + $user = false; // try to get a session ID from REQUEST array diff --git a/tests/config/config_test.php b/tests/config/config_test.php new file mode 100644 index 0000000000..73a365c847 --- /dev/null +++ b/tests/config/config_test.php @@ -0,0 +1,112 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +class phpbb_config_test extends phpbb_test_case +{ + public function test_offset_exists() + { + $config = new phpbb_config(array('foo' => 'bar')); + + $this->assertTrue(isset($config['foo'])); + $this->assertFalse(isset($config['foobar'])); + } + + public function test_offset_get() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_offset_get_missing() + { + $config = new phpbb_config(array()); + $this->assertEquals('', $config['foo']); + } + + public function test_offset_set() + { + $config = new phpbb_config(array()); + $config['foo'] = 'x'; + $this->assertEquals('x', $config['foo']); + } + + public function test_offset_unset_fails() + { + $this->setExpectedTriggerError(E_USER_ERROR); + $config = new phpbb_config(array('foo' => 'x')); + unset($config['foo']); + } + + public function test_count() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertEquals(1, count($config)); + } + + public function test_iterate() + { + $vars = array('foo' => '23', 'bar' => '42'); + $config = new phpbb_config($vars); + + $count = 0; + foreach ($config as $key => $value) + { + $this->assertTrue(isset($vars[$key])); + $this->assertEquals($vars[$key], $value); + + $count++; + } + + $this->assertEquals(count($vars), $count); + } + + public function test_set_overwrite() + { + $config = new phpbb_config(array('foo' => 'x')); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_new() + { + $config = new phpbb_config(array()); + $config->set('foo', 'bar'); + $this->assertEquals('bar', $config['foo']); + } + + public function test_set_atomic_overwrite() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertTrue($config->set_atomic('foo', 'bar', '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_new() + { + $config = new phpbb_config(array()); + $this->assertTrue($config->set_atomic('foo', false, '23')); + $this->assertEquals('23', $config['foo']); + } + + public function test_set_atomic_failure() + { + $config = new phpbb_config(array('foo' => 'bar')); + $this->assertFalse($config->set_atomic('foo', 'wrong', '23')); + $this->assertEquals('bar', $config['foo']); + } + + public function test_increment() + { + $config = new phpbb_config(array('foo' => '23')); + $config->increment('foo', 3); + $this->assertEquals(26, $config['foo']); + $config->increment('foo', 1); + $this->assertEquals(27, $config['foo']); + } +} diff --git a/tests/config/db_test.php b/tests/config/db_test.php new file mode 100644 index 0000000000..04277e869b --- /dev/null +++ b/tests/config/db_test.php @@ -0,0 +1,117 @@ +<?php +/** +* +* @package testing +* @copyright (c) 2010 phpBB Group +* @license http://opensource.org/licenses/gpl-license.php GNU Public License +* +*/ + +require_once __DIR__ . '/../mock/cache.php'; + +class phpbb_config_db_test extends phpbb_database_test_case +{ + private $cache; + private $db; + private $config; + + public function getDataSet() + { + return $this->createXMLDataSet(__DIR__ . '/fixtures/config.xml'); + } + + public function setUp() + { + parent::setUp(); + + $this->cache = new phpbb_mock_cache; + $this->db = $this->new_dbal(); + $this->config = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + } + + public function test_load_config() + { + $this->assertEquals('23', $this->config['foo']); + $this->assertEquals('42', $this->config['bar']); + } + + public function test_offset_set() + { + $this->config['foo'] = 'x'; // temporary set + $this->assertEquals('x', $this->config['foo']); + + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->assertEquals('23', $config2['foo']); + } + + public function test_set_overwrite() + { + $this->config->set('foo', '17'); + $this->assertEquals('17', $this->config['foo']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '17')); + } + + public function test_set_overwrite_uncached() + { + $this->config->set('bar', '17', false); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_new() + { + $this->config->set('foobar', '5'); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23', 'foobar' => '5')); + } + + public function test_set_new_uncached() + { + $this->config->set('foobar', '5', false); + $this->assertEquals('5', $this->config['foobar']); + + // re-read config and populate cache + $config2 = new phpbb_config_db($this->cache, $this->db, 'phpbb_config'); + $this->cache->checkVar($this, 'config', array('foo' => '23')); + } + + public function test_set_atomic_overwrite() + { + $this->assertTrue($this->config->set_atomic('foo', '23', '17')); + $this->assertEquals('17', $this->config['foo']); + } + + public function test_set_atomic_new() + { + $this->assertTrue($this->config->set_atomic('foobar', false, '5')); + $this->assertEquals('5', $this->config['foobar']); + } + + public function test_set_atomic_failure() + { + $this->assertFalse($this->config->set_atomic('foo', 'wrong', '17')); + $this->assertEquals('23', $this->config['foo']); + } + + public function test_increment() + { + $this->config->increment('foo', 3); + $this->assertEquals(26, $this->config['foo']); + $this->config->increment('foo', 1); + $this->assertEquals(27, $this->config['foo']); + } + + public function test_increment_new() + { + $this->config->increment('foobar', 3); + $this->assertEquals(3, $this->config['foobar']);; + } +} diff --git a/tests/config/fixtures/config.xml b/tests/config/fixtures/config.xml new file mode 100644 index 0000000000..9d395b685c --- /dev/null +++ b/tests/config/fixtures/config.xml @@ -0,0 +1,18 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<dataset> + <table name="phpbb_config"> + <column>config_name</column> + <column>config_value</column> + <column>is_dynamic</column> + <row> + <value>foo</value> + <value>23</value> + <value>0</value> + </row> + <row> + <value>bar</value> + <value>42</value> + <value>1</value> + </row> + </table> +</dataset> diff --git a/tests/mock/cache.php b/tests/mock/cache.php index 3bfb31f1be..dd29e0e9e3 100644 --- a/tests/mock/cache.php +++ b/tests/mock/cache.php @@ -37,6 +37,11 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface $test->assertEquals($data, $this->data[$var_name]); } + public function checkVarUnset(PHPUnit_Framework_Assert $test, $var_name) + { + $test->assertFalse(isset($this->data[$var_name])); + } + public function check(PHPUnit_Framework_Assert $test, $data) { $test->assertEquals($data, $this->data); @@ -59,6 +64,7 @@ class phpbb_mock_cache implements phpbb_cache_driver_interface } function destroy($var_name, $table = '') { + unset($this->data[$var_name]); } public function _exists($var_name) { |