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