PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/unit-test/bpm-testcase/test_system_config.php

http://buddypress-media.googlecode.com/
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
  1. <?php
  2. /**
  3. * BP-MEDIA UNIT TEST SCRIPT - CONFIG CLASS
  4. * Exercises all functions of the configuration class, including the cache.
  5. *
  6. * @version 0.1.9
  7. * @since 0.1.9
  8. * @package BP-Media
  9. * @subpackage Unit Test
  10. * @license GPL v2.0
  11. * @link http://code.google.com/p/buddypress-media/
  12. *
  13. * ========================================================================================================
  14. */
  15. // TODO: Add a routine that loads the entire DB into the cache, then drops the DB table, then checks
  16. // that all items can still be accessed (from the cache).
  17. class core_config extends BPM_testCase {
  18. var $cls;
  19. var $check_array = array();
  20. var $module_id_max = 5;
  21. var $type_id_max = 5;
  22. var $branch_id_max = 3;
  23. var $key_max = 5;
  24. function setUp() {
  25. parent::setUp();
  26. $this->cls = new BPM_albumTypePolicy();
  27. $this->cls->install();
  28. }
  29. function test_ProcessHTML(){
  30. $cls = new BPM_config();
  31. $original_array = array(); // A simple [$tree][$branch][$key][$val] containing the original
  32. // values we assigned, that we can easily check against using assert()
  33. $post_array = array(); // The simulated superglobal $_POST array that the function has to process
  34. $names_array = array(); // Used to build the comma separated list of var names within the $_POST
  35. // array that our function needs to process
  36. $check_array = array(); // A simple [$tree][$branch][$key][$val] containing the updated values
  37. // that should have been changed by the function
  38. $tree_max = 7;
  39. $branch_max = 10;
  40. $key_max = 25;
  41. // Fill table with thousands of config options having random sizes
  42. // and data types. Save a copy of these to a local array.
  43. // =================================================================
  44. for( $tree = 0; $tree < $tree_max; $tree++) {
  45. for( $branch = 0; $branch < $branch_max; $branch++) {
  46. for ($key = 0; $key < $key_max; $key++) {
  47. // The loop indicies are prefixed with a letter to ensure they are treated as strings. If we try
  48. // to use "0" for $tree, the function will reject it as an invalid tree name.
  49. $tree_str = "T".$tree;
  50. $branch_str = "B".$branch;
  51. $key_str = "K".$key;
  52. // Randomly choose a data type for the test key
  53. $valid_types = array("bool", "int", "float", "string");
  54. $current_type = $valid_types[mt_rand(0,3)];
  55. switch($current_type){
  56. case "bool" : {
  57. // Write a value to config class and original array
  58. $val = mt_rand(0,1);
  59. $val = (bool)$val;
  60. $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
  61. $original_array[$tree_str][$branch_str][$key_str] = $val;
  62. // Write a new value to the $post_array, $check_array, and $names_array
  63. $val = mt_rand(0,1);
  64. $val = (bool)$val;
  65. $post_array["$tree_str#$branch_str#$key_str"] = $val;
  66. $check_array[$tree_str][$branch_str][$key_str] = $val;
  67. $names_array[] = "$tree_str#$branch_str#$key_str";
  68. } break;
  69. case "int" : {
  70. // Write a value to config class and original array
  71. $val = mt_rand(-2000000,2000000);
  72. $val = (int)$val;
  73. $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
  74. $original_array[$tree_str][$branch_str][$key_str] = $val;
  75. // Write a new value to the $post_array, $check_array, and $names_array
  76. $val = mt_rand(-2000000,2000000);
  77. $val = (int)$val;
  78. $post_array["$tree_str#$branch_str#$key_str"] = $val;
  79. $check_array[$tree_str][$branch_str][$key_str] = $val;
  80. $names_array[] = "$tree_str#$branch_str#$key_str";
  81. } break;
  82. case "float" : {
  83. // Write a value to config class and original array
  84. $val = (mt_rand(-2000000,2000000) / 17);
  85. $val = (float)$val;
  86. $cls->createKey($tree_str, $branch_str, $key_str, $val, $current_type, $ctrl=null);
  87. $original_array[$tree_str][$branch_str][$key_str] = $val;
  88. // Write a new value to the $post_array, $check_array, and $names_array
  89. $val = (mt_rand(-2000000,120000000) / 17);
  90. $val = (float)$val;
  91. $post_array["$tree_str#$branch_str#$key_str"] = $val;
  92. $check_array[$tree_str][$branch_str][$key_str] = $val;
  93. $names_array[] = "$tree_str#$branch_str#$key_str";
  94. } break;
  95. case "string" : {
  96. // Note that both strings must have the same width, because the HTML
  97. // form function cannot change the max field width value.
  98. $width = mt_rand(0, 87);
  99. // Write a value to config class and original array
  100. $val = BPM_sUtil::random_string($width);
  101. $cls->createKey($tree_str, $branch_str, $key_str, $val, "textAndNumbers", $width);
  102. $original_array[$tree_str][$branch_str][$key_str] = $val;
  103. // Write a new value to the $post_array, $check_array, and $names_array
  104. $val = BPM_sUtil::random_string($width);
  105. $post_array["$tree_str#$branch_str#$key_str"] = $val;
  106. $check_array[$tree_str][$branch_str][$key_str] = $val;
  107. $names_array[] = "$tree_str#$branch_str#$key_str";
  108. } break;
  109. } // End of switch($current_type)
  110. } // End of for ($key
  111. } // End of for($branch
  112. } // End of for($tree
  113. // Load every config option stored to the db and compare it against
  114. // the value stored in the local $original_array
  115. // ===================================================================
  116. for( $tree = 0; $tree < $tree_max; $tree++) {
  117. for( $branch = 0; $branch < $branch_max; $branch++) {
  118. for ($key = 0; $key < $key_max; $key++) {
  119. $tree_str = "T".$tree;
  120. $branch_str = "B".$branch;
  121. $key_str = "K".$key;
  122. $result = $cls->getKeyVal($tree_str, $branch_str, $key_str);
  123. $this->assertEquals($result, $original_array[$tree_str][$branch_str][$key_str]);
  124. }
  125. }
  126. }
  127. // Assemble the names array into a comma-separated string of names,
  128. // add it to the $post_array, and then process the array
  129. // ====================================================================
  130. $names_left = sizeof($names_array) - 1;
  131. foreach($names_array as $name){
  132. $post_array["key_names"] .= $name;
  133. if($names_left != 0){
  134. $post_array["key_names"] .= ", ";
  135. $names_left--;
  136. }
  137. }
  138. $cls->processHTMLForm($post_array);
  139. // Load every config option stored to the db and compare it against
  140. // the updated values stored in the local $check_array
  141. // ===================================================================
  142. for( $tree = 0; $tree < $tree_max; $tree++) {
  143. for( $branch = 0; $branch < $branch_max; $branch++) {
  144. for ($key = 0; $key < $key_max; $key++) {
  145. $tree_str = "T".$tree;
  146. $branch_str = "B".$branch;
  147. $key_str = "K".$key;
  148. $result = $cls->getKeyVal($tree_str, $branch_str, $key_str);
  149. $this->assertEquals($result, $check_array[$tree_str][$branch_str][$key_str], $message=null, $delta=0.001);
  150. }
  151. }
  152. }
  153. $cls->truncate();
  154. }
  155. function tearDown() {
  156. $class = new BPM_config();
  157. $class->uninstall();
  158. parent::tearDown();
  159. }
  160. }
  161. ?>