if (!$nameParam && empty($params['id'])) {
$params['name'] = CRM_Utils_String::titleToVar($params['title']);
}
+ if (!empty($params['name']) && !CRM_Case_BAO_CaseType::isValidName($params['name'])) {
+ throw new CRM_Core_Exception("Cannot create caseType with malformed name [{$params['name']}]");
+ }
// function to format definition column
if (isset($params['definition']) && is_array($params['definition'])) {
$caseType->id = $caseTypeId;
return $caseType->delete();
}
+
+ /**
+ * Determine if a case-type name is well-formed
+ *
+ * @param string $caseType
+ * @return bool
+ */
+ static function isValidName($caseType) {
+ return preg_match('/^[a-zA-Z0-9_]+$/', $caseType);
+ }
}
}
/**
- * @param $caseType
+ * This function was previously used to convert a case-type's
+ * machine-name to a file-name. However, it's mind-boggling
+ * that the file-name might be a munged version of the
+ * machine-name (which is itself a munged version of the
+ * display-name), and naming is now a more visible issue (since
+ * the overhaul of CaseType admin UI).
+ *
+ * Usage note: This is called externally by civix stubs as a
+ * sort of side-ways validation of the case-type's name
+ * (validation which was needed because of the unintuitive
+ * double-munge). We should update civix templates and then
+ * remove this function in Civi 4.6 or 5.0.
*
- * @return mixed|string
+ * @param string $caseType
+ * @return string
+ * @deprecated
+ * @see CRM_Case_BAO_CaseType::isValidName
*/
public static function mungeCaseType($caseType) {
// trim all spaces from $caseType
return simplexml_load_string($definition);
}
- $caseType = CRM_Case_XMLProcessor::mungeCaseType($caseType);
+ if (!CRM_Case_BAO_CaseType::isValidName($caseType)) {
+ // perhaps caller provider a the label instead of the name?
+ throw new CRM_Core_Exception("Cannot load caseType with malformed name [$caseType]");
+ }
if (!CRM_Utils_Array::value($caseType, $this->xml)) {
// first check custom templates directory
$this->assertEquals($result['values'][$id]['title'], $params['title'], 'in line ' . __LINE__);
}
+ /**
+ * Create a case with an invalid name
+ */
+ function testCaseTypeCreate_invalidName() {
+ // Create Case Type
+ $params = array(
+ 'title' => 'Application',
+ 'name' => 'Appl ication', // spaces are not allowed
+ 'is_active' => 1,
+ 'weight' => 4,
+ );
+
+ $this->callAPIFailure('CaseType', 'create', $params);
+ }
+
+
/**
* Test update (create with id) function with valid parameters
*/