Created class for work with settings and test case for it

This commit is contained in:
Dmitriy Simushev 2012-07-16 12:15:11 +00:00
parent ac64783a29
commit 3929f26791
2 changed files with 235 additions and 0 deletions

View File

@ -0,0 +1,67 @@
<?php
require_once dirname(__FILE__) . '/../../../../webim/libs/classes/settings.php';
require_once dirname(__FILE__) . '/../../../../webim/libs/config.php';
require_once dirname(__FILE__) . '/../../../../webim/libs/classes/database.php';
/**
* Test class for Settings.
* Generated by PHPUnit on 2012-07-16 at 15:07:10.
*/
class SettingsTest extends PHPUnit_Framework_TestCase {
public static function setUpBeforeClass() {
$db = Database::getInstance();
$db->query(
"INSERT INTO {chatconfig} (vckey, vcvalue) " .
"VALUES (?, ?)",
array('some_test_key', 'some_test_value')
);
}
public static function tearDownAfterClass() {
$db = Database::getInstance();
$db->query(
"DELETE FROM {chatconfig} WHERE vckey = ? OR vckey = ?",
array('some_test_key', 'some_another_test_key')
);
}
public function testGet() {
$this->assertEquals('some_test_value', Settings::get('some_test_key'));
}
public function testSet() {
Settings::set('some_test_key', 'some_another_value');
$this->assertEquals('some_another_value', Settings::get('some_test_key'));
Settings::set('some_test_key', 'some_test_value');
}
public function testUpdate() {
$db = Database::getInstance();
Settings::set('some_test_key', 'some_value_for_update');
Settings::set('some_another_test_key', 'some_another_value_for_update');
Settings::update();
list($count) = $db->query(
"SELECT COUNT(*) FROM {chatconfig} WHERE vckey = ? AND vcvalue = ?",
array('some_test_key', 'some_value_for_update'),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM
)
);
$this->assertEquals(1, $count);
list($count) = $db->query(
"SELECT COUNT(*) FROM {chatconfig} WHERE vckey = ? AND vcvalue = ?",
array('some_another_test_key', 'some_another_value_for_update'),
array(
'return_rows' => Database::RETURN_ONE_ROW,
'fetch_type' => Database::FETCH_NUM
)
);
$this->assertEquals(1, $count);
}
}
?>

View File

@ -0,0 +1,168 @@
<?php
/*
* Copyright 2005-2013 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* Encapsulates work with system settings.
*/
Class Settings {
/**
* An instance of Settings class
* @var Settings
*/
protected static $instance = null;
/**
* Array of settings
* @var array
*/
protected $settings = array();
/**
* Array of settings stored in database
* @var array
*/
protected $settingsInDb = array();
/**
* Returns an instance of Settings class
* @return Settings
*/
protected static function getInstance(){
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Settings class constructor. Set default values and load setting from database.
* @global $home_locale Specifies home locale. Defined in libs/config.php
*/
protected function __construct() {
global $home_locale;
// Set default values
$this->settings = array(
'dbversion' => 0,
'featuresversion' => 0,
'title' => 'Your Company',
'hosturl' => 'http://mibew.org',
'logo' => '',
'usernamepattern' => '{name}',
'chatstyle' => 'default',
'chattitle' => 'Live Support',
'geolink' => 'http://api.hostip.info/get_html.php?ip={ip}',
'geolinkparams' => 'width=440,height=100,toolbar=0,scrollbars=0,location=0,status=1,menubar=0,resizable=1',
'max_uploaded_file_size' => 100000,
'max_connections_from_one_host' => 10,
'thread_lifetime' => 600,
'email' => '', /* inbox for left messages */
'left_messages_locale' => $home_locale,
'sendmessagekey' => 'center',
'enableban' => '0',
'enablessl' => '0',
'forcessl' => '0',
'usercanchangename' => '1',
'enablegroups' => '0',
'enablegroupsisolation' => '0',
'enablestatistics' => '1',
'enabletracking' => '0',
'enablepresurvey' => '1',
'surveyaskmail' => '0',
'surveyaskgroup' => '1',
'surveyaskmessage' => '0',
'enablepopupnotification' => '0',
'showonlineoperators' => '0',
'enablecaptcha' => '0',
'online_timeout' => 30, /* Timeout (in seconds) when online operator becomes offline */
'updatefrequency_operator' => 2,
'updatefrequency_chat' => 2,
'updatefrequency_oldchat' => 7,
'updatefrequency_tracking' => 10,
'visitors_limit' => 20, /* Number of visitors to look over */
'invitation_lifetime' => 60, /* Lifetime for invitation to chat */
'tracking_lifetime' => 600, /* Time to store tracked old visitors' data */
);
// Load values from database
$db = Database::getInstance();
$rows = $db->query(
"select vckey,vcvalue from {chatconfig}",
NULL,
array('return_rows' => Database::RETURN_ALL_ROWS)
);
foreach ($rows as $row) {
$name = $row['vckey'];
$this->settings[$name] = $row['vcvalue'];
$this->settingsInDb[$name] = true;
}
}
/**
* Get setting value.
*
* @param string $name Variable's name
* @return mixed
*/
public static function get($name) {
$instance = self::getInstance();
return $instance->settings[$name];
}
/**
* Set setting value.
*
* @param string $name Variables's name
* @param mixed $value Variable's value
*/
public static function set($name, $value) {
$instance = self::getInstance();
$instance->settings[$name] = $value;
}
/**
* Updates settings in database.
*/
public static function update() {
$instance = self::getInstance();
$db = Database::getInstance();
foreach ($instance->settings as $key => $value) {
if (!isset($instance->settingsInDb[$key])) {
$db->query(
"insert into {chatconfig} (vckey) values (?)",
array($key)
);
}
$db->query(
"update {chatconfig} set vcvalue=? where vckey=?",
array($value, $key)
);
}
}
/**
* Implementation of destructor
*/
public function __destruct() {}
}
?>