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