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