Skip to content

Commit

Permalink
Fix CRM-15984: "Add campaign field on Email activity form"
Browse files Browse the repository at this point in the history
  • Loading branch information
twomice committed Jan 29, 2017
1 parent e0773cc commit 8229046
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 7 deletions.
4 changes: 3 additions & 1 deletion CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,8 @@ public static function sendEmail(
$cc = NULL,
$bcc = NULL,
$contactIds = NULL,
$additionalDetails = NULL
$additionalDetails = NULL,
$campaign_id = NULL
) {
// get the contact details of logged in contact, which we set as from email
if ($userID == NULL) {
Expand Down Expand Up @@ -1239,6 +1240,7 @@ public static function sendEmail(
'details' => $details,
// FIXME: check for name Completed and get ID from that lookup
'status_id' => 2,
'campaign_id' => $campaign_id,
);

// 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 @@ -525,7 +528,8 @@ public static function postProcess(&$form) {
$cc,
$bcc,
array_keys($form->_toContactDetails),
$additionalDetails
$additionalDetails,
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
117 changes: 112 additions & 5 deletions tests/phpunit/CRM/Activity/BAO/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,16 @@
* @group headless
*/
class CRM_Activity_BAO_ActivityTest extends CiviUnitTestCase {

public function setUp() {
parent::setUp();
}

public function tearDown() {
// truncate a few tables
$tablesToTruncate = array('civicrm_contact', 'civicrm_activity', 'civicrm_activity_contact');
$tablesToTruncate = array(
'civicrm_contact',
'civicrm_activity',
'civicrm_activity_contact',
'civicrm_uf_match',
'civicrm_campaign',
);
$this->quickCleanup($tablesToTruncate);
}

Expand Down Expand Up @@ -608,4 +611,108 @@ public function testGetActivitiesforContactSummaryWithNoActivities() {
$this->assertEquals(0, count($activities));
}

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,
$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.');
}

}

0 comments on commit 8229046

Please sign in to comment.