Merge pull request #15676 from artfulrobot/handle-exceptions-from-payment-processors
[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
43 protected $_contactID;
44
45 protected $_params;
46
47 protected $test_activity_type_value;
48
49 /**
50 * Test setup for every test.
51 *
52 * Connect to the database, truncate the tables that will be used
53 * and redirect stdin to a temporary file
54 */
55 public function setUp() {
56 // Connect to the database
57 parent::setUp();
58
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',
65 'sequential' => 1,
66 ]);
67 $this->test_activity_type_id = $activityTypes['id'];
68 $this->_params = [
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',
73 'status_id' => 2,
74 'priority_id' => 1,
75 'duration' => 120,
76 'location' => 'Pennsylvania',
77 'details' => 'a test activity',
78 ];
79 // create a logged in USER since the code references it for source_contact_id
80 $this->createLoggedInUser();
81 }
82
83 /**
84 * Tears down the fixture, for example, closes a network connection.
85 *
86 * This method is called after a test is executed.
87 */
88 public function tearDown() {
89 $tablesToTruncate = [
90 'civicrm_contact',
91 'civicrm_activity',
92 'civicrm_activity_contact',
93 'civicrm_uf_match',
94 ];
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]);
99 }
100 }
101
102 /**
103 * Test that activity.get api works when filtering on subject.
104 */
105 public function testSearchBySubjectOnly() {
106 $subject = 'test activity ' . __FUNCTION__;
107 $params = $this->_params;
108 $params['subject'] = $subject;
109 $this->callAPISuccess('Activity', 'Create', $params);
110
111 $case = [
112 'form_value' => [
113 'activity_text' => $subject,
114 'activity_option' => 3,
115 ],
116 'expected_count' => 1,
117 'expected_contact' => [$this->_contactID],
118 ];
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'];
124 }
125 $this->assertEquals($case['expected_count'], count($groupContacts));
126 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
127 }
128
129 /**
130 * Test that activity.get api works when filtering on subject.
131 */
132 public function testSearchBySubjectBoth() {
133 $subject = 'test activity ' . __FUNCTION__;
134 $params = $this->_params;
135 $params['subject'] = $subject;
136 $activity = $this->callAPISuccess('Activity', 'Create', $params);
137
138 $case = [
139 'form_value' => [
140 'activity_text' => $subject,
141 'activity_option' => 6,
142 ],
143 'expected_count' => 1,
144 'expected_contact' => [$this->_contactID],
145 ];
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'];
151 }
152 $this->assertEquals($case['expected_count'], count($groupContacts));
153 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
154 }
155
156 /**
157 * Test that activity.get api works when filtering on subject.
158 */
159 public function testSearchByDetailsOnly() {
160 $details = 'test activity ' . __FUNCTION__;
161 $params = $this->_params;
162 $params['details'] = $details;
163 $activity = $this->callAPISuccess('Activity', 'Create', $params);
164
165 $case = [
166 'form_value' => [
167 'activity_text' => $details,
168 'activity_option' => 2,
169 ],
170 'expected_count' => 1,
171 'expected_contact' => [$this->_contactID],
172 ];
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'];
178 }
179 $this->assertEquals($case['expected_count'], count($groupContacts));
180 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
181 }
182
183 /**
184 * Test that activity.get api works when filtering on details.
185 */
186 public function testSearchByDetailsBoth() {
187 $details = 'test activity ' . __FUNCTION__;
188 $params = $this->_params;
189 $params['details'] = $details;
190 $activity = $this->callAPISuccess('Activity', 'Create', $params);
191
192 $case = [
193 'form_value' => [
194 'activity_text' => $details,
195 'activity_option' => 6,
196 ],
197 'expected_count' => 1,
198 'expected_contact' => [$this->_contactID],
199 ];
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'];
205 }
206 $this->assertEquals($case['expected_count'], count($groupContacts));
207 $this->checkArrayEquals($case['expected_contact'], $groupContacts);
208 }
209
210 }