Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 TO |
9 | +--------------------------------------------------------------------+ |
10 | */ | |
11 | ||
6a488035 TO |
12 | /** |
13 | * Test APIv3 civicrm_phone* functions | |
14 | * | |
6c6e6187 TO |
15 | * @package CiviCRM_APIv3 |
16 | * @subpackage API_Contact | |
acb109b7 | 17 | * @group headless |
6a488035 TO |
18 | */ |
19 | class api_v3_PhoneTest extends CiviUnitTestCase { | |
6a488035 TO |
20 | protected $_contactID; |
21 | protected $_locationType; | |
22 | protected $_params; | |
f19fee10 | 23 | protected $_entity; |
b7c9bc4c | 24 | |
00be9182 | 25 | public function setUp() { |
f19fee10 | 26 | $this->_entity = 'Phone'; |
6a488035 | 27 | parent::setUp(); |
ea1cc713 | 28 | $this->useTransaction(); |
6a488035 | 29 | |
5896d037 TO |
30 | $this->_contactID = $this->organizationCreate(); |
31 | $loc = $this->locationTypeCreate(); | |
6a488035 | 32 | $this->_locationType = $loc->id; |
2683ce94 | 33 | CRM_Core_PseudoConstant::flush(); |
9099cab3 | 34 | $this->_params = [ |
6a488035 TO |
35 | 'contact_id' => $this->_contactID, |
36 | 'location_type_id' => $this->_locationType, | |
37 | 'phone' => '(123) 456-7890', | |
38 | 'is_primary' => 1, | |
6a488035 | 39 | 'phone_type_id' => 1, |
9099cab3 | 40 | ]; |
6a488035 TO |
41 | } |
42 | ||
2d932085 CW |
43 | /** |
44 | * @param int $version | |
45 | * @dataProvider versionThreeAndFour | |
46 | */ | |
47 | public function testCreatePhone($version) { | |
48 | $this->_apiversion = $version; | |
6a488035 | 49 | |
ca985406 | 50 | $result = $this->callAPIAndDocument('phone', 'create', $this->_params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
51 | $this->assertEquals(1, $result['count']); |
52 | $this->assertNotNull($result['values'][$result['id']]['id']); | |
6a488035 | 53 | |
9099cab3 | 54 | $this->callAPISuccess('phone', 'delete', ['id' => $result['id']]); |
6a488035 TO |
55 | } |
56 | ||
0e70279a AS |
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 | ||
2d932085 CW |
73 | /** |
74 | * @param int $version | |
75 | * @dataProvider versionThreeAndFour | |
76 | */ | |
77 | public function testDeletePhone($version) { | |
78 | $this->_apiversion = $version; | |
6a488035 | 79 | //create one |
ca985406 | 80 | $create = $this->callAPISuccess('phone', 'create', $this->_params); |
6a488035 | 81 | |
9099cab3 | 82 | $result = $this->callAPIAndDocument('phone', 'delete', ['id' => $create['id']], __FUNCTION__, __FILE__); |
ba4a1892 | 83 | $this->assertEquals(1, $result['count']); |
9099cab3 | 84 | $get = $this->callAPISuccess('phone', 'get', [ |
ca985406 | 85 | 'id' => $create['id'], |
86 | 'location_type_id' => $this->_locationType, | |
9099cab3 | 87 | ]); |
6a488035 TO |
88 | $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__); |
89 | } | |
90 | ||
6a488035 TO |
91 | /** |
92 | * Test civicrm_phone_get with empty params. | |
2d932085 CW |
93 | * @param int $version |
94 | * @dataProvider versionThreeAndFour | |
6a488035 | 95 | */ |
2d932085 CW |
96 | public function testGetEmptyParams($version) { |
97 | $this->_apiversion = $version; | |
9099cab3 | 98 | $result = $this->callAPISuccess('Phone', 'Get', []); |
6a488035 TO |
99 | } |
100 | ||
101 | /** | |
102 | * Test civicrm_phone_get with wrong params. | |
103 | */ | |
104 | public function testGetWrongParams() { | |
9099cab3 CW |
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']); | |
6a488035 TO |
108 | } |
109 | ||
110 | /** | |
111 | * Test civicrm_phone_get - success expected. | |
2d932085 CW |
112 | * @param int $version |
113 | * @dataProvider versionThreeAndFour | |
6a488035 | 114 | */ |
2d932085 CW |
115 | public function testGet($version) { |
116 | $this->_apiversion = $version; | |
0376409d | 117 | $phone = $this->callAPISuccess('phone', 'create', $this->_params); |
9099cab3 | 118 | $params = [ |
6c6e6187 | 119 | 'contact_id' => $this->_params['contact_id'], |
6a488035 | 120 | 'phone' => $phone['values'][$phone['id']]['phone'], |
9099cab3 | 121 | ]; |
ca985406 | 122 | $result = $this->callAPIAndDocument('Phone', 'Get', $params, __FUNCTION__, __FILE__); |
ba4a1892 TM |
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']); | |
6a488035 TO |
127 | } |
128 | ||
129 | ///////////////// civicrm_phone_create methods | |
130 | ||
6a488035 TO |
131 | /** |
132 | * Ensure numeric_phone field is correctly populated (this happens via sql trigger) | |
2d932085 CW |
133 | * @param int $version |
134 | * @dataProvider versionThreeAndFour | |
6a488035 | 135 | */ |
2d932085 CW |
136 | public function testNumericPhone($version) { |
137 | $this->_apiversion = $version; | |
ca985406 | 138 | $result = $this->callAPISuccess('phone', 'create', $this->_params); |
6a488035 | 139 | $id = $result['id']; |
9099cab3 | 140 | $params = ['id' => $id, 'return.phone_numeric' => 1]; |
ca985406 | 141 | $result = $this->callAPISuccess('phone', 'get', $params); |
6a488035 TO |
142 | $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']); |
143 | } | |
144 | ||
145 | /** | |
fe482240 | 146 | * If a new phone is set to is_primary the prev should no longer be. |
6a488035 TO |
147 | * |
148 | * If is_primary is not set then it should become is_primary is no others exist | |
2d932085 CW |
149 | * @param int $version |
150 | * @dataProvider versionThreeAndFour | |
6a488035 | 151 | */ |
2d932085 CW |
152 | public function testCreatePhonePrimaryHandlingChangeToPrimary($version) { |
153 | $this->_apiversion = $version; | |
6a488035 TO |
154 | $params = $this->_params; |
155 | unset($params['is_primary']); | |
ca985406 | 156 | $phone1 = $this->callAPISuccess('phone', 'create', $params); |
6a488035 | 157 | //now we check & make sure it has been set to primary |
9099cab3 | 158 | $check = $this->callAPISuccess('phone', 'getcount', [ |
5896d037 TO |
159 | 'is_primary' => 1, |
160 | 'id' => $phone1['id'], | |
9099cab3 | 161 | ]); |
6a488035 TO |
162 | $this->assertEquals(1, $check); |
163 | } | |
5896d037 | 164 | |
2d932085 CW |
165 | /** |
166 | * @param int $version | |
167 | * @dataProvider versionThreeAndFour | |
168 | */ | |
169 | public function testCreatePhonePrimaryHandlingChangeExisting($version) { | |
170 | $this->_apiversion = $version; | |
ca985406 | 171 | $phone1 = $this->callAPISuccess('phone', 'create', $this->_params); |
172 | $phone2 = $this->callAPISuccess('phone', 'create', $this->_params); | |
9099cab3 | 173 | $check = $this->callAPISuccess('phone', 'getcount', [ |
ca985406 | 174 | 'is_primary' => 1, |
175 | 'contact_id' => $this->_contactID, | |
9099cab3 | 176 | ]); |
6a488035 TO |
177 | $this->assertEquals(1, $check); |
178 | } | |
96025800 | 179 | |
6a488035 | 180 | } |