Пример #1
0
 /**
  * Creates a test object, including any required objects it needs via recursion
  * createOnly: only create in database, do not store or return the objects (useful for perf testing)
  * ONLY USE FOR TESTING
  *
  * @param string $daoName
  * @param array $params
  * @param int $numObjects
  * @param bool $createOnly
  *
  * @return object|array|NULL
  *   NULL if $createOnly. A single object if $numObjects==1. Otherwise, an array of multiple objects.
  */
 public static function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE)
 {
     //this is a test function  also backtrace is set for the test suite it sometimes unsets itself
     // so we re-set here in case
     $config = CRM_Core_Config::singleton();
     $config->backtrace = TRUE;
     static $counter = 0;
     CRM_Core_DAO::$_testEntitiesToSkip = array('CRM_Core_DAO_Worldregion', 'CRM_Core_DAO_StateProvince', 'CRM_Core_DAO_Country', 'CRM_Core_DAO_Domain', 'CRM_Financial_DAO_FinancialType');
     // Prefer to instantiate BAO's instead of DAO's (when possible)
     // so that assignTestValue()/assignTestFK() can be overloaded.
     $baoName = str_replace('_DAO_', '_BAO_', $daoName);
     if (class_exists($baoName)) {
         $daoName = $baoName;
     }
     for ($i = 0; $i < $numObjects; ++$i) {
         ++$counter;
         /** @var CRM_Core_DAO $object */
         $object = new $daoName();
         $fields =& $object->fields();
         foreach ($fields as $fieldName => $fieldDef) {
             $dbName = $fieldDef['name'];
             $FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
             $required = CRM_Utils_Array::value('required', $fieldDef);
             if (CRM_Utils_Array::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
                 $object->{$dbName} = $params[$dbName];
             } elseif ($dbName != 'id') {
                 if ($FKClassName != NULL) {
                     $object->assignTestFK($fieldName, $fieldDef, $params);
                     continue;
                 } else {
                     $object->assignTestValue($fieldName, $fieldDef, $counter);
                 }
             }
         }
         $object->save();
         if (!$createOnly) {
             $objects[$i] = $object;
         } else {
             unset($object);
         }
     }
     if ($createOnly) {
         return NULL;
     } elseif ($numObjects == 1) {
         return $objects[0];
     } else {
         return $objects;
     }
 }
