4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Entity
;
22 use api\v
4\UnitTestCase
;
23 use Civi\Api4\Contact
;
29 class SavedSearchTest
extends UnitTestCase
{
32 * @throws \API_Exception
33 * @throws \Civi\API\Exception\NotImplementedException
35 public function testContactSmartGroup(): void
{
36 $in = Contact
::create(FALSE)->addValue('first_name', 'yes')->addValue('do_not_phone', TRUE)->execute()->first();
37 $out = Contact
::create(FALSE)->addValue('first_name', 'no')->addValue('do_not_phone', FALSE)->execute()->first();
39 $savedSearch = civicrm_api4('SavedSearch', 'create', [
41 'api_entity' => 'Contact',
45 ['do_not_phone', '=', TRUE],
50 'group' => ['Group', 'create', ['values' => ['title' => 'Contact Test', 'saved_search_id' => '$id']], 0],
54 $ins = civicrm_api4('Contact', 'get', [
55 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]],
57 $this->assertCount(1, $ins);
58 $this->assertArrayHasKey($in['id'], (array) $ins);
60 $outs = civicrm_api4('Contact', 'get', [
61 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]],
63 $this->assertArrayHasKey($out['id'], (array) $outs);
64 $this->assertArrayNotHasKey($in['id'], (array) $outs);
67 public function testEmailSmartGroup() {
68 $in = Contact
::create(FALSE)->addValue('first_name', 'yep')->execute()->first();
69 $out = Contact
::create(FALSE)->addValue('first_name', 'nope')->execute()->first();
70 $email = uniqid() . '@' . uniqid();
71 Email
::create(FALSE)->addValue('email', $email)->addValue('contact_id', $in['id'])->execute();
73 $savedSearch = civicrm_api4('SavedSearch', 'create', [
75 'api_entity' => 'Email',
78 'select' => ['contact_id'],
80 ['email', '=', $email],
85 'group' => ['Group', 'create', ['values' => ['title' => 'Email Test', 'saved_search_id' => '$id']], 0],
89 $ins = civicrm_api4('Contact', 'get', [
90 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]],
92 $this->assertCount(1, $ins);
93 $this->assertArrayHasKey($in['id'], (array) $ins);
95 $outs = civicrm_api4('Contact', 'get', [
96 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]],
98 $this->assertArrayHasKey($out['id'], (array) $outs);
99 $this->assertArrayNotHasKey($in['id'], (array) $outs);
102 public function testSmartGroupWithHaving() {
103 $in = Contact
::create(FALSE)->addValue('first_name', 'yes')->addValue('last_name', 'siree')->execute()->first();
104 $in2 = Contact
::create(FALSE)->addValue('first_name', 'yessir')->addValue('last_name', 'ee')->execute()->first();
105 $out = Contact
::create(FALSE)->addValue('first_name', 'yess')->execute()->first();
107 $savedSearch = civicrm_api4('SavedSearch', 'create', [
109 'api_entity' => 'Contact',
112 'select' => ['id', 'CONCAT(first_name, last_name) AS whole_name'],
114 ['id', '>=', $in['id']],
117 ['whole_name', '=', 'yessiree'],
122 'group' => ['Group', 'create', ['values' => ['title' => 'Having Test', 'saved_search_id' => '$id']], 0],
126 $ins = civicrm_api4('Contact', 'get', [
127 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]],
129 $this->assertCount(2, $ins);
130 $this->assertArrayHasKey($in['id'], (array) $ins);
131 $this->assertArrayHasKey($in2['id'], (array) $ins);
133 $outs = civicrm_api4('Contact', 'get', [
134 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]],
136 $this->assertArrayHasKey($out['id'], (array) $outs);
137 $this->assertArrayNotHasKey($in['id'], (array) $outs);
138 $this->assertArrayNotHasKey($in2['id'], (array) $outs);
141 public function testMultipleSmartGroups() {
142 $inGroup = $outGroup = [];
143 $inName = uniqid('inGroup');
144 $outName = uniqid('outGroup');
145 for ($i = 0; $i < 10; ++
$i) {
146 $inGroup[] = Contact
::create(FALSE)
147 ->setValues(['first_name' => "$i", 'last_name' => $inName])
148 ->execute()->first()['id'];
149 $outGroup[] = Contact
::create(FALSE)
150 ->setValues(['first_name' => "$i", 'last_name' => $outName])
151 ->execute()->first()['id'];
154 $parentGroupId = \Civi\Api4\Group
::create(FALSE)
155 ->setValues(['title' => uniqid()])
156 ->execute()->first()['id'];
158 $savedSearchA = civicrm_api4('SavedSearch', 'create', [
160 'api_entity' => 'Contact',
164 ['last_name', '=', $inName],
169 'group' => ['Group', 'create', ['values' => ['parents' => [$parentGroupId], 'title' => 'In A Test', 'saved_search_id' => '$id']], 0],
173 $savedSearchB = civicrm_api4('SavedSearch', 'create', [
175 'api_entity' => 'Contact',
179 ['last_name', 'IN', [$inName, $outName]],
180 ['first_name', '>', '4'],
185 'group' => ['Group', 'create', ['values' => ['parents' => [$parentGroupId], 'title' => 'In B Test', 'saved_search_id' => '$id']], 0],
189 $bothGroups = civicrm_api4('Contact', 'get', [
190 'where' => [['groups:name', 'IN', [$savedSearchA['group']['name'], $savedSearchB['group']['name']]]],
192 $this->assertCount(15, $bothGroups);
194 // Parent group includes both groups a & b so should give the same results as above
195 $parentGroup = civicrm_api4('Contact', 'get', [
196 'where' => [['groups', 'IN', [$parentGroupId]]],
198 $this->assertCount(15, $parentGroup);
200 $aNotB = civicrm_api4('Contact', 'get', [
202 ['groups:name', 'IN', [$savedSearchA['group']['name']]],
203 ['groups:name', 'NOT IN', [$savedSearchB['group']['name']]],
206 $this->assertCount(5, $aNotB);