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