Skip to content

Commit

Permalink
Fix test to do things in a logical order so contacts exist when the g…
Browse files Browse the repository at this point in the history
…roup is created
  • Loading branch information
eileenmcnaughton committed May 15, 2021
1 parent 6877b82 commit 336d3b1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
59 changes: 33 additions & 26 deletions tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase {

/**
* Manually add and remove contacts from a smart group.
*
* @throws \API_Exception
* @throws \CRM_Core_Exception
* @throws \CiviCRM_API3_Exception
*/
public function testManualAddRemove() {
list($group, $living, $deceased) = $this->setupSmartGroup();
public function testManualAddRemove(): void {
[$group, $living, $deceased] = $this->setupSmartGroup();

// Add $n1 to $g
$this->callAPISuccess('group_contact', 'create', [
$this->callAPISuccess('GroupContact', 'create', [
'contact_id' => $living[0]->id,
'group_id' => $group->id,
]);
Expand Down Expand Up @@ -54,9 +58,15 @@ public function testManualAddRemove() {
}

/**
* Allow removing contact from a parent group even if contact is in a child group. (CRM-8858).
* Allow removing contact from a parent group even if contact is in a child
* group. (CRM-8858).
*
* @throws \CRM_Core_Exception
*/
public function testRemoveFromParentSmartGroup() {
public function testRemoveFromParentSmartGroup(): void {
// Create $c1, $c2, $c3
$deceased = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 1], 3);

// Create smart group $parent
$params = [
'name' => 'Deceased Contacts',
Expand All @@ -77,9 +87,6 @@ public function testRemoveFromParentSmartGroup() {
$child = CRM_Contact_BAO_Group::create($params);
$this->registerTestObjects([$child]);

// Create $c1, $c2, $c3
$deceased = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 1], 3);

// Add $c1, $c2, $c3 to $child
foreach ($deceased as $contact) {
$this->callAPISuccess('group_contact', 'create', [
Expand All @@ -95,7 +102,7 @@ public function testRemoveFromParentSmartGroup() {
);

// Remove $c1 from $parent
$this->callAPISuccess('group_contact', 'create', [
$this->callAPISuccess('GroupContact', 'create', [
'contact_id' => $deceased[0]->id,
'group_id' => $parent->id,
'status' => 'Removed',
Expand Down Expand Up @@ -129,7 +136,7 @@ public function testRemoveFromParentSmartGroup() {
* Array(int).
* @param int $groupId
*/
public function assertCacheMatches($expectedContactIds, $groupId) {
public function assertCacheMatches($expectedContactIds, $groupId): void {
$sql = 'SELECT contact_id FROM civicrm_group_contact_cache WHERE group_id = %1';
$params = [1 => [$groupId, 'Integer']];
$dao = CRM_Core_DAO::executeQuery($sql, $params);
Expand All @@ -147,7 +154,7 @@ public function assertCacheMatches($expectedContactIds, $groupId) {
* Test the opportunistic refresh cache function does not touch non-expired entries.
*/
public function testOpportunisticRefreshCacheNoChangeIfNotExpired() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
$this->assertCacheMatches(
[$deceased[0]->id, $deceased[1]->id, $deceased[2]->id],
Expand All @@ -162,7 +169,7 @@ public function testOpportunisticRefreshCacheNoChangeIfNotExpired() {
* Test the opportunistic refresh cache function does refresh stale entries.
*/
public function testOpportunisticRefreshChangeIfCacheDateFieldStale() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
CRM_Core_DAO::executeQuery('UPDATE civicrm_group SET cache_date = DATE_SUB(NOW(), INTERVAL 7 MINUTE) WHERE id = ' . $group->id);
$group->find(TRUE);
Expand All @@ -177,7 +184,7 @@ public function testOpportunisticRefreshChangeIfCacheDateFieldStale() {
* Test the opportunistic refresh cache function does refresh expired entries if mode is deterministic.
*/
public function testOpportunisticRefreshNoChangeWithDeterministicSetting() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Setting', 'create', ['smart_group_cache_refresh_mode' => 'deterministic']);
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
$this->makeCacheStale($group);
Expand All @@ -190,7 +197,7 @@ public function testOpportunisticRefreshNoChangeWithDeterministicSetting() {
* Test the deterministic cache function refreshes with the deterministic setting.
*/
public function testDeterministicRefreshChangeWithDeterministicSetting() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Setting', 'create', ['smart_group_cache_refresh_mode' => 'deterministic']);
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
$this->makeCacheStale($group);
Expand All @@ -203,7 +210,7 @@ public function testDeterministicRefreshChangeWithDeterministicSetting() {
* Test the deterministic cache function refresh doesn't mess up non-expired.
*/
public function testDeterministicRefreshChangeDoesNotTouchNonExpired() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Setting', 'create', ['smart_group_cache_refresh_mode' => 'deterministic']);
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
CRM_Contact_BAO_GroupContactCache::deterministicCacheFlush();
Expand All @@ -217,7 +224,7 @@ public function testDeterministicRefreshChangeDoesNotTouchNonExpired() {
* (hey it's an opportunity!).
*/
public function testDeterministicRefreshChangeWithOpportunisticSetting() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Setting', 'create', ['smart_group_cache_refresh_mode' => 'opportunistic']);
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
$this->makeCacheStale($group);
Expand All @@ -229,7 +236,7 @@ public function testDeterministicRefreshChangeWithOpportunisticSetting() {
* Test the api job wrapper around the deterministic refresh works.
*/
public function testJobWrapper() {
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();
$this->callAPISuccess('Setting', 'create', ['smart_group_cache_refresh_mode' => 'opportunistic']);
$this->callAPISuccess('Contact', 'create', ['id' => $deceased[0]->id, 'is_deceased' => 0]);
$this->makeCacheStale($group);
Expand Down Expand Up @@ -314,7 +321,13 @@ public function deleteTestObjects() {
*
* @return array
*/
protected function setupSmartGroup() {
protected function setupSmartGroup(): array {
// Create contacts $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g
$living = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 0], 3);
$deceased = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 1], 3);
$this->assertCount(3, $deceased);
$this->assertCount(3, $living);

$params = [
'name' => 'Deceased Contacts',
'title' => 'Deceased Contacts',
Expand All @@ -324,12 +337,6 @@ protected function setupSmartGroup() {
$group = CRM_Contact_BAO_Group::createSmartGroup($params);
$this->registerTestObjects([$group]);

// Create contacts $y1, $y2, $y3 which do match $g; create $n1, $n2, $n3 which do not match $g
$living = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 0], 3);
$deceased = $this->createTestObject('CRM_Contact_DAO_Contact', ['is_deceased' => 1], 3);
$this->assertEquals(3, count($deceased));
$this->assertEquals(3, count($living));

// Assert: $g cache has exactly $y1, $y2, $y3
CRM_Contact_BAO_GroupContactCache::load($group);
$group->find(TRUE);
Expand Down Expand Up @@ -393,7 +400,7 @@ public function testSmartGroupSearchBuilder() {
'sort_name' => 1,
'group' => 1,
];
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();

$params = [
'name' => 'Living Contacts',
Expand Down Expand Up @@ -467,7 +474,7 @@ public function testMultipleGroupWhereClause() {
'sort_name' => 1,
'group' => 1,
];
list($group, $living, $deceased) = $this->setupSmartGroup();
[$group, $living, $deceased] = $this->setupSmartGroup();

$params = [
'name' => 'Living Contacts',
Expand Down
35 changes: 20 additions & 15 deletions tests/phpunit/CRM/Mailing/BAO/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,27 @@ public function hook_civicrm_aclGroup($type, $contactID, $tableName, &$allGroups
* contact 8 : smart 5 (base)
*
* here 'contact 1 : static 0 (inc)' identified as static group $groupIDs[0]
* that has 'contact 1' identified as $contactIDs[0] and Included in the mailing recipient list
* that has 'contact 1' identified as $contactIDs[0] and Included in the
* mailing recipient list
*
* @throws \CiviCRM_API3_Exception
* @throws \CRM_Core_Exception
* @throws \API_Exception
*/
public function testgetRecipientsEmailGroupIncludeExclude() {
public function testGetRecipientsEmailGroupIncludeExclude(): void {
// Create contacts
$contactIDs = [
$this->individualCreate(['last_name' => 'smart5'], 0),
$this->individualCreate([], 1),
$this->individualCreate([], 2),
$this->individualCreate([], 3),
$this->individualCreate(['last_name' => 'smart3'], 4),
$this->individualCreate(['last_name' => 'smart3'], 5),
$this->individualCreate(['last_name' => 'smart4'], 6),
$this->individualCreate(['last_name' => 'smart4'], 7),
$this->individualCreate(['last_name' => 'smart5'], 8),
];

// Set up groups; 3 standard, 4 smart
$groupIDs = [];
for ($i = 0; $i < 7; $i++) {
Expand All @@ -291,19 +309,6 @@ public function testgetRecipientsEmailGroupIncludeExclude() {
}
}

// Create contacts
$contactIDs = [
$this->individualCreate(['last_name' => 'smart5'], 0),
$this->individualCreate([], 1),
$this->individualCreate([], 2),
$this->individualCreate([], 3),
$this->individualCreate(['last_name' => 'smart3'], 4),
$this->individualCreate(['last_name' => 'smart3'], 5),
$this->individualCreate(['last_name' => 'smart4'], 6),
$this->individualCreate(['last_name' => 'smart4'], 7),
$this->individualCreate(['last_name' => 'smart5'], 8),
];

// Add contacts to static groups
$this->callAPISuccess('GroupContact', 'Create', [
'group_id' => $groupIDs[0],
Expand Down

0 comments on commit 336d3b1

Please sign in to comment.