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