Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
81621fee | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 | 27 | |
6a488035 TO |
28 | /** |
29 | * Test class for CRM_Contact_BAO_GroupContact BAO | |
30 | * | |
6c6e6187 | 31 | * @package CiviCRM |
acb109b7 | 32 | * @group headless |
6a488035 TO |
33 | */ |
34 | class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { | |
35 | ||
36 | /** | |
eceb18cc | 37 | * Manually add and remove contacts from a smart group. |
6a488035 | 38 | */ |
00be9182 | 39 | public function testManualAddRemove() { |
0a0e1aab | 40 | list($group, $living, $deceased) = $this->setupSmartGroup(); |
6a488035 TO |
41 | |
42 | // Add $n1 to $g | |
2828c36d | 43 | $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
44 | 'contact_id' => $living[0]->id, |
45 | 'group_id' => $group->id, | |
6a488035 | 46 | )); |
2828c36d | 47 | |
6a488035 TO |
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 | |
2828c36d | 55 | $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
56 | 'contact_id' => $deceased[0]->id, |
57 | 'group_id' => $group->id, | |
58 | 'status' => 'Removed', | |
6a488035 | 59 | )); |
2828c36d | 60 | |
6a488035 TO |
61 | CRM_Contact_BAO_GroupContactCache::load($group, TRUE); |
62 | $this->assertCacheMatches( | |
2828c36d | 63 | array( |
92915c55 TO |
64 | $deceased[1]->id, |
65 | $deceased[2]->id, | |
28a04ea9 | 66 | $living[0]->id, |
92915c55 | 67 | ), |
6a488035 TO |
68 | $group->id |
69 | ); | |
70 | } | |
b6708aeb | 71 | |
6a488035 | 72 | /** |
2828c36d | 73 | * Allow removing contact from a parent group even if contact is in a child group. (CRM-8858). |
6a488035 | 74 | */ |
00be9182 | 75 | public function testRemoveFromParentSmartGroup() { |
6a488035 TO |
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) { | |
2828c36d | 101 | $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
102 | 'contact_id' => $contact->id, |
103 | 'group_id' => $child->id, | |
6a488035 | 104 | )); |
6a488035 TO |
105 | } |
106 | ||
6a488035 TO |
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 | ); | |
b6708aeb | 112 | |
6a488035 | 113 | // Remove $c1 from $parent |
2828c36d | 114 | $this->callAPISuccess('group_contact', 'create', array( |
6a488035 TO |
115 | 'contact_id' => $deceased[0]->id, |
116 | 'group_id' => $parent->id, | |
117 | 'status' => 'Removed', | |
6a488035 | 118 | )); |
b6708aeb | 119 | |
6a488035 TO |
120 | // Assert $c1 not in $parent |
121 | CRM_Contact_BAO_GroupContactCache::load($parent, TRUE); | |
122 | $this->assertCacheMatches( | |
2828c36d | 123 | array( |
92915c55 | 124 | $deceased[1]->id, |
28a04ea9 | 125 | $deceased[2]->id, |
92915c55 | 126 | ), |
6a488035 TO |
127 | $parent->id |
128 | ); | |
b6708aeb | 129 | |
6a488035 | 130 | // Assert $c1 still in $child |
b6708aeb | 131 | $this->assertDBQuery(1, |
6a488035 TO |
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 | /** | |
eceb18cc | 142 | * Assert that the cache for a group contains exactly the listed contacts. |
6a488035 | 143 | * |
5a4f6742 | 144 | * @param array $expectedContactIds |
e16033b4 | 145 | * Array(int). |
5a4f6742 | 146 | * @param int $groupId |
6a488035 | 147 | */ |
00be9182 | 148 | public function assertCacheMatches($expectedContactIds, $groupId) { |
6a488035 TO |
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 | ||
0a0e1aab | 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 | ); | |
2b68a50c | 172 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
0a0e1aab | 173 | |
2a4fb809 | 174 | $this->assertCacheNotRefreshed($deceased, $group); |
0a0e1aab | 175 | } |
176 | ||
177 | /** | |
2a4fb809 | 178 | * Test the opportunistic refresh cache function does refresh stale entries. |
0a0e1aab | 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); | |
2a4fb809 | 184 | $group->find(TRUE); |
0a0e1aab | 185 | Civi::$statics['CRM_Contact_BAO_GroupContactCache']['is_refresh_init'] = FALSE; |
186 | sleep(1); | |
2b68a50c | 187 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
0a0e1aab | 188 | |
2a4fb809 | 189 | $this->assertCacheRefreshed($group); |
190 | } | |
0a0e1aab | 191 | |
2a4fb809 | 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); | |
2b68a50c | 200 | CRM_Contact_BAO_GroupContactCache::opportunisticCacheFlush(); |
2a4fb809 | 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); | |
2b68a50c | 213 | CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush(); |
2a4fb809 | 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)); | |
2b68a50c | 225 | CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush(); |
2a4fb809 | 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); | |
2b68a50c | 240 | CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush(); |
2a4fb809 | 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); | |
0a0e1aab | 254 | } |
255 | ||
6a488035 TO |
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. | |
2828c36d | 265 | * |
6a488035 | 266 | * This method is called before a test is executed. |
6a488035 TO |
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. | |
2828c36d | 275 | * |
6a488035 | 276 | * This method is called after a test is executed. |
6a488035 TO |
277 | */ |
278 | protected function tearDown() { | |
279 | parent::tearDown(); | |
eb68c129 | 280 | $this->deleteTestObjects(); |
6a488035 TO |
281 | } |
282 | ||
283 | /** | |
2828c36d | 284 | * This is a wrapper for CRM_Core_DAO::createTestObject which tracks created entities. |
6a488035 TO |
285 | * |
286 | * @see CRM_Core_DAO::createTestObject | |
2828c36d | 287 | * |
288 | * @param string $daoName | |
1e1fdcf6 EM |
289 | * @param array $params |
290 | * @param int $numObjects | |
291 | * @param bool $createOnly | |
2828c36d | 292 | * |
293 | * @return array|NULL|object | |
6a488035 | 294 | */ |
28a04ea9 | 295 | public function createTestObject($daoName, $params = array(), $numObjects = 1, $createOnly = FALSE) { |
6a488035 TO |
296 | $objects = CRM_Core_DAO::createTestObject($daoName, $params, $numObjects, $createOnly); |
297 | if (is_array($objects)) { | |
298 | $this->registerTestObjects($objects); | |
0db6c3e1 TO |
299 | } |
300 | else { | |
6a488035 TO |
301 | $this->registerTestObjects(array($objects)); |
302 | } | |
303 | return $objects; | |
304 | } | |
305 | ||
306 | /** | |
2828c36d | 307 | * Register test objects. |
308 | * | |
5a4f6742 CW |
309 | * @param array $objects |
310 | * DAO or BAO objects. | |
6a488035 | 311 | */ |
00be9182 | 312 | public function registerTestObjects($objects) { |
6a488035 TO |
313 | foreach ($objects as $object) { |
314 | $daoName = preg_replace('/_BAO_/', '_DAO_', get_class($object)); | |
315 | $this->_testObjects[$daoName][] = $object->id; | |
316 | } | |
317 | } | |
318 | ||
2828c36d | 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 | */ | |
00be9182 | 328 | public function deleteTestObjects() { |
6a488035 TO |
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 | ||
0a0e1aab | 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 | ||
2a4fb809 | 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 | ||
6a488035 | 412 | } |