From e25611c715742cb89cd7c6ad35970ad23e4b413f Mon Sep 17 00:00:00 2001 From: Pradeep Nayak Date: Mon, 30 Mar 2020 03:21:57 +0100 Subject: [PATCH] Added unit test for multi-select autocomplete --- .../phpunit/CRM/Core/BAO/CustomFieldTest.php | 43 +++++++++++++++++++ tests/phpunit/CRM/Export/BAO/ExportTest.php | 40 +++++++++++++++++ .../CRMTraits/Custom/CustomDataTrait.php | 13 ++++++ 3 files changed, 96 insertions(+) diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index 6642d75c77..082f24ed68 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -983,4 +983,47 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { } } + /** + * Test for single select Autocomplete custom field. + * + */ + public function testSingleSelectAutoComplete() { + $customGroupId = $this->customGroupCreate([ + 'extends' => 'Individual', + ])['id']; + $colors = ['Y' => 'Yellow', 'G' => 'Green']; + $fieldId = $this->createAutoCompleteCustomField([ + 'custom_group_id' => $customGroupId, + 'option_values' => $colors, + ])['id']; + $contactId = $this->individualCreate(['custom_' . $fieldId => 'Y']); + $value = $this->callAPISuccessGetValue('Contact', [ + 'id' => $contactId, + 'return' => 'custom_' . $fieldId, + ]); + $this->assertEquals('Y', $value); + } + + /** + * Test for multi select Autocomplete custom field. + * + */ + public function testMultiSelectAutoComplete() { + $customGroupId = $this->customGroupCreate([ + 'extends' => 'Individual', + ])['id']; + $colors = ['Y' => 'Yellow', 'G' => 'Green']; + $fieldId = $this->createAutoCompleteCustomField([ + 'custom_group_id' => $customGroupId, + 'serialize' => '1', + 'option_values' => $colors, + ])['id']; + $contactId = $this->individualCreate(['custom_' . $fieldId => ['Y', 'G']]); + $value = $this->callAPISuccessGetValue('Contact', [ + 'id' => $contactId, + 'return' => 'custom_' . $fieldId, + ]); + $this->assertEquals(array_keys($colors), $value); + } + } diff --git a/tests/phpunit/CRM/Export/BAO/ExportTest.php b/tests/phpunit/CRM/Export/BAO/ExportTest.php index 3db128633f..3599bfd61d 100644 --- a/tests/phpunit/CRM/Export/BAO/ExportTest.php +++ b/tests/phpunit/CRM/Export/BAO/ExportTest.php @@ -2979,4 +2979,44 @@ class CRM_Export_BAO_ExportTest extends CiviUnitTestCase { $this->callAPISuccess('address', 'create', $params); } + /** + * Test for single select Autocomplete custom field. + * + */ + public function testSingleAndMultiSelectAutoComplete() { + $customGroupId = $this->customGroupCreate([ + 'extends' => 'Individual', + ])['id']; + $colors = ['Y' => 'Yellow', 'G' => 'Green', 'R' => 'Red']; + $fieldId1 = $this->createAutoCompleteCustomField([ + 'custom_group_id' => $customGroupId, + 'option_values' => $colors, + 'label' => 'Autocomplete Color', + ])['id']; + $fieldId2 = $this->createAutoCompleteCustomField([ + 'custom_group_id' => $customGroupId, + 'option_values' => $colors, + 'label' => 'Autocomplete Colors', + 'serialize' => 1, + ])['id']; + $contactId = $this->individualCreate([ + "custom_$fieldId1" => 'Y', + "custom_$fieldId2" => ['Y', 'G'], + ]); + $selectedFields = [ + ['name' => 'contact_id'], + ['name' => "custom_{$fieldId1}"], + ['name' => "custom_{$fieldId2}"], + ]; + + $this->doExportTest([ + 'ids' => [$contactId], + 'fields' => $selectedFields, + 'exportMode' => CRM_Export_Form_Select::CONTACT_EXPORT, + ]); + $row = $this->csv->fetchOne(); + $this->assertEquals('Yellow', $row['Autocomplete Color']); + $this->assertEquals('Yellow, Green', $row['Autocomplete Colors']); + } + } diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 251326c7ce..85617bd058 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -343,6 +343,19 @@ trait CRMTraits_Custom_CustomDataTrait { return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; } + /** + * Create custom select field. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createAutoCompleteCustomField(array $params): array { + $params = array_merge($this->getFieldsValuesByType('String', 'Autocomplete-Select'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } + /** * Create a custom field of type date. * -- 2.25.1