Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / TagTest.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_tag_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Core
17 * @group headless
18 */
19 class api_v3_TagTest extends CiviUnitTestCase {
20 protected $_apiversion = 3;
21 /**
22 * @var array
23 * @ids array of values to be cleaned up in the tear down
24 */
25 protected $ids = [];
26 /**
27 * Tag id.
28 *
29 * @var int
30 */
31 protected $tag = [];
32
33 protected $tagID;
34
35 public function setUp() {
36 parent::setUp();
37 $this->useTransaction(TRUE);
38 $this->tag = $this->tagCreate();
39 $this->ids['tag'][] = $this->tagID = $this->tag['id'];
40 }
41
42 ///////////////// civicrm_tag_get methods
43
44 /**
45 * Test civicrm_tag_get with wrong params.
46 * @param int $version
47 * @dataProvider versionThreeAndFour
48 */
49 public function testGetWrongParams($version) {
50 $this->_apiversion = $version;
51 $params = ['name' => 'Wrong Tag Name'];
52 $result = $this->callAPISuccess('tag', 'get', $params);
53 $this->assertEquals(0, $result['count']);
54 }
55
56 /**
57 * Test civicrm_tag_get - success expected.
58 * @param int $version
59 * @dataProvider versionThreeAndFour
60 */
61 public function testGet($version) {
62 $this->_apiversion = $version;
63 $params = [
64 'id' => $this->tagID,
65 'name' => $this->tag['name'],
66 ];
67 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__);
68 $this->assertEquals($this->tag['description'], $result['values'][$this->tagID]['description']);
69 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
70 }
71
72 /**
73 * Test civicrm_tag_get - success expected.
74 * @param int $version
75 * @dataProvider versionThreeAndFour
76 */
77 public function testGetReturnArray($version) {
78 $this->_apiversion = $version;
79 $description = "Demonstrates use of Return as an array.";
80 $subfile = "GetReturnArray";
81
82 $params = [
83 'id' => $this->tagID,
84 'name' => $this->tag['name'],
85 'return' => ['name'],
86 ];
87 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
88 $this->assertTrue(empty($result['values'][$this->tagID]['description']));
89 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
90 }
91
92 ///////////////// civicrm_tag_create methods
93
94 /**
95 * Test civicrm_tag_create with empty params.
96 * @param int $version
97 * @dataProvider versionThreeAndFour
98 */
99 public function testCreateEmptyParams($version) {
100 $this->_apiversion = $version;
101 $result = $this->callAPIFailure('tag', 'create', [], 'name');
102 }
103
104 /**
105 * Test civicrm_tag_create.
106 * @param int $version
107 * @dataProvider versionThreeAndFour
108 */
109 public function testCreatePasstagInParams($version) {
110 $this->_apiversion = $version;
111 $params = [
112 'tag' => 10,
113 'name' => 'New Tag23',
114 'description' => 'This is description for New Tag 02',
115 ];
116 $result = $this->callAPISuccess('tag', 'create', $params);
117 $this->assertEquals(10, $result['id']);
118 }
119
120 /**
121 * Test civicrm_tag_create - success expected.
122 * Skipping v4 because used_for is an array
123 */
124 public function testCreate() {
125 $params = [
126 'name' => 'Super Heros',
127 'description' => 'Outside undie-wearers',
128 ];
129 $result = $this->callAPIAndDocument('tag', 'create', $params, __FUNCTION__, __FILE__);
130 $this->assertNotNull($result['id']);
131 $params['used_for'] = 'civicrm_contact';
132 $this->getAndCheck($params, $result['id'], 'tag');
133 }
134
135 /**
136 * Test civicrm_tag_create activity tag- success expected.
137 *
138 * Test checks that used_for is set and not over-written by default on update.
139 *
140 * Skipping v4 because used_for is an array
141 */
142 public function testCreateEntitySpecificTag() {
143 $params = [
144 'name' => 'New Tag4',
145 'description' => 'This is description for New Activity tag',
146 'used_for' => 'civicrm_activity',
147 ];
148 $result = $this->callAPISuccess('tag', 'create', $params);
149 $this->callAPISuccess('tag', 'get', []);
150 $this->getAndCheck($params, $result['id'], 'tag', 0, __FUNCTION__ . ' tag first created');
151 unset($params['used_for']);
152 $params['id'] = $result['id'];
153 $result = $this->callAPISuccess('tag', 'create', $params);
154 $params['used_for'] = 'civicrm_activity';
155 $this->getAndCheck($params, $result['id'], 'tag', 1, __FUNCTION__ . ' tag updated in line ' . __LINE__);
156 }
157
158 ///////////////// civicrm_tag_delete methods
159
160 /**
161 * Test civicrm_tag_delete without tag id.
162 * @param int $version
163 * @dataProvider versionThreeAndFour
164 */
165 public function testDeleteWithoutTagId($version) {
166 $this->_apiversion = $version;
167 $result = $this->callAPIFailure('tag', 'delete', []);
168 }
169
170 /**
171 * Test civicrm_tag_delete .
172 * @param int $version
173 * @dataProvider versionThreeAndFour
174 */
175 public function testTagDeleteOldSyntax($version) {
176 $this->_apiversion = $version;
177 $params = [
178 'tag_id' => $this->tagID,
179 ];
180 $result = $this->callAPISuccess('tag', 'delete', $params);
181 unset($this->ids['tag']);
182 }
183
184 /**
185 * Test civicrm_tag_delete = $params['id'] is correct
186 * @param int $version
187 * @dataProvider versionThreeAndFour
188 */
189 public function testTagDeleteCorrectSyntax($version) {
190 $this->_apiversion = $version;
191 $params = [
192 'id' => $this->tagID,
193 ];
194 $result = $this->callAPIAndDocument('tag', 'delete', $params, __FUNCTION__, __FILE__);
195 unset($this->ids['tag']);
196 }
197
198 public function testTagGetfields() {
199 $description = "Demonstrate use of getfields to interrogate api.";
200 $params = ['action' => 'create'];
201 $result = $this->callAPIAndDocument('tag', 'getfields', $params, __FUNCTION__, __FILE__, $description, NULL);
202 $this->assertEquals('civicrm_contact', $result['values']['used_for']['api.default']);
203 }
204
205 public function testTagGetList() {
206 $description = "Demonstrates use of api.getlist for autocomplete and quicksearch applications.";
207 $params = [
208 'input' => $this->tag['name'],
209 'extra' => ['used_for'],
210 ];
211 $result = $this->callAPIAndDocument('tag', 'getlist', $params, __FUNCTION__, __FILE__, $description);
212 $this->assertEquals($this->tag['id'], $result['values'][0]['id']);
213 $this->assertEquals($this->tag['description'], $result['values'][0]['description'][0]);
214 $this->assertEquals($this->tag['used_for'], $result['values'][0]['extra']['used_for']);
215 }
216
217 }