From 9d4f6fb6e06484094d0b452162bdf915436d1a65 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Wed, 19 May 2021 19:25:40 +1000 Subject: [PATCH] Ensure that we only replace entity reference fields and add unit test --- .../core/Civi/Api4/Action/Afform/Submit.php | 10 ++++- .../tests/phpunit/api/v4/AfformUsageTest.php | 41 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php index f639dcc0b1..56e0768f45 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/Submit.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/Submit.php @@ -20,10 +20,16 @@ class Submit extends AbstractProcessor { protected $values; protected function processForm() { - $entityValues = $entityIds = $entityMapping = []; + $entityValues = $entityIds = $entityMapping = $entityRefFields = []; foreach ($this->_formDataModel->getEntities() as $entityName => $entity) { $entityIds[$entityName] = NULL; $entityMapping[$entityName] = $entity['type']; + $entityFields = \civicrm_api4($entity['type'], 'getFields', ['checkPermissions' => FALSE]); + foreach ($entityFields as $field) { + if ($field['input_type'] === 'EntityRef') { + $entityRefFields[] = $field['name']; + } + } foreach ($this->values[$entityName] ?? [] as $values) { $entityValues[$entity['type']][$entityName][] = $values + ['fields' => []]; // Predetermined values override submitted values @@ -42,7 +48,7 @@ class Submit extends AbstractProcessor { foreach ($eValues as $key => $record) { foreach ($record as $k => $v) { foreach ($v as $field => $value) { - if (array_key_exists($value, $event->entityIds) && !empty($event->entityIds[$value])) { + if (array_key_exists($value, $event->entityIds) && !empty($event->entityIds[$value]) && in_array($field, $entityRefFields, TRUE)) { $eValues[$key][$k][$field] = $event->entityIds[$value]; } } diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php index 6c39003d46..fb5219743e 100644 --- a/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformUsageTest.php @@ -125,7 +125,48 @@ EOHTML; $this->assertEquals('Register A site', $contact['source']); // Check that the contact and the activity were correctly linked up as per the form. $this->callAPISuccess('ActivityContact', 'get', ['contact_id' => $contact['id'], 'activity_id' => $activity['id']]); + } + + public function testCheckEntityReferenceFieldsReplacement(): void { + $this->useValues([ + 'layout' => self::$layouts['registerSite'], + 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + ]); + + CRM_Core_Config::singleton()->userPermissionTemp = new CRM_Core_Permission_Temp(); + $values = [ + 'Individual1' => [ + [ + 'fields' => [ + 'first_name' => 'Test Register Individual1', + 'last_name' => 'site', + 'source' => 'test source', + ], + ], + ], + 'Activity1' => [ + [ + 'fields' => [ + 'subject' => 'Test Register Site Form Submission Individual1', + ], + ], + ], + ]; + Civi\Api4\Afform::submit() + ->setName($this->formName) + ->setArgs([]) + ->setValues($values) + ->execute(); + // Check that Activity was submitted correctly. + $activity = \Civi\Api4\Activity::get()->setCheckPermissions(FALSE)->execute()->first(); + $this->assertEquals('Test Register Site Form Submission', $activity['subject']); + $contact = \Civi\Api4\Contact::get()->addWhere('first_name', '=', 'Test Register')->execute()->first(); + $this->assertEquals('site', $contact['last_name']); + // Check that the data overrides form submsision + $this->assertEquals('Register A site Individual1', $contact['source']); + // Check that the contact and the activity were correctly linked up as per the form. + $this->callAPISuccess('ActivityContact', 'get', ['contact_id' => $contact['id'], 'activity_id' => $activity['id']]); } public function testAboutMeForbidden(): void { -- 2.25.1