Unit test for custom date parsing
authoreileen <emcnaughton@wikimedia.org>
Thu, 8 Aug 2019 00:50:03 +0000 (12:50 +1200)
committereileen <emcnaughton@wikimedia.org>
Thu, 8 Aug 2019 01:29:21 +0000 (13:29 +1200)
Unit test for https://github.com/civicrm/civicrm-core/pull/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 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
    // it seems to ignore the latter in favour of the former - which seems wrong.

tests/phpunit/CRM/Contribute/Import/Parser/ContributionTest.php
tests/phpunit/CRMTraits/Custom/CustomDataTrait.php

index 44a6161911e77718727ee688dccfda7909123f60..a6b689ff4dd6cd99371430b19367eae4ff097041 100644 (file)
  */
 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.
@@ -142,6 +150,29 @@ class CRM_Contribute_Import_Parser_ContributionTest extends CiviUnitTestCase {
     $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.
    *
index f8dba7b5318867110c96306bea4ef1fc14c30dc4..a90b465d6fe53cf148f3efe3b08afe448fbc48d8 100644 (file)
@@ -72,7 +72,7 @@ trait CRMTraits_Custom_CustomDataTrait {
    * @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');
     }
@@ -86,6 +86,10 @@ trait CRMTraits_Custom_CustomDataTrait {
       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'];
   }
@@ -102,18 +106,7 @@ trait CRMTraits_Custom_CustomDataTrait {
     $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 = [
@@ -252,4 +245,26 @@ trait CRMTraits_Custom_CustomDataTrait {
     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']];
+  }
+
 }