Пример #2
0
 static function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE)
 {
     static $counter = 0;
     CRM_Core_DAO::$_testEntitiesToSkip = array('CRM_Core_DAO_Worldregion', 'CRM_Core_DAO_StateProvince', 'CRM_Core_DAO_Country', 'CRM_Core_DAO_Domain');
     for ($i = 0; $i < $numObjects; ++$i) {
         ++$counter;
         $object = new $daoName();
         $fields =& $object->fields();
         foreach ($fields as $name => $value) {
             $dbName = $value['name'];
             if ($dbName == 'contact_sub_type' && empty($params['contact_sub_type'])) {
                 //coming up with a rule to set this is too complex let's not set it
                 continue;
             }
             $FKClassName = CRM_Utils_Array::value('FKClassName', $value);
             $required = CRM_Utils_Array::value('required', $value);
             if (CRM_Utils_Array::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
                 $object->{$dbName} = $params[$dbName];
             } elseif ($dbName != 'id') {
                 if ($FKClassName != NULL) {
                     //skip the FK if it is not required
                     // if it's contact id we should create even if not required
                     // we'll have a go @ fetching first though
                     // we WILL create campaigns though for so tests with a campaign pseudoconstant will complete
                     if ($FKClassName === 'CRM_Campaign_DAO_Campaign' && $daoName != $FKClassName) {
                         $required = TRUE;
                     }
                     if (!$required && $dbName != 'contact_id') {
                         $fkDAO = new $FKClassName();
                         if ($fkDAO->find(TRUE)) {
                             $object->{$dbName} = $fkDAO->id;
                         }
                         unset($fkDAO);
                         continue;
                     }
                     if (in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)) {
                         $depObject = new $FKClassName();
                         $depObject->find(TRUE);
                     } elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $name == 'member_of_contact_id') {
                         // FIXME: the fields() metadata is not specific enough
                         $depObject = CRM_Core_DAO::createTestObject($FKClassName, array('contact_type' => 'Organization'));
                     } else {
                         //if it is required we need to generate the dependency object first
                         $depObject = CRM_Core_DAO::createTestObject($FKClassName, CRM_Utils_Array::value($dbName, $params, 1));
                     }
                     $object->{$dbName} = $depObject->id;
                     unset($depObject);
                     continue;
                 }
                 // Pick an option value if needed
                 if ($value['type'] !== CRM_Utils_Type::T_BOOLEAN) {
                     $options = $daoName::buildOptions($dbName, 'create');
                     if ($options) {
                         $object->{$dbName} = key($options);
                         continue;
                     }
                 }
                 switch ($value['type']) {
                     case CRM_Utils_Type::T_INT:
                     case CRM_Utils_Type::T_FLOAT:
                     case CRM_Utils_Type::T_MONEY:
                         $object->{$dbName} = $counter;
                         break;
                     case CRM_Utils_Type::T_BOOLEAN:
                         if (isset($value['default'])) {
                             $object->{$dbName} = $value['default'];
                         } elseif ($value['name'] == 'is_deleted' || $value['name'] == 'is_test') {
                             $object->{$dbName} = 0;
                         } else {
                             $object->{$dbName} = 1;
                         }
                         break;
                     case CRM_Utils_Type::T_DATE:
                     case CRM_Utils_Type::T_TIMESTAMP:
                     case CRM_Utils_Type::T_DATE + CRM_Utils_Type::T_TIME:
                         $object->{$dbName} = '19700101';
                         if ($dbName == 'end_date') {
                             // put this in the future
                             $object->{$dbName} = '20200101';
                         }
                         break;
                     case CRM_Utils_Type::T_TIME:
                         CRM_Core_Error::fatal('T_TIME shouldnt be used.');
                         //$object->$dbName='000000';
                         //break;
                     //$object->$dbName='000000';
                     //break;
                     case CRM_Utils_Type::T_CCNUM:
                         $object->{$dbName} = '4111 1111 1111 1111';
                         break;
                     case CRM_Utils_Type::T_URL:
                         $object->{$dbName} = 'http://www.civicrm.org';
                         break;
                     case CRM_Utils_Type::T_STRING:
                     case CRM_Utils_Type::T_BLOB:
                     case CRM_Utils_Type::T_MEDIUMBLOB:
                     case CRM_Utils_Type::T_TEXT:
                     case CRM_Utils_Type::T_LONGTEXT:
                     case CRM_Utils_Type::T_EMAIL:
                     default:
                         if (isset($value['enumValues'])) {
                             if (isset($value['default'])) {
                                 $object->{$dbName} = $value['default'];
                             } else {
                                 if (is_array($value['enumValues'])) {
                                     $object->{$dbName} = $value['enumValues'][0];
                                 } else {
                                     $defaultValues = explode(',', $value['enumValues']);
                                     $object->{$dbName} = $defaultValues[0];
                                 }
                             }
                         } else {
                             $object->{$dbName} = $dbName . '_' . $counter;
                             $maxlength = CRM_Utils_Array::value('maxlength', $value);
                             if ($maxlength > 0 && strlen($object->{$dbName}) > $maxlength) {
                                 $object->{$dbName} = substr($object->{$dbName}, 0, $value['maxlength']);
                             }
                         }
                 }
             }
         }
         $object->save();
         if (!$createOnly) {
             $objects[$i] = $object;
         } else {
             unset($object);
         }
     }
     if ($createOnly) {
         return;
     } elseif ($numObjects == 1) {
         return $objects[0];
     } else {
         return $objects;
     }
 }
Пример #3
0
 /**
  * Creates a test object, including any required objects it needs via recursion
  * createOnly: only create in database, do not store or return the objects (useful for perf testing)
  * ONLY USE FOR TESTING
  *
  * @param $daoName
  * @param array $params
  * @param int $numObjects
  * @param bool $createOnly
  *
  * @return
  */
 static function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE)
 {
     //this is a test function  also backtrace is set for the test suite it sometimes unsets itself
     // so we re-set here in case
     $config = CRM_Core_Config::singleton();
     $config->backtrace = TRUE;
     static $counter = 0;
     CRM_Core_DAO::$_testEntitiesToSkip = array('CRM_Core_DAO_Worldregion', 'CRM_Core_DAO_StateProvince', 'CRM_Core_DAO_Country', 'CRM_Core_DAO_Domain');
     for ($i = 0; $i < $numObjects; ++$i) {
         ++$counter;
         /** @var CRM_Core_DAO $object */
         $object = new $daoName();
         $fields =& $object->fields();
         foreach ($fields as $fieldName => $fieldDef) {
             $dbName = $fieldDef['name'];
             $FKClassName = CRM_Utils_Array::value('FKClassName', $fieldDef);
             $required = CRM_Utils_Array::value('required', $fieldDef);
             if (CRM_Utils_Array::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
                 $object->{$dbName} = $params[$dbName];
             } elseif ($dbName != 'id') {
                 if ($FKClassName != NULL) {
                     $object->assignTestFK($fieldName, $fieldDef, $params);
                     continue;
                 } else {
                     $object->assignTestValue($fieldName, $fieldDef, $counter);
                 }
             }
         }
         $object->save();
         if (!$createOnly) {
             $objects[$i] = $object;
         } else {
             unset($object);
         }
     }
     if ($createOnly) {
         return;
     } elseif ($numObjects == 1) {
         return $objects[0];
     } else {
         return $objects;
     }
 }
