Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CRM-15984: "Add campaign field on Email activity form" #10317

Merged
merged 2 commits into from
May 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,6 +1403,8 @@ public static function deprecatedGetActivitySQLClause($input) {
* Contact ids.
* @param string $additionalDetails
* The additional information of CC and BCC appended to the activity Details.
* @param array $contributionIds
* @param int $campaignId
*
* @return array
* ( sent, activityId) if any email is sent and activityId
Expand All @@ -1420,7 +1422,8 @@ public static function sendEmail(
$bcc = NULL,
$contactIds = NULL,
$additionalDetails = NULL,
$contributionIds = NULL
$contributionIds = NULL,
$campaignId = NULL
) {
// get the contact details of logged in contact, which we set as from email
if ($userID == NULL) {
Expand Down Expand Up @@ -1469,6 +1472,7 @@ public static function sendEmail(
'details' => $details,
// FIXME: check for name Completed and get ID from that lookup
'status_id' => 2,
'campaign_id' => $campaignId,
);

// CRM-5916: strip [case #…] before saving the activity (if present in subject)
Expand Down
6 changes: 5 additions & 1 deletion CRM/Contact/Form/Task/EmailCommon.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,9 @@ public static function buildQuickForm(&$form) {
}
}

//Added for CRM-15984: Add campaign field
CRM_Campaign_BAO_Campaign::addCampaign($form);

$form->addFormRule(array('CRM_Contact_Form_Task_EmailCommon', 'formRule'), $form);
CRM_Core_Resources::singleton()->addScriptFile('civicrm', 'templates/CRM/Contact/Form/Task/EmailCommon.js', 0, 'html-header');
}
Expand Down Expand Up @@ -531,7 +534,8 @@ public static function postProcess(&$form) {
$bcc,
array_keys($form->_toContactDetails),
$additionalDetails,
$contributionIds
$contributionIds,
CRM_Utils_Array::value('campaign_id', $formValues)
);

$followupStatus = '';
Expand Down
2 changes: 2 additions & 0 deletions templates/CRM/Contact/Form/Task/Email.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
{help id="id-token-subject" tplFile=$tplFile isAdmin=$isAdmin file="CRM/Contact/Form/Task/Email.hlp"}
</td>
</tr>
{* CRM-15984 --add campaign to email activities *}
{include file="CRM/Campaign/Form/addCampaignToComponent.tpl" campaignTrClass="crm-contactEmail-form-block-campaign_id"}
</table>

{include file="CRM/Contact/Form/Task/EmailCommon.tpl"}
Expand Down
113 changes: 112 additions & 1 deletion tests/phpunit/CRM/Activity/BAO/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ public function setUp() {
* Clean up after tests.
*/
public function tearDown() {
$tablesToTruncate = array('civicrm_activity', 'civicrm_activity_contact', 'civicrm_email');
$tablesToTruncate = array(
'civicrm_activity',
'civicrm_activity_contact',
'civicrm_uf_match',
'civicrm_campaign',
'civicrm_email',
);
$this->quickCleanup($tablesToTruncate);
$this->cleanUpAfterACLs();
parent::tearDown();
Expand Down Expand Up @@ -808,4 +814,109 @@ protected function setUpForActivityDashboardTests() {
);
}

public function testSendEmailBasic() {
$contactId = $this->individualCreate();

// create a logged in USER since the code references it for sendEmail user.
$this->createLoggedInUser();
$session = CRM_Core_Session::singleton();
$loggedInUser = $session->get('userID');

$contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion));
$contactDetailsIntersectKeys = array(
'contact_id' => '',
'sort_name' => '',
'display_name' => '',
'do_not_email' => '',
'preferred_mail_format' => '',
'is_deceased' => '',
'email' => '',
'on_hold' => '',
);
$contactDetails = array(
array_intersect_key($contact, $contactDetailsIntersectKeys),
);

$subject = __FUNCTION__ . ' subject';
$html = __FUNCTION__ . ' html';
$text = __FUNCTION__ . ' text';
$userID = $loggedInUser;

list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail(
$contactDetails,
$subject,
$text,
$html,
$contact['email'],
$userID,
$from = __FUNCTION__ . '@example.com'
);

$activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion));
$details = "-ALTERNATIVE ITEM 0-
$html
-ALTERNATIVE ITEM 1-
$text
-ALTERNATIVE END-
";
$this->assertEquals($activity['details'], $details, 'Activity details does not match.');
$this->assertEquals($activity['subject'], $subject, 'Activity subject does not match.');
}

public function testSendEmailWithCampaign() {
// Create a contact and contactDetails array.
$contactId = $this->individualCreate();

// create a logged in USER since the code references it for sendEmail user.
$this->createLoggedInUser();
$session = CRM_Core_Session::singleton();
$loggedInUser = $session->get('userID');

$contact = $this->civicrm_api('contact', 'getsingle', array('id' => $contactId, 'version' => $this->_apiversion));
$contactDetailsIntersectKeys = array(
'contact_id' => '',
'sort_name' => '',
'display_name' => '',
'do_not_email' => '',
'preferred_mail_format' => '',
'is_deceased' => '',
'email' => '',
'on_hold' => '',
);
$contactDetails = array(
array_intersect_key($contact, $contactDetailsIntersectKeys),
);

// Create a campaign.
$result = $this->civicrm_api('Campaign', 'create', array(
'version' => $this->_apiversion,
'title' => __FUNCTION__ . ' campaign',
));
$campaign_id = $result['id'];

$subject = __FUNCTION__ . ' subject';
$html = __FUNCTION__ . ' html';
$text = __FUNCTION__ . ' text';
$userID = $loggedInUser;

list($sent, $activity_id) = $email_result = CRM_Activity_BAO_Activity::sendEmail(
$contactDetails,
$subject,
$text,
$html,
$contact['email'],
$userID,
$from = __FUNCTION__ . '@example.com',
$attachments = NULL,
$cc = NULL,
$bcc = NULL,
$contactIds = NULL,
$additionalDetails = NULL,
NULL,
$campaign_id
);
$activity = $this->civicrm_api('activity', 'getsingle', array('id' => $activity_id, 'version' => $this->_apiversion));
$this->assertEquals($activity['campaign_id'], $campaign_id, 'Activity campaign_id does not match.');
}

}