[Ref] [Test] Move custom search tests to extension
[civicrm-core.git] / ext / legacycustomsearches / tests / phpunit / Civi / Searches / SampleTest.php
1 <?php
2
3 namespace Civi\Searches;
4
5 use Civi\Api4\Address;
6 use Civi\Api4\Contact;
7 use Civi\Test;
8 use Civi\Test\HeadlessInterface;
9 use Civi\Test\HookInterface;
10 use Civi\Test\TransactionalInterface;
11 use Civi\Api4\SavedSearch;
12 use Civi\Api4\OptionValue;
13 use CRM_Contact_BAO_SavedSearch;
14 use CRM_Contact_Form_Search_Custom_Group;
15 use CRM_Contact_Form_Search_Custom_Sample;
16 use CRM_Core_DAO;
17 use PHPUnit\Framework\TestCase;
18
19 /**
20 * FIXME - Add test description.
21 *
22 * Tips:
23 * - With HookInterface, you may implement CiviCRM hooks directly in the test
24 * class. Simply create corresponding functions (e.g. "hook_civicrm_post(...)"
25 * or similar).
26 * - With TransactionalInterface, any data changes made by setUp() or
27 * test****() functions will rollback automatically -- as long as you don't
28 * manipulate schema or truncate tables. If this test needs to manipulate
29 * schema or truncate tables, then either: a. Do all that using setupHeadless()
30 * and Civi\Test. b. Disable TransactionalInterface, and handle all
31 * setup/teardown yourself.
32 *
33 * @group headless
34 */
35 class SampleTest extends TestCase implements HeadlessInterface, HookInterface, TransactionalInterface {
36
37 /**
38 * Civi\Test has many helpers, like install(), uninstall(), sql(), and
39 * sqlFile(). See:
40 * https://github.com/civicrm/org.civicrm.testapalooza/blob/master/civi-test.md
41 */
42 public function setUpHeadless(): Test\CiviEnvBuilder {
43 return Test::headless()
44 ->install(['legacycustomsearches'])
45 ->apply();
46 }
47
48 /**
49 * Set up for test.
50 *
51 * @throws \API_Exception
52 * @throws \Civi\API\Exception\UnauthorizedException
53 */
54 public function setUp(): void {
55 OptionValue::create()->setValues([
56 'option_group_id:name' => 'custom_search',
57 'label' => 'CRM_Contact_Form_Search_Custom_Sample',
58 'value' => 100,
59 'name' => 'CRM_Contact_Form_Search_Custom_Sample',
60 'description' => 'Household Name and State',
61 ])->execute();
62 }
63
64 /**
65 * Get data for tests.
66 *
67 * @return array
68 */
69 public function dataProvider(): array {
70 return [
71 // Search by Household name: 'Household 9'
72 [
73 'form_values' => ['household_name' => 'Household - No state'],
74 'names' => [
75 'Household - No state',
76 ],
77 ],
78 // Search by Household name: 'Household'
79 [
80 'form_values' => ['household_name' => 'Household'],
81 'id' => [
82 'Household - No state',
83 'Household - CA',
84 'Household - CA - 2',
85 'Household - NY',
86 ],
87 ],
88 // Search by State: California
89 [
90 'form_values' => ['state_province_id' => '1004'],
91 'id' => [
92 'Household - CA',
93 'Household - CA - 2',
94 ],
95 ],
96 // Search by State: New York
97 [
98 'form_values' => ['state_province_id' => '1031'],
99 'id' => [
100 'Household - NY',
101 ],
102 ],
103 ];
104 }
105
106 /**
107 * Test CRM_Contact_Form_Search_Custom_Sample::count()
108 *
109 * @dataProvider dataProvider
110 *
111 * @param array $formValues
112 * @param array $names
113 *
114 * @throws \API_Exception
115 */
116 public function testCount(array $formValues, array $names): void {
117 $this->setupSampleData();
118 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
119 $this->assertEquals(count($names), $obj->count());
120 }
121
122 /**
123 * Test CRM_Contact_Form_Search_Custom_Sample::all()
124 *
125 * @dataProvider dataProvider
126 *
127 * @param array $formValues
128 * @param array $names
129 *
130 * @throws \API_Exception
131 */
132 public function testAll(array $formValues, array $names): void {
133 $this->setupSampleData();
134 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
135 $sql = $obj->all(0, 0, 'contact_id');
136 $this->assertIsString($sql);
137 $dao = CRM_Core_DAO::executeQuery($sql);
138 $all = [];
139 while ($dao->fetch()) {
140 $all[] = [
141 'contact_id' => $dao->contact_id,
142 'contact_type' => $dao->contact_type,
143 'household_name' => $dao->sort_name,
144 ];
145 }
146 $full = [];
147 foreach ($names as $name) {
148 $full[] = [
149 'contact_type' => 'Household',
150 'household_name' => $name,
151 'contact_id' => Contact::get()
152 ->addWhere('household_name', '=', $name)
153 ->execute()
154 ->first()['id'],
155 ];
156 }
157 asort($all);
158 $this->assertEquals($full, $all);
159 }
160
161 /**
162 * Test CRM_Contact_Form_Search_Custom_Sample::contactIDs().
163 *
164 * @dataProvider dataProvider
165 *
166 * @param array $formValues
167 * @param array $names
168 *
169 * @throws \API_Exception
170 */
171 public function testContactIDs(array $formValues, array $names): void {
172 $this->setupSampleData();
173 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
174 $sql = $obj->contactIDs();
175 $this->assertIsString($sql);
176 $dao = CRM_Core_DAO::executeQuery($sql);
177 $contacts = [];
178 while ($dao->fetch()) {
179 $contacts[$dao->contact_id] = 1;
180 }
181 $contacts = array_keys($contacts);
182 sort($contacts, SORT_NUMERIC);
183 $this->assertEquals($this->getContactIDs($names), $contacts);
184 }
185
186 /**
187 * Test CRM_Contact_Form_Search_Custom_Group::columns().
188 *
189 * It returns an array of translated name => keys
190 */
191 public function testColumns(): void {
192 $formValues = [];
193 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
194 $columns = $obj->columns();
195 $this->assertIsArray($columns);
196 foreach ($columns as $key => $value) {
197 $this->assertIsString($key);
198 $this->assertIsString($value);
199 }
200 }
201
202 /**
203 * Test CRM_Contact_Form_Search_Custom_Group::summary()
204 * It returns NULL
205 */
206 public function testSummary(): void {
207 $formValues = [];
208 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
209 $this->assertNull($obj->summary());
210 }
211
212 /**
213 * Test CRM_Contact_Form_Search_Custom_Sample::templateFile()
214 * Returns the path to the file as a string
215 */
216 public function testTemplateFile(): void {
217 $formValues = [];
218 $obj = new CRM_Contact_Form_Search_Custom_Group($formValues);
219 $fileName = $obj->templateFile();
220 $this->assertIsString($fileName);
221 }
222
223 /**
224 * Test CRM_Contact_Form_Search_Custom_Sample with saved_search_id
225 * With true argument it returns list of contact IDs
226 *
227 * @throws \API_Exception
228 * @throws \CRM_Core_Exception
229 * @throws \CiviCRM_API3_Exception
230 * @throws \Civi\API\Exception\UnauthorizedException
231 */
232 public function testSavedSearch(): void {
233 $this->setupSampleData();
234 $this->setupSavedSearches();
235 $dataset[1] = ['id' => $this->getContactIDs(['Household - NY'])];
236 $dataset[2] = [
237 'id' => $this->getContactIDs([
238 'Household - CA',
239 'Household - CA - 2',
240 ]),
241 ];
242 $searches = SavedSearch::get()->addSelect('*')->execute();
243 foreach ($searches as $search) {
244 $formValues = CRM_Contact_BAO_SavedSearch::getFormValues($search['id']);
245 $obj = new CRM_Contact_Form_Search_Custom_Sample($formValues);
246 $sql = $obj->contactIDs();
247 $this->assertIsString($sql);
248 $dao = CRM_Core_DAO::executeQuery($sql);
249 $contacts = [];
250 while ($dao->fetch()) {
251 $contacts[] = $dao->contact_id;
252 }
253 sort($contacts, SORT_NUMERIC);
254 $this->assertEquals($dataset[$search['id']]['id'], $contacts);
255 }
256 }
257
258 /**
259 * Set up our sample data.
260 *
261 * @throws \API_Exception
262 */
263 public function setupSampleData(): void {
264 $households = [
265 'Household - No state' => '',
266 'Household - CA' => 1004,
267 'Household - CA - 2' => 1004,
268 'Household - NY' => 1031,
269 ];
270 foreach ($households as $household => $state) {
271 $create = Contact::create(FALSE)->setValues([
272 'contact_type' => 'Household',
273 'household_name' => $household,
274 ]);
275 if ($state) {
276 $create->addChain(
277 'address',
278 Address::create()->setValues([
279 'contact_id' => '$id',
280 'location_type_id' => 1,
281 'state_province_id' => $state,
282 ]));
283 }
284 $create->execute();
285 }
286 }
287
288 /**
289 * Get the ids for the relevant contacts.@
290 *
291 * @return array
292 * IDs of the contacts.
293 *
294 * @throws \API_Exception
295 */
296 protected function getContactIDs($names): array {
297 return array_keys((array) Contact::get()->addWhere(
298 'display_name', 'IN', $names
299 )->addOrderBy('id')->execute()->indexBy('id'));
300 }
301
302 /**
303 * Set up saved searches.
304 */
305 protected function setupSavedSearches(): void {
306 SavedSearch::create()->setValues([
307 'form_values' => [
308 [
309 0 => 'csid',
310 1 => '=',
311 2 => '1',
312 3 => 0,
313 4 => 0,
314 ],
315 [
316 0 => 'household_name',
317 1 => '=',
318 2 => 'Household - NY',
319 3 => 0,
320 4 => 0,
321 ],
322 [
323 0 => 'state_province_id',
324 1 => '=',
325 2 => '1031',
326 3 => 0,
327 4 => 0,
328 ],
329 6 =>
330 [
331 0 => 'customSearchID',
332 1 => '=',
333 2 => '1',
334 3 => 0,
335 4 => 0,
336 ],
337 7 =>
338 [
339 0 => 'customSearchClass',
340 1 => '=',
341 2 => 'CRM_Contact_Form_Search_Custom_Sample',
342 3 => 0,
343 4 => 0,
344 ],
345 ],
346 ])->execute();
347
348 SavedSearch::create()->setValues([
349 'form_values' => [
350 'csid' => '1',
351 'household_name' => '',
352 'state_province_id' => '1004',
353 ],
354 ])->execute();
355 }
356
357 }