Commit | Line | Data |
---|---|---|
4e97c268 CW |
1 | <?php |
2 | ||
3 | /* | |
4 | +--------------------------------------------------------------------+ | |
5 | | Copyright CiviCRM LLC. All rights reserved. | | |
6 | | | | |
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 | +--------------------------------------------------------------------+ | |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
16 | * @copyright CiviCRM LLC https://civicrm.org/licensing | |
4e97c268 CW |
17 | */ |
18 | ||
19 | ||
20 | namespace api\v4\Entity; | |
21 | ||
22 | use api\v4\UnitTestCase; | |
23 | use Civi\Api4\Contact; | |
48102254 | 24 | use Civi\Api4\Email; |
4e97c268 CW |
25 | |
26 | /** | |
27 | * @group headless | |
28 | */ | |
29 | class SavedSearchTest extends UnitTestCase { | |
30 | ||
48102254 | 31 | public function testContactSmartGroup() { |
fe806431 CW |
32 | $in = Contact::create(FALSE)->addValue('first_name', 'yes')->addValue('do_not_phone', TRUE)->execute()->first(); |
33 | $out = Contact::create(FALSE)->addValue('first_name', 'no')->addValue('do_not_phone', FALSE)->execute()->first(); | |
4e97c268 CW |
34 | |
35 | $savedSearch = civicrm_api4('SavedSearch', 'create', [ | |
36 | 'values' => [ | |
37 | 'api_entity' => 'Contact', | |
38 | 'api_params' => [ | |
39 | 'version' => 4, | |
40 | 'where' => [ | |
41 | ['do_not_phone', '=', TRUE], | |
42 | ], | |
43 | ], | |
44 | ], | |
45 | 'chain' => [ | |
48102254 | 46 | 'group' => ['Group', 'create', ['values' => ['title' => 'Contact Test', 'saved_search_id' => '$id']], 0], |
4e97c268 CW |
47 | ], |
48 | ])->first(); | |
49 | ||
a1415a02 CW |
50 | $ins = civicrm_api4('Contact', 'get', [ |
51 | 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]], | |
52 | ])->indexBy('id'); | |
53 | $this->assertCount(1, $ins); | |
54 | $this->assertArrayHasKey($in['id'], (array) $ins); | |
55 | ||
56 | $outs = civicrm_api4('Contact', 'get', [ | |
57 | 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]], | |
58 | ])->indexBy('id'); | |
59 | $this->assertArrayHasKey($out['id'], (array) $outs); | |
60 | $this->assertArrayNotHasKey($in['id'], (array) $outs); | |
48102254 CW |
61 | } |
62 | ||
63 | public function testEmailSmartGroup() { | |
fe806431 CW |
64 | $in = Contact::create(FALSE)->addValue('first_name', 'yep')->execute()->first(); |
65 | $out = Contact::create(FALSE)->addValue('first_name', 'nope')->execute()->first(); | |
48102254 | 66 | $email = uniqid() . '@' . uniqid(); |
fe806431 | 67 | Email::create(FALSE)->addValue('email', $email)->addValue('contact_id', $in['id'])->execute(); |
48102254 CW |
68 | |
69 | $savedSearch = civicrm_api4('SavedSearch', 'create', [ | |
70 | 'values' => [ | |
71 | 'api_entity' => 'Email', | |
72 | 'api_params' => [ | |
73 | 'version' => 4, | |
74 | 'select' => ['contact_id'], | |
75 | 'where' => [ | |
76 | ['email', '=', $email], | |
77 | ], | |
78 | ], | |
79 | ], | |
80 | 'chain' => [ | |
81 | 'group' => ['Group', 'create', ['values' => ['title' => 'Email Test', 'saved_search_id' => '$id']], 0], | |
82 | ], | |
83 | ])->first(); | |
84 | ||
a1415a02 CW |
85 | $ins = civicrm_api4('Contact', 'get', [ |
86 | 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]], | |
87 | ])->indexBy('id'); | |
88 | $this->assertCount(1, $ins); | |
89 | $this->assertArrayHasKey($in['id'], (array) $ins); | |
90 | ||
91 | $outs = civicrm_api4('Contact', 'get', [ | |
92 | 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]], | |
93 | ])->indexBy('id'); | |
94 | $this->assertArrayHasKey($out['id'], (array) $outs); | |
95 | $this->assertArrayNotHasKey($in['id'], (array) $outs); | |
4e97c268 CW |
96 | } |
97 | ||
23e56526 CW |
98 | public function testSmartGroupWithHaving() { |
99 | $in = Contact::create(FALSE)->addValue('first_name', 'yes')->addValue('last_name', 'siree')->execute()->first(); | |
100 | $in2 = Contact::create(FALSE)->addValue('first_name', 'yessir')->addValue('last_name', 'ee')->execute()->first(); | |
101 | $out = Contact::create(FALSE)->addValue('first_name', 'yess')->execute()->first(); | |
102 | ||
103 | $savedSearch = civicrm_api4('SavedSearch', 'create', [ | |
104 | 'values' => [ | |
105 | 'api_entity' => 'Contact', | |
106 | 'api_params' => [ | |
107 | 'version' => 4, | |
108 | 'select' => ['id', 'CONCAT(first_name, last_name) AS whole_name'], | |
109 | 'where' => [ | |
110 | ['id', '>=', $in['id']], | |
111 | ], | |
112 | 'having' => [ | |
113 | ['whole_name', '=', 'yessiree'], | |
114 | ], | |
115 | ], | |
116 | ], | |
117 | 'chain' => [ | |
118 | 'group' => ['Group', 'create', ['values' => ['title' => 'Having Test', 'saved_search_id' => '$id']], 0], | |
119 | ], | |
120 | ])->first(); | |
121 | ||
a1415a02 CW |
122 | $ins = civicrm_api4('Contact', 'get', [ |
123 | 'where' => [['groups', 'IN', [$savedSearch['group']['id']]]], | |
124 | ])->indexBy('id'); | |
125 | $this->assertCount(2, $ins); | |
126 | $this->assertArrayHasKey($in['id'], (array) $ins); | |
127 | $this->assertArrayHasKey($in2['id'], (array) $ins); | |
128 | ||
129 | $outs = civicrm_api4('Contact', 'get', [ | |
130 | 'where' => [['groups', 'NOT IN', [$savedSearch['group']['id']]]], | |
131 | ])->indexBy('id'); | |
132 | $this->assertArrayHasKey($out['id'], (array) $outs); | |
133 | $this->assertArrayNotHasKey($in['id'], (array) $outs); | |
134 | $this->assertArrayNotHasKey($in2['id'], (array) $outs); | |
135 | } | |
136 | ||
137 | public function testMultipleSmartGroups() { | |
138 | $inGroup = $outGroup = []; | |
139 | $inName = uniqid('inGroup'); | |
140 | $outName = uniqid('outGroup'); | |
141 | for ($i = 0; $i < 10; ++$i) { | |
142 | $inGroup[] = Contact::create(FALSE) | |
143 | ->setValues(['first_name' => "$i", 'last_name' => $inName]) | |
144 | ->execute()->first()['id']; | |
145 | $outGroup[] = Contact::create(FALSE) | |
146 | ->setValues(['first_name' => "$i", 'last_name' => $outName]) | |
147 | ->execute()->first()['id']; | |
148 | } | |
149 | ||
150 | $savedSearchA = civicrm_api4('SavedSearch', 'create', [ | |
151 | 'values' => [ | |
152 | 'api_entity' => 'Contact', | |
153 | 'api_params' => [ | |
154 | 'version' => 4, | |
155 | 'where' => [ | |
156 | ['last_name', '=', $inName], | |
157 | ], | |
158 | ], | |
159 | ], | |
160 | 'chain' => [ | |
161 | 'group' => ['Group', 'create', ['values' => ['title' => 'In A Test', 'saved_search_id' => '$id']], 0], | |
162 | ], | |
163 | ])->first(); | |
164 | ||
165 | $savedSearchB = civicrm_api4('SavedSearch', 'create', [ | |
166 | 'values' => [ | |
167 | 'api_entity' => 'Contact', | |
168 | 'api_params' => [ | |
169 | 'version' => 4, | |
170 | 'where' => [ | |
171 | ['last_name', 'IN', [$inName, $outName]], | |
172 | ['first_name', '>', '4'], | |
173 | ], | |
174 | ], | |
175 | ], | |
176 | 'chain' => [ | |
177 | 'group' => ['Group', 'create', ['values' => ['title' => 'In B Test', 'saved_search_id' => '$id']], 0], | |
178 | ], | |
179 | ])->first(); | |
180 | ||
181 | $bothGroups = civicrm_api4('Contact', 'get', [ | |
182 | 'where' => [['groups:name', 'IN', [$savedSearchA['group']['name'], $savedSearchB['group']['name']]]], | |
183 | ]); | |
184 | $this->assertCount(15, $bothGroups); | |
185 | ||
186 | $aNotB = civicrm_api4('Contact', 'get', [ | |
187 | 'where' => [ | |
188 | ['groups:name', 'IN', [$savedSearchA['group']['name']]], | |
189 | ['groups:name', 'NOT IN', [$savedSearchB['group']['name']]], | |
190 | ], | |
191 | ]); | |
192 | $this->assertCount(5, $aNotB); | |
23e56526 CW |
193 | } |
194 | ||
4e97c268 | 195 | } |