From d20cd5d41468ab958760d3a68980847e4db6e153 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Sat, 13 May 2017 12:28:14 +1000 Subject: [PATCH] Add Tests of processInbound function with hook --- CRM/SMS/Provider.php | 11 +++++------ tests/phpunit/CRM/SMS/ProviderTest.php | 21 +++++++++++++++++++++ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/CRM/SMS/Provider.php b/CRM/SMS/Provider.php index 9119b7c1ddd0..bc7476766f27 100644 --- a/CRM/SMS/Provider.php +++ b/CRM/SMS/Provider.php @@ -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'))); } @@ -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; } } @@ -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); diff --git a/tests/phpunit/CRM/SMS/ProviderTest.php b/tests/phpunit/CRM/SMS/ProviderTest.php index 217c071ba2fe..23c509f94a73 100644 --- a/tests/phpunit/CRM/SMS/ProviderTest.php +++ b/tests/phpunit/CRM/SMS/ProviderTest.php @@ -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; + } + } /**