PageRenderTime 48ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/unit-test/testcase/test_album.type.policy.php

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