CRM-19821 performance improvement on the activity search.
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ActivitySearchTest.php
CommitLineData
288674ef
AS
1<?php
2/**
3 * @file
4 * File for the ActivitySearchTest class
5 *
6 * (PHP 5)
7 *
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
11 * @package CiviCRM
12 *
13 * This file is part of CiviCRM
14 *
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.
19 *
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.
24 *
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/>.
28 */
29
30/**
31 * Include class definitions
32 */
33
34/**
35 * Test APIv3 civicrm_activity_* functions
36 *
37 * @package CiviCRM_APIv3
38 * @subpackage API_Activity
39 * @group headless
40 */
41class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase {
42 protected $_contactID;
43 protected $_params;
44 protected $test_activity_type_value;
45
46
47 /**
48 * Test setup for every test.
49 *
50 * Connect to the database, truncate the tables that will be used
51 * and redirect stdin to a temporary file
52 */
53 public function setUp() {
54 // Connect to the database
55 parent::setUp();
56
57 $this->_contactID = $this->individualCreate();
58 //create activity types
59 $activityTypes = $this->callAPISuccess('option_value', 'create', array(
60 'option_group_id' => 2,
61 'name' => 'Test activity type',
62 'label' => 'Test activity type',
63 'sequential' => 1,
64 ));
65 $this->test_activity_type_id = $activityTypes['id'];
66 $this->_params = array(
67 'source_contact_id' => $this->_contactID,
68 'activity_type_id' => $activityTypes['values'][0]['value'],
69 'subject' => 'test activity type id',
70 'activity_date_time' => '2011-06-02 14:36:13',
71 'status_id' => 2,
72 'priority_id' => 1,
73 'duration' => 120,
74 'location' => 'Pennsylvania',
75 'details' => 'a test activity',
76 );
77 // create a logged in USER since the code references it for source_contact_id
78 $this->createLoggedInUser();
79 }
80
81 /**
82 * Tears down the fixture, for example, closes a network connection.
83 *
84 * This method is called after a test is executed.
85 */
86 public function tearDown() {
87 $tablesToTruncate = array(
88 'civicrm_contact',
89 'civicrm_activity',
90 'civicrm_activity_contact',
91 'civicrm_uf_match',
92 );
93 $this->quickCleanup($tablesToTruncate, TRUE);
94 $type = $this->callAPISuccess('optionValue', 'get', array('id' => $this->test_activity_type_id));
95 if (!empty($type['count'])) {
96 $this->callAPISuccess('option_value', 'delete', array('id' => $this->test_activity_type_id));
97 }
98 }
99
100 /**
101 * Test that activity.get api works when filtering on subject.
102 */
103 public function testSearchBySubjectOnly() {
104 $subject = 'test activity ' . __FUNCTION__;
105 $params = $this->_params;
106 $params['subject'] = $subject;
107 $activity = $this->callAPISuccess('Activity', 'Create', $params);
108
109 $case = array(
110 'form_value' => array(
111 'activity_text' => $subject,
112 'activity_option' => 3,
113 ),
114 'expected_count' => 1,
115 'expected_contact' => array($this->_contactID),
116 );
117 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
118 list($select, $from, $where, $having) = $query->query();
119 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
120 foreach ($groupContacts as $key => $value) {
121 $groupContacts[$key] = $value['id'];
122 }
123 $this->assertEquals($case['expected_count'], count($groupContacts));
124 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
125 }
126
127 /**
128 * Test that activity.get api works when filtering on subject.
129 */
130 public function testSearchBySubjectBoth() {
131 $subject = 'test activity ' . __FUNCTION__;
132 $params = $this->_params;
133 $params['subject'] = $subject;
134 $activity = $this->callAPISuccess('Activity', 'Create', $params);
135
136 $case = array(
137 'form_value' => array(
138 'activity_text' => $subject,
139 'activity_option' => 6,
140 ),
141 'expected_count' => 1,
142 'expected_contact' => array($this->_contactID),
143 );
144 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
145 list($select, $from, $where, $having) = $query->query();
146 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
147 foreach ($groupContacts as $key => $value) {
148 $groupContacts[$key] = $value['id'];
149 }
150 $this->assertEquals($case['expected_count'], count($groupContacts));
151 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
152 }
153
154 /**
155 * Test that activity.get api works when filtering on subject.
156 */
157 public function testSearchByDetailsOnly() {
158 $details = 'test activity ' . __FUNCTION__;
159 $params = $this->_params;
160 $params['details'] = $details;
161 $activity = $this->callAPISuccess('Activity', 'Create', $params);
162
163 $case = array(
164 'form_value' => array(
165 'activity_text' => $details,
166 'activity_option' => 2,
167 ),
168 'expected_count' => 1,
169 'expected_contact' => array($this->_contactID),
170 );
171 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
172 list($select, $from, $where, $having) = $query->query();
173 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
174 foreach ($groupContacts as $key => $value) {
175 $groupContacts[$key] = $value['id'];
176 }
177 $this->assertEquals($case['expected_count'], count($groupContacts));
178 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
179 }
180
181 /**
182 * Test that activity.get api works when filtering on details.
183 */
184 public function testSearchByDetailsBoth() {
185 $details = 'test activity ' . __FUNCTION__;
186 $params = $this->_params;
187 $params['details'] = $details;
188 $activity = $this->callAPISuccess('Activity', 'Create', $params);
189
190 $case = array(
191 'form_value' => array(
192 'activity_text' => $details,
193 'activity_option' => 6,
194 ),
195 'expected_count' => 1,
196 'expected_contact' => array($this->_contactID),
197 );
198 $query = new CRM_Contact_BAO_Query(CRM_Contact_BAO_Query::convertFormValues($case['form_value']));
199 list($select, $from, $where, $having) = $query->query();
200 $groupContacts = CRM_Core_DAO::executeQuery("SELECT DISTINCT contact_a.id $from $where")->fetchAll();
201 foreach ($groupContacts as $key => $value) {
202 $groupContacts[$key] = $value['id'];
203 }
204 $this->assertEquals($case['expected_count'], count($groupContacts));
205 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
206 }
207
208}