Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.3 | | |
5 | +--------------------------------------------------------------------+ | |
6 | | Copyright CiviCRM LLC (c) 2004-2013 | | |
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 | require_once 'CiviTest/CiviUnitTestCase.php'; | |
29 | ||
30 | ||
31 | /** | |
32 | * Test APIv3 civicrm_tag_* functions | |
33 | * | |
34 | * @package CiviCRM_APIv3 | |
35 | * @subpackage API_Core | |
36 | */ | |
37 | ||
38 | class api_v3_TagTest extends CiviUnitTestCase { | |
39 | protected $_apiversion; | |
40 | public $_eNoticeCompliant = TRUE; | |
41 | function setUp() { | |
42 | $this->_apiversion = 3; | |
43 | parent::setUp(); | |
44 | } | |
45 | ||
46 | function tearDown() {} | |
47 | ||
48 | ///////////////// civicrm_tag_get methods | |
49 | ||
50 | /** | |
51 | * Test civicrm_tag_get with wrong params type. | |
52 | */ | |
53 | public function testGetWrongParamsType() { | |
54 | $params = 'is_string'; | |
d0e1eff2 | 55 | $result = $this->callAPIFailure('tag', 'get', $params); |
6a488035 TO |
56 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); |
57 | } | |
58 | ||
6a488035 TO |
59 | /** |
60 | * Test civicrm_tag_get with wrong params. | |
61 | */ | |
62 | public function testGetWrongParams() { | |
63 | $params = array('name' => 'Wrong Tag Name', 'version' => $this->_apiversion); | |
64 | $result = civicrm_api('tag', 'get', $params); | |
65 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
66 | $this->assertEquals(0, $result['count'], 'In line ' . __LINE__); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Test civicrm_tag_get - success expected. | |
71 | */ | |
72 | public function testGet() { | |
73 | $tag = $this->tagCreate(NULL); | |
74 | $this->assertEquals(0, $tag['is_error'], 'In line ' . __LINE__); | |
75 | ||
76 | $params = array( | |
77 | 'id' => $tag['id'], | |
78 | 'name' => $tag['values'][$tag['id']]['name'], | |
79 | 'version' => $this->_apiversion, | |
80 | ); | |
81 | $result = civicrm_api('tag', 'get', $params); | |
82 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
83 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
84 | $this->assertEquals($tag['values'][$tag['id']]['description'], $result['values'][$tag['id']]['description'], 'In line ' . __LINE__); | |
85 | $this->assertEquals($tag['values'][$tag['id']]['name'], $result['values'][$tag['id']]['name'], 'In line ' . __LINE__); | |
86 | } | |
87 | ||
88 | /** | |
89 | * Test civicrm_tag_get - success expected. | |
90 | */ | |
91 | public function testGetReturnArray() { | |
92 | $description = "demonstrates use of Return as an array"; | |
93 | $subfile = "getReturnArray"; | |
94 | $tag = $this->tagCreate(NULL); | |
95 | $this->assertEquals(0, $tag['is_error'], 'In line ' . __LINE__); | |
96 | ||
97 | $params = array( | |
98 | 'id' => $tag['id'], | |
99 | 'name' => $tag['values'][$tag['id']]['name'], | |
100 | 'version' => $this->_apiversion, | |
101 | 'return' => array('name'), | |
102 | ); | |
103 | $result = civicrm_api('tag', 'get', $params); | |
104 | $this->documentMe($params, $result, __FUNCTION__, __FILE__, $description, $subfile); | |
105 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
106 | $this->assertTrue(empty($result['values'][$tag['id']]['description']), 'In line ' . __LINE__); | |
107 | $this->assertEquals($tag['values'][$tag['id']]['name'], $result['values'][$tag['id']]['name'], 'In line ' . __LINE__); | |
108 | } | |
109 | ||
110 | ///////////////// civicrm_tag_create methods | |
111 | ||
112 | /** | |
113 | * Test civicrm_tag_create with wrong params type. | |
114 | */ | |
115 | function testCreateWrongParamsType() { | |
116 | $params = 'a string'; | |
d0e1eff2 | 117 | $result = $this->callAPIFailure('tag', 'create', $params); |
6a488035 TO |
118 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); |
119 | } | |
120 | ||
121 | /** | |
122 | * Test civicrm_tag_create with empty params. | |
123 | */ | |
124 | function testCreateEmptyParams() { | |
125 | $params = array('version' => $this->_apiversion); | |
d0e1eff2 | 126 | $result = $this->callAPIFailure('tag', 'create', $params); |
6a488035 TO |
127 | $this->assertEquals('Mandatory key(s) missing from params array: name', $result['error_message'], 'In line ' . __LINE__); |
128 | } | |
129 | ||
130 | /** | |
131 | * Test civicrm_tag_create | |
132 | */ | |
133 | function testCreatePasstagInParams() { | |
134 | $params = array( | |
135 | 'tag' => 10, | |
136 | 'name' => 'New Tag23', | |
137 | 'description' => 'This is description for New Tag 02', | |
138 | 'version' => $this->_apiversion, | |
139 | ); | |
140 | $result = civicrm_api('tag', 'create', $params); | |
141 | $this->assertEquals(10, $result['id'], 'In line ' . __LINE__); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Test civicrm_tag_create - success expected. | |
146 | */ | |
147 | function testCreate() { | |
148 | $params = array( | |
149 | 'name' => 'New Tag3', | |
150 | 'description' => 'This is description for New Tag 02', | |
151 | 'version' => $this->_apiversion, | |
152 | ); | |
153 | ||
154 | $result = civicrm_api('tag', 'create', $params); | |
155 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
156 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
157 | $this->assertNotNull($result['id'], 'In line ' . __LINE__); | |
158 | $params['used_for'] = 'civicrm_contact'; | |
159 | $this->getAndCheck($params, $result['id'], 'tag'); | |
160 | } | |
161 | ||
162 | /** | |
163 | * Test civicrm_tag_create contribution tag- success expected. Test checks that used_for is set | |
164 | * and not over-written by default on update | |
165 | */ | |
166 | function testCreateContributionTag() { | |
167 | $params = array( | |
168 | 'name' => 'New Tag4', | |
169 | 'description' => 'This is description for New Cont tag', | |
170 | 'version' => $this->_apiversion, | |
171 | 'used_for' => 'civicrm_contribution', | |
172 | ); | |
173 | $result = civicrm_api('tag', 'create', $params); | |
174 | $this->assertAPISuccess($result, "contribution tag created"); | |
175 | $check = civicrm_api('tag', 'get', array('version' => 3)); | |
176 | $this->getAndCheck($params, $result['id'], 'tag', 0, __FUNCTION__ . ' tag first created'); | |
177 | unset($params['used_for']); | |
178 | $this->assertAPISuccess($result, 'tag created'); | |
179 | $params['id'] = $result['id']; | |
180 | $result = civicrm_api('tag', 'create', $params); | |
181 | $this->assertAPISuccess($result); | |
182 | $params['used_for'] = 'civicrm_contribution'; | |
183 | $this->getAndCheck($params, $result['id'], 'tag', 1, __FUNCTION__ . ' tag updated in line ' . __LINE__); | |
184 | } | |
185 | ///////////////// civicrm_tag_delete methods | |
186 | ||
187 | /** | |
188 | * Test civicrm_tag_delete with wrong parameters type. | |
189 | */ | |
190 | function testDeleteWrongParamsType() { | |
191 | $tag = 'is string'; | |
d0e1eff2 | 192 | $result = $this->callAPIFailure('tag', 'delete', $tag); |
6a488035 TO |
193 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); |
194 | } | |
195 | ||
196 | /** | |
197 | * Test civicrm_tag_delete with empty parameters. | |
198 | */ | |
199 | function testDeleteEmptyParams() { | |
200 | $tag = array('version' => $this->_apiversion); | |
d0e1eff2 | 201 | $result = $this->callAPIFailure('tag', 'delete', $tag); |
6a488035 TO |
202 | $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__); |
203 | } | |
204 | ||
205 | /** | |
206 | * Test civicrm_tag_delete without tag id. | |
207 | */ | |
208 | function testDeleteWithoutTagId() { | |
209 | $tag = array('version' => 3); | |
210 | ||
d0e1eff2 | 211 | $result = $this->callAPIFailure('tag', 'delete', $tag); |
6a488035 TO |
212 | $this->assertEquals('Mandatory key(s) missing from params array: id', $result['error_message'], 'In line ' . __LINE__); |
213 | } | |
214 | ||
215 | /** | |
216 | * Test civicrm_tag_delete with wrong tag id type. | |
217 | */ | |
218 | function testDeleteWrongParams() { | |
d0e1eff2 | 219 | $result = $this->callAPIFailure('tag', 'delete', 'tyttyd'); |
6a488035 TO |
220 | $this->assertEquals('Input variable `params` is not an array', $result['error_message'], 'In line ' . __LINE__); |
221 | } | |
222 | ||
223 | /** | |
224 | * Test civicrm_tag_delete . | |
225 | */ | |
226 | function testTagDeleteOldSyntax() { | |
227 | $tagID = $this->tagCreate(NULL); | |
228 | $params = array( | |
229 | 'tag_id' => $tagID['id'], | |
230 | 'version' => $this->_apiversion, | |
231 | ); | |
232 | $result = civicrm_api('tag', 'delete', $params); | |
233 | $this->assertAPISuccess($result, 'In line ' . __LINE__); | |
234 | } | |
235 | ||
236 | /** | |
237 | * Test civicrm_tag_delete = $params['id'] is correct | |
238 | */ | |
239 | function testTagDeleteCorrectSyntax() { | |
240 | $tagID = $this->tagCreate(NULL); | |
241 | $params = array( | |
242 | 'id' => $tagID['id'], | |
243 | 'version' => $this->_apiversion, | |
244 | ); | |
245 | $result = civicrm_api('tag', 'delete', $params); | |
246 | $this->documentMe($params, $result, __FUNCTION__, __FILE__); | |
247 | $this->assertEquals(0, $result['is_error'], 'In line ' . __LINE__); | |
248 | } | |
249 | ||
250 | function testTaggetfields() { | |
251 | $description = "demonstrate use of getfields to interogate api"; | |
252 | $params = array('version' => 3, 'action' => 'create'); | |
253 | $result = civicrm_api('tag', 'getfields', $params); | |
254 | $this->assertEquals('civicrm_contact', $result['values']['used_for']['api.default']); | |
255 | } | |
256 | } | |
257 |