From 0464c24e430a04b793fd9e1fe57180513b9fa6d6 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Sun, 18 Sep 2022 11:36:55 -0400 Subject: [PATCH] Afform - Fix "Existing" autocomplete field and add tests Followup to #24165 which regressed when the action was changed. Locks in the fix with unit tests. --- .../Civi/AfformAdmin/AfformAdminMeta.php | 4 +- .../Api4/Action/Afform/AbstractProcessor.php | 2 +- .../Civi/Afform/AfformGetFieldsTest.php | 27 ------- .../Civi/Afform/AfformMetadataTest.php | 35 +++++++++ .../api/v4/AfformAutocompleteUsageTest.php | 74 +++++++++++++++++++ 5 files changed, 112 insertions(+), 30 deletions(-) delete mode 100644 ext/afform/core/tests/phpunit/Civi/Afform/AfformGetFieldsTest.php create mode 100644 ext/afform/core/tests/phpunit/Civi/Afform/AfformMetadataTest.php create mode 100644 ext/afform/mock/tests/phpunit/api/v4/AfformAutocompleteUsageTest.php diff --git a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php index 469b97dd92..4b16828c3e 100644 --- a/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php +++ b/ext/afform/admin/Civi/AfformAdmin/AfformAdminMeta.php @@ -87,7 +87,7 @@ class AfformAdminMeta { $params += [ 'checkPermissions' => FALSE, 'loadOptions' => ['id', 'label'], - 'action' => 'update', + 'action' => 'create', 'select' => ['name', 'label', 'input_type', 'input_attrs', 'required', 'options', 'help_pre', 'help_post', 'serialize', 'data_type', 'fk_entity', 'readonly'], 'where' => [['input_type', 'IS NOT NULL']], ]; @@ -123,7 +123,7 @@ class AfformAdminMeta { } // Index by name $fields = array_column($fields, NULL, 'name'); - if ($params['action'] === 'update') { + if ($params['action'] === 'create') { // Add existing entity field $idField = CoreUtil::getIdFieldName($entityName); $fields[$idField]['readonly'] = FALSE; diff --git a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php index fb58a574da..35af3eb3ef 100644 --- a/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php +++ b/ext/afform/core/Civi/Api4/Action/Afform/AbstractProcessor.php @@ -106,7 +106,7 @@ abstract class AbstractProcessor extends \Civi\Api4\Generic\AbstractAction { $api4 = $this->_formDataModel->getSecureApi4($entity['name']); $idField = CoreUtil::getIdFieldName($entity['type']); if (!empty($entity['fields'][$idField]['saved_search'])) { - $ids = $this->validateBySavedSearch($entity, $idField, $ids); + $ids = $this->validateBySavedSearch($entity, $ids); } if (!$ids) { return; diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetFieldsTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetFieldsTest.php deleted file mode 100644 index b42cd4af4c..0000000000 --- a/ext/afform/core/tests/phpunit/Civi/Afform/AfformGetFieldsTest.php +++ /dev/null @@ -1,27 +0,0 @@ -installMe(__DIR__)->apply(); - } - - public function testGetFields() { - $fields = Afform::getFields(FALSE) - ->setAction('get') - ->execute()->indexBy('name'); - $this->assertTrue($fields['type']['options']); - $this->assertEquals(['name', 'label', 'icon', 'description'], $fields['type']['suffixes']); - - $this->assertTrue($fields['base_module']['options']); - $this->assertTrue($fields['contact_summary']['options']); - } - -} diff --git a/ext/afform/core/tests/phpunit/Civi/Afform/AfformMetadataTest.php b/ext/afform/core/tests/phpunit/Civi/Afform/AfformMetadataTest.php new file mode 100644 index 0000000000..5dfc62e1bc --- /dev/null +++ b/ext/afform/core/tests/phpunit/Civi/Afform/AfformMetadataTest.php @@ -0,0 +1,35 @@ +install(['org.civicrm.search_kit', 'org.civicrm.afform', 'org.civicrm.afform_admin'])->apply(); + } + + public function testGetFields():void { + $fields = Afform::getFields(FALSE) + ->setAction('get') + ->execute()->indexBy('name'); + $this->assertTrue($fields['type']['options']); + $this->assertEquals(['name', 'label', 'icon', 'description'], $fields['type']['suffixes']); + + $this->assertTrue($fields['base_module']['options']); + $this->assertTrue($fields['contact_summary']['options']); + } + + public function testGetEntityFields():void { + $individualFields = \Civi\AfformAdmin\AfformAdminMeta::getFields('Individual'); + + // Ensure the "Existing" contact field exists + $this->assertEquals('Existing Contact', $individualFields['id']['label']); + $this->assertEquals('Existing', $individualFields['id']['input_type']); + } + +} diff --git a/ext/afform/mock/tests/phpunit/api/v4/AfformAutocompleteUsageTest.php b/ext/afform/mock/tests/phpunit/api/v4/AfformAutocompleteUsageTest.php new file mode 100644 index 0000000000..748c8fe32e --- /dev/null +++ b/ext/afform/mock/tests/phpunit/api/v4/AfformAutocompleteUsageTest.php @@ -0,0 +1,74 @@ + + +
+
+ + +
+
+ +EOHTML; + + $this->useValues([ + 'layout' => $layout, + 'permission' => CRM_Core_Permission::ALWAYS_ALLOW_PERMISSION, + ]); + + // Saved search for filtering + \Civi\Api4\SavedSearch::create(FALSE) + ->setValues([ + 'name' => 'the_unit_test_search', + 'label' => 'the_unit_test_search', + 'api_entity' => 'Contact', + 'api_params' => [ + 'version' => 4, + 'select' => ['id', 'display_name'], + 'orderBy' => [], + 'where' => [ + ['contact_type:name', '=', 'Individual'], + ['source', '=', 'Yes'], + ], + ], + ]) + ->execute(); + + $lastName = uniqid(__FUNCTION__); + + $sampleContacts = [ + ['source' => 'Yes', 'first_name' => 'B'], + ['source' => 'Yes', 'first_name' => 'A'], + ['source' => 'No', 'first_name' => 'C'], + ]; + Contact::save(FALSE) + ->setRecords($sampleContacts) + ->addDefault('last_name', $lastName) + ->execute(); + + $result = Contact::autocomplete() + ->setFormName('afform:' . $this->formName) + ->setFieldName('Individual1:id') + ->setInput($lastName) + ->execute(); + + $this->assertCount(2, $result); + $this->assertEquals('A ' . $lastName, $result[0]['label']); + $this->assertEquals('B ' . $lastName, $result[1]['label']); + } + +} -- 2.25.1