Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APIv4 - Add fixes & tests for domain-specific managed entities #22173

Merged
merged 1 commit into from
Dec 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Civi/Api4/Dashboard.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
* @package Civi\Api4
*/
class Dashboard extends Generic\DAOEntity {
use Generic\Traits\ManagedEntity;

}
28 changes: 26 additions & 2 deletions Civi/Api4/Generic/Traits/DAOActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ protected function formatWriteValues(&$record) {
* @param array $record
*/
private function resolveFKValues(array &$record): void {
// Resolve domain id first
uksort($record, function($a, $b) {
return substr($a, 0, 9) == 'domain_id' ? -1 : 1;
});
foreach ($record as $key => $value) {
if (substr_count($key, '.') !== 1) {
if (!$value || substr_count($key, '.') !== 1) {
continue;
}
[$fieldName, $fkField] = explode('.', $key);
Expand All @@ -208,7 +212,27 @@ private function resolveFKValues(array &$record): void {
continue;
}
$fkDao = CoreUtil::getBAOFromApiName($field['fk_entity']);
$record[$fieldName] = \CRM_Core_DAO::getFieldValue($fkDao, $value, 'id', $fkField);
// Constrain search to the domain of the current entity
$domainConstraint = NULL;
if (isset($fkDao::getSupportedFields()['domain_id'])) {
if (!empty($record['domain_id'])) {
$domainConstraint = $record['domain_id'] === 'current_domain' ? \CRM_Core_Config::domainID() : $record['domain_id'];
}
elseif (!empty($record['id']) && isset($this->entityFields()['domain_id'])) {
$domainConstraint = \CRM_Core_DAO::getFieldValue($this->getBaoName(), $record['id'], 'domain_id');
}
}
if ($domainConstraint) {
$fkSearch = new $fkDao();
$fkSearch->domain_id = $domainConstraint;
$fkSearch->$fkField = $value;
$fkSearch->find(TRUE);
$record[$fieldName] = $fkSearch->id;
}
// Simple lookup without all the fuss about domains
else {
$record[$fieldName] = \CRM_Core_DAO::getFieldValue($fkDao, $value, 'id', $fkField);
}
unset($record[$key]);
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/phpunit/api/v4/Entity/ManagedEntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace api\v4\Entity;

use api\v4\UnitTestCase;
use Civi\Api4\Domain;
use Civi\Api4\Navigation;
use Civi\Api4\OptionGroup;
use Civi\Api4\OptionValue;
Expand Down Expand Up @@ -460,6 +461,18 @@ public function testManagedNavigationWeights() {
],
];

// Throw a monkey wrench by placing duplicates in another domain
$d2 = Domain::create(FALSE)
->addValue('name', 'Decoy domain')
->addValue('version', \CRM_Utils_System::version())
->execute()->single();
foreach ($this->_managedEntities as $item) {
$decoys[] = civicrm_api4('Navigation', 'create', [
'checkPermissions' => FALSE,
'values' => ['domain_id' => $d2['id']] + $item['params']['values'],
])->first();
}

// Refresh managed entities with module active
$allModules = [
new \CRM_Core_Module('unit.test.fake.ext', TRUE),
Expand Down Expand Up @@ -491,6 +504,15 @@ public function testManagedNavigationWeights() {
$this->assertEquals(TRUE, $children[$index]['is_active']);
}

// Try exporting the decoy records
$decoyExport = Navigation::export(FALSE)
->setId($decoys[0]['id'])
->execute();
$this->assertCount(4, $decoyExport);
$this->assertEquals('Decoy domain', $decoyExport[0]['params']['values']['domain_id.name']);
$this->assertEquals('Decoy domain', $decoyExport[1]['params']['values']['domain_id.name']);
$this->assertArrayNotHasKey('weight', $decoyExport[1]['params']['values']);

// Refresh managed entities with module disabled
$allModules = [
new \CRM_Core_Module('unit.test.fake.ext', FALSE),
Expand Down