Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / ImTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_im_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contact
17 * @group headless
18 */
19 class api_v3_ImTest extends CiviUnitTestCase {
20 protected $_params;
21 protected $id;
22 protected $_entity;
23
24 public $DBResetRequired = FALSE;
25
26 public function setUp() {
27 parent::setUp();
28 $this->useTransaction(TRUE);
29
30 $this->_entity = 'im';
31 $this->_contactID = $this->organizationCreate();
32 $this->_params = [
33 'contact_id' => $this->_contactID,
34 'name' => 'My Yahoo IM Handle',
35 'location_type_id' => 1,
36 'provider_id' => 1,
37 ];
38 }
39
40 /**
41 * @param int $version
42 *
43 * @dataProvider versionThreeAndFour
44 *
45 * @throws \Exception
46 */
47 public function testCreateIm($version) {
48 $this->_apiversion = $version;
49 $result = $this->callAPIAndDocument($this->_entity, 'create', $this->_params, __FUNCTION__, __FILE__);
50 $this->assertEquals(1, $result['count']);
51 $this->getAndCheck($this->_params, $result['id'], $this->_entity);
52 $this->assertNotNull($result['values'][$result['id']]['id']);
53 }
54
55 /**
56 * If no location is specified when creating a new IM, it should default to
57 * the LocationType default
58 *
59 * @param int $version
60 * @dataProvider versionThreeAndFour
61 */
62 public function testCreateImDefaultLocation($version) {
63 $this->_apiversion = $version;
64 $params = $this->_params;
65 unset($params['location_type_id']);
66 $result = $this->callAPIAndDocument($this->_entity, 'create', $params, __FUNCTION__, __FILE__);
67 $this->assertEquals(CRM_Core_BAO_LocationType::getDefault()->id, $result['values'][$result['id']]['location_type_id']);
68 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
69 }
70
71 /**
72 * @param int $version
73 *
74 * @dataProvider versionThreeAndFour
75 */
76 public function testGetIm($version) {
77 $this->_apiversion = $version;
78 $this->callAPISuccess($this->_entity, 'create', $this->_params);
79 $result = $this->callAPIAndDocument($this->_entity, 'get', $this->_params, __FUNCTION__, __FILE__);
80 $this->assertEquals(1, $result['count']);
81 $this->assertNotNull($result['values'][$result['id']]['id']);
82 $this->callAPISuccess($this->_entity, 'delete', ['id' => $result['id']]);
83 }
84
85 /**
86 * @param int $version
87 *
88 * @dataProvider versionThreeAndFour
89 */
90 public function testDeleteIm($version) {
91 $this->_apiversion = $version;
92 $result = $this->callAPISuccess($this->_entity, 'create', $this->_params);
93 $deleteParams = ['id' => $result['id']];
94 $this->callAPIAndDocument($this->_entity, 'delete', $deleteParams, __FUNCTION__, __FILE__);
95 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
96 $this->assertEquals(0, $checkDeleted['count']);
97 }
98
99 /**
100 * Skip api4 test - delete behaves differently
101 */
102 public function testDeleteImInvalid() {
103 $this->callAPISuccess($this->_entity, 'create', $this->_params);
104 $deleteParams = ['id' => 600];
105 $this->callAPIFailure($this->_entity, 'delete', $deleteParams);
106 $checkDeleted = $this->callAPISuccess($this->_entity, 'get', []);
107 $this->assertEquals(1, $checkDeleted['count']);
108 }
109
110 }