Merge pull request #13731 from eileenmcnaughton/5.11
[civicrm-core.git] / tests / phpunit / CRM / Contact / BAO / GroupContactCacheTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Test class for CRM_Contact_BAO_GroupContact BAO
30 *
31 * @package CiviCRM
32 * @group headless
33 */
34 class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase {
35
36 /**
37 * Manually add and remove contacts from a smart group.
38 */
39 public function testManualAddRemove() {
40 list($group, $living, $deceased) = $this->setupSmartGroup();
41
42 // Add $n1 to $g
43 $this->callAPISuccess('group_contact', 'create', array(
44 'contact_id' => $living[0]->id,
45 'group_id' => $group->id,
46 ));
47
48 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
49 $this->assertCacheMatches(
50 array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id, $living[0]->id),
51 $group->id
52 );
53
54 // Remove $y1 from $g
55 $this->callAPISuccess('group_contact', 'create', array(
56 'contact_id' => $deceased[0]->id,
57 'group_id' => $group->id,
58 'status' => 'Removed',
59 ));
60
61 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
62 $this->assertCacheMatches(
63 array(
64 $deceased[1]->id,
65 $deceased[2]->id,
66 $living[0]->id,
67 ),
68 $group->id
69 );
70 }
71
72 /**
73 * Allow removing contact from a parent group even if contact is in a child group. (CRM-8858).
74 */
75 public function testRemoveFromParentSmartGroup() {
76 // Create smart group $parent
77 $params = array(
78 'name' => 'Deceased Contacts',
79 'title' => 'Deceased Contacts',
80 'is_active' => 1,
81 'formValues' => array('is_deceased' => 1),
82 );
83 $parent = CRM_Contact_BAO_Group::createSmartGroup($params);
84 $this->registerTestObjects(array($parent));
85
86 // Create group $child in $parent
87 $params = array(
88 'name' => 'Child Group',
89 'title' => 'Child Group',
90 'is_active' => 1,
91 'parents' => array($parent->id => 1),
92 );
93 $child = CRM_Contact_BAO_Group::create($params);
94 $this->registerTestObjects(array($child));
95
96 // Create $c1, $c2, $c3
97 $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3);
98
99 // Add $c1, $c2, $c3 to $child
100 foreach ($deceased as $contact) {
101 $this->callAPISuccess('group_contact', 'create', array(
102 'contact_id' => $contact->id,
103 'group_id' => $child->id,
104 ));
105 }
106
107 CRM_Contact_BAO_GroupContactCache::load($parent, TRUE);
108 $this->assertCacheMatches(
109 array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id),
110 $parent->id
111 );
112
113 // Remove $c1 from $parent
114 $this->callAPISuccess('group_contact', 'create', array(
115 'contact_id' => $deceased[0]->id,
116 'group_id' => $parent->id,
117 'status' => 'Removed',
118 ));
119
120 // Assert $c1 not in $parent
121 CRM_Contact_BAO_GroupContactCache::load($parent, TRUE);
122 $this->assertCacheMatches(
123 array(
124 $deceased[1]->id,
125 $deceased[2]->id,
126 ),
127 $parent->id
128 );
129
130 // Assert $c1 still in $child
131 $this->assertDBQuery(1,
132 'select count(*) from civicrm_group_contact where group_id=%1 and contact_id=%2 and status=%3',
133 array(
134 1 => array($child->id, 'Integer'),
135 2 => array($deceased[0]->id, 'Integer'),
136 3 => array('Added', 'String'),
137 )
138 );
139 }
140
141 /**
142 * Assert that the cache for a group contains exactly the listed contacts.
143 *
144 * @param array $expectedContactIds
145 * Array(int).
146 * @param int $groupId
147 */
148 public function assertCacheMatches($expectedContactIds, $groupId) {
149 $sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
150 $params = array(1 => array($groupId, 'Integer'));
151 $dao = CRM_Core_DAO::executeQuery($sql, $params);
152 $actualContactIds = array();
153 while ($dao->fetch()) {
154 $actualContactIds[] = $dao->contact_id;
155 }
156
157 sort($expectedContactIds);
158 sort($actualContactIds);
159 $this->assertEquals($expectedContactIds, $actualContactIds);
160 }
161
162 /**
163 * Test the opportunistic refresh cache function does not touch non-expired entries.
164 */
165 public function testOpportunisticRefreshCacheNoChangeIfNotExpired() {
166 list($group, $living, $deceased) = $this->setupSmartGroup();
167 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
168 $this->assertCacheMatches(
169 array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id),
170 $group->id
171 );
172 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
173
174 $this->assertCacheNotRefreshed($deceased, $group);
175 }
176
177 /**
178 * Test the opportunistic refresh cache function does refresh stale entries.
179 */
180 public function testOpportunisticRefreshChangeIfCacheDateFieldStale() {
181 list($group, $living, $deceased) = $this->setupSmartGroup();
182 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
183 CRM_Core_DAO::executeQuery('UPDATE civicrm_group SET cache_date = DATE_SUB(NOW(), INTERVAL 7 MINUTE) WHERE id = ' . $group->id);
184 $group->find(TRUE);
185 Civi::$statics['CRM_Contact_BAO_GroupContactCache']['is_refresh_init'] = FALSE;
186 sleep(1);
187 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
188
189 $this->assertCacheRefreshed($group);
190 }
191
192 /**
193 * Test the opportunistic refresh cache function does refresh expired entries if mode is deterministic.
194 */
195 public function testOpportunisticRefreshNoChangeWithDeterministicSetting() {
196 list($group, $living, $deceased) = $this->setupSmartGroup();
197 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'deterministic'));
198 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
199 $this->makeCacheStale($group);
200 CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush();
201 $this->assertCacheNotRefreshed($deceased, $group);
202 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'opportunistic'));
203 }
204
205 /**
206 * Test the deterministic cache function refreshes with the deterministic setting.
207 */
208 public function testDeterministicRefreshChangeWithDeterministicSetting() {
209 list($group, $living, $deceased) = $this->setupSmartGroup();
210 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'deterministic'));
211 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
212 $this->makeCacheStale($group);
213 CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
214 $this->assertCacheRefreshed($group);
215 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'opportunistic'));
216 }
217
218 /**
219 * Test the deterministic cache function refresh doesn't mess up non-expired.
220 */
221 public function testDeterministicRefreshChangeDoesNotTouchNonExpired() {
222 list($group, $living, $deceased) = $this->setupSmartGroup();
223 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'deterministic'));
224 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
225 CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
226 $this->assertCacheNotRefreshed($deceased, $group);
227 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'opportunistic'));
228 }
229
230 /**
231 * Test the deterministic cache function refreshes with the opportunistic setting.
232 *
233 * (hey it's an opportunity!).
234 */
235 public function testDeterministicRefreshChangeWithOpportunisticSetting() {
236 list($group, $living, $deceased) = $this->setupSmartGroup();
237 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'opportunistic'));
238 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
239 $this->makeCacheStale($group);
240 CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
241 $this->assertCacheRefreshed($group);
242 }
243
244 /**
245 * Test the api job wrapper around the deterministic refresh works.
246 */
247 public function testJobWrapper() {
248 list($group, $living, $deceased) = $this->setupSmartGroup();
249 $this->callAPISuccess('Setting', 'create', array('smart_group_cache_refresh_mode' => 'opportunistic'));
250 $this->callAPISuccess('Contact', 'create', array('id' => $deceased[0]->id, 'is_deceased' => 0));
251 $this->makeCacheStale($group);
252 $this->callAPISuccess('Job', 'group_cache_flush', array());
253 $this->assertCacheRefreshed($group);
254 }
255
256 // *** Everything below this should be moved to parent class ****
257
258 /**
259 * @var array(DAO_Name => array(int)) List of items to garbage-collect during tearDown
260 */
261 private $_testObjects;
262
263 /**
264 * Sets up the fixture, for example, opens a network connection.
265 *
266 * This method is called before a test is executed.
267 */
268 protected function setUp() {
269 $this->_testObjects = array();
270 parent::setUp();
271 }
272
273 /**
274 * Tears down the fixture, for example, closes a network connection.
275 *
276 * This method is called after a test is executed.
277 */
278 protected function tearDown() {
279 parent::tearDown();
280 $this->deleteTestObjects();
281 }
282
283 /**
284 * This is a wrapper for CRM_Core_DAO::createTestObject which tracks created entities.
285 *
286 * @see CRM_Core_DAO::createTestObject
287 *
288 * @param string $daoName
289 * @param array $params
290 * @param int $numObjects
291 * @param bool $createOnly
292 *
293 * @return array|NULL|object
294 */
295 public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) {
296 $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly);
297 if (is_array($objects)) {
298 $this->registerTestObjects($objects);
299 }
300 else {
301 $this->registerTestObjects(array($objects));
302 }
303 return $objects;
304 }
305
306 /**
307 * Register test objects.
308 *
309 * @param array $objects
310 * DAO or BAO objects.
311 */
312 public function registerTestObjects($objects) {
313 foreach ($objects as $object) {
314 $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object));
315 $this->_testObjects[$daoName][] = $object->id;
316 }
317 }
318
319 /**
320 * Delete test objects.
321 *
322 * Note: You might argue that the FK relations between test
323 * objects could make this problematic; however, it should
324 * behave intuitively as long as we mentally split our
325 * test-objects between the "manual/primary records"
326 * and the "automatic/secondary records"
327 */
328 public function deleteTestObjects() {
329 foreach ($this->_testObjects as $daoName => $daoIds) {
330 foreach ($daoIds as $daoId) {
331 CRM_Core_DAO::deleteTestObjects($daoName, array('id' => $daoId));
332 }
333 }
334 $this->_testObjects = array();
335 }
336
337 /**
338 * Set up a smart group testing scenario.
339 *
340 * @return array
341 */
342 protected function setupSmartGroup() {
343 $params = array(
344 'name' => 'Deceased Contacts',
345 'title' => 'Deceased Contacts',
346 'is_active' => 1,
347 'formValues' => array('is_deceased' => 1),
348 );
349 $group = CRM_Contact_BAO_Group::createSmartGroup($params);
350 $this->registerTestObjects(array($group));
351
352 // Create contacts $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g
353 $living = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 0), 3);
354 $deceased = $this->createTestObject('CRM_Contact_DAO_Contact', array('is_deceased' => 1), 3);
355 $this->assertEquals(3, count($deceased));
356 $this->assertEquals(3, count($living));
357
358 // Assert: $g cache has exactly $y1, $y2, $y3
359 CRM_Contact_BAO_GroupContactCache::load($group, TRUE);
360 $group->find(TRUE);
361 $this->assertCacheMatches(
362 array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id),
363 $group->id
364 );
365 // Reload the group so we have the cache_date & refresh_date.
366 return array($group, $living, $deceased);
367 }
368
369 /**
370 * @param $deceased
371 * @param $group
372 *
373 * @throws \Exception
374 */
375 protected function assertCacheNotRefreshed($deceased, $group) {
376 $this->assertCacheMatches(
377 array($deceased[0]->id, $deceased[1]->id, $deceased[2]->id),
378 $group->id
379 );
380 $afterGroup = $this->callAPISuccessGetSingle('Group', array('id' => $group->id));
381 $this->assertEquals($group->cache_date, $afterGroup['cache_date']);
382 }
383
384 /**
385 * Make the cache for the group stale, resetting it to before the timeout period.
386 *
387 * @param CRM_Contact_BAO_Group $group
388 */
389 protected function makeCacheStale(&$group) {
390 CRM_Core_DAO::executeQuery('UPDATE civicrm_group SET cache_date = DATE_SUB(NOW(), INTERVAL 7 MINUTE) WHERE id = ' . $group->id);
391 unset($group->cache_date);
392 $group->find(TRUE);
393 Civi::$statics['CRM_Contact_BAO_GroupContactCache']['is_refresh_init'] = FALSE;
394 }
395
396 /**
397 * @param $group
398 *
399 * @throws \Exception
400 */
401 protected function assertCacheRefreshed($group) {
402 $this->assertCacheMatches(
403 array(),
404 $group->id
405 );
406
407 $afterGroup = $this->callAPISuccessGetSingle('Group', array('id' => $group->id));
408 $this->assertTrue(empty($afterGroup['cache_date']), 'refresh date should not be set as the cache is not built');
409 $this->assertTrue(empty($afterGroup['refresh_date']), 'refresh date should not be set as the cache is not built');
410 }
411
412 /**
413 * Test Smart group search
414 */
415 public function testSmartGroupSearchBuilder() {
416 $returnProperties = array(
417 'contact_type' => 1,
418 'contact_sub_type' => 1,
419 'sort_name' => 1,
420 'group' => 1,
421 );
422 list($group, $living, $deceased) = $this->setupSmartGroup();
423
424 $params = array(
425 'name' => 'Living Contacts',
426 'title' => 'Living Contacts',
427 'is_active' => 1,
428 'formValues' => array('is_deceased' => 0),
429 );
430 $group2 = CRM_Contact_BAO_Group::createSmartGroup($params);
431
432 //Filter on smart group with =, !=, IN and NOT IN operator.
433 $params = array(array('group', '=', $group2->id, 1, 0));
434 $query = new CRM_Contact_BAO_Query(
435 $params, $returnProperties,
436 NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS,
437 FALSE,
438 FALSE, FALSE
439 );
440 $ids = $query->searchQuery(0, 0, NULL,
441 FALSE, FALSE, FALSE,
442 TRUE, FALSE
443 );
444 $key = $query->getGroupCacheTableKey();
445 $expectedWhere = "civicrm_group_contact_cache_{$key}.group_id IN (\"{$group2->id}\")";
446 $this->assertContains($expectedWhere, $query->_whereClause);
447 $this->_assertContactIds($query, "group_id = {$group2->id}");
448
449 $params = array(array('group', '!=', $group->id, 1, 0));
450 $query = new CRM_Contact_BAO_Query(
451 $params, $returnProperties,
452 NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS,
453 FALSE,
454 FALSE, FALSE
455 );
456 $key = $query->getGroupCacheTableKey();
457 //Assert if proper where clause is present.
458 $expectedWhere = "civicrm_group_contact_{$key}.group_id != {$group->id} AND civicrm_group_contact_cache_{$key}.group_id IS NULL OR ( civicrm_group_contact_cache_{$key}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
459 $this->assertContains($expectedWhere, $query->_whereClause);
460 $this->_assertContactIds($query, "group_id != {$group->id}");
461
462 $params = array(array('group', 'IN', array($group->id, $group2->id), 1, 0));
463 $query = new CRM_Contact_BAO_Query(
464 $params, $returnProperties,
465 NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS,
466 FALSE,
467 FALSE, FALSE
468 );
469 $key = $query->getGroupCacheTableKey();
470 $expectedWhere = "civicrm_group_contact_cache_{$key}.group_id IN (\"{$group->id}\", \"{$group2->id}\")";
471 $this->assertContains($expectedWhere, $query->_whereClause);
472 $this->_assertContactIds($query, "group_id IN ({$group->id}, {$group2->id})");
473
474 $params = array(array('group', 'NOT IN', array($group->id), 1, 0));
475 $query = new CRM_Contact_BAO_Query(
476 $params, $returnProperties,
477 NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTACTS,
478 FALSE,
479 FALSE, FALSE
480 );
481 $key = $query->getGroupCacheTableKey();
482 $expectedWhere = "civicrm_group_contact_{$key}.group_id NOT IN ( {$group->id} ) AND civicrm_group_contact_cache_{$key}.group_id IS NULL OR ( civicrm_group_contact_cache_{$key}.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
483 $this->assertContains($expectedWhere, $query->_whereClause);
484 $this->_assertContactIds($query, "group_id NOT IN ({$group->id})");
485 }
486
487 /**
488 * Check if contact ids are fetched correctly.
489 *
490 * @param object $query
491 * @param string $groupWhereClause
492 */
493 public function _assertContactIds($query, $groupWhereClause) {
494 $contactIds = explode(',', $query->searchQuery(0, 0, NULL,
495 FALSE, FALSE, FALSE,
496 TRUE, FALSE
497 ));
498 $expectedContactIds = array();
499 $groupDAO = CRM_Core_DAO::executeQuery("SELECT contact_id FROM civicrm_group_contact_cache WHERE {$groupWhereClause}");
500 while ($groupDAO->fetch()) {
501 $expectedContactIds[] = $groupDAO->contact_id;
502 }
503 $this->assertEquals(sort($expectedContactIds), sort($contactIds));
504 }
505
506 }