Skip to content

Commit

Permalink
Add unit test for GroupTokens
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Oct 5, 2022
1 parent 655771e commit b59a1fb
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 25 deletions.
17 changes: 9 additions & 8 deletions CRM/Mailing/Event/BAO/Confirm.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ class CRM_Mailing_Event_BAO_Confirm extends CRM_Mailing_Event_DAO_Confirm {
*
* @return bool
* True on success
* @throws \CRM_Core_Exception
*/
public static function confirm($contact_id, $subscribe_id, $hash) {
$se = &CRM_Mailing_Event_BAO_Subscribe::verify(
public static function confirm(int $contact_id, int $subscribe_id, string $hash) {
$se = CRM_Mailing_Event_BAO_Subscribe::verify(
$contact_id,
$subscribe_id,
$hash
Expand All @@ -52,7 +53,7 @@ public static function confirm($contact_id, $subscribe_id, $hash) {
// if so, we should ignore this request and hence avoid sending multiple
// emails - CRM-11157
$details = CRM_Contact_BAO_GroupContact::getMembershipDetail($contact_id, $se->group_id);
if ($details && $details->status == 'Added') {
if ($details && $details->status === 'Added') {
// This contact is already subscribed
// lets return the group title
return CRM_Core_DAO::getFieldValue(
Expand All @@ -79,9 +80,9 @@ public static function confirm($contact_id, $subscribe_id, $hash) {

$transaction->commit();

list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
[$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail();

list($display_name, $email) = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id);
[$display_name, $email] = CRM_Contact_BAO_Contact_Location::getEmailDetails($se->contact_id);

$group = new CRM_Contact_DAO_Group();
$group->id = $se->group_id;
Expand Down Expand Up @@ -118,12 +119,12 @@ public static function confirm($contact_id, $subscribe_id, $hash) {
$tokenProcessor = new TokenProcessor(\Civi::dispatcher(), [
'controller' => __CLASS__,
'smarty' => FALSE,
'schema' => ['contactId'],
'schema' => ['contactId', 'groupId'],
]);

$tokenProcessor->addMessage('body_html', $html, 'text/html');
$tokenProcessor->addMessage('body_text', $text, 'text/plain');
$tokenProcessor->addRow(['contactId' => $contact_id]);
$tokenProcessor->addRow(['contactId' => $contact_id, 'groupId' => $group->id]);
$tokenProcessor->evaluate();
$html = $tokenProcessor->getRow(0)->render('body_html');
$text = $tokenProcessor->getRow(0)->render('body_text');
Expand All @@ -140,7 +141,7 @@ public static function confirm($contact_id, $subscribe_id, $hash) {
'text' => $text,
];
// send - ignore errors because the desired status change has already been successful
$unused_result = CRM_Utils_Mail::send($mailParams);
CRM_Utils_Mail::send($mailParams);

return $group->title;
}
Expand Down
4 changes: 2 additions & 2 deletions CRM/Mailing/Event/BAO/Subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public static function &subscribe($group_id, $email, $contactId = NULL, $context
* @return object|null
* The subscribe event object, or null on failure
*/
public static function &verify($contact_id, $subscribe_id, $hash) {
public static function verify(int $contact_id, int $subscribe_id, $hash) {
$success = NULL;
$se = new CRM_Mailing_Event_BAO_Subscribe();
$se->contact_id = $contact_id;
Expand All @@ -175,7 +175,7 @@ public function send_confirm_request($email) {
$domain = CRM_Core_BAO_Domain::getDomain();

//get the default domain email address.
list($domainEmailName, $domainEmailAddress) = CRM_Core_BAO_Domain::getNameAndEmail();
[$domainEmailName, $domainEmailAddress] = CRM_Core_BAO_Domain::getNameAndEmail();

$localpart = CRM_Core_BAO_MailSettings::defaultLocalpart();
$emailDomain = CRM_Core_BAO_MailSettings::defaultDomain();
Expand Down
4 changes: 4 additions & 0 deletions Civi/Core/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,10 @@ public function createContainer() {
'CRM_Contribute_RecurTokens',
[]
))->addTag('kernel.event_subscriber')->setPublic(TRUE);
$container->setDefinition('crm_group_tokens', new Definition(
'CRM_Core_GroupTokens',
[]
))->addTag('kernel.event_subscriber')->setPublic(TRUE);
$container->setDefinition('crm_domain_tokens', new Definition(
'CRM_Core_DomainTokens',
[]
Expand Down
2 changes: 1 addition & 1 deletion Civi/Test/ContactTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public function contactDelete($contactID) {
* @return int
* groupId of created group
*/
public function groupCreate($params = []) {
public function groupCreate(array $params = []): int {
$params = array_merge([
'name' => 'Test Group 1',
'domain_id' => 1,
Expand Down
72 changes: 72 additions & 0 deletions tests/phpunit/CRM/Mailing/BAO/ConfirmTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/

use Civi\Api4\Group;
use Civi\Api4\GroupContact;
use Civi\Api4\Mailing;
use Civi\Api4\MailingGroup;
use Civi\Api4\SubscriptionHistory;

/**
* Class CRM_Mailing_BAO_MailingTest
*/
class CRM_Mailing_BAO_ConfirmTest extends CiviUnitTestCase {

/**
* Test confirm function, with group token.
*
* @throws CRM_Core_Exception
*/
public function testConfirm(): void {
$mailUtil = new CiviMailUtils($this);
$contactID = $this->individualCreate();
$groupID = Group::create()->setValues([
'name' => 'Test Group',
'title' => 'Test Group',
'frontend_title' => 'Test Group',
])->execute()->first()['id'];
GroupContact::create()->setValues(['contact_id' => $contactID, 'status' => 'Added', 'group_id' => $groupID])->execute();
SubscriptionHistory::create()->setValues([
'contact_id' => $contactID,
'group_id' => $groupID,
'method' => 'Email',
])->execute();
$mailingID = Mailing::create()->execute()->first()['id'];
MailingGroup::create()->setValues([
'mailing_id' => $mailingID,
'group_type' => 'Include',
'entity_table' => 'civicrm_group',
'entity_id' => $groupID,
])->execute()->first()['id'];

$mailingComponentID = $this->callAPISuccess('MailingComponent', 'get', ['component_type' => 'Welcome'])['id'];
$this->callAPISuccess('MailingComponent', 'create', [
// Swap {welcome.group} to {group.frontend_title} which is the standardised token.
// The intent is to make this version the default, but need to ensure it is required.
'body_html' => 'Welcome. Your subscription to the {group.frontend_title} mailing list has been activated.',
'body_text' => 'Welcome. Your subscription to the {group.frontend_title} mailing list has been activated.',
'id' => $mailingComponentID,
]);
$hash = 4;
CRM_Core_DAO::executeQuery("
INSERT INTO civicrm_mailing_event_subscribe (group_id, contact_id, hash) VALUES ($groupID, $contactID, $hash)
");

CRM_Mailing_Event_BAO_Confirm::confirm($contactID, 1, $hash);
$mailUtil->checkAllMailLog([
'From: "FIXME" <[email protected]>',
'To: "Mr. Anthony Anderson II" <[email protected]>',
'Subject: Your Subscription has been Activated',
'Welcome. Your subscription to the Test Group mailing list has been activated.',
]);
}

}
2 changes: 1 addition & 1 deletion tests/phpunit/CRM/Mailing/BAO/MailingJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CRM_Mailing_BAO_MailingJobTest extends CiviUnitTestCase {
/**
* Tests CRM_Mailing_BAO_MailingJob::isTemporaryError() method.
*/
public function testIsTemporaryError() {
public function testIsTemporaryError(): void {
$testcases[] = ['return' => TRUE, 'message' => 'Failed to set sender: [email protected] [SMTP: Invalid response code received from SMTP server while sending email. This is often caused by a misconfiguration in Outbound Email settings. Please verify the settings at Administer CiviCRM >> Global Settings >> Outbound Email (SMTP). (code: 421, response: Timeout waiting for data from client.)]'];
$testcases[] = ['return' => TRUE, 'message' => 'Failed to send data [SMTP: Invalid response code received from SMTP server while sending email. This is often caused by a misconfiguration in Outbound Email settings. Please verify the settings at Administer CiviCRM >> Global Settings >> Outbound Email (SMTP). (code: 454, response: Throttling failure: Maximum sending rate exceeded.)]'];
$testcases[] = ['return' => TRUE, 'message' => 'Failed to set sender: [email protected] [SMTP: Failed to write to socket: not connected (code: -1, response: )]'];
Expand Down
23 changes: 10 additions & 13 deletions tests/phpunit/CRM/Mailing/BAO/MailingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ private function assertRecipientsCorrect($mailingID, $expectedRecipients) {
* @param $mailingID
* @param $groupID
* @param string $type
*
* @return array|int
*/
private function createMailingGroup($mailingID, $groupID, $type = 'Include') {
private function createMailingGroup($mailingID, $groupID, string $type = 'Include') {
return $this->callAPISuccess('MailingGroup', 'create', [
'mailing_id' => $mailingID,
'group_type' => $type,
Expand All @@ -69,15 +70,15 @@ private function createMailingGroup($mailingID, $groupID, $type = 'Include') {
/**
* Test to ensure that using ACL permitted contacts are correctly fetched for bulk mailing
*/
public function testgetRecipientsUsingACL() {
public function testgetRecipientsUsingACL(): void {
$this->prepareForACLs();
$this->createLoggedInUser();
// create hook to build ACL where clause which choses $this->allowedContactId as the only contact to be considered as mail recipient
$this->hookClass->setHook('civicrm_aclWhereClause', [$this, 'aclWhereAllowedOnlyOne']);
CRM_Core_Config::singleton()->userPermissionClass->permissions = ['access CiviCRM', 'view my contact'];

// Create dummy group and assign 2 contacts
$name = 'Test static group ' . substr(sha1(rand()), 0, 7);
$name = 'Test static group 1';
$groupID = $this->groupCreate([
'name' => $name,
'title' => $name,
Expand Down Expand Up @@ -111,20 +112,16 @@ public function testgetRecipientsUsingACL() {
}

/**
* Test mailing receipients when using previous mailing as include and contact is in exclude as well
* Test mailing recipients when using previous mailing as include and contact is in exclude as well
*/
public function testMailingIncludePreviousMailingExcludeGroup() {
$groupName = 'Test static group ' . substr(sha1(rand()), 0, 7);
$groupName2 = 'Test static group 2' . substr(sha1(rand()), 0, 7);
public function testMailingIncludePreviousMailingExcludeGroup(): void {
$groupID = $this->groupCreate([
'name' => $groupName,
'title' => $groupName,
'is_active' => 1,
'name' => 'Test static group 1',
'title' => 'Test static group 1',
]);
$groupID2 = $this->groupCreate([
'name' => $groupName2,
'title' => $groupName2,
'is_active' => 1,
'name' => 'Test static group 2',
'title' => 'Test static group 2',
]);
$contactID = $this->individualCreate([], 0);
$contactID2 = $this->individualCreate([], 2);
Expand Down

0 comments on commit b59a1fb

Please sign in to comment.