Merge pull request #14283 from eileenmcnaughton/db_test3
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / ActivitySearchTest.php
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 */
41 class CRM_Contact_BAO_ActivitySearchTest extends CiviUnitTestCase {
42 protected $_contactID;
43 protected $_params;
44 protected $test_activity_type_value;
45
46 /**
47 * Test setup for every test.
48 *
49 * Connect to the database, truncate the tables that will be used
50 * and redirect stdin to a temporary file
51 */
52 public function setUp() {
53 // Connect to the database
54 parent::setUp();
55
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',
62 'sequential' => 1,
63 ));
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',
70 'status_id' => 2,
71 'priority_id' => 1,
72 'duration' => 120,
73 'location' => 'Pennsylvania',
74 'details' => 'a test activity',
75 );
76 // create a logged in USER since the code references it for source_contact_id
77 $this->createLoggedInUser();
78 }
79
80 /**
81 * Tears down the fixture, for example, closes a network connection.
82 *
83 * This method is called after a test is executed.
84 */
85 public function tearDown() {
86 $tablesToTruncate = array(
87 'civicrm_contact',
88 'civicrm_activity',
89 'civicrm_activity_contact',
90 'civicrm_uf_match',
91 );
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));
96 }
97 }
98
99 /**
100 * Test that activity.get api works when filtering on subject.
101 */
102 public function testSearchBySubjectOnly() {
103 $subject = 'test activity ' . __FUNCTION__;
104 $params = $this->_params;
105 $params['subject'] = $subject;
106 $this->callAPISuccess('Activity', 'Create', $params);
107
108 $case = array(
109 'form_value' => array(
110 'activity_text' => $subject,
111 'activity_option' => 3,
112 ),
113 'expected_count' => 1,
114 'expected_contact' => array($this->_contactID),
115 );
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'];
121 }
122 $this->assertEquals($case['expected_count'], count($groupContacts));
123 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
124 }
125
126 /**
127 * Test that activity.get api works when filtering on subject.
128 */
129 public function testSearchBySubjectBoth() {
130 $subject = 'test activity ' . __FUNCTION__;
131 $params = $this->_params;
132 $params['subject'] = $subject;
133 $activity = $this->callAPISuccess('Activity', 'Create', $params);
134
135 $case = array(
136 'form_value' => array(
137 'activity_text' => $subject,
138 'activity_option' => 6,
139 ),
140 'expected_count' => 1,
141 'expected_contact' => array($this->_contactID),
142 );
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'];
148 }
149 $this->assertEquals($case['expected_count'], count($groupContacts));
150 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
151 }
152
153 /**
154 * Test that activity.get api works when filtering on subject.
155 */
156 public function testSearchByDetailsOnly() {
157 $details = 'test activity ' . __FUNCTION__;
158 $params = $this->_params;
159 $params['details'] = $details;
160 $activity = $this->callAPISuccess('Activity', 'Create', $params);
161
162 $case = array(
163 'form_value' => array(
164 'activity_text' => $details,
165 'activity_option' => 2,
166 ),
167 'expected_count' => 1,
168 'expected_contact' => array($this->_contactID),
169 );
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'];
175 }
176 $this->assertEquals($case['expected_count'], count($groupContacts));
177 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
178 }
179
180 /**
181 * Test that activity.get api works when filtering on details.
182 */
183 public function testSearchByDetailsBoth() {
184 $details = 'test activity ' . __FUNCTION__;
185 $params = $this->_params;
186 $params['details'] = $details;
187 $activity = $this->callAPISuccess('Activity', 'Create', $params);
188
189 $case = array(
190 'form_value' => array(
191 'activity_text' => $details,
192 'activity_option' => 6,
193 ),
194 'expected_count' => 1,
195 'expected_contact' => array($this->_contactID),
196 );
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'];
202 }
203 $this->assertEquals($case['expected_count'], count($groupContacts));
204 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
205 }
206
207 }