3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
33 * Test APIv3 civicrm_phone* functions
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contact
38 class api_v3_PhoneTest
extends CiviUnitTestCase
{
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_locationType;
43 public $_eNoticeCompliant = TRUE;
46 $this->_apiversion
= 3;
49 $this->_contactID
= $this->organizationCreate();
50 $loc = $this->locationTypeCreate();
51 $this->_locationType
= $loc->id
;
52 CRM_Core_PseudoConstant
::flush();
53 $this->quickCleanup(array('civicrm_phone'));
54 $this->_params
= array(
55 'contact_id' => $this->_contactID
,
56 'location_type_id' => $this->_locationType
,
57 'phone' => '(123) 456-7890',
59 'version' => $this->_apiversion
,
65 $this->locationTypeDelete($this->_locationType
);
66 $this->contactDelete($this->_contactID
);
69 public function testCreatePhone() {
71 $result = civicrm_api('phone', 'create', $this->_params
);
73 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
74 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
75 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
76 $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__
);
78 // $this->assertEquals( 1, $result['id'], 'In line ' . __LINE__ );
80 $delresult = civicrm_api('phone', 'delete', array('id' => $result['id'], 'version' => $this->_apiversion
));
81 $this->assertEquals(0, $delresult['is_error'], 'In line ' . __LINE__
);
84 public function testDeletePhone() {
86 $create = civicrm_api('phone', 'create', $this->_params
);
88 $this->assertAPISuccess($create, 'In line ' . __LINE__
);
90 $result = civicrm_api('phone', 'delete', array('id' => $create['id'], 'version' => $this->_apiversion
));
91 $this->documentMe($this->_params
, $result, __FUNCTION__
, __FILE__
);
92 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
93 $this->assertEquals(1, $result['count'], 'In line ' . __LINE__
);
94 $get = civicrm_api('phone', 'get', array(
95 'version' => $this->_apiversion
, 'id' => $create['id'],
96 'location_type_id' => $this->_locationType
,
98 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__
);
99 $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__
);
103 * Test civicrm_phone_get with wrong params type.
105 public function testGetWrongParamsType() {
106 $params = 'is_string';
107 $result = civicrm_api('Phone', 'Get', ($params));
108 $this->assertEquals(1, $result['is_error'], 'In line ' . __LINE__
);
112 * Test civicrm_phone_get with empty params.
114 public function testGetEmptyParams() {
115 $params = array('version' => $this->_apiversion
);
116 $result = civicrm_api('Phone', 'Get', ($params));
117 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
121 * Test civicrm_phone_get with wrong params.
123 public function testGetWrongParams() {
124 $this->callAPIFailure('Phone', 'Get', array('contact_id' => 'abc'));
126 $this->callAPIFailure('Phone', 'Get', array('location_type_id' => 'abc'));
128 $this->callAPIFailure('Phone', 'Get', array('phone_type_id' => 'abc'));
132 * Test civicrm_phone_get - success expected.
134 public function testGet() {
135 $phone = $this->callAPISuccess('phone', 'create', $this->_params
);
137 'contact_id' => $this->_params
['contact_id'],
138 'phone' => $phone['values'][$phone['id']]['phone'],
139 'version' => $this->_apiversion
,
141 $result = civicrm_api('Phone', 'Get', ($params));
142 $this->documentMe($params, $result, __FUNCTION__
, __FILE__
);
143 $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__
);
144 $this->assertEquals($phone['values'][$phone['id']]['location_type_id'], $result['values'][$phone['id']]['location_type_id'], 'In line ' . __LINE__
);
145 $this->assertEquals($phone['values'][$phone['id']]['phone_type_id'], $result['values'][$phone['id']]['phone_type_id'], 'In line ' . __LINE__
);
146 $this->assertEquals($phone['values'][$phone['id']]['is_primary'], $result['values'][$phone['id']]['is_primary'], 'In line ' . __LINE__
);
147 $this->assertEquals($phone['values'][$phone['id']]['phone'], $result['values'][$phone['id']]['phone'], 'In line ' . __LINE__
);
150 ///////////////// civicrm_phone_create methods
153 * Test civicrm_phone_create with wrong params type.
155 public function testCreateWrongParamsType() {
156 $params = 'a string';
157 $result = $this->callAPIFailure('Phone', 'Create', $params);
161 * Ensure numeric_phone field is correctly populated (this happens via sql trigger)
163 public function testNumericPhone() {
164 $result = civicrm_api('phone', 'create', $this->_params
);
165 $this->assertAPISuccess($result, 'In line ' . __LINE__
);
167 $params = array('id' => $id, 'version' => $this->_apiversion
, 'return.phone_numeric' => 1);
168 $result = civicrm_api('phone', 'get', $params);
169 $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']);
173 * If a new phone is set to is_primary the prev should no longer be
175 * If is_primary is not set then it should become is_primary is no others exist
177 public function testCreatePhonePrimaryHandlingChangeToPrimary() {
178 $params = $this->_params
;
179 unset($params['is_primary']);
180 $phone1 = civicrm_api('phone', 'create', $params);
181 $this->assertApiSuccess($phone1, 'In line ' . __LINE__
);
182 //now we check & make sure it has been set to primary
183 $check = civicrm_api('phone', 'getcount', array(
186 'id' => $phone1['id'],
188 $this->assertEquals(1, $check);
190 public function testCreatePhonePrimaryHandlingChangeExisting() {
191 $phone1 = civicrm_api('phone', 'create', $this->_params
);
192 $this->assertApiSuccess($phone1, 'In line ' . __LINE__
);
193 $phone2 = civicrm_api('phone', 'create', $this->_params
);
194 $this->assertApiSuccess($phone2, 'In line ' . __LINE__
);
195 $check = civicrm_api('phone', 'getcount', array(
198 'contact_id' => $this->_contactID
,
200 $this->assertEquals(1, $check);