* @param string $tableName
* @return string|CRM_Core_DAO|NULL
*/
- public static function getClassForTable($tableName) {
+ public static function getClassForTable(string $tableName) {
//CRM-19677: on multilingual setup, trim locale from $tableName to fetch class name
if (CRM_Core_I18n::isMultilingual()) {
global $dbLocale;
$tableName = str_replace($dbLocale, '', $tableName);
}
- return CRM_Utils_Array::value($tableName, self::tables());
+ return self::tables()[$tableName] ?? NULL;
}
/**
return $this->getFkID('Contact');
}
}
+ // If there are no options but the field is supposed to have them, we may need to
+ // create a new option
+ if (!empty($field['suffixes']) && !empty($field['table_name'])) {
+ $optionValue = $this->createOptionValue($field['table_name'], $field['name']);
+ if ($optionValue) {
+ return $optionValue;
+ }
+ }
$randomValue = $this->getRandomValue($field['data_type']);
throw new \CRM_Core_Exception('Could not provide default value');
}
+ /**
+ * Creates a dummy option value when one is required but the option list is empty
+ *
+ * @param string $tableName
+ * @param string $fieldName
+ * @return mixed|null
+ */
+ private function createOptionValue(string $tableName, string $fieldName) {
+ $daoName = \CRM_Core_DAO_AllCoreTables::getClassForTable($tableName);
+ $pseudoconstant = $daoName::getSupportedFields()[$fieldName]['pseudoconstant'] ?? NULL;
+ if (!empty($pseudoconstant['optionGroupName'])) {
+ $newOption = $this->createTestRecord('OptionValue', [
+ 'option_group_id:name' => $pseudoconstant['optionGroupName'],
+ ]);
+ return $newOption['value'];
+ }
+ // Other types of
+ return NULL;
+ }
+
/**
* Delete records previously created by the `saveTestRecords` function.
*