/unit-test/bpm-testcase/test_album.type.policy.php
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
- <?php
- /**
- * BP-MEDIA UNIT TEST SCRIPT - ALBUM TYPES POLICY SUB CLASS
- * Exercises all functions of the class
- *
- * @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/
- *
- * ========================================================================================================
- */
- class core_albumType_policy 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 generateData($type=null){
- $valid_types = array("null", "bool", "int", "float", "string", "array", "object");
- if($type === null){ // "0" is a valid type
- $current_type = $valid_types[mt_rand(0,6)];
- }
- else {
- // Using the modulus operator causes the function to cycle
- // through types if $type variable exceeds (int)5.
- $type_idx = $type % 6;
- $current_type = $valid_types[$type_idx];
- }
- switch($current_type){
- case "null" : {
- $val = null;
- } break;
- case "bool" : {
- $val = mt_rand(0,1);
- $val = (bool)$val;
- } break;
- case "int" : {
- $val = mt_rand(-100,100);
- $val = (int)$val;
- } break;
- case "float" : {
- $val = (mt_rand(-100,100) / 17);
- $val = (float)$val;
- } break;
- case "string" : {
- $width = mt_rand(0, 87);
- $val = BPM_sUtil::random_string($width);
- } break;
- case "array" : {
- $named_keys = mt_rand(0,1);
- $val = array();
- if(!$named_keys){
- for($i=0; $i<5; $i++){
- $val[] = mt_rand(-2500, 2500);
- }
- }
- else {
- for($i=0; $i<5; $i++){
- $width = mt_rand(1, 37);
- // PHP does not support arrays with numeric string keys. It silently converts them
- // to int keys. For example: $test_array["1234"] = "foo"; $test_array["bar"] = "baz";
- // var_dump($test_array) produces [ test_array[(int)1234] = "foo", test_array[(string)bar]
- // = "baz"]
- //
- // BPM_sUtil::random_string() can (and does) produce values like (string)7. To avoid
- // debugging problems, prepend all the key names with "k" to ensure PHP treats them as strings.
- $key_name = "k" . BPM_sUtil::random_string($width);
- $val[$key_name] = mt_rand(-2500, 2500);
- }
- }
- } break;
- case "object" : {
- $val = new stdClass();
- for($i=0; $i<3; $i++){
- $width = mt_rand(1, 17);
- // We prepend all the key names with "k" because BPM_sUtil::random_string()
- // can (and does) produce values like (string)7, which is an illegal name for a
- // variable in an object [you can't do $test_object->7 = "foo"; ...because the parser
- // doesn't know if you mean (string)7 or (int)7 ]
- //
- // But, PHP *doesn't catch* this defect when the variable name is created dynamically
- // [ $name = "7"; $test_object->{$name} = "foo" ]. Instead, it converts the key's type
- // from (string)7 to (int)7 and allows the variable to be accessed only if the name is
- // created dynamically [$name = "7"; echo ($test_object->{$name}; ]
- $key_name = "k" . BPM_sUtil::random_string($width);
- $val->{$key_name} = mt_rand(-2500, 2500);
- }
- } break;
- } // End of switch($current_type)
- return $val;
- }
- function load_table(){
- // Fill table with policy options having random sizes
- // and data types. Save a copy of these to a local array.
- // =================================================================
- $total_keys = 0;
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- for ($key=1; $key < $this->key_max; $key++) {
- // Cycle through data types for the first 50 keys, then
- // randomly pick data types
- if($total_keys < 50){
- $val = self::generateData($total_keys);
- }
- else {
- $val = self::generateData();
- }
- $key_name = "key" . $key;
- // Write generated value to policy class and check array
- $set_ok = $this->cls->setKey($module_id, $type_id, $branch_id, $key_name, $val);
- $this->assertEquals(true, $set_ok);
- $this->check_array[$module_id][$type_id][$branch_id][$key_name] = $val;
- $total_keys++;
- } // ENDOF for( $key
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- }
- function check_table(){
- // Load every key stored to the table and compare it against
- // the value stored in the check array, making sure data types were
- // correctly recovered (bool) false doesn't become (int) 0 or "NULL"
- // ====================================================================
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- for ($key=1; $key < $this->key_max; $key++) {
- $key_name = "key" . $key;
- $expected = $this->check_array[$module_id][$type_id][$branch_id][$key_name];
- $result = $this->cls->getKey($module_id, $type_id, $branch_id, $key_name, &$valid);
- if( is_array($this->check_array[$module_id][$type_id][$branch_id]) &&
- array_key_exists($key_name, $this->check_array[$module_id][$type_id][$branch_id]) ){
- $key_exists = true;
- }
- else {
- $key_exists = false;
- }
- if($key_exists){
- //var_dump($result);
- $this->assertEquals($expected, $result);
- $this->assertEquals(true, $valid);
- }
- else {
- $this->assertEquals(null, $result);
- $this->assertEquals(false, $valid);
- }
- }
- }
- }
- }
- }
- function test_create(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_create
-
- function test_update(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
-
- // Overwrite half of the items in the table with random new data
- // of random type
- // =================================================================
- $total_keys = 0;
-
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- for ($key=1; $key < $this->key_max; $key++) {
- $overwrite = mt_rand(0,1);
- if($overwrite){
- // Cycle through data types for the first 50 keys, then
- // randomly pick data types
- if($total_keys < 50){
- $val = self::generateData($total_keys);
- }
- else {
- $val = self::generateData();
- }
- $key_name = "key" . $key;
- // Write generated value to policy class and check array. Note: we cannot check for
- // a "true" return value from the function, because in some cases the overwrite function
- // tries to write the same value that is currently in a key to a key ...in which case
- // the db returns (int)0 because no rows were changed by the query, and so setKey()
- // returns (bool)false as a result.
- $this->cls->setKey($module_id, $type_id, $branch_id, $key_name, $val);
- $this->check_array[$module_id][$type_id][$branch_id][$key_name] = $val;
- $total_keys++;
- } // ENDOF if($overwrite)
- } // ENDOF for( $key
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_update
-
- function test_dropKey_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropKey
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- for ($key=1; $key < $this->key_max; $key++) {
- $key_name = "key" . $key;
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the key
- $this->cls->dropKey($module_id, $type_id, $branch_id, $key_name);
- unset($this->check_array[$module_id][$type_id][$branch_id][$key_name]);
- } // ENDOF if($drop)
- } // ENDOF for( $key
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropKey_single
- function test_dropBranch_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropBranch
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the branch
- $this->cls->dropBranch($module_id, $type_id, $branch_id);
- unset($this->check_array[$module_id][$type_id][$branch_id]);
- } // ENDOF if($drop)
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropBranch_single
- function test_dropType_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropType
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the branch
- $this->cls->dropType($module_id, $type_id);
- unset($this->check_array[$module_id][$type_id]);
- } // ENDOF if($drop)
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropType_single
- function test_dropModule_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropModule
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the branch
- $this->cls->dropModule($module_id);
- unset($this->check_array[$module_id]);
- } // ENDOF if($drop)
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropModule_single
- function test_dropKey_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropKey
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- $drop_array = array();
- $drop = true;
- for ($key=1; $key < $this->key_max; $key++) {
- $key_name = "key" . $key;
- if($drop){
- $drop_array[] = $key_name;
- $drop = false;
- }
- else {
- $drop = true;
- }
- } // ENDOF for( $key
- // Drop the keys
- $this->cls->dropKey($module_id, $type_id, $branch_id, $drop_array);
- foreach($drop_array as $drop_key){
- unset($this->check_array[$module_id][$type_id][$branch_id][$drop_key]);
- }
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropKey_array
-
- function test_dropBranch_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropKey
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- $drop_array = array();
- $drop = true;
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
-
- if($drop){
- $drop_array[] = $branch_id;
- $drop = false;
- }
- else {
- $drop = true;
- }
- } // ENDOF for( $branch_id
- // Drop the branches
- $this->cls->dropBranch($module_id, $type_id, $drop_array);
- foreach($drop_array as $drop_branch){
- unset($this->check_array[$module_id][$type_id][$drop_branch]);
- }
- } // ENDOF: for( $type_id
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropBranch_array
-
-
- function test_dropType_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the items in the table, using single string dropKey
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- $drop = true;
- $drop_array = array();
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- if($drop){
- $drop_array[] = $type_id;
- $drop = false;
- }
- else {
- $drop = true;
- }
- } // ENDOF: for( $type_id
- // Drop the types
- $this->cls->dropType($module_id, $drop_array);
- foreach($drop_array as $drop_type){
- unset($this->check_array[$module_id][$drop_type]);
- }
- } // ENDOF: for( $module_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropType_array
- function test_dropModule_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- $drop = true;
- $drop_array = array();
- // Drop half of the items in the table, using single string dropKey
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- if($drop){
- $drop_array[] = $module_id;
- $drop = false;
- }
- else {
- $drop = true;
- }
- } // ENDOF: for( $module_id
- // Drop the modules
- $this->cls->dropModule($drop_array);
- foreach($drop_array as $drop_module){
- unset($this->check_array[$drop_module]);
- }
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropModule_array
- function test_dropSiteKey_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the keys in the table for *all modules*
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- for ($key=1; $key < $this->key_max; $key++) {
- $key_name = "key" . $key;
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the key from the db
- $this->cls->dropSiteKey($type_id, $branch_id, $key_name);
- // Drop the key from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id][$branch_id][$key_name]);
- }
- } // ENDOF if($drop)
- } // ENDOF for( $key
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteKey_single
- function test_dropSiteKey_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- $drop_array = array();
- for ($key=1; $key < $this->key_max; $key++) {
- $key_name = "key" . $key;
- $drop = mt_rand(0,1);
- if($drop){
- // Add key to drop array
- $drop_array[] = $key_name;
- // Drop the key from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id][$branch_id][$key_name]);
- }
- } // ENDOF if($drop)
- } // ENDOF for( $key
- // Drop the array of keys
- $this->cls->dropSiteKey($type_id, $branch_id, $drop_array);
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteKey_array
- function test_dropSiteBranch_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the branches in the table for *all modules*
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the branch from the db
- $this->cls->dropSiteBranch($type_id, $branch_id);
- // Drop the branch from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id][$branch_id]);
- }
- } // ENDOF if($drop)
- } // ENDOF for( $branch_id
- } // ENDOF: for( $type_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteBranch_single
- function test_dropSiteBranch_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the branches in the table for *all modules*
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- $drop_array = array();
- for( $branch_id=1; $branch_id < $this->branch_id_max; $branch_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- // Add branch to drop array
- $drop_array[] = $branch_id;
- // Drop the branch from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id][$branch_id]);
- }
- } // ENDOF if($drop)
- } // ENDOF for( $branch_id
- // Drop the branches from the db
- $this->cls->dropSiteBranch($type_id, $drop_array);
- } // ENDOF: for( $type_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteBranch_array
- function test_dropSiteType_single(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- // Drop half of the keys in the table for *all modules*
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- // Drop the type from the db
- $this->cls->dropSiteType($type_id);
- // Drop the type from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id]);
- }
- } // ENDOF if($drop)
- } // ENDOF: for( $type_id
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteType_single
- function test_dropSiteType_array(){
- // Clear the db
- $this->cls->truncate();
- // Delete all entries in the class cache
- $this->cls->flushCache();
- // Load the database and check array with test data
- self::load_table();
- $drop_array = array();
- // Drop half of the types in the table for *all modules*
- for( $type_id=1; $type_id < $this->type_id_max; $type_id++) {
- $drop = mt_rand(0,1);
- if($drop){
- $drop_array[] = $type_id;
- // Drop the type from the check array for *all modules*
- for( $module_id=1; $module_id < $this->module_id_max; $module_id++){
- unset($this->check_array[$module_id][$type_id]);
- }
- } // ENDOF if($drop)
- } // ENDOF: for( $type_id
- // Drop the types from the db
- $this->cls->dropSiteType($drop_array);
- // Verify the stored data matches the check array
- self::check_table();
- } // End of test_dropSiteType_array
- function tearDown() {
- $this->cls->uninstall();
- parent::tearDown();
- }
- }
- ?>