From: eileen Date: Wed, 8 Apr 2020 04:37:43 +0000 (+1200) Subject: [NFC][Test] cleanup on customDataTrait for tests X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=79c9d4c2e794abec64a02aeac7ea376e70c42e1b;hp=3ad1af927298370b2c1b4c272e268e1f4ed7c8c0;p=civicrm-core.git [NFC][Test] cleanup on customDataTrait for tests --- diff --git a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php index e0a1cf9b62..0ed1ff2a17 100644 --- a/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php +++ b/tests/phpunit/CRM/Core/BAO/CustomFieldTest.php @@ -461,7 +461,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '0', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'country_' . $this->getCustomFieldID('country'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.country_' . $this->getCustomFieldID('country'), @@ -477,7 +477,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { $this->getCustomFieldName('file') => [ 'name' => $this->getCustomFieldName('file'), 'type' => 2, - 'title' => 'Custom Field', + 'title' => 'My file', 'headerPattern' => '//', 'import' => 1, 'custom_field_id' => $this->getCustomFieldID('file'), @@ -487,7 +487,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'html_type' => 'File', 'is_search_range' => '0', 'id' => $this->getCustomFieldID('file'), - 'label' => 'Custom Field', + 'label' => 'My file', 'groupTitle' => 'Custom Group', 'default_value' => NULL, 'custom_group_id' => $customGroupID, @@ -499,10 +499,10 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '0', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, - 'column_name' => 'custom_field_' . $this->getCustomFieldID('file'), - 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.custom_field_' . $this->getCustomFieldID('file'), + 'column_name' => 'my_file_' . $this->getCustomFieldID('file'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.my_file_' . $this->getCustomFieldID('file'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', ], @@ -531,7 +531,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'enter_text_here_' . $this->getCustomFieldID('text'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.enter_text_here_' . $this->getCustomFieldID('text'), @@ -564,7 +564,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => $this->callAPISuccessGetValue('CustomField', ['id' => $this->getCustomFieldID('select_string'), 'return' => 'option_group_id']), 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'pick_color_' . $this->getCustomFieldID('select_string'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.pick_color_' . $this->getCustomFieldID('select_string'), @@ -632,7 +632,7 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => 'test_link_' . $this->getCustomFieldID('link'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.test_link_' . $this->getCustomFieldID('link'), @@ -664,13 +664,45 @@ class CRM_Core_BAO_CustomFieldTest extends CiviUnitTestCase { 'option_group_id' => NULL, 'date_format' => NULL, 'time_format' => NULL, - 'is_required' => '1', + 'is_required' => 0, 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, 'column_name' => $this->getCustomFieldColumnName('int'), 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('int'), 'extends_table' => 'civicrm_contact', 'search_table' => 'contact_a', ], + $this->getCustomFieldName('contact_reference') => [ + 'name' => $this->getCustomFieldName('contact_reference'), + 'type' => CRM_Utils_Type::T_INT, + 'title' => 'Contact reference field', + 'headerPattern' => '//', + 'import' => 1, + 'custom_field_id' => $this->getCustomFieldID('contact_reference'), + 'options_per_line' => NULL, + 'text_length' => NULL, + 'data_type' => 'ContactReference', + 'html_type' => 'Autocomplete-Select', + 'is_search_range' => '0', + 'id' => $this->getCustomFieldID('contact_reference'), + 'label' => 'Contact reference field', + 'groupTitle' => 'Custom Group', + 'default_value' => NULL, + 'custom_group_id' => $customGroupID, + 'extends' => 'Contact', + 'extends_entity_column_value' => NULL, + 'extends_entity_column_id' => NULL, + 'is_view' => '0', + 'is_multiple' => '0', + 'option_group_id' => NULL, + 'date_format' => NULL, + 'time_format' => NULL, + 'is_required' => 0, + 'table_name' => 'civicrm_value_custom_group_' . $customGroupID, + 'column_name' => $this->getCustomFieldColumnName('contact_reference'), + 'where' => 'civicrm_value_custom_group_' . $customGroupID . '.' . $this->getCustomFieldColumnName('contact_reference'), + 'extends_table' => 'civicrm_contact', + 'search_table' => 'contact_a', + ], ]; $this->assertEquals($expected, CRM_Core_BAO_CustomField::getFieldsForImport()); } diff --git a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php index 6830c8a517..e19100d8b9 100644 --- a/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php +++ b/tests/phpunit/CRMTraits/Custom/CustomDataTrait.php @@ -113,53 +113,14 @@ trait CRMTraits_Custom_CustomDataTrait { public function createCustomFieldsOfAllTypes() { $customGroupID = $this->ids['CustomGroup']['Custom Group']; $ids = []; - $customField = $this->createTextCustomField(['custom_group_id' => $customGroupID]); - $ids['text'] = $customField['id']; - - if ((!empty($this->entity) && $this->entity !== 'Contribution') || empty($this->entity)) { - $customField = $this->createSelectCustomField(['custom_group_id' => $customGroupID]); - $ids['select_string'] = $customField['id']; - } - - $customField = $this->createDateCustomField(['custom_group_id' => $customGroupID]); - $ids['select_date'] = $customField['id']; - - $customField = $this->createIntCustomField(['custom_group_id' => $customGroupID]); - $ids['int'] = $customField['id']; - - $params = [ - 'custom_group_id' => $customGroupID, - 'name' => 'test_link', - 'label' => 'test_link', - 'html_type' => 'Link', - 'data_type' => 'Link', - 'default_value' => 'http://civicrm.org', - 'weight' => 4, - 'is_required' => 1, - 'is_searchable' => 0, - 'is_active' => 1, - ]; - - $customField = $this->callAPISuccess('custom_field', 'create', $params); - - $ids['link'] = $customField['id']; - $fileField = $this->customFieldCreate([ - 'custom_group_id' => $customGroupID, - 'data_type' => 'File', - 'html_type' => 'File', - 'default_value' => '', - ]); - - $ids['file'] = $fileField['id']; - $ids['country'] = $this->customFieldCreate([ - 'custom_group_id' => $customGroupID, - 'data_type' => 'Country', - 'html_type' => 'Select Country', - 'default_value' => '', - 'label' => 'Country', - 'option_type' => 0, - ])['id']; - + $ids['text'] = (int) $this->createTextCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['select_string'] = (int) $this->createSelectCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['select_date'] = (int) $this->createDateCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['int'] = (int) $this->createIntCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['link'] = (int) $this->createLinkCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['file'] = (int) $this->createFileCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['country'] = (int) $this->createCountryCustomField(['custom_group_id' => $customGroupID])['id']; + $ids['contact_reference'] = $this->createContactReferenceCustomField(['custom_group_id' => $customGroupID])['id']; return $ids; } @@ -202,21 +163,23 @@ trait CRMTraits_Custom_CustomDataTrait { * @return array */ protected function createIntCustomField($params = []) { - $params = array_merge([ - 'label' => 'Enter integer here', - 'html_type' => 'Text', - 'data_type' => 'Int', - 'default_value' => '4', - 'weight' => 1, - 'is_required' => 1, - 'sequential' => 1, - 'is_searchable' => 1, - 'is_search_range' => 1, - ], $params); - + $params = array_merge($this->getFieldsValuesByType('Int'), $params); return $this->callAPISuccess('CustomField', 'create', $params)['values'][0]; } + /** + * Create a custom text fields. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createContactReferenceCustomField($params = []) { + $params = array_merge($this->getFieldsValuesByType('ContactReference'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } + /** * Create a custom text fields. * @@ -226,64 +189,60 @@ trait CRMTraits_Custom_CustomDataTrait { * @return array */ protected function createTextCustomField($params = []) { - $params = array_merge([ - 'label' => 'Enter text here', - 'html_type' => 'Text', - 'data_type' => 'String', - 'default_value' => 'xyz', - 'weight' => 1, - 'is_required' => 1, - 'sequential' => 1, - 'is_searchable' => 1, - 'text_length' => 300, - ], $params); + $params = array_merge($this->getFieldsValuesByType('String'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } - return $this->callAPISuccess('CustomField', 'create', $params)['values'][0]; + /** + * Create a custom text fields. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createLinkCustomField($params = []) { + $params = array_merge($this->getFieldsValuesByType('Link'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; } /** - * Create custom select field. + * Create a custom text fields. * * @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, - ], - ]; + protected function createCountryCustomField($params = []) { + $params = array_merge($this->getFieldsValuesByType('Country'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } - $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); + /** + * Create a custom text fields. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createFileCustomField($params = []) { + $params = array_merge($this->getFieldsValuesByType('File'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } - $customField = $this->callAPISuccess('custom_field', 'create', $params); - return $customField['values'][$customField['id']]; + /** + * Create custom select field. + * + * @param array $params + * Parameter overrides, must include custom_group_id. + * + * @return array + */ + protected function createSelectCustomField(array $params): array { + $params = array_merge($this->getFieldsValuesByType('String', 'Select'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; } /** @@ -294,20 +253,130 @@ trait CRMTraits_Custom_CustomDataTrait { * @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, + $params = array_merge($this->getFieldsValuesByType('Date'), $params); + return $this->callAPISuccess('custom_field', 'create', $params)['values'][0]; + } + + /** + * Get default field values for the type of field. + * + * @param $dataType + * @param string $htmlType + * + * @return mixed + */ + public function getFieldsValuesByType($dataType, $htmlType = 'default') { + $values = $this->getAvailableFieldCombinations()[$dataType]; + return array_merge([ 'is_searchable' => 1, - 'is_search_range' => 1, - 'time_format' => 1, - ], $params); + 'sequential' => 1, + 'default_value' => '', + 'is_required' => 0, + ], array_merge($values['default'], $values[$htmlType]) + ); + } - $customField = $this->callAPISuccess('custom_field', 'create', $params); - return $customField['values'][$customField['id']]; + /** + * Get data available for custom fields. + * + * The 'default' key holds general values. Where more than one html type is an option + * then the any values that differ to the defaults are keyed by html key. + * + * The order below is consistent with the UI. + * + * @return array + */ + protected function getAvailableFieldCombinations() { + return [ + 'String' => [ + 'default' => [ + 'label' => 'Enter text here', + 'html_type' => 'Text', + 'data_type' => 'String', + 'default_value' => 'xyz', + 'text_length' => 300, + ], + 'Select' => [ + 'label' => 'Pick Color', + 'html_type' => 'Select', + 'data_type' => 'String', + 'text_length' => '', + 'default_value' => '', + 'option_values' => [ + [ + '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, + ], + ], + ], + ], + 'Int' => [ + 'default' => [ + 'label' => 'Enter integer here', + 'html_type' => 'Text', + 'data_type' => 'Int', + 'default_value' => '4', + 'is_search_range' => 1, + ], + ], + 'Date' => [ + 'default' => [ + 'name' => 'test_date', + 'label' => 'Test Date', + 'html_type' => 'Select Date', + 'data_type' => 'Date', + 'default_value' => '20090711', + 'weight' => 3, + 'is_search_range' => 1, + 'time_format' => 1, + ], + ], + 'Country' => [ + 'default' => [ + 'data_type' => 'Country', + 'html_type' => 'Select Country', + 'label' => 'Country', + 'option_type' => 0, + ], + ], + 'File' => [ + 'default' => [ + 'label' => 'My file', + 'data_type' => 'File', + 'html_type' => 'File', + ], + ], + 'Link' => [ + 'default' => [ + 'name' => 'test_link', + 'label' => 'test_link', + 'html_type' => 'Link', + 'data_type' => 'Link', + 'default_value' => 'http://civicrm.org', + ], + ], + 'ContactReference' => [ + 'default' => [ + 'label' => 'Contact reference field', + 'html_type' => 'Autocomplete-Select', + 'data_type' => 'ContactReference', + ], + ], + ]; } } diff --git a/tests/phpunit/api/v3/ContactTest.php b/tests/phpunit/api/v3/ContactTest.php index 85457a2c34..8ea8a587f4 100644 --- a/tests/phpunit/api/v3/ContactTest.php +++ b/tests/phpunit/api/v3/ContactTest.php @@ -3893,7 +3893,9 @@ class api_v3_ContactTest extends CiviUnitTestCase { /** * Test merging 2 contacts with custom fields. * + * @throws \API_Exception * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException */ public function testMergeCustomFields() { $contact1 = $this->individualCreate(); @@ -3913,6 +3915,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { $dateField = $this->getCustomFieldName('select_date'); $selectField = $this->getCustomFieldName('select_string'); $countryField = $this->getCustomFieldName('country'); + $referenceField = $this->getCustomFieldName('contact_reference'); $countriesByName = array_flip(CRM_Core_PseudoConstant::country(FALSE, FALSE)); $customFieldValues = [ @@ -3922,6 +3925,7 @@ class api_v3_ContactTest extends CiviUnitTestCase { $dateField => '2018-01-01 17:10:56', $selectField => 'G', $countryField => $countriesByName['New Zealand'], + $referenceField => $this->householdCreate(), ]; $this->callAPISuccess('Contact', 'create', array_merge([ 'id' => $contact1, diff --git a/tests/phpunit/api/v3/ContributionTest.php b/tests/phpunit/api/v3/ContributionTest.php index b727d7cf61..d18afe4039 100644 --- a/tests/phpunit/api/v3/ContributionTest.php +++ b/tests/phpunit/api/v3/ContributionTest.php @@ -4494,8 +4494,14 @@ class api_v3_ContributionTest extends CiviUnitTestCase { $this->assertNotContains('US Dollar', $result['values']); } + /** + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ public function testSetCustomDataInCreateAndHook() { - $this->createCustomGroupWithFieldsOfAllTypes(); + $this->createCustomGroupWithFieldOfType([], 'int'); + $this->ids['CustomField']['text'] = (int) $this->createTextCustomField(['custom_group_id' => $this->ids['CustomGroup']['Custom Group']])['id']; $this->hookClass->setHook('civicrm_post', [ $this, 'civicrmPostContributionCustom', @@ -4507,10 +4513,20 @@ class api_v3_ContributionTest extends CiviUnitTestCase { 'id' => $contribution['id'], 'return' => ['id', 'custom_' . $this->ids['CustomField']['text'], 'custom_' . $this->ids['CustomField']['int']], ]); - $this->assertEquals(5, $getContribution['values'][$contribution['id']]['custom_' . $this->ids['CustomField']['int']]); + $this->assertEquals(5, $getContribution['values'][$contribution['id']][$this->getCustomFieldName('int')]); $this->assertEquals('Some Text', $getContribution['values'][$contribution['id']]['custom_' . $this->ids['CustomField']['text']]); } + /** + * Implement post hook. + * + * @param string $op + * @param string $objectName + * @param int $objectId + * @param \CRM_Core_DAO $objectRef + * + * @throws \CRM_Core_Exception + */ public function civicrmPostContributionCustom($op, $objectName, $objectId, &$objectRef) { if ($objectName === 'Contribution' && $op === 'create') { $this->callAPISuccess('Contribution', 'create', [