Merge pull request #15834 from francescbassas/patch-16
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / CustomQueryTest.php
index dc9f057997f5f08b6ad1c1ee817947d30d663fe8..32e62037e4c119793a907b3de3226ce752acd90d 100644 (file)
@@ -52,8 +52,8 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
       "civicrm_value_testsearchcus_1.date_field_2 BETWEEN '" . date('Y') . "0101000000' AND '" . date('Y') . "1231235959'",
       $queryObj->_where[0][0]
     );
-    $this->assertEquals("date field is This calendar year (between January 1st, " . date('Y') . " 12:00 AM and December 31st, " . date('Y') . " 11:59 PM)", $queryObj->_qill[0][0]);
-    $queryObj = new CRM_Core_BAO_CustomQuery($params);
+    $this->assertEquals('date field is This calendar year (between January 1st, ' . date('Y') . " 12:00 AM and December 31st, " . date('Y') . " 11:59 PM)", $queryObj->_qill[0][0]);
+    $queryObj = new CRM_Contact_BAO_Query($params);
     $this->assertEquals([
       'id' => $dateCustomField['id'],
       'label' => 'date field',
@@ -84,8 +84,78 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
       'name' => 'custom_' . $dateCustomField['id'],
       'type' => 4,
       'where' => 'civicrm_value_testsearchcus_' . $ids['custom_group_id'] . '.date_field_' . $dateCustomField['id'],
-    ], $queryObj->getFields()[$dateCustomField['id']]);
+      'import' => 1,
+    ], $queryObj->getFieldSpec('custom_' . $dateCustomField['id']));
+
+  }
+
+  /**
+   * Test filtering by the renamed custom date fields.
+   *
+   * The conversion to date picker will result int these fields
+   * being renamed _high & _low and needing to return correctly.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSearchCustomDataDateHighLow() {
+    $this->createCustomGroupWithFieldOfType([], 'date');
+    $dateCustomFieldName = $this->getCustomFieldName('date');
+    // Assigning the relevant form value to be within a custom key is normally done in
+    // build field params. It would be better if it were all done in convertFormValues
+    // but for now we just imitate it.
+    $formValues = [
+      $dateCustomFieldName . '_low' => '2014-06-06',
+      $dateCustomFieldName . '_high' => '2015-06-06',
+    ];
+
+    $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+    $queryObject = new CRM_Contact_BAO_Query($params);
+    $queryObject->query();
+    $this->assertEquals(
+      '( civicrm_value_group_with_fi_1.' . $this->getCustomFieldColumnName('date') . ' >= \'20140606000000\' ) AND
+( civicrm_value_group_with_fi_1.' . $this->getCustomFieldColumnName('date') . ' <= \'20150606235959\' )',
+      trim($queryObject->_where[0][0])
+    );
+    $this->assertEquals('Test Date - greater than or equal to "June 6th, 2014 12:00 AM" AND less than or equal to "June 6th, 2015 11:59 PM"', $queryObject->_qill[0][0]);
+    $this->assertEquals(1, $queryObject->_whereTables['civicrm_contact']);
+    $this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
+  }
+
+  /**
+   * Test filtering by the renamed custom date fields.
+   *
+   * The conversion to date picker will result int these fields
+   * being renamed _high & _low and needing to return correctly.
+   *
+   * @throws \CRM_Core_Exception
+   */
+  public function testSearchCustomDataDateLowWithPermsInPlay() {
+    $this->createLoggedInUser();
+    CRM_Core_Config::singleton()->userPermissionClass->permissions = ['view all contacts', 'access all custom data'];
+    $this->createCustomGroupWithFieldOfType([], 'date');
+    $dateCustomFieldName = $this->getCustomFieldName('date');
+    // Assigning the relevant form value to be within a custom key is normally done in
+    // build field params. It would be better if it were all done in convertFormValues
+    // but for now we just imitate it.
+    $formValues = [
+      $dateCustomFieldName . '_low' => '2014-06-06',
+    ];
 
+    $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+    $queryObject = new CRM_Contact_BAO_Query($params);
+    $queryObject->query();
+    $this->assertEquals(
+      'civicrm_value_group_with_fi_1.' . $this->getCustomFieldColumnName('date') . ' >= \'20140606000000\'',
+      trim($queryObject->_where[0][0])
+    );
+    $this->assertEquals(
+      'FROM civicrm_contact contact_a   LEFT JOIN civicrm_address ON ( contact_a.id = civicrm_address.contact_id AND civicrm_address.is_primary = 1 )  LEFT JOIN civicrm_country ON ( civicrm_address.country_id = civicrm_country.id )  LEFT JOIN civicrm_email ON (contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1)  LEFT JOIN civicrm_phone ON (contact_a.id = civicrm_phone.contact_id AND civicrm_phone.is_primary = 1)  LEFT JOIN civicrm_im ON (contact_a.id = civicrm_im.contact_id AND civicrm_im.is_primary = 1)  LEFT JOIN civicrm_worldregion ON civicrm_country.region_id = civicrm_worldregion.id  
+LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id',
+      trim($queryObject->_fromClause)
+    );
+    $this->assertEquals('Test Date - greater than or equal to "June 6th, 2014 12:00 AM"', $queryObject->_qill[0][0]);
+    $this->assertEquals(1, $queryObject->_whereTables['civicrm_contact']);
+    $this->assertEquals('LEFT JOIN ' . $this->getCustomGroupTable() . ' ON ' . $this->getCustomGroupTable() . '.entity_id = `contact_a`.id', trim($queryObject->_whereTables[$this->getCustomGroupTable()]));
   }
 
   /**
@@ -111,8 +181,8 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
       $dateCustomFieldName . '_to' => '2015-06-06',
     ];
 
-    $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues);
-    $queryObj = new CRM_Core_BAO_CustomQuery($params);
+    $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+    $queryObj = new CRM_Contact_BAO_Query($params);
     $queryObj->query();
     $this->assertEquals(
       'civicrm_value_testsearchcus_1.date_field_2 BETWEEN "20140606000000" AND "20150606235959"',
@@ -127,6 +197,8 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
 
   /**
    * Test filtering by relative custom data.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testSearchCustomDataFromTo() {
     $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
@@ -156,11 +228,11 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
         $customFieldName . '_to' => $to,
       ];
 
-      $params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
-      $queryObj = new CRM_Core_BAO_CustomQuery($params);
+      $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+      $queryObj = new CRM_Contact_BAO_Query($params);
       $queryObj->query();
       $this->assertEquals(
-        "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"",
+        'civicrm_value_testsearchcus_1.' . strtolower($type) . "_field_{$customField['id']} BETWEEN \"$from\" AND \"$to\"",
         $queryObj->_where[0][0]
       );
       $this->assertEquals($queryObj->_qill[0][0], "$type field BETWEEN $from, $to");
@@ -169,16 +241,20 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
 
   /**
    * Test filtering by relative custom data.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testSearchCustomDataFromAndTo() {
     $ids = $this->entityCustomGroupWithSingleFieldCreate(__FUNCTION__, 'ContactTestTest');
-    $datas = [
-      'Date' => '2015-06-06',
-      'Int' => 2,
-      'Float' => 12.123,
-      'Money' => 91.21,
+    $dataSet = [
+      'Date' => ['value' => '2015-06-06', 'sql_string' => '"20150606235959"', 'qill_string' => "'June 6th, 2015 11:59 PM'", 'qill_string_greater' => "'June 6th, 2015 12:00 AM'"],
+      // @todo - investigate the impact of using quotes on what should be an integer field.
+      'Int' => ['value' => 2, 'sql_string' => '"2"'],
+      'Float' => ['value' => 12.123, 'sql_string' => '"12.123"'],
+      'Money' => ['value' => 91.21],
     ];
-    foreach ($datas as $type => $data) {
+    foreach ($dataSet as $type => $values) {
+      $data = $values['value'];
       $isDate = ($type === 'Date');
       $customField = $this->customFieldCreate(
         [
@@ -191,49 +267,49 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
       );
       $customFieldName = 'custom_' . $customField['id'];
 
-      $expectedValue = ($isDate) ? '"20150606235959"' : (($type == 'Money') ? $data : "\"$data\"");
-      $expectedQillValue = ($isDate) ? "'June 6th, 2015 11:59 PM'" : $data;
+      $expectedValue = $values['sql_string'] ?? $data;
+      $expectedQillValue = $values['qill_string'] ?? $data;
+      $toQillValue = chr(226) . chr(137) . chr(164) . ' ' . $expectedQillValue;
+      $fromQillValue = chr(226) . chr(137) . chr(165) . ' ' . ($values['qill_string_greater'] ?? $expectedQillValue);
 
       // Assigning the relevant form value to be within a custom key is normally done in
       // build field params. It would be better if it were all done in convertFormValues
       // but for now we just imitate it.
 
-      //Scenrio 2 : TO date filter
+      //Scenario 2 : TO date filter
       $formValues = [
         $customFieldName . '_to' => $data,
       ];
 
-      $params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
-      $queryObj = new CRM_Core_BAO_CustomQuery($params);
+      $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+      $queryObj = new CRM_Contact_BAO_Query($params);
       $queryObj->query();
-      $wierdStringThatMeansGreaterEquals = chr(226) . chr(137) . chr(164);
 
       $this->assertEquals(
-        "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} <= $expectedValue",
+        'civicrm_value_testsearchcus_1.' . strtolower($type) . "_field_{$customField['id']} <= $expectedValue",
         $queryObj->_where[0][0]
       );
       $this->assertEquals($queryObj->_qill[0][0],
-        "$type field " . $wierdStringThatMeansGreaterEquals . " $expectedQillValue"
+        "$type field $toQillValue"
       );
 
-      //Scenrio 2 : FROM date filter
+      //Scenario 2 : FROM date filter
       $formValues = [
-        $customFieldName . '_from' => $data,
+        $customFieldName . '_from' => $values['value'],
       ];
 
-      $params = [$customField['id'] => CRM_Contact_BAO_Query::convertFormValues($formValues)];
-      $queryObj = new CRM_Core_BAO_CustomQuery($params);
+      $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+      $queryObj = new CRM_Contact_BAO_Query($params);
       $queryObj->query();
-      $wierdStringThatMeansLessThanEquals = chr(226) . chr(137) . chr(165);
 
       $expectedValue = ($isDate) ? '"20150606000000"' : $expectedValue;
-      $expectedQillValue = ($isDate) ? "'June 6th, 2015 12:00 AM'" : $expectedQillValue;
       $this->assertEquals(
-        "civicrm_value_testsearchcus_1." . strtolower($type) . "_field_{$customField['id']} >= $expectedValue",
+        'civicrm_value_testsearchcus_1.' . strtolower($type) . "_field_{$customField['id']} >= $expectedValue",
         $queryObj->_where[0][0]
       );
-      $this->assertEquals($queryObj->_qill[0][0],
-        "$type field " . $wierdStringThatMeansLessThanEquals . " $expectedQillValue"
+      $this->assertEquals(
+        "$type field $fromQillValue",
+        $queryObj->_qill[0][0]
       );
     }
   }
@@ -258,8 +334,8 @@ class CRM_Core_BAO_CustomQueryTest extends CiviUnitTestCase {
     // build field params. It would be better if it were all done in convertFormValues
     // but for now we just imitate it.
     $formValues = [$dateCustomFieldName => '2015-06-06'];
-    $params[$dateCustomField['id']] = CRM_Contact_BAO_Query::convertFormValues($formValues);
-    $queryObj = new CRM_Core_BAO_CustomQuery($params);
+    $params = CRM_Contact_BAO_Query::convertFormValues($formValues);
+    $queryObj = new CRM_Contact_BAO_Query($params);
     $queryObj->query();
 
     $this->assertEquals(