From 8acc3e8acbab4089866f63c35bd496960187341d Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 25 Jul 2019 12:19:15 +1200 Subject: [PATCH] [Test] Add test to demonstrate bug that turns out not to exist I thought there was a bug in import handling in custom data so I wrote a test to try to capture it - tests pass & I'm off to re-check my data & see if the bug was more of a pebkac error. Anyway test is good --- .../CRM/Contact/Import/Parser/ContactTest.php | 47 ++++++++++ .../CRMTraits/Custom/CustomDataTrait.php | 91 ++++++++++++------- 2 files changed, 104 insertions(+), 34 deletions(-) diff --git a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php index 835f393f2754..7b12d3c44a43 100644 --- a/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php +++ b/tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php @@ -37,6 +37,15 @@ * @group headless */ class CRM_Contact_Import_Parser_ContactTest extends CiviUnitTestCase { + use CRMTraits_Custom_CustomDataTrait; + + /** + * Main entity for the class. + * + * @var string + */ + protected $entity = 'Contact'; + protected $_tablesToTruncate = ['civicrm_address', 'civicrm_phone', 'civicrm_email']; /** @@ -316,6 +325,44 @@ public function testGenderLabel() { $this->callAPISuccessGetSingle('Contact', $contactValues); } + /** + * Test that labels work for importing custom data. + * + * @throws \CRM_Core_Exception + */ + public function testCustomDataLabel() { + $this->createCustomGroupWithFieldOfType([], 'select'); + $contactValues = [ + 'first_name' => 'Bill', + 'last_name' => 'Gates', + 'email' => 'bill.gates@microsoft.com', + 'nick_name' => 'Billy-boy', + $this->getCustomFieldName('select') => 'Yellow', + ]; + $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]); + $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')])); + $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]); + } + + /** + * Test that names work for importing custom data. + * + * @throws \CRM_Core_Exception + */ + public function testCustomDataName() { + $this->createCustomGroupWithFieldOfType([], 'select'); + $contactValues = [ + 'first_name' => 'Bill', + 'last_name' => 'Gates', + 'email' => 'bill.gates@microsoft.com', + 'nick_name' => 'Billy-boy', + $this->getCustomFieldName('select') => 'Y', + ]; + $this->runImport($contactValues, CRM_Import_Parser::DUPLICATE_UPDATE, CRM_Import_Parser::VALID, [NULL, NULL, 'Primary', NULL, NULL]); + $contact = $this->callAPISuccessGetSingle('Contact', array_merge($contactValues, ['return' => $this->getCustomFieldName('select')])); + $this->assertEquals('Y', $contact[$this->getCustomFieldName('select')]); + } + /** * Test that the import parser adds the address to the primary location. * diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index af0e1936840d..963daae894a2 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -72,12 +72,21 @@ public function createCustomGroup($params = []) { * @throws \CRM_Core_Exception */ public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') { - if ($customFieldType !== 'text') { + $supported = ['text', 'select']; + if (!in_array($customFieldType, $supported)) { throw new CRM_Core_Exception('we have not yet extracted other custom field types from createCustomFieldsOfAllTypes, Use consistent syntax when you do'); } $groupParams['title'] = empty($groupParams['title']) ? $identifier . 'Group with field ' . $customFieldType : $groupParams['title']; $this->createCustomGroup($groupParams); - $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]); + switch ($customFieldType) { + case 'text': + $customField = $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]); + break; + + case 'select': + $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]); + break; + } $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id']; } @@ -90,38 +99,7 @@ public function createCustomFieldsOfAllTypes() { $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]); $ids['text'] = $customField['id']; - $optionValue[] = [ - 'label' => 'Red', - 'value' => 'R', - 'weight' => 1, - 'is_active' => 1, - ]; - $optionValue[] = [ - 'label' => 'Yellow', - 'value' => 'Y', - 'weight' => 2, - 'is_active' => 1, - ]; - $optionValue[] = [ - 'label' => 'Green', - 'value' => 'G', - 'weight' => 3, - 'is_active' => 1, - ]; - - $params = [ - 'label' => 'Pick Color', - 'html_type' => 'Select', - 'data_type' => 'String', - 'weight' => 2, - 'is_required' => 1, - 'is_searchable' => 0, - 'is_active' => 1, - 'option_values' => $optionValue, - 'custom_group_id' => $customGroupID, - ]; - - $customField = $this->callAPISuccess('custom_field', 'create', $params); + $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]); $ids['select_string'] = $customField['id']; $params = [ @@ -229,4 +207,49 @@ protected function createTextCustomField($params = []) { return $this->callAPISuccess('CustomField', 'create', $params)['values'][0]; } + /** + * Create custom select field. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createSelectCustomField(array $params): array { + $optionValue = [ + [ + 'label' => 'Red', + 'value' => 'R', + 'weight' => 1, + 'is_active' => 1, + ], + [ + 'label' => 'Yellow', + 'value' => 'Y', + 'weight' => 2, + 'is_active' => 1, + ], + [ + 'label' => 'Green', + 'value' => 'G', + 'weight' => 3, + 'is_active' => 1, + ], + ]; + + $params = array_merge([ + 'label' => 'Pick Color', + 'html_type' => 'Select', + 'data_type' => 'String', + 'weight' => 2, + 'is_required' => 1, + 'is_searchable' => 0, + 'is_active' => 1, + 'option_values' => $optionValue, + ], $params); + + $customField = $this->callAPISuccess('custom_field', 'create', $params); + return $customField['values'][$customField['id']]; + } + }