Пример #4
0
 static function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE)
 {
     static $counter = 0;
     CRM_Core_DAO::$_testEntitiesToSkip = array('CRM_Core_DAO_Worldregion', 'CRM_Core_DAO_StateProvince', 'CRM_Core_DAO_Country', 'CRM_Core_DAO_Domain');
     require_once str_replace('_', DIRECTORY_SEPARATOR, $daoName) . ".php";
     for ($i = 0; $i < $numObjects; ++$i) {
         ++$counter;
         eval('$object   = new ' . $daoName . '( );');
         $fields =& $object->fields();
         foreach ($fields as $name => $value) {
             $dbName = $value['name'];
             $FKClassName = CRM_Utils_Array::value('FKClassName', $value);
             $required = CRM_Utils_Array::value('required', $value);
             if (CRM_Utils_Array::value($dbName, $params) !== NULL && !is_array($params[$dbName])) {
                 $object->{$dbName} = $params[$dbName];
             } elseif ($dbName != 'id') {
                 if ($FKClassName != NULL) {
                     //skip the FK if it is not required
                     // if it's contact id we should create even if not required
                     if (!$required && $dbName != 'contact_id') {
                         continue;
                     }
                     if (in_array($FKClassName, CRM_Core_DAO::$_testEntitiesToSkip)) {
                         $depObject = new $FKClassName();
                         $depObject->find(true);
                     } elseif ($daoName == 'CRM_Member_DAO_MembershipType' && $name == 'member_of_contact_id') {
                         // FIXME: the fields() metadata is not specific enough
                         $depObject = CRM_Core_DAO::createTestObject($FKClassName, array('contact_type' => 'Organization'));
                     } else {
                         //if it is required we need to generate the dependency object first
                         $depObject = CRM_Core_DAO::createTestObject($FKClassName, CRM_Utils_Array::value($dbName, $params, 1));
                     }
                     $object->{$dbName} = $depObject->id;
                     unset($depObject);
                     continue;
                 }
                 $constant = CRM_Utils_Array::value('pseudoconstant', $value);
                 if (!empty($constant)) {
                     $constantOptions = array_keys(CRM_Core_PseudoConstant::getConstant($constant));
                     $object->{$dbName} = $constantOptions[0];
                     continue;
                 }
                 $enum = CRM_Utils_Array::value('enumValues', $value);
                 if (!empty($enum)) {
                     $options = explode(',', $enum);
                     $object->{$dbName} = $options[0];
                     continue;
                 }
                 switch ($value['type']) {
                     case CRM_Utils_Type::T_INT:
                     case CRM_Utils_Type::T_FLOAT:
                     case CRM_Utils_Type::T_MONEY:
                         $object->{$dbName} = $counter;
                         break;
                     case CRM_Utils_Type::T_BOOL:
                     case CRM_Utils_Type::T_BOOLEAN:
                         if (isset($value['default'])) {
                             $object->{$dbName} = $value['default'];
                         } elseif ($value['name'] == 'is_deleted' || $value['name'] == 'is_test') {
                             $object->{$dbName} = 0;
                         } else {
                             $object->{$dbName} = 1;
                         }
                         break;
                     case CRM_Utils_Type::T_DATE:
                     case CRM_Utils_Type::T_TIMESTAMP:
                         $object->{$dbName} = '19700101';
                         break;
                     case CRM_Utils_Type::T_TIME:
                         CRM_Core_Error::fatal('T_TIME shouldnt be used.');
                         //$object->$dbName='000000';
                         //break;
                     //$object->$dbName='000000';
                     //break;
                     case CRM_Utils_Type::T_CCNUM:
                         $object->{$dbName} = '4111 1111 1111 1111';
                         break;
                     case CRM_Utils_Type::T_URL:
                         $object->{$dbName} = 'http://www.civicrm.org';
                         break;
                     case CRM_Utils_Type::T_STRING:
                     case CRM_Utils_Type::T_BLOB:
                     case CRM_Utils_Type::T_MEDIUMBLOB:
                     case CRM_Utils_Type::T_TEXT:
                     case CRM_Utils_Type::T_LONGTEXT:
                     case CRM_Utils_Type::T_EMAIL:
                     default:
                         if (isset($value['enumValues'])) {
                             if (isset($value['default'])) {
                                 $object->{$dbName} = $value['default'];
                             } else {
                                 if (is_array($value['enumValues'])) {
                                     $object->{$dbName} = $value['enumValues'][0];
                                 } else {
                                     $defaultValues = explode(',', $value['enumValues']);
                                     $object->{$dbName} = $defaultValues[0];
                                 }
                             }
                         } else {
                             $object->{$dbName} = $dbName . '_' . $counter;
                             $maxlength = CRM_Utils_Array::value('maxlength', $value);
                             if ($maxlength > 0 && strlen($object->{$dbName}) > $maxlength) {
                                 $object->{$dbName} = substr($object->{$dbName}, 0, $value['maxlength']);
                             }
                         }
                 }
             }
         }
         $object->save();
         if (!$createOnly) {
             $objects[$i] = $object;
         } else {
             unset($object);
         }
     }
     if ($createOnly) {
         return;
     } elseif ($numObjects == 1) {
         return $objects[0];
     } else {
         return $objects;
     }
 }