use api success & api failure & apiSetup for cleaner tests
[civicrm-core.git] / tests / phpunit / api / v3 / UFJoinTest.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_UFJoinTest 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 public $_eNoticeCompliant = TRUE;
47 protected function setUp() {
48 parent::setUp();
49 // Truncate the tables
50 $this->quickCleanup(
51 array(
52 'civicrm_group',
53 'civicrm_contact',
54 'civicrm_uf_group',
55 'civicrm_uf_join',
56 'civicrm_uf_match',
57 )
58 );
59 $this->_apiversion = 3;
60 $op = new PHPUnit_Extensions_Database_Operation_Insert;
61 $op->execute(
62 $this->_dbconn,
63 new PHPUnit_Extensions_Database_DataSet_FlatXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml')
64 );
65
66 // FIXME: something NULLs $GLOBALS['_HTML_QuickForm_registered_rules'] when the tests are ran all together
67 $GLOBALS['_HTML_QuickForm_registered_rules'] = array(
68 'required' => array('html_quickform_rule_required', 'HTML/QuickForm/Rule/Required.php'),
69 'maxlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
70 'minlength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
71 'rangelength' => array('html_quickform_rule_range', 'HTML/QuickForm/Rule/Range.php'),
72 'email' => array('html_quickform_rule_email', 'HTML/QuickForm/Rule/Email.php'),
73 'regex' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
74 'lettersonly' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
75 'alphanumeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
76 'numeric' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
77 'nopunctuation' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
78 'nonzero' => array('html_quickform_rule_regex', 'HTML/QuickForm/Rule/Regex.php'),
79 'callback' => array('html_quickform_rule_callback', 'HTML/QuickForm/Rule/Callback.php'),
80 'compare' => array('html_quickform_rule_compare', 'HTML/QuickForm/Rule/Compare.php'),
81 );
82 // FIXME: …ditto for $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']
83 $GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'] = array(
84 'group' => array('HTML/QuickForm/group.php', 'HTML_QuickForm_group'),
85 'hidden' => array('HTML/QuickForm/hidden.php', 'HTML_QuickForm_hidden'),
86 'reset' => array('HTML/QuickForm/reset.php', 'HTML_QuickForm_reset'),
87 'checkbox' => array('HTML/QuickForm/checkbox.php', 'HTML_QuickForm_checkbox'),
88 'file' => array('HTML/QuickForm/file.php', 'HTML_QuickForm_file'),
89 'image' => array('HTML/QuickForm/image.php', 'HTML_QuickForm_image'),
90 'password' => array('HTML/QuickForm/password.php', 'HTML_QuickForm_password'),
91 'radio' => array('HTML/QuickForm/radio.php', 'HTML_QuickForm_radio'),
92 'button' => array('HTML/QuickForm/button.php', 'HTML_QuickForm_button'),
93 'submit' => array('HTML/QuickForm/submit.php', 'HTML_QuickForm_submit'),
94 'select' => array('HTML/QuickForm/select.php', 'HTML_QuickForm_select'),
95 'hiddenselect' => array('HTML/QuickForm/hiddenselect.php', 'HTML_QuickForm_hiddenselect'),
96 'text' => array('HTML/QuickForm/text.php', 'HTML_QuickForm_text'),
97 'textarea' => array('HTML/QuickForm/textarea.php', 'HTML_QuickForm_textarea'),
98 'fckeditor' => array('HTML/QuickForm/fckeditor.php', 'HTML_QuickForm_FCKEditor'),
99 'tinymce' => array('HTML/QuickForm/tinymce.php', 'HTML_QuickForm_TinyMCE'),
100 'dojoeditor' => array('HTML/QuickForm/dojoeditor.php', 'HTML_QuickForm_dojoeditor'),
101 'link' => array('HTML/QuickForm/link.php', 'HTML_QuickForm_link'),
102 'advcheckbox' => array('HTML/QuickForm/advcheckbox.php', 'HTML_QuickForm_advcheckbox'),
103 'date' => array('HTML/QuickForm/date.php', 'HTML_QuickForm_date'),
104 'static' => array('HTML/QuickForm/static.php', 'HTML_QuickForm_static'),
105 'header' => array('HTML/QuickForm/header.php', 'HTML_QuickForm_header'),
106 'html' => array('HTML/QuickForm/html.php', 'HTML_QuickForm_html'),
107 'hierselect' => array('HTML/QuickForm/hierselect.php', 'HTML_QuickForm_hierselect'),
108 'autocomplete' => array('HTML/QuickForm/autocomplete.php', 'HTML_QuickForm_autocomplete'),
109 'xbutton' => array('HTML/QuickForm/xbutton.php', 'HTML_QuickForm_xbutton'),
110 'advmultiselect' => array('HTML/QuickForm/advmultiselect.php', 'HTML_QuickForm_advmultiselect'),
111 );
112 }
113
114 function tearDown() {
115 // Truncate the tables
116 $this->quickCleanup(
117 array(
118 'civicrm_group',
119 'civicrm_contact',
120 'civicrm_uf_group',
121 'civicrm_uf_join',
122 'civicrm_uf_match',
123 )
124 );
125 }
126
127 /**
128 * find uf join group id
129 */
130 public function testFindUFGroupId() {
131 $params = array(
132 'module' => 'CiviContribute',
133 'entity_table' => 'civicrm_contribution_page',
134 'entity_id' => 1,
135 'weight' => 1,
136 'uf_group_id' => $this->_ufGroupId,
137 'is_active' => 1,
138 'version' => $this->_apiversion,
139 );
140 $ufJoin = civicrm_api('uf_join', 'create', $params);
141
142 $searchParams = array(
143 'entity_table' => 'civicrm_contribution_page',
144 'entity_id' => 1,
145 'version' => $this->_apiversion,
146 );
147 $result = civicrm_api('uf_join', 'get', $searchParams);
148
149 foreach ($result['values'] as $key => $value) {
150 $this->assertEquals($value['uf_group_id'], $this->_ufGroupId, 'In line ' . __LINE__);
151 }
152 }
153
154
155 public function testUFJoinEditWrongParamsType() {
156 $params = 'a string';
157 $result = civicrm_api('uf_join', 'create', $params);
158
159 $this->assertAPIFailure($result, 'In line ' . __LINE__);
160 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
161 }
162
163 public function testUFJoinEditEmptyParams() {
164 $params = array();
165 $result = civicrm_api('uf_join', 'create', $params);
166
167 $this->assertAPIFailure($result, 'In line ' . __LINE__);
168 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, module, weight, uf_group_id', 'In line ' . __LINE__);
169 }
170
171 public function testUFJoinEditWithoutUFGroupId() {
172 $params = array(
173 'module' => 'CiviContribute',
174 'entity_table' => 'civicrm_contribution_page',
175 'entity_id' => 1,
176 'weight' => 1,
177 'is_active' => 1,
178 'version' => $this->_apiversion,
179 );
180 $result = civicrm_api('uf_join', 'create', $params);
181 $this->assertAPIFailure($result, 'In line ' . __LINE__);
182 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__);
183 }
184
185 /**
186 * create/update uf join
187 */
188 public function testCreateUFJoin() {
189 $params = array(
190 'module' => 'CiviContribute',
191 'entity_table' => 'civicrm_contribution_page',
192 'entity_id' => 1,
193 'weight' => 1,
194 'uf_group_id' => $this->_ufGroupId,
195 'is_active' => 1,
196 'version' => $this->_apiversion,
197 'sequential' => 1,
198 );
199 $ufJoin = civicrm_api('uf_join', 'create', $params);
200 $this->documentMe($params, $ufJoin, __FUNCTION__, __FILE__);
201 $this->assertEquals($ufJoin['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
202 $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
203 $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__);
204
205 $params = array(
206 'id' => $ufJoin['id'],
207 'module' => 'CiviContribute',
208 'entity_table' => 'civicrm_contribution_page',
209 'entity_id' => 1,
210 'weight' => 1,
211 'uf_group_id' => $this->_ufGroupId,
212 'is_active' => 0,
213 'version' => $this->_apiversion,
214 'sequential' => 1,
215 );
216 $ufJoinUpdated = civicrm_api('uf_join', 'create', $params);
217 $this->assertEquals($ufJoinUpdated['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
218 $this->assertEquals($ufJoinUpdated['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
219 $this->assertEquals($ufJoinUpdated['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__);
220 }
221
222
223 public function testFindUFJoinWrongParamsType() {
224 $params = 'a string';
225 $result = civicrm_api('uf_join', 'create', $params);
226
227 $this->assertAPIFailure($result);
228 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
229 }
230
231 public function testFindUFJoinEmptyParams() {
232 $params = array();
233 $result = civicrm_api('uf_join', 'create', $params);
234
235 $this->assertAPIFailure($result, 'In line ' . __LINE__);
236 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: version, module, weight, uf_group_id', 'In line ' . __LINE__);
237 }
238
239 public function testFindUFJoinWithoutUFGroupId() {
240 $params = array(
241 'module' => 'CiviContribute',
242 'entity_table' => 'civicrm_contribution_page',
243 'entity_id' => 1,
244 'weight' => 1,
245 'is_active' => 1,
246 'version' => $this->_apiversion,
247 );
248 $result = civicrm_api('uf_join', 'create', $params);
249
250 $this->assertAPIFailure($result);
251 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__);
252 }
253
254 /**
255 * find uf join id
256 */
257 public function testGetUFJoinId() {
258 $params = array(
259 'module' => 'CiviContribute',
260 'entity_table' => 'civicrm_contribution_page',
261 'entity_id' => 1,
262 'weight' => 1,
263 'uf_group_id' => $this->_ufGroupId,
264 'is_active' => 1,
265 'version' => $this->_apiversion,
266 );
267
268 $ufJoin = civicrm_api('uf_join', 'create', $params);
269 $searchParams = array(
270 'entity_table' => 'civicrm_contribution_page',
271 'entity_id' => 1,
272 'version' => $this->_apiversion,
273 'sequential' => 1,
274 );
275
276 $result = civicrm_api('uf_join', 'get', $searchParams);
277 $this->documentMe($searchParams, $result, __FUNCTION__, __FILE__);
278 $this->assertEquals($result['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
279 $this->assertEquals($result['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
280 $this->assertEquals($result['values'][0]['entity_id'], $params['entity_id'], 'In line ' . __LINE__);
281 }
282
283 /**
284 * Test civicrm_activity_create() using example code
285 */
286 function testUFJoinCreateExample() {
287 require_once 'api/v3/examples/UFJoinCreate.php';
288 $result = UF_join_create_example();
289 $expectedResult = UF_join_create_expectedresult();
290 $this->assertEquals($result, $expectedResult);
291 }
292 }
293