Merge pull request #14948 from civicrm/5.16
[civicrm-core.git] / tests / phpunit / api / v3 / ImTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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_im_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contact
33 * @group headless
34 */
35 class api_v3_ImTest extends CiviUnitTestCase {
36 protected $_params;
37 protected $id;
38 protected $_entity;
39
40 public $DBResetRequired = FALSE;
41
42 public function setUp() {
43 parent::setUp();
44 $this->useTransaction(TRUE);
45
46 $this->_entity = 'im';
47 $this->_contactID = $this->organizationCreate();
48 $this->_params = [
49 'contact_id' => $this->_contactID,
50 'name' => 'My Yahoo IM Handle',
51 'location_type_id' => 1,
52 'provider_id' => 1,
53 ];
54 }
55
56 /**
57 * @param int $version
58 *
59 * @dataProvider versionThreeAndFour
60 *
61 * @throws \Exception
62 */
63 public function testCreateIm($version) {
64 $this->_apiversion = $version;
65 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
66 $this->assertEquals(1, $result['count']);
67 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
68 $this->assertNotNull($result['values'][$result['id']]['id']);
69 }
70
71 /**
72 * If no location is specified when creating a new IM, it should default to
73 * the LocationType default
74 *
75 * @param int $version
76 * @dataProvider versionThreeAndFour
77 */
78 public function testCreateImDefaultLocation($version) {
79 $this->_apiversion = $version;
80 $params = $this->_params;
81 unset($params['location_type_id']);
82 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
83 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
84 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
85 }
86
87 /**
88 * @param int $version
89 *
90 * @dataProvider versionThreeAndFour
91 */
92 public function testGetIm($version) {
93 $this->_apiversion = $version;
94 $this->callAPISuccess($this->_entity, 'create', $this->_params);
95 $result = $this->callAPIAndDocument($this->_entity, 'get', $this->_params, __FUNCTION__, __FILE__);
96 $this->assertEquals(1, $result['count']);
97 $this->assertNotNull($result['values'][$result['id']]['id']);
98 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
99 }
100
101 /**
102 * @param int $version
103 *
104 * @dataProvider versionThreeAndFour
105 */
106 public function testDeleteIm($version) {
107 $this->_apiversion = $version;
108 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
109 $deleteParams = ['id' => $result['id']];
110 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
111 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
112 $this->assertEquals(0, $checkDeleted['count']);
113 }
114
115 /**
116 * Skip api4 test - delete behaves differently
117 */
118 public function testDeleteImInvalid() {
119 $this->callAPISuccess($this->_entity, 'create', $this->_params);
120 $deleteParams = ['id' => 600];
121 $this->callAPIFailure($this->_entity, 'delete', $deleteParams);
122 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
123 $this->assertEquals(1, $checkDeleted['count']);
124 }
125
126 }