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