Skip to content

Commit

Permalink
dev/core#1913 Allow for schemas to be added by extensions if they are…
Browse files Browse the repository at this point in the history
… in the format of <entityname>Model

Update test doc blocks
  • Loading branch information
seamuslee001 committed Jul 29, 2020
1 parent 3296183 commit 22877e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
15 changes: 14 additions & 1 deletion CRM/UF/Page/ProfileEditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,20 @@ public static function getSchema($entityTypes) {
break;

default:
throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
if (strpos($entityType, 'Model') !== FALSE) {
$entity = str_replace('Model', '', $entityType);
$backboneModel = self::convertCiviModelToBackboneModel(
$entity,
ts('%1', [1 => $entity]),
$availableFields
);
if (!empty($backboneModel['schema'])) {
$civiSchema[$entityType] = $backboneModel;
}
}
if (!isset($civiSchema[$entityType])) {
throw new CRM_Core_Exception("Unrecognized entity type: $entityType");
}
}
}

Expand Down
24 changes: 24 additions & 0 deletions tests/phpunit/CRM/UF/Page/ProfileEditorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,28 @@ public function testGetSchema() {

}

/**
* Test that with an extension adding in UF Fields for an enttiy that isn't supplied by Core e.g. Grant
* That an appropriate entitytype can be specfied in the backbone.marionette profile editor e.g. GrantModel
*/
public function testGetSchemaWithHooks() {
CRM_Utils_Hook::singleton()->setHook('civicrm_alterUFFields', [$this, 'hook_civicrm_alterUFFIelds']);
$schema = CRM_UF_Page_ProfileEditor::getSchema(['IndividualModel', 'GrantModel']);
$this->assertEquals('Grant', $schema['GrantModel']['schema']['grant_decision_date']['civiFieldType']);
}

/**
* Tries to load up the profile schema for a model where there is no corresponding set of fields avaliable.
*
* @expectedException \CRM_Core_Exception
*/
public function testGetSchemaWithHooksWithInvalidModel() {
CRM_Utils_Hook::singleton()->setHook('civicrm_alterUFFields', [$this, 'hook_civicrm_alterUFFIelds']);
$schema = CRM_UF_Page_ProfileEditor::getSchema(['IndividualModel', 'GrantModel', 'PledgeModel']);
}

public function hook_civicrm_alterUFFIelds(&$fields) {
$fields['Grant'] = CRM_Grant_BAO_Grant::exportableFields();
}

}

0 comments on commit 22877e0

Please sign in to comment.