Skip to content

Commit

Permalink
Add Tests of processInbound function with hook
Browse files Browse the repository at this point in the history
  • Loading branch information
seamuslee001 committed May 15, 2017
1 parent eb8f750 commit 8119455
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
11 changes: 5 additions & 6 deletions CRM/SMS/Provider.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
if (!$message->fromContactID) {
// find sender by phone number if $fromContactID not set by hook
$formatFrom = '%' . $this->formatPhone($this->stripPhone($message->from), $like, "like");
$message->fromContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE '%1'", array(
$message->fromContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
1 => array($formatFrom, 'String')));
}

Expand Down Expand Up @@ -247,14 +247,14 @@ public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
$message->fromContactID = $fromContact->id;
}

if (!($message->toContactID)) {
if (!$message->toContactID) {
// find recipient if $toContactID not set by hook
if ($message->to) {
$message->toContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE '%1'", array(
$message->toContactID = CRM_Core_DAO::singleValueQuery("SELECT contact_id FROM civicrm_phone JOIN civicrm_contact ON civicrm_contact.id = civicrm_phone.contact_id WHERE !civicrm_contact.is_deleted AND phone LIKE %1", array(
1 => array('%' . $message->to, 'String')));
}
else {
$message->toContactID = $fromContactID;
$message->toContactID = $message->fromContactID;
}
}

Expand All @@ -273,8 +273,7 @@ public function processInbound($from, $body, $to = NULL, $trackID = NULL) {
'phone_number' => $message->from,
);
if ($message->trackID) {
$trackID = CRM_Utils_Type::escape($message->trackID, 'String');
$activityParams['result'] = $trackID;
$activityParams['result'] = CRM_Utils_Type::escape($message->trackID, 'String');
}

$result = CRM_Activity_BAO_Activity::create($activityParams);
Expand Down
21 changes: 21 additions & 0 deletions tests/phpunit/CRM/SMS/ProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,27 @@ public function testProcessInboundNoTo() {
$this->assertEquals($contact['id'], $activity['target_contact_id'][0]);
}

/**
* CRM-20238 Add test of ProcessInbound function where no To number is passed into the function but the toContactId gets set in a hook
*/
public function testProcessInboundSetToContactIDUsingHook() {
$provider = new testSMSProvider();
$this->hookClass->setHook('civicrm_inboundSMS', array($this, 'smsHookTest'));
$result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345');
$this->assertEquals('This is a test message', $result->details);
$this->assertEquals('+61412345678', $result->phone_number);
$this->assertEquals('12345', $result->result);
$contact = $this->callAPISuccess('contact', 'getsingle', array('phone' => '+61487654321'));
$activity = $this->callAPISuccess('activity', 'getsingle', array('id' => $result->id, 'return' => array('source_contact_id', 'target_contact_id', 'assignee_contact_id')));
$this->assertEquals($contact['id'], $activity['source_contact_id']);
}


public function smsHookTest(&$message) {
$testSourceContact = $this->individualCreate(array('phone' => array(1 => array('phone' => '+61487654321'))));
$message->toContactID = $testSourceContact;
}

}

/**
Expand Down

0 comments on commit 8119455

Please sign in to comment.