/unit-test/bpm-testcase/test_system_config.php
PHP | 248 lines | 118 code | 87 blank | 43 comment | 11 complexity | 19605dfa20206c3b5479d5f8428fdbae MD5 | raw file
Possible License(s): AGPL-1.0, Apache-2.0, GPL-2.0, LGPL-2.1
- <?php
- /**
- * BP-MEDIA UNIT TEST SCRIPT - CONFIG CLASS
- * Exercises all functions of the configuration class, including the cache.
- *
- * @version 0.1.9
- * @since 0.1.9
- * @package BP-Media
- * @subpackage Unit Test
- * @license GPL v2.0
- * @link http://code.google.com/p/buddypress-media/
- *
- * ========================================================================================================
- */
- // TODO: Add a routine that loads the entire DB into the cache, then drops the DB table, then checks
- // that all items can still be accessed (from the cache).
- class core_config extends BPM_testCase {
- var $cls;
- var $check_array = array();
- var $module_id_max = 5;
- var $type_id_max = 5;
- var $branch_id_max = 3;
- var $key_max = 5;
- function setUp() {
- parent::setUp();
- $this->cls = new BPM_albumTypePolicy();
- $this->cls->install();
- }
- function test_ProcessHTML(){
-
- $cls = new BPM_config();
- $original_array = array(); // A simple [$tree][$branch][$key][$val] containing the original
- // values we assigned, that we can easily check against using assert()
- $post_array = array(); // The simulated superglobal $_POST array that the function has to process
- $names_array = array(); // Used to build the comma separated list of var names within the $_POST
- // array that our function needs to process
- $check_array = array(); // A simple [$tree][$branch][$key][$val] containing the updated values
- // that should have been changed by the function
- $tree_max = 7;
- $branch_max = 10;
- $key_max = 25;
- // Fill table with thousands of config options having random sizes
- // and data types. Save a copy of these to a local array.
- // =================================================================
- for( $tree = 0; $tree < $tree_max; $tree++) {
- for( $branch = 0; $branch < $branch_max; $branch++) {
- for ($key = 0; $key < $key_max; $key++) {
- // The loop indicies are prefixed with a letter to ensure they are treated as strings. If we try
- // to use "0" for $tree, the function will reject it as an invalid tree name.
- $tree_str = "T".$tree;
- $branch_str = "B".$branch;
- $key_str = "K".$key;
- // Randomly choose a data type for the test key
- $valid_types = array("bool", "int", "float", "string");
- $current_type = $valid_types[mt_rand(0,3)];
- switch($current_type){
- case "bool" : {
- // Write a value to config class and original array
- $val = mt_rand(0,1);
- $val = (bool)$val;
- $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
- $original_array[$tree_str][$branch_str][$key_str] = $val;
- // Write a new value to the $post_array, $check_array, and $names_array
- $val = mt_rand(0,1);
- $val = (bool)$val;
- $post_array["$tree_str#$branch_str#$key_str"] = $val;
- $check_array[$tree_str][$branch_str][$key_str] = $val;
- $names_array[] = "$tree_str#$branch_str#$key_str";
- } break;
- case "int" : {
- // Write a value to config class and original array
- $val = mt_rand(-2000000,2000000);
- $val = (int)$val;
- $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
- $original_array[$tree_str][$branch_str][$key_str] = $val;
- // Write a new value to the $post_array, $check_array, and $names_array
- $val = mt_rand(-2000000,2000000);
- $val = (int)$val;
- $post_array["$tree_str#$branch_str#$key_str"] = $val;
- $check_array[$tree_str][$branch_str][$key_str] = $val;
- $names_array[] = "$tree_str#$branch_str#$key_str";
- } break;
- case "float" : {
- // Write a value to config class and original array
- $val = (mt_rand(-2000000,2000000) / 17);
- $val = (float)$val;
- $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
- $original_array[$tree_str][$branch_str][$key_str] = $val;
- // Write a new value to the $post_array, $check_array, and $names_array
- $val = (mt_rand(-2000000,120000000) / 17);
- $val = (float)$val;
- $post_array["$tree_str#$branch_str#$key_str"] = $val;
- $check_array[$tree_str][$branch_str][$key_str] = $val;
- $names_array[] = "$tree_str#$branch_str#$key_str";
- } break;
- case "string" : {
- // Note that both strings must have the same width, because the HTML
- // form function cannot change the max field width value.
- $width = mt_rand(0, 87);
- // Write a value to config class and original array
- $val = BPM_sUtil::random_string($width);
- $cls->createKey($tree_str, $branch_str, $key_str, $val, "textAndNumbers", $width);
- $original_array[$tree_str][$branch_str][$key_str] = $val;
- // Write a new value to the $post_array, $check_array, and $names_array
- $val = BPM_sUtil::random_string($width);
- $post_array["$tree_str#$branch_str#$key_str"] = $val;
- $check_array[$tree_str][$branch_str][$key_str] = $val;
- $names_array[] = "$tree_str#$branch_str#$key_str";
- } break;
- } // End of switch($current_type)
- } // End of for ($key
- } // End of for($branch
- } // End of for($tree
- // Load every config option stored to the db and compare it against
- // the value stored in the local $original_array
- // ===================================================================
- for( $tree = 0; $tree < $tree_max; $tree++) {
- for( $branch = 0; $branch < $branch_max; $branch++) {
- for ($key = 0; $key < $key_max; $key++) {
- $tree_str = "T".$tree;
- $branch_str = "B".$branch;
- $key_str = "K".$key;
- $result = $cls->getKeyVal($tree_str, $branch_str, $key_str);
- $this->assertEquals($result, $original_array[$tree_str][$branch_str][$key_str]);
- }
- }
- }
- // Assemble the names array into a comma-separated string of names,
- // add it to the $post_array, and then process the array
- // ====================================================================
- $names_left = sizeof($names_array) - 1;
- foreach($names_array as $name){
- $post_array["key_names"] .= $name;
- if($names_left != 0){
- $post_array["key_names"] .= ", ";
- $names_left--;
- }
- }
- $cls->processHTMLForm($post_array);
- // Load every config option stored to the db and compare it against
- // the updated values stored in the local $check_array
- // ===================================================================
- for( $tree = 0; $tree < $tree_max; $tree++) {
- for( $branch = 0; $branch < $branch_max; $branch++) {
- for ($key = 0; $key < $key_max; $key++) {
- $tree_str = "T".$tree;
- $branch_str = "B".$branch;
- $key_str = "K".$key;
- $result = $cls->getKeyVal($tree_str, $branch_str, $key_str);
- $this->assertEquals($result, $check_array[$tree_str][$branch_str][$key_str], $message=null, $delta=0.001);
- }
- }
- }
- $cls->truncate();
-
- }
-
- function tearDown() {
- $class = new BPM_config();
- $class->uninstall();
- parent::tearDown();
- }
-
- }
- ?>