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() {
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() {
92 'civicrm_activity_contact',
95 $this->quickCleanup($tablesToTruncate, TRUE);
96 $type = $this->callAPISuccess('optionValue', 'get', ['id' => $this->test_activity_type_id
]);
97 if (!empty($type['count'])) {
98 $this->callAPISuccess('option_value', 'delete', ['id' => $this->test_activity_type_id
]);
103 * Test that activity.get api works when filtering on subject.
105 public function testSearchBySubjectOnly() {
106 $subject = 'test activity ' . __FUNCTION__
;
107 $params = $this->_params
;
108 $params['subject'] = $subject;
109 $this->callAPISuccess('Activity', 'Create', $params);
113 'activity_text' => $subject,
114 'activity_option' => 3,
116 'expected_count' => 1,
117 'expected_contact' => [$this->_contactID
],
119 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
120 list($select, $from, $where, $having) = $query->query();
121 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
122 foreach ($groupContacts as $key => $value) {
123 $groupContacts[$key] = $value['id'];
125 $this->assertEquals($case['expected_count'], count($groupContacts));
126 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
130 * Test that activity.get api works when filtering on subject.
132 public function testSearchBySubjectBoth() {
133 $subject = 'test activity ' . __FUNCTION__
;
134 $params = $this->_params
;
135 $params['subject'] = $subject;
136 $activity = $this->callAPISuccess('Activity', 'Create', $params);
140 'activity_text' => $subject,
141 'activity_option' => 6,
143 'expected_count' => 1,
144 'expected_contact' => [$this->_contactID
],
146 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
147 list($select, $from, $where, $having) = $query->query();
148 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
149 foreach ($groupContacts as $key => $value) {
150 $groupContacts[$key] = $value['id'];
152 $this->assertEquals($case['expected_count'], count($groupContacts));
153 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
157 * Test that activity.get api works when filtering on subject.
159 public function testSearchByDetailsOnly() {
160 $details = 'test activity ' . __FUNCTION__
;
161 $params = $this->_params
;
162 $params['details'] = $details;
163 $activity = $this->callAPISuccess('Activity', 'Create', $params);
167 'activity_text' => $details,
168 'activity_option' => 2,
170 'expected_count' => 1,
171 'expected_contact' => [$this->_contactID
],
173 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
174 list($select, $from, $where, $having) = $query->query();
175 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
176 foreach ($groupContacts as $key => $value) {
177 $groupContacts[$key] = $value['id'];
179 $this->assertEquals($case['expected_count'], count($groupContacts));
180 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
184 * Test that activity.get api works when filtering on details.
186 public function testSearchByDetailsBoth() {
187 $details = 'test activity ' . __FUNCTION__
;
188 $params = $this->_params
;
189 $params['details'] = $details;
190 $activity = $this->callAPISuccess('Activity', 'Create', $params);
194 'activity_text' => $details,
195 'activity_option' => 6,
197 'expected_count' => 1,
198 'expected_contact' => [$this->_contactID
],
200 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
201 list($select, $from, $where, $having) = $query->query();
202 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
203 foreach ($groupContacts as $key => $value) {
204 $groupContacts[$key] = $value['id'];
206 $this->assertEquals($case['expected_count'], count($groupContacts));
207 $this->checkArrayEquals($case['expected_contact'], $groupContacts);