Skip to content

Commit

Permalink
Add test cover for subscription history create
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Feb 10, 2022
1 parent bead78b commit 6b85b95
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 47 deletions.
10 changes: 9 additions & 1 deletion CRM/Contact/BAO/GroupContact.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\SubscriptionHistory;
use Civi\Core\Event\PostEvent;

/**
Expand Down Expand Up @@ -47,7 +48,14 @@ public static function self_hook_civicrm_post(PostEvent $event): void {
$event->object->find(TRUE);
}
$params = $event->object->toArray();
CRM_Contact_BAO_SubscriptionHistory::create($params);
try {
SubscriptionHistory::save(FALSE)->setRecords([$params])->execute();
}
catch (API_Exception $e) {
// A failure to create the history might be a deadlock or similar
// This record is not important enough to trigger a larger fail.
Civi::log()->warning('Failed to add civicrm_subscription_history record with error :error', ['error' => $e->getMessage()]);
}
}
}

Expand Down
91 changes: 45 additions & 46 deletions tests/phpunit/api/v3/GroupContactTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
+--------------------------------------------------------------------+
*/

use Civi\Api4\SubscriptionHistory;

/**
* Class api_v3_GroupContactTest
* @group headless
Expand Down Expand Up @@ -36,8 +38,6 @@ class api_v3_GroupContactTest extends CiviUnitTestCase {
*/
public function setUp(): void {
parent::setUp();
$this->useTransaction(TRUE);

$this->_contactId = $this->individualCreate();

$this->_groupId1 = $this->groupCreate();
Expand Down Expand Up @@ -70,6 +70,14 @@ public function setUp(): void {
];
}

/**
* Cleanup after test.
*/
public function tearDown(): void {
$this->quickCleanup(['civicrm_group', 'civicrm_group_contact', 'civicrm_subscription_history']);
parent::tearDown();
}

/**
* Test GroupContact.get by ID.
*/
Expand Down Expand Up @@ -101,67 +109,43 @@ public function testGetGroupID() {
}
}

public function testCreateWithEmptyParams() {
$params = [];
$groups = $this->callAPIFailure('group_contact', 'create', $params);
$this->assertEquals($groups['error_message'],
'Mandatory key(s) missing from params array: group_id, contact_id'
);
}

public function testCreateWithoutGroupIdParams() {
$params = [
'contact_id' => $this->_contactId,
];

$groups = $this->callAPIFailure('group_contact', 'create', $params);
$this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: group_id');
}

public function testCreateWithoutContactIdParams() {
$params = [
'group_id' => $this->_groupId1,
];
$groups = $this->callAPIFailure('group_contact', 'create', $params);
$this->assertEquals($groups['error_message'], 'Mandatory key(s) missing from params array: contact_id');
}

public function testCreate() {
$cont = [
/**
* Test group contact create.
*/
public function testCreate(): void {
$this->_contactId1 = $this->individualCreate([
'first_name' => 'Amiteshwar',
'middle_name' => 'L.',
'last_name' => 'Prasad',
'prefix_id' => 3,
'suffix_id' => 3,
'email' => '[email protected]',
'contact_type' => 'Individual',
];

$this->_contactId1 = $this->individualCreate($cont);
]);
$params = [
'contact_id' => $this->_contactId,
'contact_id.2' => $this->_contactId1,
'group_id' => $this->_groupId1,
];

$result = $this->callAPIAndDocument('group_contact', 'create', $params, __FUNCTION__, __FILE__);
$this->assertEquals($result['not_added'], 1);
$this->assertEquals($result['added'], 1);
$this->assertEquals($result['total_count'], 2);
$result = $this->callAPIAndDocument('GroupContact', 'create', $params, __FUNCTION__, __FILE__);
$this->assertEquals(1, $result['not_added']);
$this->assertEquals(1, $result['added']);
$this->assertEquals(2, $result['total_count']);
}

/**
* Test GroupContact.delete by contact+group ID.
*/
public function testDelete() {
public function testDelete(): void {
$params = [
'contact_id' => $this->_contactId,
'group_id' => $this->_groupId1,
];

$result = $this->callAPIAndDocument('group_contact', 'delete', $params, __FUNCTION__, __FILE__);
$this->assertEquals($result['removed'], 1);
$this->assertEquals($result['total_count'], 1);
$this->assertEquals(1, $result['removed']);
$this->assertEquals(1, $result['total_count']);
}

public function testDeletePermanent() {
Expand Down Expand Up @@ -219,9 +203,16 @@ public function testDeleteAndReAddWithId() {
}

/**
* CRM-19979 test that group cotnact delete action works when contact is in status of pendin.
* CRM-19979 test that group contact delete action works when contact is in
* status of pending.
*
* @param int $version
*
* @dataProvider versionThreeAndFour
* @throws \API_Exception
*/
public function testDeleteWithPending() {
public function testDeleteWithPending(int $version): void {
$this->_apiversion = $version;
$groupId3 = $this->groupCreate([
'name' => 'Test Group 3',
'domain_id' => 1,
Expand All @@ -235,11 +226,19 @@ public function testDeleteWithPending() {
'group_id' => $groupId3,
'status' => 'Pending',
];
$groupContact = $this->callAPISuccess('groupContact', 'create', $groupContactCreateParams);
$groupGetContact = $this->CallAPISuccess('groupContact', 'get', $groupContactCreateParams);
$this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'status' => 'Removed']);
$this->callAPISuccess('groupContact', 'delete', ['id' => $groupGetContact['id'], 'skip_undelete' => TRUE]);
$this->callAPISuccess('group', 'delete', ['id' => $groupId3]);
$this->callAPISuccess('GroupContact', 'create', $groupContactCreateParams);
$groupGetContact = $this->CallAPISuccess('GroupContact', 'get', $groupContactCreateParams);
$history = SubscriptionHistory::get()
->addSelect('*')
->addWhere('group_id', '=', $groupId3)
->addWhere('status', '=', 'Pending')
->addWhere('contact_id', '=', $this->_contactId)
->execute();
$this->assertCount(1, $history);
$this->callAPISuccess('GroupContact', 'delete', ['id' => $groupGetContact['id'], 'status' => 'Removed']);
$this->callAPISuccess('GroupContact', 'delete', ['id' => $groupGetContact['id'], 'skip_undelete' => TRUE]);
$this->callAPISuccess('Group', 'delete', ['id' => $groupId3]);

}

/**
Expand Down

0 comments on commit 6b85b95

Please sign in to comment.