Update Unit test styling to cover the future coder version
[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 * @var integer
45 */
46 protected $tag = array();
47
48 protected $tagID;
49
50 public function setUp() {
51 parent::setUp();
52 $this->useTransaction(TRUE);
53 $this->tag = $this->tagCreate();
54 $this->ids['tag'][] = $this->tagID = $this->tag['id'];
55 }
56
57 ///////////////// civicrm_tag_get methods
58
59 /**
60 * Test civicrm_tag_get with wrong params.
61 */
62 public function testGetWrongParams() {
63 $params = array('name' => 'Wrong Tag Name');
64 $result = $this->callAPISuccess('tag', 'get', $params);
65 $this->assertEquals(0, $result['count']);
66 }
67
68 /**
69 * Test civicrm_tag_get - success expected.
70 */
71 public function testGet() {
72 $params = array(
73 'id' => $this->tagID,
74 'name' => $this->tag['name'],
75 );
76 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__);
77 $this->assertEquals($this->tag['description'], $result['values'][$this->tagID]['description']);
78 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
79 }
80
81 /**
82 * Test civicrm_tag_get - success expected.
83 */
84 public function testGetReturnArray() {
85 $description = "Demonstrates use of Return as an array.";
86 $subfile = "GetReturnArray";
87
88 $params = array(
89 'id' => $this->tagID,
90 'name' => $this->tag['name'],
91 'return' => array('name'),
92 );
93 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
94 $this->assertTrue(empty($result['values'][$this->tagID]['description']));
95 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
96 }
97
98 ///////////////// civicrm_tag_create methods
99
100 /**
101 * Test civicrm_tag_create with empty params.
102 */
103 public function testCreateEmptyParams() {
104 $result = $this->callAPIFailure('tag', 'create', array(), 'Mandatory key(s) missing from params array: name');
105 }
106
107 /**
108 * Test civicrm_tag_create.
109 */
110 public function testCreatePasstagInParams() {
111 $params = array(
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 */
123 public function testCreate() {
124 $params = array(
125 'name' => 'Super Heros',
126 'description' => 'Outside undie-wearers',
127 );
128 $result = $this->callAPIAndDocument('tag', 'create', $params, __FUNCTION__, __FILE__);
129 $this->assertNotNull($result['id']);
130 $params['used_for'] = 'civicrm_contact';
131 $this->getAndCheck($params, $result['id'], 'tag');
132 }
133
134 /**
135 * Test civicrm_tag_create activity tag- success expected.
136 *
137 * Test checks that used_for is set and not over-written by default on update.
138 */
139 public function testCreateEntitySpecificTag() {
140 $params = array(
141 'name' => 'New Tag4',
142 'description' => 'This is description for New Activity tag',
143 'used_for' => 'civicrm_activity',
144 );
145 $result = $this->callAPISuccess('tag', 'create', $params);
146 $this->callAPISuccess('tag', 'get', array());
147 $this->getAndCheck($params, $result['id'], 'tag', 0, __FUNCTION__ . ' tag first created');
148 unset($params['used_for']);
149 $params['id'] = $result['id'];
150 $result = $this->callAPISuccess('tag', 'create', $params);
151 $params['used_for'] = 'civicrm_activity';
152 $this->getAndCheck($params, $result['id'], 'tag', 1, __FUNCTION__ . ' tag updated in line ' . __LINE__);
153 }
154
155 ///////////////// civicrm_tag_delete methods
156
157 /**
158 * Test civicrm_tag_delete without tag id.
159 */
160 public function testDeleteWithoutTagId() {
161 $result = $this->callAPIFailure('tag', 'delete', array(), 'Mandatory key(s) missing from params array: id');
162 }
163
164 /**
165 * Test civicrm_tag_delete .
166 */
167 public function testTagDeleteOldSyntax() {
168 $params = array(
169 'tag_id' => $this->tagID,
170 );
171 $result = $this->callAPISuccess('tag', 'delete', $params);
172 unset($this->ids['tag']);
173 }
174
175 /**
176 * Test civicrm_tag_delete = $params['id'] is correct
177 */
178 public function testTagDeleteCorrectSyntax() {
179 $params = array(
180 'id' => $this->tagID,
181 );
182 $result = $this->callAPIAndDocument('tag', 'delete', $params, __FUNCTION__, __FILE__);
183 unset($this->ids['tag']);
184 }
185
186 public function testTagGetfields() {
187 $description = "Demonstrate use of getfields to interrogate api.";
188 $params = array('action' => 'create');
189 $result = $this->callAPIAndDocument('tag', 'getfields', $params, __FUNCTION__, __FILE__, $description, NULL);
190 $this->assertEquals('civicrm_contact', $result['values']['used_for']['api.default']);
191 }
192
193 public function testTagGetList() {
194 $description = "Demonstrates use of api.getlist for autocomplete and quicksearch applications.";
195 $params = array(
196 'input' => $this->tag['name'],
197 'extra' => array('used_for'),
198 );
199 $result = $this->callAPIAndDocument('tag', 'getlist', $params, __FUNCTION__, __FILE__, $description);
200 $this->assertEquals($this->tag['id'], $result['values'][0]['id']);
201 $this->assertEquals($this->tag['description'], $result['values'][0]['description'][0]);
202 $this->assertEquals($this->tag['used_for'], $result['values'][0]['extra']['used_for']);
203 }
204
205 }