[NFC][Test] cleanup on customDataTrait for tests
authoreileen <emcnaughton@wikimedia.org>
Wed, 8 Apr 2020 04:37:43 +0000 (16:37 +1200)
committereileen <emcnaughton@wikimedia.org>
Wed, 8 Apr 2020 07:10:46 +0000 (19:10 +1200)
tests/phpunit/CRM/Core/BAO/CustomFieldTest.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php
tests/phpunit/api/v3/ContactTest.php
tests/phpunit/api/v3/ContributionTest.php

index e0a1cf9b62ccb6bf2e6f8108e5a7574d6419dddd..0ed1ff2a1700c2231441a09975df81349760e3e2 100644 (file)
@@ -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());
   }
index 6830c8a517dce6e6757187d697a8fe9dde35ceaf..e19100d8b9d91b0517362ed2f2b3172a2e29c6cb 100644 (file)
@@ -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',
+        ],
+      ],
+    ];
   }
 
 }
index 85457a2c34e6eb2d5dcafd6cc26a83a1829d8fd5..8ea8a587f42436e5b9c2e21f66716b70e7ea81e2 100644 (file)
@@ -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,
index b727d7cf6133b1e1632cfba1b1141bbc376b286e..d18afe4039d6b7eb7bfa980e954dd4dcb6e463a5 100644 (file)
@@ -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', [