Merge pull request #7661 from agileware/crm-17848
[civicrm-core.git] / tests / phpunit / api / v3 / UFGroupTest.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 class for UFGroup API - civicrm_uf_*
30 * @todo Split UFGroup and UFJoin tests
31 *
32 * @package CiviCRM
33 */
34 class api_v3_UFGroupTest extends CiviUnitTestCase {
35 // ids from the uf_group_test.xml fixture
36 protected $_ufGroupId;
37 protected $_ufFieldId;
38 protected $_contactId;
39 protected $_groupId;
40 protected $_apiversion = 3;
41 protected $params;
42
43 protected function setUp() {
44 parent::setUp();
45 $this->_groupId = $this->groupCreate();
46 $this->_contactId = $this->individualCreate();
47 $this->createLoggedInUser();
48 $ufGroup = $this->callAPISuccess('uf_group', 'create', array(
49 'group_type' => 'Contact',
50 'help_pre' => 'Profile to Test API',
51 'title' => 'Test Profile',
52 ));
53 $this->_ufGroupId = $ufGroup['id'];
54 $ufMatch = $this->callAPISuccess('uf_match', 'create', array(
55 'contact_id' => $this->_contactId,
56 'uf_id' => 42,
57 'uf_name' => 'email@mail.com',
58 ));
59 $this->_ufMatchId = $ufMatch['id'];
60 $this->params = array(
61 'add_captcha' => 1,
62 'add_contact_to_group' => $this->_groupId,
63 'group' => $this->_groupId,
64 'cancel_URL' => 'http://example.org/cancel',
65 'created_date' => '2009-06-27 00:00:00',
66 'created_id' => $this->_contactId,
67 'group_type' => 'Individual,Contact',
68 'help_post' => 'help post',
69 'help_pre' => 'help pre',
70 'is_active' => 0,
71 'is_cms_user' => 1,
72 'is_edit_link' => 1,
73 'is_map' => 1,
74 'is_reserved' => 1,
75 'is_uf_link' => 1,
76 'is_update_dupe' => 1,
77 'name' => 'Test_Group',
78 'notify' => 'admin@example.org',
79 'post_URL' => 'http://example.org/post',
80 'title' => 'Test Group',
81 );
82 }
83
84 public function tearDown() {
85 // Truncate the tables
86 $this->quickCleanup(
87 array(
88 'civicrm_group',
89 'civicrm_contact',
90 'civicrm_uf_group',
91 'civicrm_uf_join',
92 'civicrm_uf_match',
93 )
94 );
95 }
96
97 /**
98 * Updating group.
99 */
100 public function testUpdateUFGroup() {
101 $params = array(
102 'title' => 'Edited Test Profile',
103 'help_post' => 'Profile Pro help text.',
104 'is_active' => 1,
105 'id' => $this->_ufGroupId,
106 );
107
108 $result = $this->callAPISuccess('uf_group', 'create', $params);
109 foreach ($params as $key => $value) {
110 $this->assertEquals($result['values'][$result['id']][$key], $value);
111 }
112 }
113
114 public function testUFGroupCreate() {
115
116 $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__);
117 $this->assertAPISuccess($result);
118 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
119 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
120 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
121 foreach ($this->params as $key => $value) {
122 if ($key == 'add_contact_to_group' or $key == 'group') {
123 continue;
124 }
125 $expected = $this->params[$key];
126 $received = $result['values'][$result['id']][$key];
127 // group names are renamed to name_id by BAO
128 if ($key == 'name') {
129 $expected = $this->params[$key] . '_' . $result['id'];
130 }
131 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
132 }
133 }
134
135 public function testUFGroupCreateWithWrongParams() {
136 $result = $this->callAPIFailure('uf_group', 'create', array('name' => 'A title-less group'));
137 }
138
139 public function testUFGroupUpdate() {
140 $params = array(
141 'id' => $this->_ufGroupId,
142 'add_captcha' => 1,
143 'add_contact_to_group' => $this->_groupId,
144 'cancel_URL' => 'http://example.org/cancel',
145 'created_date' => '2009-06-27',
146 'created_id' => $this->_contactId,
147 'group' => $this->_groupId,
148 'group_type' => 'Individual,Contact',
149 'help_post' => 'help post',
150 'help_pre' => 'help pre',
151 'is_active' => 0,
152 'is_cms_user' => 1,
153 'is_edit_link' => 1,
154 'is_map' => 1,
155 'is_reserved' => 1,
156 'is_uf_link' => 1,
157 'is_update_dupe' => 1,
158 'name' => 'test_group',
159 'notify' => 'admin@example.org',
160 'post_URL' => 'http://example.org/post',
161 'title' => 'Test Group',
162 );
163 $result = $this->callAPISuccess('uf_group', 'create', $params);
164 $params['created_date'] = date('YmdHis', strtotime($params['created_date']));
165 foreach ($params as $key => $value) {
166 if ($key == 'add_contact_to_group' or $key == 'group') {
167 continue;
168 }
169 $this->assertEquals($result['values'][$result['id']][$key], $params[$key], $key . " doesn't match " . $value);
170 }
171
172 $this->assertEquals($result['values'][$this->_ufGroupId]['add_to_group_id'], $params['add_contact_to_group']);
173 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $params['group']);
174 }
175
176 public function testUFGroupGet() {
177 $result = $this->callAPISuccess('uf_group', 'create', $this->params);
178 $params = array('id' => $result['id']);
179 $result = $this->callAPIAndDocument('uf_group', 'get', $params, __FUNCTION__, __FILE__);
180 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
181 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
182 foreach ($this->params as $key => $value) {
183 // skip created date because it doesn't seem to be working properly & fixing date handling is for another day
184 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'created_date') {
185 continue;
186 }
187 $expected = $this->params[$key];
188 $received = $result['values'][$result['id']][$key];
189 // group names are renamed to name_id by BAO
190 if ($key == 'name') {
191 $expected = $this->params[$key] . '_' . $result['id'];
192 }
193 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
194 }
195 }
196
197 public function testUFGroupUpdateWithEmptyParams() {
198 $result = $this->callAPIFailure('uf_group', 'create', array(), $this->_ufGroupId);
199 }
200
201 public function testUFGroupDelete() {
202 $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params);
203 $params = array('id' => $ufGroup['id']);
204 $this->assertEquals(1, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
205 $result = $this->callAPIAndDocument('uf_group', 'delete', $params, __FUNCTION__, __FILE__);
206 $this->assertEquals(0, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
207 }
208
209 }