Merge pull request #1216 from davecivicrm/CRM-13062
[civicrm-core.git] / tests / phpunit / api / v3 / PhoneTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30
31
32 /**
33 * Test APIv3 civicrm_phone* functions
34 *
35 * @package CiviCRM_APIv3
36 * @subpackage API_Contact
37 */
38 class api_v3_PhoneTest extends CiviUnitTestCase {
39 protected $_apiversion;
40 protected $_contactID;
41 protected $_locationType;
42 protected $_params;
43 public $_eNoticeCompliant = TRUE;
44
45 function setUp() {
46 $this->_apiversion = 3;
47 parent::setUp();
48
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',
58 'is_primary' => 1,
59 'version' => $this->_apiversion,
60 'phone_type_id' => 1,
61 );
62 }
63
64 function tearDown() {
65 $this->locationTypeDelete($this->_locationType);
66 $this->contactDelete($this->_contactID);
67 }
68
69 public function testCreatePhone() {
70
71 $result = civicrm_api('phone', 'create', $this->_params);
72
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__);
77
78 // $this->assertEquals( 1, $result['id'], 'In line ' . __LINE__ );
79
80 $delresult = civicrm_api('phone', 'delete', array('id' => $result['id'], 'version' => $this->_apiversion));
81 $this->assertEquals(0, $delresult['is_error'], 'In line ' . __LINE__);
82 }
83
84 public function testDeletePhone() {
85 //create one
86 $create = civicrm_api('phone', 'create', $this->_params);
87
88 $this->assertAPISuccess($create, 'In line ' . __LINE__);
89
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,
97 ));
98 $this->assertEquals(0, $get['is_error'], 'In line ' . __LINE__);
99 $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__);
100 }
101
102 /**
103 * Test civicrm_phone_get with wrong params type.
104 */
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__);
109 }
110
111 /**
112 * Test civicrm_phone_get with empty params.
113 */
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__);
118 }
119
120 /**
121 * Test civicrm_phone_get with wrong params.
122 */
123 public function testGetWrongParams() {
124 $this->callAPIFailure('Phone', 'Get', array('contact_id' => 'abc'));
125
126 $this->callAPIFailure('Phone', 'Get', array('location_type_id' => 'abc'));
127
128 $this->callAPIFailure('Phone', 'Get', array('phone_type_id' => 'abc'));
129 }
130
131 /**
132 * Test civicrm_phone_get - success expected.
133 */
134 public function testGet() {
135 $phone = $this->callAPISuccess('phone', 'create', $this->_params);
136 $params = array(
137 'contact_id' => $this->_params['contact_id'],
138 'phone' => $phone['values'][$phone['id']]['phone'],
139 'version' => $this->_apiversion,
140 );
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__);
148 }
149
150 ///////////////// civicrm_phone_create methods
151
152 /**
153 * Test civicrm_phone_create with wrong params type.
154 */
155 public function testCreateWrongParamsType() {
156 $params = 'a string';
157 $result = $this->callAPIFailure('Phone', 'Create', $params);
158 }
159
160 /**
161 * Ensure numeric_phone field is correctly populated (this happens via sql trigger)
162 */
163 public function testNumericPhone() {
164 $result = civicrm_api('phone', 'create', $this->_params);
165 $this->assertAPISuccess($result, 'In line ' . __LINE__);
166 $id = $result['id'];
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']);
170 }
171
172 /**
173 * If a new phone is set to is_primary the prev should no longer be
174 *
175 * If is_primary is not set then it should become is_primary is no others exist
176 */
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(
184 'version' => 3,
185 'is_primary' => 1,
186 'id' => $phone1['id'],
187 ));
188 $this->assertEquals(1, $check);
189 }
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(
196 'version' => 3,
197 'is_primary' => 1,
198 'contact_id' => $this->_contactID,
199 ));
200 $this->assertEquals(1, $check);
201 }
202 }
203