예제 #1
0
 /**
  * Generate and assign an arbitrary value to a field of a test object.
  *
  * @param string $fieldName
  * @param array $fieldDef
  * @param int $counter
  *   The globally-unique ID of the test object.
  */
 protected function assignTestValue($fieldName, &$fieldDef, $counter)
 {
     $dbName = $fieldDef['name'];
     $daoName = get_class($this);
     $handled = FALSE;
     if (!$handled && $dbName == 'contact_sub_type') {
         //coming up with a rule to set this is too complex let's not set it
         $handled = TRUE;
     }
     // Pick an option value if needed
     if (!$handled && $fieldDef['type'] !== CRM_Utils_Type::T_BOOLEAN) {
         $options = $daoName::buildOptions($dbName, 'create');
         if ($options) {
             $this->{$dbName} = key($options);
             $handled = TRUE;
         }
     }
     if (!$handled) {
         switch ($fieldDef['type']) {
             case CRM_Utils_Type::T_INT:
             case CRM_Utils_Type::T_FLOAT:
             case CRM_Utils_Type::T_MONEY:
                 if (isset($fieldDef['precision'])) {
                     // $object->$dbName = CRM_Utils_Number::createRandomDecimal($value['precision']);
                     $this->{$dbName} = CRM_Utils_Number::createTruncatedDecimal($counter, $fieldDef['precision']);
                 } else {
                     $this->{$dbName} = $counter;
                 }
                 break;
             case CRM_Utils_Type::T_BOOLEAN:
                 if (isset($fieldDef['default'])) {
                     $this->{$dbName} = $fieldDef['default'];
                 } elseif ($fieldDef['name'] == 'is_deleted' || $fieldDef['name'] == 'is_test') {
                     $this->{$dbName} = 0;
                 } else {
                     $this->{$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:
                 $this->{$dbName} = '19700101';
                 if ($dbName == 'end_date') {
                     // put this in the future
                     $this->{$dbName} = '20200101';
                 }
                 break;
             case CRM_Utils_Type::T_TIME:
                 CRM_Core_Error::fatal("T_TIME shouldn't be used.");
                 //$object->$dbName='000000';
                 //break;
             //$object->$dbName='000000';
             //break;
             case CRM_Utils_Type::T_CCNUM:
                 $this->{$dbName} = '4111 1111 1111 1111';
                 break;
             case CRM_Utils_Type::T_URL:
                 $this->{$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:
                 // WAS: if (isset($value['enumValues'])) {
                 // TODO: see if this works with all pseudoconstants
                 if (isset($fieldDef['pseudoconstant'], $fieldDef['pseudoconstant']['callback'])) {
                     if (isset($fieldDef['default'])) {
                         $this->{$dbName} = $fieldDef['default'];
                     } else {
                         $options = CRM_Core_PseudoConstant::get($daoName, $fieldName);
                         if (is_array($options)) {
                             $this->{$dbName} = $options[0];
                         } else {
                             $defaultValues = explode(',', $options);
                             $this->{$dbName} = $defaultValues[0];
                         }
                     }
                 } else {
                     $this->{$dbName} = $dbName . '_' . $counter;
                     $maxlength = CRM_Utils_Array::value('maxlength', $fieldDef);
                     if ($maxlength > 0 && strlen($this->{$dbName}) > $maxlength) {
                         $this->{$dbName} = substr($this->{$dbName}, 0, $fieldDef['maxlength']);
                     }
                 }
         }
     }
 }
예제 #2
0
 /**
  * @param $value
  * @param $precision
  * @param $expectedValue
  * @dataProvider truncDecimalCases
  */
 public function testCreateTruncatedDecimal($value, $precision, $expectedValue)
 {
     list($sigFigs, $decFigs) = $precision;
     $this->assertEquals($expectedValue, CRM_Utils_Number::createTruncatedDecimal($value, $precision), "assert createTruncatedValue({$value}, ({$sigFigs},{$decFigs})) == {$expectedValue}");
 }