Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
232624b1 | 4 | | CiviCRM version 4.4 | |
6a488035 TO |
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; | |
2683ce94 | 52 | CRM_Core_PseudoConstant::flush(); |
6a488035 TO |
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, | |
6a488035 TO |
59 | 'phone_type_id' => 1, |
60 | ); | |
61 | } | |
62 | ||
63 | function tearDown() { | |
64 | $this->locationTypeDelete($this->_locationType); | |
65 | $this->contactDelete($this->_contactID); | |
66 | } | |
67 | ||
68 | public function testCreatePhone() { | |
69 | ||
ca985406 | 70 | $result = $this->callAPIAndDocument('phone', 'create', $this->_params, __FUNCTION__, __FILE__); |
6a488035 TO |
71 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
72 | $this->assertNotNull($result['values'][$result['id']]['id'], 'In line ' . __LINE__); | |
73 | ||
ca985406 | 74 | $this->callAPISuccess('phone', 'delete', array('id' => $result['id'])); |
6a488035 TO |
75 | } |
76 | ||
77 | public function testDeletePhone() { | |
78 | //create one | |
ca985406 | 79 | $create = $this->callAPISuccess('phone', 'create', $this->_params); |
6a488035 | 80 | |
ca985406 | 81 | $result = $this->callAPIAndDocument('phone', 'delete', array('id' => $create['id'],), __FUNCTION__, __FILE__); |
6a488035 | 82 | $this->assertEquals(1, $result['count'], 'In line ' . __LINE__); |
ca985406 | 83 | $get = $this->callAPISuccess('phone', 'get', array( |
84 | 'id' => $create['id'], | |
85 | 'location_type_id' => $this->_locationType, | |
86 | )); | |
6a488035 TO |
87 | $this->assertEquals(0, $get['count'], 'Phone not successfully deleted In line ' . __LINE__); |
88 | } | |
89 | ||
6a488035 TO |
90 | /** |
91 | * Test civicrm_phone_get with empty params. | |
92 | */ | |
93 | public function testGetEmptyParams() { | |
ca985406 | 94 | $result = $this->callAPISuccess('Phone', 'Get', array()); |
6a488035 TO |
95 | } |
96 | ||
97 | /** | |
98 | * Test civicrm_phone_get with wrong params. | |
99 | */ | |
100 | public function testGetWrongParams() { | |
0376409d | 101 | $this->callAPIFailure('Phone', 'Get', array('contact_id' => 'abc')); |
0376409d | 102 | $this->callAPIFailure('Phone', 'Get', array('location_type_id' => 'abc')); |
0376409d | 103 | $this->callAPIFailure('Phone', 'Get', array('phone_type_id' => 'abc')); |
6a488035 TO |
104 | } |
105 | ||
106 | /** | |
107 | * Test civicrm_phone_get - success expected. | |
108 | */ | |
109 | public function testGet() { | |
0376409d | 110 | $phone = $this->callAPISuccess('phone', 'create', $this->_params); |
6a488035 TO |
111 | $params = array( |
112 | 'contact_id' => $this->_params['contact_id'], | |
113 | 'phone' => $phone['values'][$phone['id']]['phone'], | |
6a488035 | 114 | ); |
ca985406 | 115 | $result = $this->callAPIAndDocument('Phone', 'Get', $params, __FUNCTION__, __FILE__); |
6a488035 TO |
116 | $this->assertEquals($phone['values'][$phone['id']]['location_type_id'], $result['values'][$phone['id']]['location_type_id'], 'In line ' . __LINE__); |
117 | $this->assertEquals($phone['values'][$phone['id']]['phone_type_id'], $result['values'][$phone['id']]['phone_type_id'], 'In line ' . __LINE__); | |
118 | $this->assertEquals($phone['values'][$phone['id']]['is_primary'], $result['values'][$phone['id']]['is_primary'], 'In line ' . __LINE__); | |
119 | $this->assertEquals($phone['values'][$phone['id']]['phone'], $result['values'][$phone['id']]['phone'], 'In line ' . __LINE__); | |
120 | } | |
121 | ||
122 | ///////////////// civicrm_phone_create methods | |
123 | ||
6a488035 TO |
124 | /** |
125 | * Ensure numeric_phone field is correctly populated (this happens via sql trigger) | |
126 | */ | |
127 | public function testNumericPhone() { | |
ca985406 | 128 | $result = $this->callAPISuccess('phone', 'create', $this->_params); |
6a488035 | 129 | $id = $result['id']; |
ca985406 | 130 | $params = array('id' => $id, 'return.phone_numeric' => 1); |
131 | $result = $this->callAPISuccess('phone', 'get', $params); | |
6a488035 TO |
132 | $this->assertEquals('1234567890', $result['values'][$id]['phone_numeric']); |
133 | } | |
134 | ||
135 | /** | |
136 | * If a new phone is set to is_primary the prev should no longer be | |
137 | * | |
138 | * If is_primary is not set then it should become is_primary is no others exist | |
139 | */ | |
140 | public function testCreatePhonePrimaryHandlingChangeToPrimary() { | |
141 | $params = $this->_params; | |
142 | unset($params['is_primary']); | |
ca985406 | 143 | $phone1 = $this->callAPISuccess('phone', 'create', $params); |
6a488035 | 144 | //now we check & make sure it has been set to primary |
ca985406 | 145 | $check = $this->callAPISuccess('phone', 'getcount', array( |
6a488035 TO |
146 | 'is_primary' => 1, |
147 | 'id' => $phone1['id'], | |
148 | )); | |
149 | $this->assertEquals(1, $check); | |
150 | } | |
151 | public function testCreatePhonePrimaryHandlingChangeExisting() { | |
ca985406 | 152 | $phone1 = $this->callAPISuccess('phone', 'create', $this->_params); |
153 | $phone2 = $this->callAPISuccess('phone', 'create', $this->_params); | |
154 | $check = $this->callAPISuccess('phone', 'getcount', array( | |
155 | 'is_primary' => 1, | |
156 | 'contact_id' => $this->_contactID, | |
157 | )); | |
6a488035 TO |
158 | $this->assertEquals(1, $check); |
159 | } | |
160 | } | |
161 |