dev/core#2502 Add unit test to lock in fix on tag searching in search builder
authorSeamus Lee <seamuslee001@gmail.com>
Fri, 9 Apr 2021 00:55:57 +0000 (10:55 +1000)
committerSeamus Lee <seamuslee001@gmail.com>
Fri, 9 Apr 2021 00:56:21 +0000 (10:56 +1000)
tests/phpunit/CRM/Contact/SelectorTest.php

index b1486aab5cc239b7ff5d7013eee7b5d2c4fbee3b..1fcfaffbdf30880aa797b819894f8dd547d86d78 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->assertContains($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']]);
   }
 
   /**
@@ -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',