PageRenderTime 43ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 1ms

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

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