Merge pull request #23516 from eileenmcnaughton/sms
[civicrm-core.git] / tests / phpunit / CRM / SMS / ProviderTest.php
CommitLineData
1370cab4
SL
1<?php
2
3/*
4 +--------------------------------------------------------------------+
7d61e75f 5 | Copyright CiviCRM LLC. All rights reserved. |
1370cab4 6 | |
7d61e75f
TO
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 |
1370cab4
SL
10 +--------------------------------------------------------------------+
11 */
12/**
13 * Test CRM_SMS_Provider functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
1a7f0799
MW
19require_once 'CiviTest/CiviTestSMSProvider.php';
20
1370cab4
SL
21class CRM_SMS_ProviderTest extends CiviUnitTestCase {
22
23 /**
24 * Set Up Funtion
25 */
0b49aa04 26 public function setUp(): void {
1370cab4 27 parent::setUp();
9099cab3 28 $option = $this->callAPISuccess('option_value', 'create', ['option_group_id' => 'sms_provider_name', 'name' => 'test_provider_name', 'label' => 'test_provider_name', 'value' => 1]);
1370cab4
SL
29 $this->option_value = $option['id'];
30 }
31
32 /**
33 * Clean up after each test.
34 */
dd09ee0c 35 public function tearDown(): void {
1370cab4 36 parent::tearDown();
9099cab3
CW
37 $this->quickCleanup(['civicrm_email', 'civicrm_phone', 'civicrm_activity', 'civicrm_activity_contact']);
38 $this->callAPISuccess('option_value', 'delete', ['id' => $this->option_value]);
1370cab4
SL
39 }
40
41 /**
42 * CRM-20238 Add test of the processInbound function for SMSs
43 */
44 public function testProcessInbound() {
9099cab3 45 $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone_type_id' => 'Phone', 'location_type_id' => 'Home', 'phone' => '+61487654321']]]);
1a7f0799 46 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
1370cab4
SL
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() {
1a7f0799 56 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
1370cab4
SL
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);
9099cab3
CW
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']);
1370cab4
SL
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
d20cd5d4
SL
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() {
1a7f0799 72 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
9099cab3 73 $this->hookClass->setHook('civicrm_inboundSMS', [$this, 'smsHookTest']);
d20cd5d4
SL
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);
9099cab3
CW
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']]);
d20cd5d4
SL
80 $this->assertEquals($contact['id'], $activity['source_contact_id']);
81 }
82
d20cd5d4 83 public function smsHookTest(&$message) {
9099cab3 84 $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone' => '+61487654321']]]);
d20cd5d4
SL
85 $message->toContactID = $testSourceContact;
86 }
87
646b0eee 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
1370cab4 110}