Skip to content

Commit

Permalink
[Test] Add test to demonstrate bug that turns out not to exist
Browse files Browse the repository at this point in the history
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
  • Loading branch information
eileenmcnaughton committed Jul 25, 2019
1 parent 44134d5 commit 8acc3e8
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 34 deletions.
47 changes: 47 additions & 0 deletions tests/phpunit/CRM/Contact/Import/Parser/ContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];

/**
Expand Down Expand Up @@ -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.
*
Expand Down
91 changes: 57 additions & 34 deletions tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
}

Expand All @@ -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 = [
Expand Down Expand Up @@ -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']];
}

}

0 comments on commit 8acc3e8

Please sign in to comment.