Merge pull request #7722 from kcristiano/17593
[civicrm-core.git] / tests / phpunit / api / v3 / PhoneTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * Test APIv3 civicrm_phone* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
33 */
34 class api_v3_PhoneTest extends CiviUnitTestCase {
35 protected $_apiversion;
36 protected $_contactID;
37 protected $_locationType;
38 protected $_params;
39
40
41 public function setUp() {
42 $this->_apiversion = 3;
43 parent::setUp();
44 $this->useTransaction();
45
46 $this->_contactID = $this->organizationCreate();
47 $loc = $this->locationTypeCreate();
48 $this->_locationType = $loc->id;
49 CRM_Core_PseudoConstant::flush();
50 $this->_params = array(
51 'contact_id' => $this->_contactID,
52 'location_type_id' => $this->_locationType,
53 'phone' => '(123) 456-7890',
54 'is_primary' => 1,
55 'phone_type_id' => 1,
56 );
57 }
58
59 public function testCreatePhone() {
60
61 $result = $this->callAPIAndDocument('phone', 'create', $this->_params, __FUNCTION__, __FILE__);
62 $this->assertEquals(1, $result['count']);
63 $this->assertNotNull($result['values'][$result['id']]['id']);
64
65 $this->callAPISuccess('phone', 'delete', array('id' => $result['id']));
66 }
67
68 public function testDeletePhone() {
69 //create one
70 $create = $this->callAPISuccess('phone', 'create', $this->_params);
71
72 $result = $this->callAPIAndDocument('phone', 'delete', array('id' => $create['id']), __FUNCTION__, __FILE__);
73 $this->assertEquals(1, $result['count']);
74 $get = $this->callAPISuccess('phone', 'get', array(
75 'id' => $create['id'],
76 'location_type_id' => $this->_locationType,
77 ));
78 $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__);
79 }
80
81 /**
82 * Test civicrm_phone_get with empty params.
83 */
84 public function testGetEmptyParams() {
85 $result = $this->callAPISuccess('Phone', 'Get', array());
86 }
87
88 /**
89 * Test civicrm_phone_get with wrong params.
90 */
91 public function testGetWrongParams() {
92 $this->callAPIFailure('Phone', 'Get', array('contact_id' => 'abc'));
93 $this->callAPIFailure('Phone', 'Get', array('location_type_id' => 'abc'));
94 $this->callAPIFailure('Phone', 'Get', array('phone_type_id' => 'abc'));
95 }
96
97 /**
98 * Test civicrm_phone_get - success expected.
99 */
100 public function testGet() {
101 $phone = $this->callAPISuccess('phone', 'create', $this->_params);
102 $params = array(
103 'contact_id' => $this->_params['contact_id'],
104 'phone' => $phone['values'][$phone['id']]['phone'],
105 );
106 $result = $this->callAPIAndDocument('Phone', 'Get', $params, __FUNCTION__, __FILE__);
107 $this->assertEquals($phone['values'][$phone['id']]['location_type_id'], $result['values'][$phone['id']]['location_type_id']);
108 $this->assertEquals($phone['values'][$phone['id']]['phone_type_id'], $result['values'][$phone['id']]['phone_type_id']);
109 $this->assertEquals($phone['values'][$phone['id']]['is_primary'], $result['values'][$phone['id']]['is_primary']);
110 $this->assertEquals($phone['values'][$phone['id']]['phone'], $result['values'][$phone['id']]['phone']);
111 }
112
113 ///////////////// civicrm_phone_create methods
114
115 /**
116 * Ensure numeric_phone field is correctly populated (this happens via sql trigger)
117 */
118 public function testNumericPhone() {
119 $result = $this->callAPISuccess('phone', 'create', $this->_params);
120 $id = $result['id'];
121 $params = array('id' => $id, 'return.phone_numeric' => 1);
122 $result = $this->callAPISuccess('phone', 'get', $params);
123 $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']);
124 }
125
126 /**
127 * If a new phone is set to is_primary the prev should no longer be.
128 *
129 * If is_primary is not set then it should become is_primary is no others exist
130 */
131 public function testCreatePhonePrimaryHandlingChangeToPrimary() {
132 $params = $this->_params;
133 unset($params['is_primary']);
134 $phone1 = $this->callAPISuccess('phone', 'create', $params);
135 //now we check & make sure it has been set to primary
136 $check = $this->callAPISuccess('phone', 'getcount', array(
137 'is_primary' => 1,
138 'id' => $phone1['id'],
139 ));
140 $this->assertEquals(1, $check);
141 }
142
143 public function testCreatePhonePrimaryHandlingChangeExisting() {
144 $phone1 = $this->callAPISuccess('phone', 'create', $this->_params);
145 $phone2 = $this->callAPISuccess('phone', 'create', $this->_params);
146 $check = $this->callAPISuccess('phone', 'getcount', array(
147 'is_primary' => 1,
148 'contact_id' => $this->_contactID,
149 ));
150 $this->assertEquals(1, $check);
151 }
152
153 }