PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/unit-test/bpm-testcase/test_config.system.php

http://buddypress-media.googlecode.com/
PHP | 904 lines | 397 code | 372 blank | 135 comment | 66 complexity | 054d6faaff6033b882d2744ce81274f0 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. class config_system extends BPM_testCase {
  16. var $cls;
  17. var $check_array = array();
  18. var $delimiter;
  19. var $tree_max = 5;
  20. var $branch_max = 3;
  21. var $key_max = 5;
  22. function setUp() {
  23. parent::setUp();
  24. $this->cls = new BPM_config();
  25. $this->cls->install();
  26. $this->delimiter = $this->cls->key_delimiter;
  27. }
  28. function generateData($type=null){
  29. $valid_types = array("null", "bool", "int", "float", "string", "array", "object");
  30. if($type === null){ // "0" is a valid type
  31. $current_type = $valid_types[mt_rand(0,6)];
  32. }
  33. else {
  34. // Using the modulus operator causes the function to cycle
  35. // through types if $type variable exceeds (int)5.
  36. $treex = $type % 6;
  37. $current_type = $valid_types[$treex];
  38. }
  39. switch($current_type){
  40. case "null" : {
  41. $val = null;
  42. } break;
  43. case "bool" : {
  44. $val = mt_rand(0,1);
  45. $val = (bool)$val;
  46. } break;
  47. case "int" : {
  48. $val = mt_rand(-100,100);
  49. $val = (int)$val;
  50. } break;
  51. case "float" : {
  52. $val = (mt_rand(-100,100) / 17);
  53. $val = (float)$val;
  54. } break;
  55. case "string" : {
  56. $width = mt_rand(0, 87);
  57. $val = BPM_sUtil::random_string($width);
  58. } break;
  59. case "array" : {
  60. $named_keys = mt_rand(0,1);
  61. $val = array();
  62. if(!$named_keys){
  63. for($i=0; $i<5; $i++){
  64. $val[] = mt_rand(-2500, 2500);
  65. }
  66. }
  67. else {
  68. for($i=0; $i<5; $i++){
  69. $width = mt_rand(1, 37);
  70. // PHP does not support arrays with numeric string keys. It silently converts them
  71. // to int keys. For example: $test_array["1234"] = "foo"; $test_array["bar"] = "baz";
  72. // var_dump($test_array) produces [ test_array[(int)1234] = "foo", test_array[(string)bar]
  73. // = "baz"]
  74. //
  75. // BPM_sUtil::random_string() can (and does) produce values like (string)7. To avoid
  76. // debugging problems, prepend all the key names with "k" to ensure PHP treats them as strings.
  77. $key_name = "k" . BPM_sUtil::random_string($width);
  78. $val[$key_name] = mt_rand(-2500, 2500);
  79. }
  80. }
  81. } break;
  82. case "object" : {
  83. $val = new stdClass();
  84. for($i=0; $i<3; $i++){
  85. $width = mt_rand(1, 17);
  86. // We prepend all the key names with "k" because BPM_sUtil::random_string()
  87. // can (and does) produce values like (string)7, which is an illegal name for a
  88. // variable in an object [you can't do $test_object->7 = "foo"; ...because the parser
  89. // doesn't know if you mean (string)7 or (int)7 ]
  90. //
  91. // But, PHP *doesn't catch* this defect when the variable name is created dynamically
  92. // [ $name = "7"; $test_object->{$name} = "foo" ]. Instead, it converts the key's type
  93. // from (string)7 to (int)7 and allows the variable to be accessed only if the name is
  94. // created dynamically [$name = "7"; echo ($test_object->{$name}; ]
  95. $key_name = "k" . BPM_sUtil::random_string($width);
  96. $val->{$key_name} = mt_rand(-2500, 2500);
  97. }
  98. } break;
  99. } // End of switch($current_type)
  100. return $val;
  101. }
  102. function load_table(){
  103. // Fill table with policy options having random sizes
  104. // and data types. Save a copy of these to a local array.
  105. // =================================================================
  106. $total_keys = 0;
  107. for( $tree=1; $tree < $this->tree_max; $tree++) {
  108. for( $branch=1; $branch < $this->branch_max; $branch++) {
  109. for ($key=1; $key < $this->key_max; $key++) {
  110. // Cycle through data types for the first 50 keys, then
  111. // randomly pick data types
  112. if($total_keys < 50){
  113. $val = self::generateData($total_keys);
  114. }
  115. else {
  116. $val = self::generateData();
  117. }
  118. $tree_name = "T" . $tree;
  119. $branch_name = "B" . $branch;
  120. $key_name = "K" . $key;
  121. $filter = "debug";
  122. $ctrl = false;
  123. // Write generated value to policy class and check array
  124. $set_ok = $this->cls->createKey($tree_name, $branch_name, $key_name, $val, $filter, $ctrl);
  125. $this->assertEquals(true, $set_ok);
  126. $this->check_array[$tree_name][$branch_name][$key_name]["val"] = $val;
  127. $this->check_array[$tree_name][$branch_name][$key_name]["filter"] = $filter;
  128. $this->check_array[$tree_name][$branch_name][$key_name]["ctrl"] = $ctrl;
  129. $total_keys++;
  130. } // ENDOF for( $key
  131. } // ENDOF for( $branch
  132. } // ENDOF: for( $tree
  133. }
  134. function check_table(){
  135. // Load every key stored to the table and compare it against
  136. // the value stored in the check array, making sure data types were
  137. // correctly recovered (bool) false doesn't become (int) 0 or "NULL"
  138. // ====================================================================
  139. for( $tree=1; $tree < $this->tree_max; $tree++) {
  140. for( $branch=1; $branch < $this->branch_max; $branch++) {
  141. for ($key=1; $key < $this->key_max; $key++) {
  142. $tree_name = "T" . $tree;
  143. $branch_name = "B" . $branch;
  144. $key_name = "K" . $key;
  145. $expected_val = $this->check_array[$tree_name][$branch_name][$key_name]["val"];
  146. $expected_filter = $this->check_array[$tree_name][$branch_name][$key_name]["filter"];
  147. $expected_ctrl = $this->check_array[$tree_name][$branch_name][$key_name]["ctrl"];
  148. $result = $this->cls->getKey($tree_name, $branch_name, $key_name, &$valid);
  149. if( BPM_sUtil::keyExists($key_name, $this->check_array[$tree_name][$branch_name]) ){
  150. $this->assertEquals(true, $valid);
  151. $result_val = $result["val"];
  152. $result_filter = $result["filter"];
  153. $result_ctrl = $result["ctrl"];
  154. $this->assertEquals($expected_val, $result_val);
  155. $this->assertEquals($expected_filter, $result_filter);
  156. $this->assertEquals($expected_ctrl, $result_ctrl);
  157. }
  158. else {
  159. $this->assertEquals(null, $result);
  160. $this->assertEquals(false, $valid);
  161. }
  162. }
  163. }
  164. }
  165. }
  166. function test_create(){
  167. // Clear the db
  168. $this->cls->truncate();
  169. // Delete all entries in the class cache
  170. $this->cls->flushCache();
  171. // Load the database and check array with test data
  172. self::load_table();
  173. // Verify the stored data matches the check array
  174. self::check_table();
  175. } // End of test_create
  176. function test_update(){
  177. // Clear the db
  178. $this->cls->truncate();
  179. // Delete all entries in the class cache
  180. $this->cls->flushCache();
  181. // Load the database and check array with test data
  182. self::load_table();
  183. // Overwrite half of the items in the table with random new data
  184. // of random type
  185. // =================================================================
  186. $total_keys = 0;
  187. for( $tree=1; $tree < $this->tree_max; $tree++) {
  188. for( $branch=1; $branch < $this->branch_max; $branch++) {
  189. for ($key=1; $key < $this->key_max; $key++) {
  190. $overwrite = mt_rand(0,1);
  191. if($overwrite){
  192. // Cycle through data types for the first 50 keys, then
  193. // randomly pick data types
  194. if($total_keys < 50){
  195. $val = self::generateData($total_keys);
  196. }
  197. else {
  198. $val = self::generateData();
  199. }
  200. $tree_name = "T" . $tree;
  201. $branch_name = "B" . $branch;
  202. $key_name = "K" . $key;
  203. // Write generated value to config class and check array. Note: we cannot check for
  204. // a "true" return value from the function, because in some cases the overwrite function
  205. // tries to write the same value that is currently in a key to a key ...in which case
  206. // the db returns (int)0 because no rows were changed by the query, and so setKey()
  207. // returns (bool)false as a result.
  208. $this->cls->setKey($tree_name, $branch_name, $key_name, $val);
  209. // Update the check array with the new value
  210. $this->check_array[$tree_name][$branch_name][$key_name]["val"] = $val;
  211. $total_keys++;
  212. } // ENDOF if($overwrite)
  213. } // ENDOF for( $key
  214. } // ENDOF for( $branch
  215. } // ENDOF: for( $tree
  216. // Delete all entries in the class cache
  217. $this->cls->flushCache();
  218. // Verify the stored data matches the check array
  219. self::check_table();
  220. } // End of test_update
  221. function test_dropKey_single(){
  222. // Clear the db
  223. $this->cls->truncate();
  224. // Delete all entries in the class cache
  225. $this->cls->flushCache();
  226. // Load the database and check array with test data
  227. self::load_table();
  228. // Drop half of the items in the table, using single string dropKey
  229. for( $tree=1; $tree < $this->tree_max; $tree++) {
  230. for( $branch=1; $branch < $this->branch_max; $branch++) {
  231. for ($key=1; $key < $this->key_max; $key++) {
  232. $key_name = "key" . $key;
  233. $drop = mt_rand(0,1);
  234. if($drop){
  235. // Drop the key
  236. $this->cls->dropKey($tree, $branch, $key_name);
  237. unset($this->check_array[$tree][$branch][$key_name]);
  238. } // ENDOF if($drop)
  239. } // ENDOF for( $key
  240. } // ENDOF for( $branch
  241. } // ENDOF: for( $tree
  242. // Verify the stored data matches the check array
  243. self::check_table();
  244. } // End of test_dropKey_single
  245. function test_dropBranch_single(){
  246. // Clear the db
  247. $this->cls->truncate();
  248. // Delete all entries in the class cache
  249. $this->cls->flushCache();
  250. // Load the database and check array with test data
  251. self::load_table();
  252. // Drop half of the items in the table, using single string dropBranch
  253. for( $tree=1; $tree < $this->tree_max; $tree++) {
  254. for( $branch=1; $branch < $this->branch_max; $branch++) {
  255. $drop = mt_rand(0,1);
  256. if($drop){
  257. // Drop the branch
  258. $this->cls->dropBranch($tree, $branch);
  259. unset($this->check_array[$tree][$branch]);
  260. } // ENDOF if($drop)
  261. } // ENDOF for( $branch
  262. } // ENDOF: for( $tree
  263. // Verify the stored data matches the check array
  264. self::check_table();
  265. } // End of test_dropBranch_single
  266. function test_dropTree_single(){
  267. // Clear the db
  268. $this->cls->truncate();
  269. // Delete all entries in the class cache
  270. $this->cls->flushCache();
  271. // Load the database and check array with test data
  272. self::load_table();
  273. // Drop half of the items in the table, using single string dropType
  274. for( $tree=1; $tree < $this->tree_max; $tree++) {
  275. $drop = mt_rand(0,1);
  276. if($drop){
  277. // Drop the branch
  278. $this->cls->dropTree($tree);
  279. unset($this->check_array[$tree]);
  280. } // ENDOF if($drop)
  281. } // ENDOF: for( $tree
  282. // Verify the stored data matches the check array
  283. self::check_table();
  284. } // End of test_dropType_single
  285. function test_dropKey_array(){
  286. // Clear the db
  287. $this->cls->truncate();
  288. // Delete all entries in the class cache
  289. $this->cls->flushCache();
  290. // Load the database and check array with test data
  291. self::load_table();
  292. // Drop half of the items in the table, using single string dropKey
  293. for( $tree=1; $tree < $this->tree_max; $tree++) {
  294. for( $branch=1; $branch < $this->branch_max; $branch++) {
  295. $drop_array = array();
  296. $drop = true;
  297. for ($key=1; $key < $this->key_max; $key++) {
  298. $key_name = "key" . $key;
  299. if($drop){
  300. $drop_array[] = $key_name;
  301. $drop = false;
  302. }
  303. else {
  304. $drop = true;
  305. }
  306. } // ENDOF for( $key
  307. // Drop the keys
  308. $this->cls->dropKey($tree, $branch, $drop_array);
  309. foreach($drop_array as $drop_key){
  310. unset($this->check_array[$tree][$branch][$drop_key]);
  311. }
  312. } // ENDOF for( $branch
  313. } // ENDOF: for( $tree
  314. // Verify the stored data matches the check array
  315. self::check_table();
  316. } // End of test_dropKey_array
  317. function test_dropBranch_array(){
  318. // Clear the db
  319. $this->cls->truncate();
  320. // Delete all entries in the class cache
  321. $this->cls->flushCache();
  322. // Load the database and check array with test data
  323. self::load_table();
  324. // Drop half of the items in the table, using single string dropKey
  325. for( $tree=1; $tree < $this->tree_max; $tree++) {
  326. $drop_array = array();
  327. $drop = true;
  328. for( $branch=1; $branch < $this->branch_max; $branch++) {
  329. if($drop){
  330. $drop_array[] = $branch;
  331. $drop = false;
  332. }
  333. else {
  334. $drop = true;
  335. }
  336. } // ENDOF for( $branch
  337. // Drop the branches
  338. $this->cls->dropBranch($tree, $drop_array);
  339. foreach($drop_array as $drop_branch){
  340. unset($this->check_array[$tree][$drop_branch]);
  341. }
  342. } // ENDOF: for( $tree
  343. // Verify the stored data matches the check array
  344. self::check_table();
  345. } // End of test_dropBranch_array
  346. function test_dropTree_array(){
  347. // Clear the db
  348. $this->cls->truncate();
  349. // Delete all entries in the class cache
  350. $this->cls->flushCache();
  351. // Load the database and check array with test data
  352. self::load_table();
  353. // Drop half of the items in the table, using single string dropKey
  354. $drop = true;
  355. $drop_array = array();
  356. for( $tree=1; $tree < $this->tree_max; $tree++) {
  357. if($drop){
  358. $drop_array[] = $tree;
  359. $drop = false;
  360. }
  361. else {
  362. $drop = true;
  363. }
  364. } // ENDOF: for( $tree
  365. // Drop the types
  366. $this->cls->dropTree($drop_array);
  367. foreach($drop_array as $drop_type){
  368. unset($this->check_array[$drop_type]);
  369. }
  370. // Verify the stored data matches the check array
  371. self::check_table();
  372. } // End of test_dropType_array
  373. function test_dropSiteKey_single(){
  374. // Clear the db
  375. $this->cls->truncate();
  376. // Delete all entries in the class cache
  377. $this->cls->flushCache();
  378. // Load the database and check array with test data
  379. self::load_table();
  380. for( $branch=1; $branch < $this->branch_max; $branch++) {
  381. for ($key=1; $key < $this->key_max; $key++) {
  382. $key_name = "key" . $key;
  383. $drop = true; //mt_rand(0,1);
  384. if($drop){
  385. // Drop the key from the db
  386. $this->cls->dropSiteKey($branch, $key_name);
  387. // Drop the key from the check array for *all trees*
  388. for( $drop_tree=1; $drop_tree < $this->tree_max; $drop_tree++){
  389. unset($this->check_array[$drop_tree][$branch][$key_name]);
  390. }
  391. } // ENDOF if($drop)
  392. } // ENDOF for( $key
  393. } // ENDOF for( $branch
  394. // Verify the stored data matches the check array
  395. self::check_table();
  396. } // End of test_dropSiteKey_single
  397. function test_dropSiteKey_array(){
  398. // Clear the db
  399. $this->cls->truncate();
  400. // Delete all entries in the class cache
  401. $this->cls->flushCache();
  402. // Load the database and check array with test data
  403. self::load_table();
  404. for( $tree=1; $tree < $this->tree_max; $tree++) {
  405. for( $branch=1; $branch < $this->branch_max; $branch++) {
  406. $drop_array = array();
  407. for ($key=1; $key < $this->key_max; $key++) {
  408. $key_name = "key" . $key;
  409. $drop = mt_rand(0,1);
  410. if($drop){
  411. // Add key to drop array
  412. $drop_array[] = $key_name;
  413. // Drop the key from the check array for *all trees*
  414. for( $tree=1; $tree < $this->tree_max; $tree++){
  415. unset($this->check_array[$tree][$branch][$key_name]);
  416. }
  417. } // ENDOF if($drop)
  418. } // ENDOF for( $key
  419. // Drop the array of keys
  420. $this->cls->dropSiteKey($branch, $drop_array);
  421. } // ENDOF for( $branch
  422. } // ENDOF fpr( $tree
  423. // Verify the stored data matches the check array
  424. self::check_table();
  425. } // End of test_dropSiteKey_array
  426. function test_dropSiteBranch_single(){
  427. // Clear the db
  428. $this->cls->truncate();
  429. // Delete all entries in the class cache
  430. $this->cls->flushCache();
  431. // Load the database and check array with test data
  432. self::load_table();
  433. for( $tree=1; $tree < $this->tree_max; $tree++) {
  434. for( $branch=1; $branch < $this->branch_max; $branch++) {
  435. $drop = mt_rand(0,1);
  436. if($drop){
  437. // Drop the branch from the db
  438. $this->cls->dropSiteBranch($branch);
  439. // Drop the key from the check array for *all trees*
  440. for( $tree=1; $tree < $this->tree_max; $tree++){
  441. unset($this->check_array[$tree][$branch]);
  442. }
  443. } // ENDOF if($drop)
  444. } // ENDOF for( $branch
  445. } // ENDOF for( $tree
  446. // Verify the stored data matches the check array
  447. self::check_table();
  448. } // End of test_dropSiteBranch_single
  449. function test_dropSiteBranch_array(){
  450. // Clear the db
  451. $this->cls->truncate();
  452. // Delete all entries in the class cache
  453. $this->cls->flushCache();
  454. // Load the database and check array with test data
  455. self::load_table();
  456. $drop_array = array();
  457. for( $tree=1; $tree < $this->tree_max; $tree++) {
  458. for( $branch=1; $branch < $this->branch_max; $branch++) {
  459. $drop = mt_rand(0,1);
  460. if($drop){
  461. // Add branch to drop array
  462. $drop_array[] = $branch;
  463. // Drop the key from the check array for *all trees*
  464. for( $tree=1; $tree < $this->tree_max; $tree++){
  465. unset($this->check_array[$tree][$branch]);
  466. }
  467. } // ENDOF if($drop)
  468. } // ENDOF for( $branch
  469. } // ENDOF for( $tree
  470. // Drop the branches from the db
  471. $this->cls->dropSiteBranch($drop_array);
  472. // Verify the stored data matches the check array
  473. self::check_table();
  474. } // End of test_dropSiteBranch_array
  475. function test_ProcessHTMLForm(){
  476. // Clear the db
  477. $this->cls->truncate();
  478. // Delete all entries in the class cache
  479. $this->cls->flushCache();
  480. // Load the database and check array with test data. This is necessary because we
  481. // want to use the setKey() function for form data, not createKey()
  482. self::load_table();
  483. // Verify the stored data matches the check array
  484. self::check_table();
  485. $post_array = array(); // The simulated superglobal $_POST array that the function has to process
  486. $names_array = array(); // Used to build the comma separated list of var names within the $_POST
  487. // array that our function needs to process
  488. // Assemble the names array into a comma-separated string of names,
  489. // add it to the $post_array, and then process the array
  490. // ====================================================================
  491. foreach( $this->check_array as $tree_name => $branches ){
  492. foreach( $branches as $branch_name => $keys ){
  493. foreach( $keys as $key_name => $data ) {
  494. $post_key = $tree_name . $this->delimiter . $branch_name . $this->delimiter . $key_name;
  495. $post_array[$post_key] = $data["val"];
  496. $names_array[] = $post_key;
  497. }
  498. }
  499. }
  500. unset($tree_name, $branches, $branch_name, $keys, $key_name);
  501. $names_left = sizeof($names_array) - 1;
  502. foreach($names_array as $name){
  503. $post_array["key_names"] .= $name;
  504. if($names_left != 0){
  505. $post_array["key_names"] .= ", ";
  506. $names_left--;
  507. }
  508. }
  509. unset($name);
  510. $this->cls->processHTMLForm($post_array);
  511. // Load every config option stored to the db and compare it against
  512. // the updated values stored in the local $check_array
  513. // ===================================================================
  514. for( $tree = 0; $tree < $tree_max; $tree++) {
  515. for( $branch = 0; $branch < $branch_max; $branch++) {
  516. for ($key = 0; $key < $key_max; $key++) {
  517. $tree_str = "T".$tree;
  518. $branch_str = "B".$branch;
  519. $key_str = "K".$key;
  520. $result = $this->cls->getKeyVal($tree_str, $branch_str, $key_str);
  521. $this->assertEquals($result, $check_array[$tree_str][$branch_str][$key_str], $message=null, $delta=0.001);
  522. }
  523. }
  524. }
  525. }
  526. function tearDown() {
  527. $class = new BPM_config();
  528. $class->uninstall();
  529. parent::tearDown();
  530. }
  531. }
  532. ?>