dev/core#534 Re-instate pay-now button
[civicrm-core.git] / tests / phpunit / CRM / Contact / Page / AjaxTest.php
CommitLineData
1f51ef4e 1<?php
2
3/**
4 * @group headless
5 */
6class CRM_Contact_Page_AjaxTest extends CiviUnitTestCase {
7
e0001b13 8 /**
9 * Original $_REQUEST
10 *
11 * We are messing with globals so fix afterwards.
12 *
13 * @var array
14 */
15 protected $originalRequest = [];
1f51ef4e 16
17 public function setUp() {
18 $this->useTransaction(TRUE);
19 parent::setUp();
e0001b13 20 $this->originalRequest = $_REQUEST;
21 }
22
23 public function tearDown() {
24 $_REQUEST = $this->originalRequest;
25 parent::tearDown();
1f51ef4e 26 }
27
28 /**
29 * Minimal test on the testGetDupes function to make sure it completes without error.
30 */
31 public function testGetDedupes() {
32 $_REQUEST['gid'] = 1;
33 $_REQUEST['rgid'] = 1;
34 $_REQUEST['columns'] = array(
35 array(
36 'search' => array(
37 'value' => array(
38 'src' => 'first_name',
39 ),
40 ),
41 'data' => 'src',
42 ),
43 );
44 $_REQUEST['is_unit_test'] = TRUE;
45 $result = CRM_Contact_Page_AJAX::getDedupes();
46 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
47 }
48
ed92673b 49 public function testGetDedupesPostCode() {
50 $_REQUEST['gid'] = 1;
51 $_REQUEST['rgid'] = 1;
52 $_REQUEST['snippet'] = 4;
53 $_REQUEST['draw'] = 3;
54 $_REQUEST['columns'] = array(
55 0 => array(
56 'data' => 'is_selected_input',
57 'name' => '',
58 'searchable' => TRUE,
59 'search' => array(
60 'value' => '',
61 'regex' => FALSE,
62 ),
63 ),
64
65 1 => array(
66 'data' => 'src_image',
67 'name' => '',
68 'searchable' => TRUE,
69 'orderable' => FALSE,
70 'search' => array(
71 'value' => '',
72 'regex' => FALSE,
73 ),
74 ),
75
76 2 => array(
77 'data' => 'src',
78 'name' => '',
79 'searchable' => TRUE,
80 'orderable' => TRUE,
81 'search' => array(
82 'value' => '',
83 'regex' => FALSE,
84 ),
85 ),
86
87 3 => array(
88 'data' => 'dst_image',
89 'name' => '',
90 'searchable' => TRUE,
91 'orderable' => FALSE,
92 'search' => array(
93 'value' => '',
94 'regex' => FALSE,
95 ),
96 ),
97
98 4 => array(
99 'data' => 'dst',
100 'name' => '',
101 'searchable' => TRUE,
102 'orderable' => TRUE,
103 'search' => array(
104 'value' => '',
105 'regex' => FALSE,
106 ),
107 ),
108
109 5 => array(
110 'data' => 'src_email',
111 'name' => '',
112 'searchable' => TRUE,
113 'orderable' => TRUE,
114 'search' => array(
115 'value' => '',
116 'regex' => FALSE,
117 ),
118 ),
119
120 6 => array(
121 'data' => 'dst_email',
122 'name' => '',
123 'searchable' => TRUE,
124 'orderable' => TRUE,
125 'search' => array(
126 'value' => '',
127 'regex' => FALSE,
128 ),
129 ),
130
131 7 => array(
132 'data' => 'src_street',
133 'name' => '',
134 'searchable' => TRUE,
135 'orderable' => TRUE,
136 'search' => array(
137 'value' => '',
138 'regex' => FALSE,
139 ),
140 ),
141
142 8 => array(
143 'data' => 'dst_street',
144 'name' => '',
145 'searchable' => TRUE,
146 'orderable' => TRUE,
147 'search' => array(
148 'value' => '',
149 'regex' => FALSE,
150 ),
151 ),
152
153 9 => array(
154 'data' => 'src_postcode',
155 'name' => '',
156 'searchable' => TRUE,
157 'orderable' => TRUE,
158 'search' => array(
159 'value' => 123,
160 'regex' => FALSE,
161 ),
162 ),
163
164 10 => array(
165 'data' => 'dst_postcode',
166 'name' => '',
167 'searchable' => TRUE,
168 'orderable' => TRUE,
169 'search' => array(
170 'value' => '',
171 'regex' => FALSE,
172 ),
173 ),
174
175 11 => array(
176 'data' => 'conflicts',
177 'name' => '',
178 'searchable' => TRUE,
179 'orderable' => TRUE,
180 'search' => array(
181 'value' => '',
182 'regex' => FALSE,
183 ),
184 ),
185
186 12 => array(
187 'data' => 'weight',
188 'name' => '',
189 'searchable' => TRUE,
190 'orderable' => TRUE,
191 'search' => array(
192 'value' => '',
193 'regex' => FALSE,
194 ),
195 ),
196
197 13 => array(
198 'data' => 'actions',
199 'name' => '',
200 'searchable' => TRUE,
201 'orderable' => FALSE,
202 'search' => array(
203 'value' => '',
204 'regex' => FALSE,
205 ),
206 ),
207 );
208
209 $_REQUEST['start'] = 0;
210 $_REQUEST['length'] = 10;
211 $_REQUEST['search'] = array(
212 'value' => '',
213 'regex' => FALSE,
214 );
215
216 $_REQUEST['_'] = 1466478641007;
217 $_REQUEST['Drupal_toolbar_collapsed'] = 0;
218 $_REQUEST['has_js'] = 1;
219 $_REQUEST['SESSa06550b3043ecca303761d968e3c846a'] = 'qxSxw0F_UmBITMM0JaVwTRcHV1bQqBSHNmBMY9AA8Wk';
220
221 $_REQUEST['is_unit_test'] = TRUE;
222
223 $result = CRM_Contact_Page_AJAX::getDedupes();
224 $this->assertEquals(array('data' => array(), 'recordsTotal' => 0, 'recordsFiltered' => 0), $result);
225 }
226
dfe61fcf 227 /**
228 * CRM-20621 : Test to check usage count of Tag tree
229 */
230 public function testGetTagTree() {
231 $contacts = array();
232 // create three contacts
233 for ($i = 0; $i < 3; $i++) {
234 $contacts[] = $this->individualCreate();
235 }
236
237 // Create Tag called as 'Parent Tag'
238 $parentTag = $this->tagCreate(array(
239 'name' => 'Parent Tag',
240 'used_for' => 'civicrm_contact',
241 ));
242 //assign first contact to parent tag
243 $params = array(
244 'entity_id' => $contacts[0],
245 'entity_table' => 'civicrm_contact',
246 'tag_id' => $parentTag['id'],
247 );
248 // TODO: EntityTag.create API is not working
249 CRM_Core_BAO_EntityTag::add($params);
250
251 // Create child Tag of $parentTag
252 $childTag1 = $this->tagCreate(array(
253 'name' => 'Child Tag Level 1',
254 'parent_id' => $parentTag['id'],
255 'used_for' => 'civicrm_contact',
256 ));
257 //assign contact to this level 1 child tag
258 $params = array(
259 'entity_id' => $contacts[1],
260 'entity_table' => 'civicrm_contact',
261 'tag_id' => $childTag1['id'],
262 );
263 CRM_Core_BAO_EntityTag::add($params);
264
265 // Create child Tag of $childTag1
266 $childTag2 = $this->tagCreate(array(
267 'name' => 'Child Tag Level 2',
268 'parent_id' => $childTag1['id'],
269 'used_for' => 'civicrm_contact',
270 ));
271 //assign contact to this level 2 child tag
272 $params = array(
273 'entity_id' => $contacts[2],
274 'entity_table' => 'civicrm_contact',
275 'tag_id' => $childTag2['id'],
276 );
277 CRM_Core_BAO_EntityTag::add($params);
278
5e3e44a3 279 // CASE I : check the usage count of parent tag which need to be 1
280 // as the one contact added
dfe61fcf 281 $_REQUEST['is_unit_test'] = TRUE;
282 $parentTagTreeResult = CRM_Admin_Page_AJAX::getTagTree();
283 foreach ($parentTagTreeResult as $result) {
284 if ($result['id'] == $parentTag['id']) {
5e3e44a3 285 $this->assertEquals(1, $result['data']['usages']);
dfe61fcf 286 }
287 }
288
5e3e44a3 289 // CASE 2 : check the usage count of level 1 child tag, which needs to be 1
290 // as it should include the count of added one contact
dfe61fcf 291 $_GET['parent_id'] = $parentTag['id'];
292 $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
5e3e44a3 293 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
dfe61fcf 294
295 // CASE 2 : check the usage count of child tag at level 2
296 //which needs to be 1 as it has no child tag
297 $_GET['parent_id'] = $childTag1['id'];
298 $childTagTree = CRM_Admin_Page_AJAX::getTagTree();
299 $this->assertEquals(1, $childTagTree[0]['data']['usages']);
300
6e80ee11 301 // CASE 3 : check the tag IDs returned on searching with 'Level'
302 // which needs to array('parent tag id', 'level 1 child tag id', 'level 2 child tag id')
303 unset($_GET['parent_id']);
304 $_GET['str'] = 'Level';
305 $tagIDs = CRM_Admin_Page_AJAX::getTagTree();
306 $expectedTagIDs = array($parentTag['id'], $childTag1['id'], $childTag2['id']);
307 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
308
309 // CASE 4 : check the tag IDs returned on searching with 'Level 1'
310 // which needs to array('parent tag id', 'level 1 child tag id')
311 $_GET['str'] = 'Level 1';
312 $tagIDs = CRM_Admin_Page_AJAX::getTagTree();
313 $expectedTagIDs = array($parentTag['id'], $childTag1['id']);
314 $this->checkArrayEquals($tagIDs, $expectedTagIDs);
315
dfe61fcf 316 //cleanup
317 foreach ($contacts as $id) {
318 $this->callAPISuccess('Contact', 'delete', array('id' => $id));
319 }
320 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag2['id']));
321 $this->callAPISuccess('Tag', 'delete', array('id' => $childTag1['id']));
322 $this->callAPISuccess('Tag', 'delete', array('id' => $parentTag['id']));
323 }
324
da0136df 325 /**
326 * Test to check contact reference field
327 */
328 public function testContactReference() {
329 //create group
330 $groupId1 = $this->groupCreate();
331 $groupId2 = $this->groupCreate(array(
332 'name' => 'Test Group 2',
333 'domain_id' => 1,
334 'title' => 'New Test Group2 Created',
335 'description' => 'New Test Group2 Created',
336 'is_active' => 1,
337 'visibility' => 'User and User Admin Only',
338 ));
339
340 $contactIds = array();
341 foreach (array($groupId1, $groupId2) as $groupId) {
342 $this->groupContactCreate($groupId);
343 $contactIds = array_merge($contactIds, CRM_Contact_BAO_Group::getGroupContacts($groupId));
344 }
9b0a35c2 345 $contactIds = CRM_Utils_Array::collect('contact_id', $contactIds);
da0136df 346
347 // create custom group with contact reference field
348 $customGroup = $this->customGroupCreate(array('extends' => 'Contact', 'title' => 'select_test_group'));
349 $params = array(
350 'custom_group_id' => $customGroup['id'],
351 'name' => 'Worker_Lookup',
352 'label' => 'Worker Lookup',
353 // limit this field to two groups created above
354 'filter' => "action=lookup&group={$groupId1},{$groupId2}",
355 'html_type' => 'Autocomplete-Select',
356 'data_type' => 'ContactReference',
357 'weight' => 4,
358 'is_searchable' => 1,
359 'is_active' => 1,
360 );
361 $customField = $this->callAPISuccess('custom_field', 'create', $params);
362
363 $_GET = array(
364 'id' => $customField['id'],
365 'is_unit_test' => TRUE,
366 );
367 $contactList = CRM_Contact_Page_AJAX::contactReference();
9b0a35c2 368 $contactList = CRM_Utils_Array::collect('id', $contactList);
da0136df 369
370 //assert each returned contact id to be present in group contact
371 foreach ($contactList as $contactId) {
372 $this->assertTrue(in_array($contactId, $contactIds));
373 }
374 }
375
1f51ef4e 376}