use api success & api failure & apiSetup for cleaner tests
[civicrm-core.git] / tests / phpunit / api / v3 / UFGroupTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | CiviCRM version 4.3 |
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2013 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
10 | |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
19 | |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
27 */
28
29
30
31
32 require_once 'CiviTest/CiviUnitTestCase.php';
33
34 /**
35 * Test class for UFGroup API - civicrm_uf_*
36 * @todo Split UFGroup and UFJoin tests
37 *
38 * @package CiviCRM
39 */
40 class api_v3_UFGroupTest extends CiviUnitTestCase {
41 // ids from the uf_group_test.xml fixture
42 protected $_ufGroupId = 11;
43 protected $_ufFieldId;
44 protected $_contactId = 69;
45 protected $_apiversion;
46 protected $params;
47 public $_eNoticeCompliant = TRUE;
48
49 protected function setUp() {
50 parent::setUp();
51 $this->_apiversion = 3;
52 // Truncate the tables
53 $this->quickCleanup(
54 array(
55 'civicrm_group',
56 'civicrm_contact',
57 'civicrm_uf_group',
58 'civicrm_uf_join',
59 'civicrm_uf_match',
60 )
61 );
62
63 $op = new PHPUnit_Extensions_Database_Operation_Insert;
64 $op->execute(
65 $this->_dbconn,
66 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
67 );
68
69 // FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
70 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
71 'required' => array('html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'),
72 'maxlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
73 'minlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
74 'rangelength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
75 'email' => array('html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'),
76 'regex' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
77 'lettersonly' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
78 'alphanumeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
79 'numeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
80 'nopunctuation' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
81 'nonzero' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
82 'callback' => array('html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'),
83 'compare' => array('html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php'),
84 );
85 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
86 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array(
87 'group' => array('HTML/QuickForm/group.php', 'HTML_QuickForm_group'),
88 'hidden' => array('HTML/QuickForm/hidden.php', 'HTML_QuickForm_hidden'),
89 'reset' => array('HTML/QuickForm/reset.php', 'HTML_QuickForm_reset'),
90 'checkbox' => array('HTML/QuickForm/checkbox.php', 'HTML_QuickForm_checkbox'),
91 'file' => array('HTML/QuickForm/file.php', 'HTML_QuickForm_file'),
92 'image' => array('HTML/QuickForm/image.php', 'HTML_QuickForm_image'),
93 'password' => array('HTML/QuickForm/password.php', 'HTML_QuickForm_password'),
94 'radio' => array('HTML/QuickForm/radio.php', 'HTML_QuickForm_radio'),
95 'button' => array('HTML/QuickForm/button.php', 'HTML_QuickForm_button'),
96 'submit' => array('HTML/QuickForm/submit.php', 'HTML_QuickForm_submit'),
97 'select' => array('HTML/QuickForm/select.php', 'HTML_QuickForm_select'),
98 'hiddenselect' => array('HTML/QuickForm/hiddenselect.php', 'HTML_QuickForm_hiddenselect'),
99 'text' => array('HTML/QuickForm/text.php', 'HTML_QuickForm_text'),
100 'textarea' => array('HTML/QuickForm/textarea.php', 'HTML_QuickForm_textarea'),
101 'fckeditor' => array('HTML/QuickForm/fckeditor.php', 'HTML_QuickForm_FCKEditor'),
102 'tinymce' => array('HTML/QuickForm/tinymce.php', 'HTML_QuickForm_TinyMCE'),
103 'dojoeditor' => array('HTML/QuickForm/dojoeditor.php', 'HTML_QuickForm_dojoeditor'),
104 'link' => array('HTML/QuickForm/link.php', 'HTML_QuickForm_link'),
105 'advcheckbox' => array('HTML/QuickForm/advcheckbox.php', 'HTML_QuickForm_advcheckbox'),
106 'date' => array('HTML/QuickForm/date.php', 'HTML_QuickForm_date'),
107 'static' => array('HTML/QuickForm/static.php', 'HTML_QuickForm_static'),
108 'header' => array('HTML/QuickForm/header.php', 'HTML_QuickForm_header'),
109 'html' => array('HTML/QuickForm/html.php', 'HTML_QuickForm_html'),
110 'hierselect' => array('HTML/QuickForm/hierselect.php', 'HTML_QuickForm_hierselect'),
111 'autocomplete' => array('HTML/QuickForm/autocomplete.php', 'HTML_QuickForm_autocomplete'),
112 'xbutton' => array('HTML/QuickForm/xbutton.php', 'HTML_QuickForm_xbutton'),
113 'advmultiselect' => array('HTML/QuickForm/advmultiselect.php', 'HTML_QuickForm_advmultiselect'),
114 );
115 $this->params = array(
116 'add_captcha' => 1,
117 'add_contact_to_group' => 2,
118 'cancel_URL' => 'http://example.org/cancel',
119 'created_date' => '2009-06-27 00:00:00',
120 'created_id' => 69,
121 'group' => 2,
122 'group_type' => 'Individual,Contact',
123 'help_post' => 'help post',
124 'help_pre' => 'help pre',
125 'is_active' => 0,
126 'is_cms_user' => 1,
127 'is_edit_link' => 1,
128 'is_map' => 1,
129 'is_reserved' => 1,
130 'is_uf_link' => 1,
131 'is_update_dupe' => 1,
132 'name' => 'Test_Group',
133 'notify' => 'admin@example.org',
134 'post_URL' => 'http://example.org/post',
135 'title' => 'Test Group',
136 'version' => $this->_apiversion,
137 );
138 }
139
140 function tearDown() {
141
142 // Truncate the tables
143 $this->quickCleanup(
144 array(
145 'civicrm_group',
146 'civicrm_contact',
147 'civicrm_uf_group',
148 'civicrm_uf_join',
149 'civicrm_uf_match',
150 )
151 );
152 }
153
154 /**
155 * updating group
156 */
157 public function testUpdateUFGroup() {
158 $params = array(
159 'title' => 'Edited Test Profile',
160 'help_post' => 'Profile Pro help text.',
161 'is_active' => 1,
162 'id' => $this->_ufGroupId,
163 'version' => $this->_apiversion,
164 );
165
166 $result = civicrm_api('uf_group', 'create', $params);
167 unset($params['version']);
168 foreach ($params as $key => $value) {
169 $this->assertEquals($result['values'][$result['id']][$key], $value);
170 }
171 }
172
173 function testUFGroupCreate() {
174
175 $result = civicrm_api('uf_group', 'create', $this->params);
176 $this->documentMe($this->params, $result, __FUNCTION__, __FILE__);
177 $this->assertAPISuccess($result);
178 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group'], 'in line ' . __LINE__);
179 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group'], 'in line ' . __LINE__);
180 $this->params['created_date'] = date('YmdHis', strtotime($this->params['created_date']));
181 foreach ($this->params as $key => $value) {
182 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'version') {
183 continue;
184 }
185 $expected = $this->params[$key];
186 $received = $result['values'][$result['id']][$key];
187 // group names are renamed to name_id by BAO
188 if ($key == 'name') {
189 $expected = $this->params[$key] . '_' . $result['id'];
190 }
191 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
192 }
193 }
194
195 function testUFGroupCreateWithWrongParams() {
196 $result = civicrm_api('uf_group', 'create', 'a string');
197 $this->assertAPIFailure($result);
198 $result = civicrm_api('uf_group', 'create', array('name' => 'A title-less group'));
199 $this->assertAPIFailure($result);
200 }
201
202 function testUFGroupUpdate() {
203 $params = array(
204 'id' => $this->_ufGroupId,
205 'add_captcha' => 1,
206 'add_contact_to_group' => 2,
207 'cancel_URL' => 'http://example.org/cancel',
208 'created_date' => '2009-06-27',
209 'created_id' => 69,
210 'group' => 2,
211 'group_type' => 'Individual,Contact',
212 'help_post' => 'help post',
213 'help_pre' => 'help pre',
214 'is_active' => 0,
215 'is_cms_user' => 1,
216 'is_edit_link' => 1,
217 'is_map' => 1,
218 'is_reserved' => 1,
219 'is_uf_link' => 1,
220 'is_update_dupe' => 1,
221 'name' => 'test_group',
222 'notify' => 'admin@example.org',
223 'post_URL' => 'http://example.org/post',
224 'title' => 'Test Group',
225 'version' => $this->_apiversion,
226 );
227 $result = civicrm_api('uf_group', 'create', $params);
228 unset($params['version']);
229 $params['created_date'] = date('YmdHis', strtotime($params['created_date']));
230 foreach ($params as $key => $value) {
231 if ($key == 'add_contact_to_group' or $key == 'group') {
232 continue;
233 }
234 $this->assertEquals($result['values'][$result['id']][$key], $params[$key], $key . " doesn't match " . $value);
235 }
236
237 $this->assertEquals($result['values'][$this->_ufGroupId]['add_to_group_id'], $params['add_contact_to_group'], 'in line ' . __LINE__);
238 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $params['group'], 'in line ' . __LINE__);
239 }
240
241 function testUFGroupGet() {
242
243 $result = civicrm_api('uf_group', 'create', $this->params);
244 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__);
245
246 $params = array('version' => 3, 'id' => $result['id']);
247 $result = civicrm_api('uf_group', 'get', $params);
248 $this->assertEquals(0, $result['is_error'], 'in line ' . __LINE__);
249 $this->documentMe($this->params, $result, __FUNCTION__, __FILE__);
250 $this->assertEquals($result['values'][$result['id']]['add_to_group_id'], $this->params['add_contact_to_group'], 'in line ' . __LINE__);
251 $this->assertEquals($result['values'][$result['id']]['limit_listings_group_id'], $this->params['group'], 'in line ' . __LINE__);
252 foreach ($this->params as $key => $value) {
253 // skip created date because it doesn't seem to be working properly & fixing date handling is for another day
254 if ($key == 'add_contact_to_group' or $key == 'group' or $key == 'version' or $key == 'created_date') {
255 continue;
256 }
257 $expected = $this->params[$key];
258 $received = $result['values'][$result['id']][$key];
259 // group names are renamed to name_id by BAO
260 if ($key == 'name') {
261 $expected = $this->params[$key] . '_' . $result['id'];
262 }
263 $this->assertEquals($expected, $received, "The string '$received' does not equal '$expected' for key '$key' in line " . __LINE__);
264 }
265 }
266
267 function testUFGroupUpdateWithEmptyParams() {
268 $result = civicrm_api('uf_group', 'create', array(), $this->_ufGroupId);
269 $this->assertAPIFailure($result);
270 }
271
272 function testUFGroupUpdateWithWrongParams() {
273 $result = civicrm_api('uf_group', 'create', 'a string', $this->_ufGroupId);
274 $this->assertAPIFailure($result);
275 $result = civicrm_api('uf_group', 'create', array('title' => 'Title'), 'a string');
276 $this->assertAPIFailure($result);
277 }
278
279 function testUFGroupDelete() {
280
281 $ufGroup = civicrm_api('uf_group', 'create', $this->params);
282 $this->assertAPISuccess($ufGroup);
283 $params = array('version' => $this->_apiversion, 'id' => $ufGroup['id']);
284 $this->assertEquals(1, civicrm_api('uf_group', 'getcount', $params), "in line " . __LINE__);
285 $result = civicrm_api('uf_group', 'delete', $params, "in line " . __LINE__);
286 $this->documentMe($this->params, $result, __FUNCTION__, __FILE__);
287 $this->assertEquals(0, civicrm_api('uf_group', 'getcount', $params), "in line " . __LINE__);
288 $this->assertAPISuccess($result, "in line " . __LINE__);
289 }
290 }
291