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