From fe128a5aec07b9f1d653a896f52865e08c76b838 Mon Sep 17 00:00:00 2001 From: Monish Deb Date: Fri, 5 Jun 2020 23:50:46 +0530 Subject: [PATCH] core#1795: Searchable Parent tags --- CRM/Contact/BAO/Query.php | 14 +++++++++- tests/phpunit/CRM/Contact/BAO/QueryTest.php | 7 ++++- .../CRM/Contact/BAO/QueryTestDataProvider.php | 28 +++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 3cabd6f2e0..8428b99720 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -3268,7 +3268,6 @@ WHERE $smartGroupClause public function tag(&$values) { list($name, $op, $value, $grouping, $wildcard) = $values; - list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, ['onlyActive' => FALSE]); // API/Search Builder format array(operator => array(values)) if (is_array($value)) { if (in_array(key($value), CRM_Core_DAO::acceptedSQLOperators(), TRUE)) { @@ -3280,6 +3279,19 @@ WHERE $smartGroupClause } } + if (strpos($op, 'NULL') || strpos($op, 'EMPTY')) { + $value = NULL; + } + + $tagTree = CRM_Core_BAO_Tag::getChildTags(); + foreach ((array) $value as $tagID) { + if (!empty($tagTree[$tagID])) { + $value = array_unique(array_merge($value, $tagTree[$tagID])); + } + } + + list($qillop, $qillVal) = self::buildQillForFieldValue('CRM_Core_DAO_EntityTag', "tag_id", $value, $op, ['onlyActive' => FALSE]); + // implode array, then remove all spaces $value = str_replace(' ', '', implode(',', (array) $value)); if (!empty($value)) { diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTest.php b/tests/phpunit/CRM/Contact/BAO/QueryTest.php index f6c93b4dbd..69495e08e7 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTest.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTest.php @@ -58,7 +58,9 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { $tag7 = $this->ids['Tag'][7] = $this->tagCreate(['name' => 'Test Tag 7', 'description' => 'Test Tag 7'])['id']; $tag9 = $this->ids['Tag'][9] = $this->tagCreate(['name' => 'Test Tag 9', 'description' => 'Test Tag 9'])['id']; - $this->tagCreate(['name' => 'Test Tag 10']); + $tag10 = $this->ids['Tag'][10] = $this->tagCreate(['name' => 'Test Tag 10', 'description' => 'Test Tag 10', 'parent_id' => $tag9])['id']; + $tag11 = $this->ids['Tag'][11] = $this->tagCreate(['name' => 'Test Tag 11', 'description' => 'Test Tag 11', 'parent_id' => $tag10])['id']; + $groups = [ 3 => ['name' => 'Test Group 3'], 4 => ['name' => 'Test Smart Group 4', 'saved_search_id' => 1], @@ -118,6 +120,8 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { 'api.entity_tag.create' => ['tag_id' => $tag9], 'api.entity_tag.create.2' => ['tag_id' => $tag7], ], + ['first_name' => 'Test', 'last_name' => 'Test Contact 25', 'api.entity_tag.create' => ['tag_id' => $tag10]], + ['first_name' => 'Test', 'last_name' => 'Test Contact 26', 'api.entity_tag.create' => ['tag_id' => $tag11]], ]; foreach ($individuals as $individual) { $this->ids['Contact'][$individual['last_name']] = $this->individualCreate($individual); @@ -189,6 +193,7 @@ class CRM_Contact_BAO_QueryTest extends CiviUnitTestCase { foreach ($ids as $id) { $expectedIDs[] = $this->ids['Contact']['Test Contact ' . $id]; } + $this->assertEquals($expectedIDs, $contacts); } diff --git a/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php b/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php index a193317701..6c40d94231 100644 --- a/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php +++ b/tests/phpunit/CRM/Contact/BAO/QueryTestDataProvider.php @@ -141,6 +141,8 @@ class CRM_Contact_BAO_QueryTestDataProvider implements Iterator { '20', '22', '24', + '25', + '26', ], ], // Include tags 7 and 9 @@ -159,6 +161,32 @@ class CRM_Contact_BAO_QueryTestDataProvider implements Iterator { '22', '23', '24', + '25', + '26', + ], + ], + // Include tags 7 and 10 + [ + 'fv' => ['tag' => ['7', '10']], + 'id' => [ + '11', + '12', + '15', + '16', + '19', + '20', + '23', + '24', + '25', + '26', + ], + ], + // Include tags 10 and 11 + [ + 'fv' => ['tag' => ['10', '11']], + 'id' => [ + '25', + '26', ], ], // gender_id 1 = 'Female' -- 2.25.1