Merge pull request #19806 from eileenmcnaughton/msg_compat
[civicrm-core.git] / tests / phpunit / api / v3 / SavedSearchTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright Chirojeugd-Vlaanderen vzw 2015 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29 /**
30 * Class api_v3_SavedSearchTest
31 *
32 * @package CiviCRM_APIv3
33 * @group headless
34 */
35 class api_v3_SavedSearchTest extends CiviUnitTestCase {
36
37 protected $_apiversion = 3;
38 protected $params;
39 protected $id;
40 protected $_entity;
41 public $DBResetRequired = FALSE;
42
43 public function setUp(): void {
44 parent::setUp();
45
46 // The line below makes it unneccessary to do cleanup after a test,
47 // because the transaction of the test will be rolled back.
48 // see http://forum.civicrm.org/index.php/topic,35627.0.html
49 $this->useTransaction(TRUE);
50
51 $this->_entity = 'SavedSearch';
52
53 // I created a smart group using the CiviCRM gui. The smart group contains
54 // all contacts tagged with 'company'.
55 // I got the params below from the database.
56 // params for saved search that returns all volunteers for the
57 // default organization.
58 $this->params = [
59 'expires_date' => '2021-08-08',
60 'form_values' => [
61 // Is volunteer for
62 'relation_type_id' => '6_a_b',
63 'relation_target_name' => 'Default Organization',
64 ],
65 ];
66 }
67
68 /**
69 * Create a saved search, and see whether the returned values make sense.
70 */
71 public function testCreateSavedSearch(): void {
72 $contactID = $this->createLoggedInUser();
73 $result = $this->callAPIAndDocument(
74 $this->_entity, 'create', $this->params, __FUNCTION__, __FILE__)['values'];
75 $this->assertEquals(1, count($result));
76 $savedSearch = reset($result);
77
78 $this->assertEquals($contactID, $savedSearch['created_id']);
79 $this->assertEquals($contactID, $savedSearch['modified_id']);
80 $this->assertEquals('20210808000000', $savedSearch['expires_date']);
81
82 $this->assertNotNull($savedSearch['id']);
83
84 // Check whether the relation type ID is correctly returned.
85 $this->assertEquals(
86 $this->params['form_values']['relation_type_id'],
87 $savedSearch['form_values']['relation_type_id']);
88 }
89
90 /**
91 * Create a saved search, retrieve it again, and check for ID and one of
92 * the field values.
93 */
94 public function testCreateAndGetSavedSearch(): void {
95 // Arrange:
96 // (create a saved search)
97 $create_result = $this->callAPISuccess(
98 $this->_entity, 'create', $this->params);
99
100 // Act:
101 $get_result = $this->callAPIAndDocument(
102 $this->_entity, 'get', ['id' => $create_result['id']], __FUNCTION__, __FILE__);
103
104 // Assert:
105 $this->assertEquals(1, $get_result['count']);
106 $this->assertNotNull($get_result['values'][$get_result['id']]['id']);
107
108 // just check the relationship type ID of the form values.
109 $this->assertEquals(
110 $this->params['form_values']['relation_type_id'],
111 $get_result['values'][$get_result['id']]['form_values']['relation_type_id']);
112 }
113
114 /**
115 * Create a saved search, and test whether it can be used for a smart
116 * group.
117 */
118 public function testCreateSavedSearchWithSmartGroup(): void {
119 // First create a volunteer for the default organization
120
121 $result = $this->callAPISuccess('Contact', 'create', [
122 'first_name' => 'Joe',
123 'last_name' => 'Schmoe',
124 'contact_type' => 'Individual',
125 'api.Relationship.create' => [
126 'contact_id_a' => '$value.id',
127 // default organization:
128 'contact_id_b' => 1,
129 // volunteer relationship:
130 'relationship_type_id' => 6,
131 'is_active' => 1,
132 ],
133 ]);
134 $contact_id = $result['id'];
135
136 // Now create our saved search, and chain the creation of a smart group.
137 $params = $this->params;
138 $params['api.Group.create'] = [
139 'name' => 'my_smartgroup',
140 'title' => 'my smartgroup',
141 'description' => 'Volunteers for the default organization',
142 'saved_search_id' => '$value.id',
143 'is_active' => 1,
144 'visibility' => 'User and User Admin Only',
145 'is_hidden' => 0,
146 'is_reserved' => 0,
147 ];
148
149 $create_result = $this->callAPIAndDocument(
150 $this->_entity, 'create', $params, __FUNCTION__, __FILE__);
151
152 $created_search = CRM_Utils_Array::first($create_result['values']);
153 $group_id = $created_search['api.Group.create']['id'];
154
155 // Search for contacts in our new smart group
156 $get_result = $this->callAPISuccess(
157 'Contact', 'get', ['group' => $group_id], __FUNCTION__, __FILE__);
158
159 // Expect our contact to be there.
160 $this->assertEquals(1, $get_result['count']);
161 $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
162 }
163
164 /**
165 * Create a saved search, and test whether it can be used for a smart
166 * group. Also check that when the Group is deleted the associated saved search gets deleted.
167 * @dataProvider versionThreeAndFour
168 */
169 public function testSavedSearchIsDeletedWhenSmartGroupIs($apiVersion): void {
170 $this->_apiVersion = $apiVersion;
171 // First create a volunteer for the default organization
172
173 $result = $this->callAPISuccess('Contact', 'create', [
174 'first_name' => 'Joe',
175 'last_name' => 'Schmoe',
176 'contact_type' => 'Individual',
177 'api.Relationship.create' => [
178 'contact_id_a' => '$value.id',
179 // default organization:
180 'contact_id_b' => 1,
181 // volunteer relationship:
182 'relationship_type_id' => 6,
183 'is_active' => 1,
184 ],
185 ]);
186 $contact_id = $result['id'];
187
188 // Now create our saved search, and chain the creation of a smart group.
189 $params = $this->params;
190 $params['api.Group.create'] = [
191 'name' => 'my_smartgroup',
192 'title' => 'my smartgroup',
193 'description' => 'Volunteers for the default organization',
194 'saved_search_id' => '$value.id',
195 'is_active' => 1,
196 'visibility' => 'User and User Admin Only',
197 'is_hidden' => 0,
198 'is_reserved' => 0,
199 ];
200
201 $create_result = $this->callAPISuccess($this->_entity, 'create', $params);
202
203 $created_search = CRM_Utils_Array::first($create_result['values']);
204 $group_id = $created_search['api.Group.create']['id'];
205
206 // Search for contacts in our new smart group
207 $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]);
208
209 // Expect our contact to be there.
210 $this->assertEquals(1, $get_result['count']);
211 $this->assertEquals($contact_id, $get_result['values'][$contact_id]['id']);
212
213 $this->callAPISuccess('Group', 'delete', ['id' => $group_id]);
214 $savedSearch = $this->callAPISuccess('SavedSearch', 'get', ['id' => $created_search['id']]);
215 $this->assertCount(0, $savedSearch['values']);
216 }
217
218 public function testDeleteSavedSearch(): void {
219 // Create saved search, delete it again, and try to get it
220 $create_result = $this->callAPISuccess($this->_entity, 'create', $this->params);
221 $delete_params = ['id' => $create_result['id']];
222 $this->callAPIAndDocument(
223 $this->_entity, 'delete', $delete_params, __FUNCTION__, __FILE__);
224 $get_result = $this->callAPISuccess($this->_entity, 'get', []);
225
226 $this->assertEquals(0, $get_result['count']);
227 }
228
229 }