Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / PhoneTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_phone* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_PhoneTest extends CiviUnitTestCase {
20 protected $_contactID;
21 protected $_locationType;
22 protected $_params;
23 protected $_entity;
24
25 public function setUp() {
26 $this->_entity = 'Phone';
27 parent::setUp();
28 $this->useTransaction();
29
30 $this->_contactID = $this->organizationCreate();
31 $loc = $this->locationTypeCreate();
32 $this->_locationType = $loc->id;
33 CRM_Core_PseudoConstant::flush();
34 $this->_params = [
35 'contact_id' => $this->_contactID,
36 'location_type_id' => $this->_locationType,
37 'phone' => '(123) 456-7890',
38 'is_primary' => 1,
39 'phone_type_id' => 1,
40 ];
41 }
42
43 /**
44 * @param int $version
45 * @dataProvider versionThreeAndFour
46 */
47 public function testCreatePhone($version) {
48 $this->_apiversion = $version;
49
50 $result = $this->callAPIAndDocument('phone', 'create', $this->_params, __FUNCTION__, __FILE__);
51 $this->assertEquals(1, $result['count']);
52 $this->assertNotNull($result['values'][$result['id']]['id']);
53
54 $this->callAPISuccess('phone', 'delete', ['id' => $result['id']]);
55 }
56
57 /**
58 * If no location is specified when creating a new phone, it should default to
59 * the LocationType default
60 *
61 * @param int $version
62 * @dataProvider versionThreeAndFour
63 */
64 public function testCreatePhoneDefaultLocation($version) {
65 $this->_apiversion = $version;
66 $params = $this->_params;
67 unset($params['location_type_id']);
68 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
69 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
70 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
71 }
72
73 /**
74 * @param int $version
75 * @dataProvider versionThreeAndFour
76 */
77 public function testDeletePhone($version) {
78 $this->_apiversion = $version;
79 //create one
80 $create = $this->callAPISuccess('phone', 'create', $this->_params);
81
82 $result = $this->callAPIAndDocument('phone', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__);
83 $this->assertEquals(1, $result['count']);
84 $get = $this->callAPISuccess('phone', 'get', [
85 'id' => $create['id'],
86 'location_type_id' => $this->_locationType,
87 ]);
88 $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__);
89 }
90
91 /**
92 * Test civicrm_phone_get with empty params.
93 * @param int $version
94 * @dataProvider versionThreeAndFour
95 */
96 public function testGetEmptyParams($version) {
97 $this->_apiversion = $version;
98 $result = $this->callAPISuccess('Phone', 'Get', []);
99 }
100
101 /**
102 * Test civicrm_phone_get with wrong params.
103 */
104 public function testGetWrongParams() {
105 $this->callAPIFailure('Phone', 'Get', ['contact_id' => 'abc']);
106 $this->callAPIFailure('Phone', 'Get', ['location_type_id' => 'abc']);
107 $this->callAPIFailure('Phone', 'Get', ['phone_type_id' => 'abc']);
108 }
109
110 /**
111 * Test civicrm_phone_get - success expected.
112 * @param int $version
113 * @dataProvider versionThreeAndFour
114 */
115 public function testGet($version) {
116 $this->_apiversion = $version;
117 $phone = $this->callAPISuccess('phone', 'create', $this->_params);
118 $params = [
119 'contact_id' => $this->_params['contact_id'],
120 'phone' => $phone['values'][$phone['id']]['phone'],
121 ];
122 $result = $this->callAPIAndDocument('Phone', 'Get', $params, __FUNCTION__, __FILE__);
123 $this->assertEquals($phone['values'][$phone['id']]['location_type_id'], $result['values'][$phone['id']]['location_type_id']);
124 $this->assertEquals($phone['values'][$phone['id']]['phone_type_id'], $result['values'][$phone['id']]['phone_type_id']);
125 $this->assertEquals($phone['values'][$phone['id']]['is_primary'], $result['values'][$phone['id']]['is_primary']);
126 $this->assertEquals($phone['values'][$phone['id']]['phone'], $result['values'][$phone['id']]['phone']);
127 }
128
129 ///////////////// civicrm_phone_create methods
130
131 /**
132 * Ensure numeric_phone field is correctly populated (this happens via sql trigger)
133 * @param int $version
134 * @dataProvider versionThreeAndFour
135 */
136 public function testNumericPhone($version) {
137 $this->_apiversion = $version;
138 $result = $this->callAPISuccess('phone', 'create', $this->_params);
139 $id = $result['id'];
140 $params = ['id' => $id, 'return.phone_numeric' => 1];
141 $result = $this->callAPISuccess('phone', 'get', $params);
142 $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']);
143 }
144
145 /**
146 * If a new phone is set to is_primary the prev should no longer be.
147 *
148 * If is_primary is not set then it should become is_primary is no others exist
149 * @param int $version
150 * @dataProvider versionThreeAndFour
151 */
152 public function testCreatePhonePrimaryHandlingChangeToPrimary($version) {
153 $this->_apiversion = $version;
154 $params = $this->_params;
155 unset($params['is_primary']);
156 $phone1 = $this->callAPISuccess('phone', 'create', $params);
157 //now we check & make sure it has been set to primary
158 $check = $this->callAPISuccess('phone', 'getcount', [
159 'is_primary' => 1,
160 'id' => $phone1['id'],
161 ]);
162 $this->assertEquals(1, $check);
163 }
164
165 /**
166 * @param int $version
167 * @dataProvider versionThreeAndFour
168 */
169 public function testCreatePhonePrimaryHandlingChangeExisting($version) {
170 $this->_apiversion = $version;
171 $phone1 = $this->callAPISuccess('phone', 'create', $this->_params);
172 $phone2 = $this->callAPISuccess('phone', 'create', $this->_params);
173 $check = $this->callAPISuccess('phone', 'getcount', [
174 'is_primary' => 1,
175 'contact_id' => $this->_contactID,
176 ]);
177 $this->assertEquals(1, $check);
178 }
179
180 }