Update Unit test styling to cover the future coder version
[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 $_apiversion = 3;
45 protected $params;
46
47 protected function setUp() {
48 parent::setUp();
49 $this->_groupId = $this->groupCreate();
50 $this->_contactId = $this->individualCreate();
51 $this->createLoggedInUser();
52 $ufGroup = $this->callAPISuccess('uf_group', 'create', array(
53 'group_type' => 'Contact',
54 'help_pre' => 'Profile to Test API',
55 'title' => 'Test Profile',
56 ));
57 $this->_ufGroupId = $ufGroup['id'];
58 $ufMatch = $this->callAPISuccess('uf_match', 'create', array(
59 'contact_id' => $this->_contactId,
60 'uf_id' => 42,
61 'uf_name' => 'email@mail.com',
62 ));
63 $this->_ufMatchId = $ufMatch['id'];
64 $this->params = array(
65 'add_captcha' => 1,
66 'add_contact_to_group' => $this->_groupId,
67 'group' => $this->_groupId,
68 'cancel_URL' => 'http://example.org/cancel',
69 'created_date' => '2009-06-27 00:00:00',
70 'created_id' => $this->_contactId,
71 'group_type' => 'Individual,Contact',
72 'help_post' => 'help post',
73 'help_pre' => 'help pre',
74 'is_active' => 0,
75 'is_cms_user' => 1,
76 'is_edit_link' => 1,
77 'is_map' => 1,
78 'is_reserved' => 1,
79 'is_uf_link' => 1,
80 'is_update_dupe' => 1,
81 'name' => 'Test_Group',
82 'notify' => 'admin@example.org',
83 'post_URL' => 'http://example.org/post',
84 'title' => 'Test Group',
85 );
86 }
87
88 public function tearDown() {
89 // Truncate the tables
90 $this->quickCleanup(
91 array(
92 'civicrm_group',
93 'civicrm_contact',
94 'civicrm_uf_group',
95 'civicrm_uf_join',
96 'civicrm_uf_match',
97 )
98 );
99 }
100
101 /**
102 * Updating group.
103 */
104 public function testUpdateUFGroup() {
105 $params = array(
106 'title' => 'Edited Test Profile',
107 'help_post' => 'Profile Pro help text.',
108 'is_active' => 1,
109 'id' => $this->_ufGroupId,
110 );
111
112 $result = $this->callAPISuccess('uf_group', 'create', $params);
113 foreach ($params as $key => $value) {
114 $this->assertEquals($result['values'][$result['id']][$key], $value);
115 }
116 }
117
118 public function testUFGroupCreate() {
119
120 $result = $this->callAPIAndDocument('uf_group', 'create', $this->params, __FUNCTION__, __FILE__);
121 $this->assertAPISuccess($result);
122 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group']);
123 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group']);
124 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
125 foreach ($this->params as $key => $value) {
126 if ($key == 'add_contact_to_group' or $key == 'group') {
127 continue;
128 }
129 $expected = $this->params[$key];
130 $received = $result['values'][$result['id']][$key];
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 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
190 }
191 }
192
193 public function testUFGroupUpdateWithEmptyParams() {
194 $result = $this->callAPIFailure('uf_group', 'create', array(), 'Mandatory key(s) missing from params array: title');
195 }
196
197 public function testUFGroupDelete() {
198 $ufGroup = $this->callAPISuccess('uf_group', 'create', $this->params);
199 $params = array('id' => $ufGroup['id']);
200 $this->assertEquals(1, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
201 $result = $this->callAPIAndDocument('uf_group', 'delete', $params, __FUNCTION__, __FILE__);
202 $this->assertEquals(0, $this->callAPISuccess('uf_group', 'getcount', $params), "in line " . __LINE__);
203 }
204
205 }