From 867de24d7a87503aa2ae20e32a28068ae0ec14a9 Mon Sep 17 00:00:00 2001 From: Jamie McClelland Date: Tue, 2 Feb 2021 14:41:16 -0500 Subject: [PATCH] ensure we send to the right mobile numbers Depending on the order of contacts retrieved and whether or not the mobile number is set to primary, we may repeat the same mobile number over and over again. --- CRM/Contact/Form/Task/SMSCommon.php | 1 + .../CRM/Contact/Form/Task/SMSCommonTest.php | 110 ++++++++++++++++++ 2 files changed, 111 insertions(+) create mode 100644 tests/phpunit/CRM/Contact/Form/Task/SMSCommonTest.php diff --git a/CRM/Contact/Form/Task/SMSCommon.php b/CRM/Contact/Form/Task/SMSCommon.php index 269f1f4018..8c689f14ad 100644 --- a/CRM/Contact/Form/Task/SMSCommon.php +++ b/CRM/Contact/Form/Task/SMSCommon.php @@ -174,6 +174,7 @@ class CRM_Contact_Form_Task_SMSCommon { $form->_allContactDetails = $form->_contactDetails; foreach ($form->_contactIds as $key => $contactId) { + $mobilePhone = NULL; $value = $form->_contactDetails[$contactId]; //to check if the phone type is "Mobile" diff --git a/tests/phpunit/CRM/Contact/Form/Task/SMSCommonTest.php b/tests/phpunit/CRM/Contact/Form/Task/SMSCommonTest.php new file mode 100644 index 0000000000..694125265d --- /dev/null +++ b/tests/phpunit/CRM/Contact/Form/Task/SMSCommonTest.php @@ -0,0 +1,110 @@ +individualCreate([ + 'first_name' => 'First', + 'last_name' => 'Person', + 'do_not_sms' => 0, + 'phone' => [ + 1 => [ + 'phone_type_id' => $mobile_type_id, + 'location_type_id' => 1, + 'phone' => '1111111111' + ] + ] + ]); + $contact2 = $this->individualCreate([ + 'first_name' => 'Second', + 'last_name' => 'Person', + 'do_not_sms' => 0, + 'phone' => [ + 1 => [ + 'phone_type_id' => $phone_type_id, + 'location_type_id' => 1, + 'phone' => '9999999999', + 'is_primary' => 1, + ], + 2 => [ + 'phone_type_id' => $mobile_type_id, + 'location_type_id' => 1, + 'phone' => '2222222222' + ] + ] + ]); + $contact3 = $this->individualCreate([ + 'first_name' => 'Third', + 'last_name' => 'Person', + 'do_not_sms' => 0, + 'phone' => [ + 1 => [ + 'phone_type_id' => $mobile_type_id, + 'location_type_id' => 1, + 'phone' => '3333333333', + 'is_primary' => 0, + ] + ] + ]); + + // Track the contact id to correct mobile phone number. + $this->_contactMobileNumbers = [ + $contact1 => "1111111111", + $contact2 => "2222222222", + $contact3 => "3333333333" + ]; + + $this->_contactIds = array_keys($this->_contactMobileNumbers); + + } + + /** + * Test to ensure SMS Activity QuickForm displays the right phone numbers. + * + * @throws \API_Exception + * @throws \CRM_Core_Exception + * @throws \CiviCRM_API3_Exception + * @throws \Civi\API\Exception\UnauthorizedException + */ + public function testQuickFormMobileNumbersDisplay() { + $form = $this->getFormObject('CRM_Core_Form'); + $form->_contactIds = $this->_contactIds; + $form->_single = FALSE; + CRM_Contact_Form_Task_SMSCommon::buildQuickForm($form); + $contacts = json_decode($form->get_template_vars('toContact')); + foreach ($contacts as $contact) { + $id = $contact->id; + // id is in the format: contact_id::phone_number, e.g.: 5::2222222222 + $ret = preg_match('/^([0-9]+)::([0-9]+)/', $id, $matches); + $this->assertEquals(1, $ret, "Failed to find mobile number."); + $contact_id = $matches[1]; + $phone_number = $matches[2]; + $this->assertEquals($phone_number, $this->_contactMobileNumbers[$contact_id], "Returned incorrect mobile number in SMS send quick form."); + } + } + +} -- 2.25.1