Search ext: Choose smart group contact column
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / SavedSearchTest.php
CommitLineData
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
20namespace api\v4\Entity;
21
22use api\v4\UnitTestCase;
23use Civi\Api4\Contact;
48102254 24use Civi\Api4\Email;
4e97c268
CW
25
26/**
27 * @group headless
28 */
29class 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
50 // Oops we don't have an api4 syntax yet for selecting contacts in a group.
51 $ins = civicrm_api3('Contact', 'get', ['group' => $savedSearch['group']['name'], 'options' => ['limit' => 0]]);
48102254
CW
52 $this->assertEquals(1, count($ins['values']));
53 $this->assertArrayHasKey($in['id'], $ins['values']);
54 $this->assertArrayNotHasKey($out['id'], $ins['values']);
55 }
56
57 public function testEmailSmartGroup() {
fe806431
CW
58 $in = Contact::create(FALSE)->addValue('first_name', 'yep')->execute()->first();
59 $out = Contact::create(FALSE)->addValue('first_name', 'nope')->execute()->first();
48102254 60 $email = uniqid() . '@' . uniqid();
fe806431 61 Email::create(FALSE)->addValue('email', $email)->addValue('contact_id', $in['id'])->execute();
48102254
CW
62
63 $savedSearch = civicrm_api4('SavedSearch', 'create', [
64 'values' => [
65 'api_entity' => 'Email',
66 'api_params' => [
67 'version' => 4,
68 'select' => ['contact_id'],
69 'where' => [
70 ['email', '=', $email],
71 ],
72 ],
73 ],
74 'chain' => [
75 'group' => ['Group', 'create', ['values' => ['title' => 'Email Test', 'saved_search_id' => '$id']], 0],
76 ],
77 ])->first();
78
79 // Oops we don't have an api4 syntax yet for selecting contacts in a group.
80 $ins = civicrm_api3('Contact', 'get', ['group' => $savedSearch['group']['name'], 'options' => ['limit' => 0]]);
81 $this->assertEquals(1, count($ins['values']));
4e97c268
CW
82 $this->assertArrayHasKey($in['id'], $ins['values']);
83 $this->assertArrayNotHasKey($out['id'], $ins['values']);
84 }
85
86}