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