Merge remote-tracking branch 'upstream/4.4' into 4.4-master-2014-04-28-11-04-58
[civicrm-core.git] / tests / phpunit / api / v3 / TagTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 =3;
40 /**
41 * @ids array of values to be cleaned up in the tear down
42 */
43 protected $ids = array();
44 /**
45 * tag id
46 * @var integer
47 */
48 protected $tag = array();
49
50 protected $tagID;
51
52 function setUp() {
53 parent::setUp();
54 $this->tag = $this->tagCreate();
55 $this->ids['tag'][] = $this->tagID = $this->tag['id'];
56 }
57
58 function tearDown() {
59 $this->deleteFromIDSArray();
60 }
61
62 ///////////////// civicrm_tag_get methods
63 /**
64 * Test civicrm_tag_get with wrong params.
65 */
66 public function testGetWrongParams() {
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 */
75 public function testGet() {
76 $params = array(
77 'id' => $this->tagID,
78 'name' => $this->tag['name'],
79 );
80 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__);
81 $this->assertEquals($this->tag['description'], $result['values'][$this->tagID]['description'], 'In line ' . __LINE__);
82 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
83 }
84
85 /**
86 * Test civicrm_tag_get - success expected.
87 */
88 public function testGetReturnArray() {
89 $description = "demonstrates use of Return as an array";
90 $subfile = "getReturnArray";
91
92 $params = array(
93 'id' => $this->tagID,
94 'name' => $this->tag['name'],
95 'return' => array('name'),
96 );
97 $result = $this->callAPIAndDocument('tag', 'get', $params, __FUNCTION__, __FILE__, $description, $subfile);
98 $this->assertTrue(empty($result['values'][$this->tagID]['description']));
99 $this->assertEquals($this->tag['name'], $result['values'][$this->tagID]['name']);
100 }
101
102 ///////////////// civicrm_tag_create methods
103
104 /**
105 * Test civicrm_tag_create with empty params.
106 */
107 function testCreateEmptyParams() {
108 $result = $this->callAPIFailure('tag', 'create', array(),'Mandatory key(s) missing from params array: name');
109 }
110
111 /**
112 * Test civicrm_tag_create
113 */
114 function testCreatePasstagInParams() {
115 $params = array(
116 'tag' => 10,
117 'name' => 'New Tag23',
118 'description' => 'This is description for New Tag 02',
119 );
120 $result = $this->callAPISuccess('tag', 'create', $params);
121 $this->assertEquals(10, $result['id'], 'In line ' . __LINE__);
122 }
123
124 /**
125 * Test civicrm_tag_create - success expected.
126 */
127 function testCreate() {
128 $params = array(
129 'name' => 'Super Heros',
130 'description' => 'Outside undie-wearers',
131 );
132 $result = $this->callAPIAndDocument('tag', 'create', $params, __FUNCTION__, __FILE__);
133 $this->assertNotNull($result['id'], 'In line ' . __LINE__);
134 $params['used_for'] = 'civicrm_contact';
135 $this->getAndCheck($params, $result['id'], 'tag');
136 }
137
138 /**
139 * Test civicrm_tag_create activity tag- success expected. Test checks that used_for is set
140 * and not over-written by default on update
141 */
142 function testCreateEntitySpecificTag() {
143 $params = array(
144 'name' => 'New Tag4',
145 'description' => 'This is description for New Activity tag',
146 'used_for' => 'civicrm_activity',
147 );
148 $result = $this->callAPISuccess('tag', 'create', $params);
149 $this->callAPISuccess('tag', 'get', array());
150 $this->getAndCheck($params, $result['id'], 'tag', 0, __FUNCTION__ . ' tag first created');
151 unset($params['used_for']);
152 $params['id'] = $result['id'];
153 $result = $this->callAPISuccess('tag', 'create', $params);
154 $params['used_for'] = 'civicrm_activity';
155 $this->getAndCheck($params, $result['id'], 'tag', 1, __FUNCTION__ . ' tag updated in line ' . __LINE__);
156 }
157 ///////////////// civicrm_tag_delete methods
158
159 /**
160 * Test civicrm_tag_delete without tag id.
161 */
162 function testDeleteWithoutTagId() {
163 $result = $this->callAPIFailure('tag', 'delete', array(), 'Mandatory key(s) missing from params array: id');
164 }
165
166 /**
167 * Test civicrm_tag_delete .
168 */
169 function testTagDeleteOldSyntax() {
170 $params = array(
171 'tag_id' => $this->tagID,
172 );
173 $result = $this->callAPISuccess('tag', 'delete', $params);
174 unset($this->ids['tag']);
175 }
176
177 /**
178 * Test civicrm_tag_delete = $params['id'] is correct
179 */
180 function testTagDeleteCorrectSyntax() {
181 $params = array(
182 'id' => $this->tagID,
183 );
184 $result = $this->callAPIAndDocument('tag', 'delete', $params, __FUNCTION__, __FILE__);
185 unset($this->ids['tag']);
186 }
187
188 function testTagGetfields() {
189 $description = "demonstrate use of getfields to interogate api";
190 $params = array('action' => 'create');
191 $result = $this->callAPIAndDocument('tag', 'getfields', $params, __FUNCTION__, __FILE__, $description, NULL, 'getfields');
192 $this->assertEquals('civicrm_contact', $result['values']['used_for']['api.default']);
193 }
194
195 function testTagGetList() {
196 $description = "Demonstrates use of api.getlist for autocomplete and quicksearch applications";
197 $params = array(
198 'input' => $this->tag['name'],
199 'extra' => array('used_for')
200 );
201 $result = $this->callAPIAndDocument('tag', 'getlist', $params, __FUNCTION__, __FILE__, $description);
202 $this->assertEquals($this->tag['id'], $result['values'][0]['id'], 'In line ' . __LINE__);
203 $this->assertEquals($this->tag['description'], $result['values'][0]['description'][0], 'In line ' . __LINE__);
204 $this->assertEquals($this->tag['used_for'], $result['values'][0]['extra']['used_for'], 'In line ' . __LINE__);
205 }
206 }
207