Merge pull request #14934 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / CRM / SMS / ProviderTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 5 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28 /**
29 * Test CRM_SMS_Provider functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 require_once 'CiviTest/CiviTestSMSProvider.php';
36
37 class CRM_SMS_ProviderTest extends CiviUnitTestCase {
38
39 /**
40 * Set Up Funtion
41 */
42 public function setUp() {
43 parent::setUp();
44 $option = $this->callAPISuccess('option_value', 'create', ['option_group_id' => 'sms_provider_name', 'name' => 'test_provider_name', 'label' => 'test_provider_name', 'value' => 1]);
45 $this->option_value = $option['id'];
46 }
47
48 /**
49 * Clean up after each test.
50 */
51 public function tearDown() {
52 parent::tearDown();
53 $this->quickCleanup(['civicrm_email', 'civicrm_phone', 'civicrm_activity', 'civicrm_activity_contact']);
54 $this->callAPISuccess('option_value', 'delete', ['id' => $this->option_value]);
55 }
56
57 /**
58 * CRM-20238 Add test of the processInbound function for SMSs
59 */
60 public function testProcessInbound() {
61 $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone_type_id' => 'Phone', 'location_type_id' => 'Home', 'phone' => '+61487654321']]]);
62 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
63 $result = $provider->processInbound('+61412345678', 'This is a test message', '+61487654321');
64 $this->assertEquals('This is a test message', $result->details);
65 $this->assertEquals('+61412345678', $result->phone_number);
66 }
67
68 /**
69 * CRM-20238 Add test of processInbound function where no To is passed into the function
70 */
71 public function testProcessInboundNoTo() {
72 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
73 $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345');
74 $this->assertEquals('This is a test message', $result->details);
75 $this->assertEquals('+61412345678', $result->phone_number);
76 $this->assertEquals('12345', $result->result);
77 $activity = $this->callAPISuccess('activity', 'getsingle', ['id' => $result->id, 'return' => ['source_contact_id', 'target_contact_id', 'assignee_contact_id']]);
78 $contact = $this->callAPISuccess('contact', 'getsingle', ['phone' => '61412345678']);
79 // Verify that when no to is passed in by default the same contact is used for the source and target.
80 $this->assertEquals($contact['id'], $activity['source_contact_id']);
81 $this->assertEquals($contact['id'], $activity['target_contact_id'][0]);
82 }
83
84 /**
85 * CRM-20238 Add test of ProcessInbound function where no To number is passed into the function but the toContactId gets set in a hook
86 */
87 public function testProcessInboundSetToContactIDUsingHook() {
88 $provider = new CiviTestSMSProvider('CiviTestSMSProvider');
89 $this->hookClass->setHook('civicrm_inboundSMS', [$this, 'smsHookTest']);
90 $result = $provider->processInbound('+61412345678', 'This is a test message', NULL, '12345');
91 $this->assertEquals('This is a test message', $result->details);
92 $this->assertEquals('+61412345678', $result->phone_number);
93 $this->assertEquals('12345', $result->result);
94 $contact = $this->callAPISuccess('contact', 'getsingle', ['phone' => '+61487654321']);
95 $activity = $this->callAPISuccess('activity', 'getsingle', ['id' => $result->id, 'return' => ['source_contact_id', 'target_contact_id', 'assignee_contact_id']]);
96 $this->assertEquals($contact['id'], $activity['source_contact_id']);
97 }
98
99 public function smsHookTest(&$message) {
100 $testSourceContact = $this->individualCreate(['phone' => [1 => ['phone' => '+61487654321']]]);
101 $message->toContactID = $testSourceContact;
102 }
103
104 }