From 35aca6d6278e328a923b1d8a3bd40cae02ac6c10 Mon Sep 17 00:00:00 2001 From: eileen Date: Thu, 8 Aug 2019 12:50:03 +1200 Subject: [PATCH] Unit test for custom date parsing Unit test for https://github.com/civicrm/civicrm-core/pull/14986 along with some code comments in the test about issues I hit doing what I thought was the right fix - copied here for visibility // @todo I feel like we should work towards this actually parsing $params here - // & dropping formatting but // per https://github.com/civicrm/civicrm-core/pull/14986 for now $formatted is parsing // The issue I hit was that when I tried to extend to checking they were correctly imported // I was not actually sure what correct behaviour was for what dates were accepted since // it seems to ignore the latter in favour of the former - which seems wrong. --- .../Import/Parser/ContributionTest.php | 31 ++++++++++++++ .../CRMTraits/Custom/CustomDataTrait.php | 41 +++++++++++++------ 2 files changed, 59 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php index 44a6161911..a6b689ff4d 100644 --- a/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php +++ b/tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php @@ -11,6 +11,14 @@ */ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { protected $_tablesToTruncate = []; + use CRMTraits_Custom_CustomDataTrait; + + /** + * Default entity for class. + * + * @var string + */ + protected $entity = 'Contribution'; /** * Setup function. @@ -142,6 +150,29 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase { $this->callAPISuccessGetCount('Contribution', ['contact_id' => $contactID], 2); } + /** + * Test dates are parsed + * + * @throws \CRM_Core_Exception + */ + public function testParsedCustomDates() { + $this->createCustomGroupWithFieldOfType([], 'date'); + $mapperKeys = []; + $form = new CRM_Contribute_Import_Parser_Contribution($mapperKeys); + $params = [$this->getCustomFieldName('date') => '20/10/2019']; + CRM_Core_Session::singleton()->set('dateTypes', 32); + $formatted = []; + $form->formatInput($params, $formatted); + // @todo I feel like we should work towards this actually parsing $params here - + // & dropping formatting but + // per https://github.com/civicrm/civicrm-core/pull/14986 for now $formatted is parsing + // The issue I hit was that when I tried to extend to checking they were correctly imported + // I was not actually sure what correct behaviour was for what dates were accepted since + // on one hand the custom fields have a date format & on the other there is an input format & + // it seems to ignore the latter in favour of the former - which seems wrong. + $this->assertEquals('20191020000000', $formatted[$this->getCustomFieldName('date')]); + } + /** * Run the import parser. * diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index f8dba7b531..a90b465d6f 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -72,7 +72,7 @@ trait CRMTraits_Custom_CustomDataTrait { * @throws \CRM_Core_Exception */ public function createCustomGroupWithFieldOfType($groupParams = [], $customFieldType = 'text', $identifier = '') { - $supported = ['text', 'select']; + $supported = ['text', 'select', 'date']; 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'); } @@ -86,6 +86,10 @@ trait CRMTraits_Custom_CustomDataTrait { case 'select': $customField = $this->createSelectCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]); break; + + case 'date': + $customField = $this->createDateCustomField(['custom_group_id' => $this->ids['CustomGroup'][$groupParams['title']]]); + break; } $this->ids['CustomField'][$identifier . $customFieldType] = $customField['id']; } @@ -102,18 +106,7 @@ trait CRMTraits_Custom_CustomDataTrait { $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]); $ids['select_string'] = $customField['id']; - $params = [ - 'custom_group_id' => $customGroupID, - 'name' => 'test_date', - 'label' => 'test_date', - 'html_type' => 'Select Date', - 'data_type' => 'Date', - 'default_value' => '20090711', - 'weight' => 3, - 'time_format' => 1, - ]; - - $customField = $this->callAPISuccess('custom_field', 'create', $params); + $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]); $ids['select_date'] = $customField['id']; $params = [ @@ -252,4 +245,26 @@ trait CRMTraits_Custom_CustomDataTrait { return $customField['values'][$customField['id']]; } + /** + * Create a custom field of type date. + * + * @param array $params + * + * @return array + */ + protected function createDateCustomField($params): array { + $params = array_merge([ + 'name' => 'test_date', + 'label' => 'test_date', + 'html_type' => 'Select Date', + 'data_type' => 'Date', + 'default_value' => '20090711', + 'weight' => 3, + 'time_format' => 1, + ], $params); + + $customField = $this->callAPISuccess('custom_field', 'create', $params); + return $customField['values'][$customField['id']]; + } + } -- 2.25.1