From 153c10b5c0996ab0611b01a3c17235c2649b6c90 Mon Sep 17 00:00:00 2001 From: colemanw Date: Sun, 24 Sep 2023 06:12:16 -0400 Subject: [PATCH] Afform - ensure prefill entities are populated correctly --- ext/afform/core/ang/af/afForm.component.js | 1 + .../phpunit/api/v4/AfformPrefillUsageTest.php | 62 +++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/ext/afform/core/ang/af/afForm.component.js b/ext/afform/core/ang/af/afForm.component.js index ed288e65ec6c..00fb929047b7 100644 --- a/ext/afform/core/ang/af/afForm.component.js +++ b/ext/afform/core/ang/af/afForm.component.js @@ -70,6 +70,7 @@ result.forEach((item) => { // Use _.each() because item.values could be cast as an object if array keys are not sequential _.each(item.values, (values, index) => { + data[item.name][index] = data[item.name][index] || {}; data[item.name][index].joins = {}; angular.merge(data[item.name][index], values, {fields: _.cloneDeep(schema[item.name].data || {})}); }); diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformPrefillUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformPrefillUsageTest.php index 11e97469689b..936eb8ed567c 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/AfformPrefillUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformPrefillUsageTest.php @@ -100,4 +100,66 @@ public function testPrefillWithRepeat(): void { $this->assertTrue(empty($prefill['Individual1']['values'])); } + public function testPrefillByRelationship(): void { + + $layout = << + + +
+ +
+
+ +
+ + +EOHTML; + + $this->useValues([ + 'layout' => $layout, + 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + ]); + + $uid = $this->createLoggedInUser(); + + $cid = $this->saveTestRecords('Contact', [ + 'records' => [ + ['first_name' => 'Co', 'last_name' => 'Parent'], + ['first_name' => 'First', 'last_name' => 'Child'], + ['first_name' => 'Second', 'last_name' => 'Child'], + ['first_name' => 'Third', 'last_name' => 'Child'], + ], + ])->column('id'); + + // Create parent/child relationships + foreach ([1, 2, 3] as $child) { + $values = [ + 'contact_id_a' => $cid[$child], + 'contact_id_b' => $cid[0], + 'relationship_type_id:name' => 'Child of', + ]; + civicrm_api4('Relationship', 'create', ['values' => $values]); + $values['contact_id_b'] = $uid; + civicrm_api4('Relationship', 'create', ['values' => $values]); + } + + $prefill = Civi\Api4\Afform::prefill() + ->setName($this->formName) + ->execute() + ->indexBy('name'); + + $this->assertCount(3, $prefill['Children']['values']); + $children = array_column($prefill['Children']['values'], 'fields'); + $this->assertContains('First', array_column($children, 'first_name')); + $this->assertContains('Second', array_column($children, 'first_name')); + $this->assertContains('Third', array_column($children, 'first_name')); + + $this->assertCount(2, $prefill['Parents']['values']); + $parents = array_column($prefill['Parents']['values'], 'fields'); + $this->assertContains('Co', array_column($parents, 'first_name')); + $this->assertContains($uid, array_column($parents, 'id')); + $this->assertContains($cid[0], array_column($parents, 'id')); + } + }