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