Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unit test for custom date parsing #14988

Merged
merged 1 commit into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 28 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,26 @@ 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']];
}

}