4 * File for the ActivitySearchTest class
8 * @copyright Copyright CiviCRM LLC (C) 2009
9 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html
10 * GNU Affero General Public License version 3
13 * This file is part of CiviCRM
15 * CiviCRM is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU Affero General Public License
17 * as published by the Free Software Foundation; either version 3 of
18 * the License, or (at your option) any later version.
20 * CiviCRM is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU Affero General Public License for more details.
25 * You should have received a copy of the GNU Affero General Public
26 * License along with this program. If not, see
27 * <http://www.gnu.org/licenses/>.
31 * Include class definitions
35 * Test APIv3 civicrm_activity_* functions
37 * @package CiviCRM_APIv3
38 * @subpackage API_Activity
41 class CRM_Contact_BAO_ActivitySearchTest
extends CiviUnitTestCase
{
43 protected $_contactID;
47 protected $test_activity_type_value;
50 * Test setup for every test.
52 * Connect to the database, truncate the tables that will be used
53 * and redirect stdin to a temporary file
55 public function setUp(): void
{
56 // Connect to the database
59 $this->_contactID
= $this->individualCreate();
60 //create activity types
61 $activityTypes = $this->callAPISuccess('option_value', 'create', [
62 'option_group_id' => 2,
63 'name' => 'Test activity type',
64 'label' => 'Test activity type',
67 $this->test_activity_type_id
= $activityTypes['id'];
69 'source_contact_id' => $this->_contactID
,
70 'activity_type_id' => $activityTypes['values'][0]['value'],
71 'subject' => 'test activity type id',
72 'activity_date_time' => '2011-06-02 14:36:13',
76 'location' => 'Pennsylvania',
77 'details' => 'a test activity',
79 // create a logged in USER since the code references it for source_contact_id
80 $this->createLoggedInUser();
84 * Tears down the fixture, for example, closes a network connection.
86 * This method is called after a test is executed.
88 public function tearDown(): void
{
92 'civicrm_activity_contact',
96 $this->quickCleanup($tablesToTruncate, TRUE);
97 $type = $this->callAPISuccess('optionValue', 'get', ['id' => $this->test_activity_type_id
]);
98 if (!empty($type['count'])) {
99 $this->callAPISuccess('option_value', 'delete', ['id' => $this->test_activity_type_id
]);
104 * Test that activity.get api works when filtering on subject.
106 public function testSearchBySubjectOnly() {
107 $subject = 'test activity ' . __FUNCTION__
;
108 $params = $this->_params
;
109 $params['subject'] = $subject;
110 $this->callAPISuccess('Activity', 'Create', $params);
114 'activity_text' => $subject,
115 'activity_option' => 3,
117 'expected_count' => 1,
118 'expected_contact' => [$this->_contactID
],
120 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
121 list($select, $from, $where, $having) = $query->query();
122 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
123 foreach ($groupContacts as $key => $value) {
124 $groupContacts[$key] = $value['id'];
126 $this->assertEquals($case['expected_count'], count($groupContacts));
127 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
131 * Test that activity.get api works when filtering on subject.
133 public function testSearchBySubjectBoth() {
134 $subject = 'test activity ' . __FUNCTION__
;
135 $params = $this->_params
;
136 $params['subject'] = $subject;
137 $activity = $this->callAPISuccess('Activity', 'Create', $params);
141 'activity_text' => $subject,
142 'activity_option' => 6,
144 'expected_count' => 1,
145 'expected_contact' => [$this->_contactID
],
147 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
148 list($select, $from, $where, $having) = $query->query();
149 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
150 foreach ($groupContacts as $key => $value) {
151 $groupContacts[$key] = $value['id'];
153 $this->assertEquals($case['expected_count'], count($groupContacts));
154 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
158 * Test that activity.get api works when filtering on subject.
160 public function testSearchByDetailsOnly() {
161 $details = 'test activity ' . __FUNCTION__
;
162 $params = $this->_params
;
163 $params['details'] = $details;
164 $activity = $this->callAPISuccess('Activity', 'Create', $params);
168 'activity_text' => $details,
169 'activity_option' => 2,
171 'expected_count' => 1,
172 'expected_contact' => [$this->_contactID
],
174 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
175 list($select, $from, $where, $having) = $query->query();
176 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
177 foreach ($groupContacts as $key => $value) {
178 $groupContacts[$key] = $value['id'];
180 $this->assertEquals($case['expected_count'], count($groupContacts));
181 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
185 * Test that activity.get api works when filtering on details.
187 public function testSearchByDetailsBoth() {
188 $details = 'test activity ' . __FUNCTION__
;
189 $params = $this->_params
;
190 $params['details'] = $details;
191 $activity = $this->callAPISuccess('Activity', 'Create', $params);
195 'activity_text' => $details,
196 'activity_option' => 6,
198 'expected_count' => 1,
199 'expected_contact' => [$this->_contactID
],
201 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
202 list($select, $from, $where, $having) = $query->query();
203 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
204 foreach ($groupContacts as $key => $value) {
205 $groupContacts[$key] = $value['id'];
207 $this->assertEquals($case['expected_count'], count($groupContacts));
208 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
212 * Test that activity.get api works when filtering on bare tags (i.e. tags
213 * not part of a tagset).
215 public function testSearchByBareTags() {
216 $tag = $this->callAPISuccess('Tag', 'create', [
218 'used_for' => 'Activities',
220 $subject = 'test activity ' . __FUNCTION__
;
221 $params = $this->_params
;
222 $params['subject'] = $subject;
223 $activity = $this->callAPISuccess('Activity', 'Create', $params);
224 $this->callAPISuccess('EntityTag', 'create', [
225 'entity_id' => $activity['id'],
226 'entity_table' => 'civicrm_activity',
231 // It looks like it can be an array or comma-separated string.
232 // The qill will be slightly different ("IN" vs. "OR").
233 // The search form seems to use commas.
234 'activity_tags' => (string) $tag['id'],
236 'expected_count' => 1,
237 'expected_contact' => [$this->_contactID
],
239 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
240 list($select, $from, $where, $having) = $query->query();
244 0 => 'Activity Tag = a1',
247 $this->assertEquals($expectedQill, $query->_qill
);
249 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
250 foreach ($groupContacts as $key => $value) {
251 $groupContacts[$key] = $value['id'];
253 $this->assertEquals($case['expected_count'], count($groupContacts));
254 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
256 // Clean up. Don't want to use teardown to wipe the table since then
257 // the stock tags get wiped.
258 $this->callAPISuccess('Tag', 'delete', ['id' => $tag['id']]);
262 * Test that activity.get api works when filtering on a tag in a tagset.
264 public function testSearchByTagset() {
265 $tagset = $this->callAPISuccess('Tag', 'create', [
266 'name' => 'activity tagset',
268 'used_for' => 'Activities',
270 $tag = $this->callAPISuccess('Tag', 'create', [
272 'used_for' => 'Activities',
273 'parent_id' => 'activity tagset',
275 $subject = 'test activity ' . __FUNCTION__
;
276 $params = $this->_params
;
277 $params['subject'] = $subject;
278 $activity = $this->callAPISuccess('Activity', 'Create', $params);
279 $this->callAPISuccess('EntityTag', 'create', [
280 'entity_id' => $activity['id'],
281 'entity_table' => 'civicrm_activity',
286 // If multiple tags the array element value is a comma-separated string
287 // and then the qill looks like "IN a OR b".
288 "activity_taglist" => [$tagset['id'] => (string) $tag['id']],
290 'expected_count' => 1,
291 'expected_contact' => [$this->_contactID
],
293 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
294 list($select, $from, $where, $having) = $query->query();
298 0 => 'Activity Tag IN aa1',
301 $this->assertEquals($expectedQill, $query->_qill
);
303 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
304 foreach ($groupContacts as $key => $value) {
305 $groupContacts[$key] = $value['id'];
307 $this->assertEquals($case['expected_count'], count($groupContacts));
308 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
310 // Clean up. Don't want to use teardown to wipe the table since then
311 // the stock tags get wiped.
312 $this->callAPISuccess('Tag', 'delete', ['id' => $tag['id']]);