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
{
42 protected $_contactID;
44 protected $test_activity_type_value;
47 * Test setup for every test.
49 * Connect to the database, truncate the tables that will be used
50 * and redirect stdin to a temporary file
52 public function setUp() {
53 // Connect to the database
56 $this->_contactID
= $this->individualCreate();
57 //create activity types
58 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
59 'option_group_id' => 2,
60 'name' => 'Test activity type',
61 'label' => 'Test activity type',
64 $this->test_activity_type_id
= $activityTypes['id'];
65 $this->_params
= array(
66 'source_contact_id' => $this->_contactID
,
67 'activity_type_id' => $activityTypes['values'][0]['value'],
68 'subject' => 'test activity type id',
69 'activity_date_time' => '2011-06-02 14:36:13',
73 'location' => 'Pennsylvania',
74 'details' => 'a test activity',
76 // create a logged in USER since the code references it for source_contact_id
77 $this->createLoggedInUser();
81 * Tears down the fixture, for example, closes a network connection.
83 * This method is called after a test is executed.
85 public function tearDown() {
86 $tablesToTruncate = array(
89 'civicrm_activity_contact',
92 $this->quickCleanup($tablesToTruncate, TRUE);
93 $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id
));
94 if (!empty($type['count'])) {
95 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id
));
100 * Test that activity.get api works when filtering on subject.
102 public function testSearchBySubjectOnly() {
103 $subject = 'test activity ' . __FUNCTION__
;
104 $params = $this->_params
;
105 $params['subject'] = $subject;
106 $this->callAPISuccess('Activity', 'Create', $params);
109 'form_value' => array(
110 'activity_text' => $subject,
111 'activity_option' => 3,
113 'expected_count' => 1,
114 'expected_contact' => array($this->_contactID
),
116 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
117 list($select, $from, $where, $having) = $query->query();
118 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
119 foreach ($groupContacts as $key => $value) {
120 $groupContacts[$key] = $value['id'];
122 $this->assertEquals($case['expected_count'], count($groupContacts));
123 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
127 * Test that activity.get api works when filtering on subject.
129 public function testSearchBySubjectBoth() {
130 $subject = 'test activity ' . __FUNCTION__
;
131 $params = $this->_params
;
132 $params['subject'] = $subject;
133 $activity = $this->callAPISuccess('Activity', 'Create', $params);
136 'form_value' => array(
137 'activity_text' => $subject,
138 'activity_option' => 6,
140 'expected_count' => 1,
141 'expected_contact' => array($this->_contactID
),
143 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
144 list($select, $from, $where, $having) = $query->query();
145 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
146 foreach ($groupContacts as $key => $value) {
147 $groupContacts[$key] = $value['id'];
149 $this->assertEquals($case['expected_count'], count($groupContacts));
150 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
154 * Test that activity.get api works when filtering on subject.
156 public function testSearchByDetailsOnly() {
157 $details = 'test activity ' . __FUNCTION__
;
158 $params = $this->_params
;
159 $params['details'] = $details;
160 $activity = $this->callAPISuccess('Activity', 'Create', $params);
163 'form_value' => array(
164 'activity_text' => $details,
165 'activity_option' => 2,
167 'expected_count' => 1,
168 'expected_contact' => array($this->_contactID
),
170 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
171 list($select, $from, $where, $having) = $query->query();
172 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
173 foreach ($groupContacts as $key => $value) {
174 $groupContacts[$key] = $value['id'];
176 $this->assertEquals($case['expected_count'], count($groupContacts));
177 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
181 * Test that activity.get api works when filtering on details.
183 public function testSearchByDetailsBoth() {
184 $details = 'test activity ' . __FUNCTION__
;
185 $params = $this->_params
;
186 $params['details'] = $details;
187 $activity = $this->callAPISuccess('Activity', 'Create', $params);
190 'form_value' => array(
191 'activity_text' => $details,
192 'activity_option' => 6,
194 'expected_count' => 1,
195 'expected_contact' => array($this->_contactID
),
197 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query
::convertFormValues($case['form_value']));
198 list($select, $from, $where, $having) = $query->query();
199 $groupContacts = CRM_Core_DAO
::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
200 foreach ($groupContacts as $key => $value) {
201 $groupContacts[$key] = $value['id'];
203 $this->assertEquals($case['expected_count'], count($groupContacts));
204 $this->checkArrayEquals($case['expected_contact'], $groupContacts);