Skip to content

Commit

Permalink
Unit test for custom date parsing
Browse files Browse the repository at this point in the history
Unit test for civicrm#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 civicrm#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.
  • Loading branch information
eileenmcnaughton committed Aug 8, 2019
1 parent f68ee4e commit 3a77437
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
31 changes: 31 additions & 0 deletions tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -142,6 +150,29 @@ public function testContributionStatusLabel() {
$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.
*
Expand Down
42 changes: 29 additions & 13 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function createCustomGroup($params = []) {
* @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');
}
Expand All @@ -86,6 +86,10 @@ public function createCustomGroupWithFieldOfType($groupParams = [], $customField
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'];
}
Expand All @@ -102,18 +106,7 @@ public function createCustomFieldsOfAllTypes() {
$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 = [
Expand Down Expand Up @@ -252,4 +245,27 @@ protected function createSelectCustomField(array $params): array {
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']];
}


}

0 comments on commit 3a77437

Please sign in to comment.