3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
13 * Test APIv3 civicrm_phone* functions
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
19 class api_v3_PhoneTest
extends CiviUnitTestCase
{
20 protected $_contactID;
21 protected $_locationType;
25 public function setUp() {
26 $this->_entity
= 'Phone';
28 $this->useTransaction();
30 $this->_contactID
= $this->organizationCreate();
31 $loc = $this->locationTypeCreate();
32 $this->_locationType
= $loc->id
;
33 CRM_Core_PseudoConstant
::flush();
35 'contact_id' => $this->_contactID
,
36 'location_type_id' => $this->_locationType
,
37 'phone' => '(123) 456-7890',
45 * @dataProvider versionThreeAndFour
47 public function testCreatePhone($version) {
48 $this->_apiversion
= $version;
50 $result = $this->callAPIAndDocument('phone', 'create', $this->_params
, __FUNCTION__
, __FILE__
);
51 $this->assertEquals(1, $result['count']);
52 $this->assertNotNull($result['values'][$result['id']]['id']);
54 $this->callAPISuccess('phone', 'delete', ['id' => $result['id']]);
58 * If no location is specified when creating a new phone, it should default to
59 * the LocationType default
62 * @dataProvider versionThreeAndFour
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']]);
75 * @dataProvider versionThreeAndFour
77 public function testDeletePhone($version) {
78 $this->_apiversion
= $version;
80 $create = $this->callAPISuccess('phone', 'create', $this->_params
);
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
,
88 $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__
);
92 * Test civicrm_phone_get with empty params.
94 * @dataProvider versionThreeAndFour
96 public function testGetEmptyParams($version) {
97 $this->_apiversion
= $version;
98 $result = $this->callAPISuccess('Phone', 'Get', []);
102 * Test civicrm_phone_get with wrong params.
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']);
111 * Test civicrm_phone_get - success expected.
112 * @param int $version
113 * @dataProvider versionThreeAndFour
115 public function testGet($version) {
116 $this->_apiversion
= $version;
117 $phone = $this->callAPISuccess('phone', 'create', $this->_params
);
119 'contact_id' => $this->_params
['contact_id'],
120 'phone' => $phone['values'][$phone['id']]['phone'],
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']);
129 ///////////////// civicrm_phone_create methods
132 * Ensure numeric_phone field is correctly populated (this happens via sql trigger)
133 * @param int $version
134 * @dataProvider versionThreeAndFour
136 public function testNumericPhone($version) {
137 $this->_apiversion
= $version;
138 $result = $this->callAPISuccess('phone', 'create', $this->_params
);
140 $params = ['id' => $id, 'return.phone_numeric' => 1];
141 $result = $this->callAPISuccess('phone', 'get', $params);
142 $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']);
146 * If a new phone is set to is_primary the prev should no longer be.
148 * If is_primary is not set then it should become is_primary is no others exist
149 * @param int $version
150 * @dataProvider versionThreeAndFour
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', [
160 'id' => $phone1['id'],
162 $this->assertEquals(1, $check);
166 * @param int $version
167 * @dataProvider versionThreeAndFour
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', [
175 'contact_id' => $this->_contactID
,
177 $this->assertEquals(1, $check);