| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | +--------------------------------------------------------------------+ |
| 5 | | Copyright CiviCRM LLC. All rights reserved. | |
| 6 | | | |
| 7 | | This work is published under the GNU AGPLv3 license with some | |
| 8 | | permitted exceptions and without any warranty. For full license | |
| 9 | | and copyright information, see https://civicrm.org/licensing | |
| 10 | +--------------------------------------------------------------------+ |
| 11 | */ |
| 12 | /** |
| 13 | * Test CRM_SMS_Provider functions |
| 14 | * |
| 15 | * @package CiviCRM_APIv3 |
| 16 | * @subpackage API_Contribution |
| 17 | * @group headless |
| 18 | */ |
| 19 | require_once 'CiviTest/CiviTestSMSProvider.php'; |
| 20 | |
| 21 | class CRM_SMS_ProviderTest extends CiviUnitTestCase { |
| 22 | |
| 23 | /** |
| 24 | * Set Up Funtion |
| 25 | */ |
| 26 | public function setUp(): void { |
| 27 | parent::setUp(); |
| 28 | $option = $this->callAPISuccess('option_value', 'create', ['option_group_id' => 'sms_provider_name', 'name' => 'test_provider_name', 'label' => 'test_provider_name', 'value' => 1]); |
| 29 | $this->option_value = $option['id']; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Clean up after each test. |
| 34 | */ |
| 35 | public function tearDown(): void { |
| 36 | parent::tearDown(); |
| 37 | $this->quickCleanup(['civicrm_email', 'civicrm_phone', 'civicrm_activity', 'civicrm_activity_contact']); |
| 38 | $this->callAPISuccess('option_value', 'delete', ['id' => $this->option_value]); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * CRM-20238 Add test of the processInbound function for SMSs |
| 43 | */ |
| 44 | public function testProcessInbound() { |
| 45 | $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone_type_id' => 'Phone', 'location_type_id' => 'Home', 'phone' => '+61487654321']]]); |
| 46 | $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); |
| 47 | $result = $provider->processInbound('+61412345678', 'This is a test message', '+61487654321'); |
| 48 | $this->assertEquals('This is a test message', $result->details); |
| 49 | $this->assertEquals('+61412345678', $result->phone_number); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * CRM-20238 Add test of processInbound function where no To is passed into the function |
| 54 | */ |
| 55 | public function testProcessInboundNoTo() { |
| 56 | $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); |
| 57 | $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345'); |
| 58 | $this->assertEquals('This is a test message', $result->details); |
| 59 | $this->assertEquals('+61412345678', $result->phone_number); |
| 60 | $this->assertEquals('12345', $result->result); |
| 61 | $activity = $this->callAPISuccess('activity', 'getsingle', ['id' => $result->id, 'return' => ['source_contact_id', 'target_contact_id', 'assignee_contact_id']]); |
| 62 | $contact = $this->callAPISuccess('contact', 'getsingle', ['phone' => '61412345678']); |
| 63 | // Verify that when no to is passed in by default the same contact is used for the source and target. |
| 64 | $this->assertEquals($contact['id'], $activity['source_contact_id']); |
| 65 | $this->assertEquals($contact['id'], $activity['target_contact_id'][0]); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * CRM-20238 Add test of ProcessInbound function where no To number is passed into the function but the toContactId gets set in a hook |
| 70 | */ |
| 71 | public function testProcessInboundSetToContactIDUsingHook() { |
| 72 | $provider = new CiviTestSMSProvider('CiviTestSMSProvider'); |
| 73 | $this->hookClass->setHook('civicrm_inboundSMS', [$this, 'smsHookTest']); |
| 74 | $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345'); |
| 75 | $this->assertEquals('This is a test message', $result->details); |
| 76 | $this->assertEquals('+61412345678', $result->phone_number); |
| 77 | $this->assertEquals('12345', $result->result); |
| 78 | $contact = $this->callAPISuccess('contact', 'getsingle', ['phone' => '+61487654321']); |
| 79 | $activity = $this->callAPISuccess('activity', 'getsingle', ['id' => $result->id, 'return' => ['source_contact_id', 'target_contact_id', 'assignee_contact_id']]); |
| 80 | $this->assertEquals($contact['id'], $activity['source_contact_id']); |
| 81 | } |
| 82 | |
| 83 | public function smsHookTest(&$message) { |
| 84 | $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone' => '+61487654321']]]); |
| 85 | $message->toContactID = $testSourceContact; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Some providers, like the mock one for these tests at the time of writing, |
| 90 | * or the dummy SMS provider extension, might not provide a default url, |
| 91 | * but the form shouldn't fail because of that. |
| 92 | */ |
| 93 | public function testMissingUrl() { |
| 94 | $form = $this->getFormObject('CRM_SMS_Form_Provider'); |
| 95 | $_REQUEST['key'] = 'CiviTestSMSProvider'; |
| 96 | |
| 97 | // This shouldn't give a notice |
| 98 | $defaults = $form->setDefaultValues(); |
| 99 | |
| 100 | $this->assertEquals([ |
| 101 | 'name' => 'CiviTestSMSProvider', |
| 102 | 'api_url' => '', |
| 103 | 'is_default' => 1, |
| 104 | 'is_active' => 1, |
| 105 | ], $defaults); |
| 106 | |
| 107 | unset($_REQUEST['key']); |
| 108 | } |
| 109 | |
| 110 | } |