4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright Chirojeugd-Vlaanderen vzw 2015 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
30 * Class api_v3_SavedSearchTest
32 * @package CiviCRM_APIv3
35 class api_v3_SavedSearchTest
extends CiviUnitTestCase
{
37 protected $_apiversion = 3;
41 public $DBResetRequired = FALSE;
43 public function setUp(): void
{
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);
51 $this->_entity
= 'SavedSearch';
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.
59 'expires_date' => '2021-08-08',
62 'relation_type_id' => '6_a_b',
63 'relation_target_name' => 'Default Organization',
69 * Create a saved search, and see whether the returned values make sense.
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);
78 $this->assertEquals($contactID, $savedSearch['created_id']);
79 $this->assertEquals($contactID, $savedSearch['modified_id']);
80 $this->assertEquals('20210808000000', $savedSearch['expires_date']);
82 $this->assertNotNull($savedSearch['id']);
84 // Check whether the relation type ID is correctly returned.
86 $this->params
['form_values']['relation_type_id'],
87 $savedSearch['form_values']['relation_type_id']);
91 * Create a saved search, retrieve it again, and check for ID and one of
94 public function testCreateAndGetSavedSearch(): void
{
96 // (create a saved search)
97 $create_result = $this->callAPISuccess(
98 $this->_entity
, 'create', $this->params
);
101 $get_result = $this->callAPIAndDocument(
102 $this->_entity
, 'get', ['id' => $create_result['id']], __FUNCTION__
, __FILE__
);
105 $this->assertEquals(1, $get_result['count']);
106 $this->assertNotNull($get_result['values'][$get_result['id']]['id']);
108 // just check the relationship type ID of the form values.
110 $this->params
['form_values']['relation_type_id'],
111 $get_result['values'][$get_result['id']]['form_values']['relation_type_id']);
115 * Create a saved search, and test whether it can be used for a smart
118 public function testCreateSavedSearchWithSmartGroup(): void
{
119 // First create a volunteer for the default organization
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:
129 // volunteer relationship:
130 'relationship_type_id' => 6,
134 $contact_id = $result['id'];
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',
144 'visibility' => 'User and User Admin Only',
149 $create_result = $this->callAPIAndDocument(
150 $this->_entity
, 'create', $params, __FUNCTION__
, __FILE__
);
152 $created_search = CRM_Utils_Array
::first($create_result['values']);
153 $group_id = $created_search['api.Group.create']['id'];
155 // Search for contacts in our new smart group
156 $get_result = $this->callAPISuccess(
157 'Contact', 'get', ['group' => $group_id], __FUNCTION__
, __FILE__
);
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']);
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
169 public function testSavedSearchIsDeletedWhenSmartGroupIs($apiVersion): void
{
170 $this->_apiVersion
= $apiVersion;
171 // First create a volunteer for the default organization
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:
181 // volunteer relationship:
182 'relationship_type_id' => 6,
186 $contact_id = $result['id'];
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',
196 'visibility' => 'User and User Admin Only',
201 $create_result = $this->callAPISuccess($this->_entity
, 'create', $params);
203 $created_search = CRM_Utils_Array
::first($create_result['values']);
204 $group_id = $created_search['api.Group.create']['id'];
206 // Search for contacts in our new smart group
207 $get_result = $this->callAPISuccess('Contact', 'get', ['group' => $group_id]);
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']);
213 $this->callAPISuccess('Group', 'delete', ['id' => $group_id]);
214 $savedSearch = $this->callAPISuccess('SavedSearch', 'get', ['id' => $created_search['id']]);
215 $this->assertCount(0, $savedSearch['values']);
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', []);
226 $this->assertEquals(0, $get_result['count']);