Merge pull request #20947 from colemanw/sqlFunctions
[civicrm-core.git] / tests / phpunit / CRM / Contact / SelectorTest.php
index b1486aab5cc239b7ff5d7013eee7b5d2c4fbee3b..2841d6f1f4510b33284b2e53c3d4abecae039606 100644 (file)
@@ -32,6 +32,10 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
    * @throws \Exception
    */
   public function testSelectorQuery($dataSet) {
+    $tag = $this->callAPISuccess('Tag', 'create', [
+      'name' => 'Test Tag Name' . uniqid(),
+      'parent_id' => 1,
+    ]);
     if (!empty($dataSet['limitedPermissions'])) {
       CRM_Core_Config::singleton()->userPermissionClass->permissions = [
         'access CiviCRM',
@@ -61,6 +65,9 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
     foreach ($dataSet['expected_query'] as $index => $queryString) {
       $this->assertLike($this->strWrangle($queryString), $this->strWrangle($sql[$index]));
     }
+    if (!empty($dataSet['where_contains'])) {
+      $this->assertStringContainsString($this->strWrangle(str_replace('@tagid', $tag['id'], $dataSet['where_contains'])), $this->strWrangle($sql[2]));
+    }
     // Ensure that search builder return individual contact as per criteria
     if ($dataSet['context'] === 'builder') {
       $contactID = $this->individualCreate(['first_name' => 'James', 'last_name' => 'Bond']);
@@ -75,6 +82,11 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
         $this->assertEquals($contactID, $rows[0]['source_contact_id']);
       }
       else {
+        $this->callAPISuccess('EntityTag', 'create', [
+          'entity_id' => $contactID,
+          'tag_id' => $tag['id'],
+          'entity_table' => 'civicrm_contact',
+        ]);
         $this->callAPISuccess('Address', 'create', [
           'contact_id' => $contactID,
           'location_type_id' => 'Home',
@@ -99,6 +111,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
     if (!empty($dataSet['limitedPermissions'])) {
       $this->cleanUpAfterACLs();
     }
+    $this->callAPISuccess('Tag', 'delete', ['id' => $tag['id']]);
   }
 
   /**
@@ -162,7 +175,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
     //Check if email column contains (On Hold) string.
     foreach ($rows[$contactID] as $key => $value) {
       if (strpos($key, 'email') !== FALSE) {
-        $this->assertContains("(On Hold)", (string) $value);
+        $this->assertStringContainsString("(On Hold)", (string) $value);
       }
     }
   }
@@ -248,6 +261,22 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
           'expected_query' => [],
         ],
       ],
+      [
+        [
+          'description' => 'Tag Equals Test',
+          'class' => 'CRM_Contact_Selector',
+          'settings' => [],
+          'form_values' => [['contact_type', '=', 'Individual', 1, 0], ['tag', '=', '1', 1, 0]],
+          'params' => [],
+          'return_properties' => NULL,
+          'context' => 'builder',
+          'action' => CRM_Core_Action::NONE,
+          'includeContactIds' => NULL,
+          'searchDescendentGroups' => FALSE,
+          'expected_query' => [],
+          'where_contains' => 'tag_id IN ( 1,@tagid )',
+        ],
+      ],
       [
         [
           'description' => 'Normal default behaviour',
@@ -441,7 +470,7 @@ class CRM_Contact_SelectorTest extends CiviUnitTestCase {
           'searchDescendentGroups' => FALSE,
           'expected_query' => [
             0 => 'SELECT contact_a.id as contact_id, contact_a.contact_type as `contact_type`, contact_a.contact_sub_type as `contact_sub_type`, contact_a.sort_name as `sort_name`, contact_a.display_name as `display_name`, contact_a.do_not_email as `do_not_email`, contact_a.do_not_phone as `do_not_phone`, contact_a.do_not_mail as `do_not_mail`, contact_a.do_not_sms as `do_not_sms`, contact_a.do_not_trade as `do_not_trade`, contact_a.is_opt_out as `is_opt_out`, contact_a.legal_identifier as `legal_identifier`, contact_a.external_identifier as `external_identifier`, contact_a.nick_name as `nick_name`, contact_a.legal_name as `legal_name`, contact_a.image_URL as `image_URL`, contact_a.preferred_communication_method as `preferred_communication_method`, contact_a.preferred_language as `preferred_language`, contact_a.preferred_mail_format as `preferred_mail_format`, contact_a.first_name as `first_name`, contact_a.middle_name as `middle_name`, contact_a.last_name as `last_name`, contact_a.prefix_id as `prefix_id`, contact_a.suffix_id as `suffix_id`, contact_a.formal_title as `formal_title`, contact_a.communication_style_id as `communication_style_id`, contact_a.job_title as `job_title`, contact_a.gender_id as `gender_id`, contact_a.birth_date as `birth_date`, contact_a.is_deceased as `is_deceased`, contact_a.deceased_date as `deceased_date`, contact_a.household_name as `household_name`, IF ( contact_a.contact_type = \'Individual\', NULL, contact_a.organization_name ) as organization_name, contact_a.sic_code as `sic_code`, contact_a.is_deleted as `contact_is_deleted`, IF ( contact_a.contact_type = \'Individual\', contact_a.organization_name, NULL ) as current_employer, civicrm_address.id as address_id, civicrm_address.street_address as `street_address`, civicrm_address.supplemental_address_1 as `supplemental_address_1`, civicrm_address.supplemental_address_2 as `supplemental_address_2`, civicrm_address.supplemental_address_3 as `supplemental_address_3`, civicrm_address.city as `city`, civicrm_address.postal_code_suffix as `postal_code_suffix`, civicrm_address.postal_code as `postal_code`, civicrm_address.geo_code_1 as `geo_code_1`, civicrm_address.geo_code_2 as `geo_code_2`, civicrm_address.state_province_id as state_province_id, civicrm_address.country_id as country_id, civicrm_phone.id as phone_id, civicrm_phone.phone_type_id as phone_type_id, civicrm_phone.phone as `phone`, civicrm_email.id as email_id, civicrm_email.email as `email`, civicrm_email.on_hold as `on_hold`, civicrm_im.id as im_id, civicrm_im.provider_id as provider_id, civicrm_im.name as `im`, civicrm_worldregion.id as worldregion_id, civicrm_worldregion.name as `world_region`',
-            2 => 'WHERE displayRelType.relationship_type_id = 1
+            2 => 'WHERE 1 AND displayRelType.relationship_type_id = 1
 AND   displayRelType.is_active = 1
 AND ( 1 ) AND (contact_a.is_deleted = 0)',
           ],
@@ -635,7 +664,7 @@ AND ( 1 ) AND (contact_a.is_deleted = 0)',
       FALSE, FALSE
     );
     //Check if custom table is included in $query->_tables.
-    $this->assertTrue(in_array($cgTableName, array_keys($query->_tables)));
+    $this->assertTrue(array_key_exists($cgTableName, $query->_tables));
     //Assert if from clause joins the custom table.
     $this->assertTrue(strpos($query->_fromClause, $cgTableName) !== FALSE);
     $this->callAPISuccess('CustomField', 'delete', ['id' => $customField['id']]);