Merge pull request #1216 from davecivicrm/CRM-13062
[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 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
139
140 $searchParams = array(
141 'entity_table' => 'civicrm_contribution_page',
142 'entity_id' => 1, );
143 $result = $this->callAPISuccess('uf_join', 'get', $searchParams);
144
145 foreach ($result['values'] as $key => $value) {
146 $this->assertEquals($value['uf_group_id'], $this->_ufGroupId, 'In line ' . __LINE__);
147 }
148 }
149
150
151 public function testUFJoinEditWrongParamsType() {
152 $params = 'a string';
153 $result = $this->callAPIFailure('uf_join', 'create', $params);
154 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
155 }
156
157 public function testUFJoinEditEmptyParams() {
158 $params = array();
159 $result = $this->callAPIFailure('uf_join', 'create', $params);
160 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id', 'In line ' . __LINE__);
161 }
162
163 public function testUFJoinEditWithoutUFGroupId() {
164 $params = array(
165 'module' => 'CiviContribute',
166 'entity_table' => 'civicrm_contribution_page',
167 'entity_id' => 1,
168 'weight' => 1,
169 'is_active' => 1, );
170 $result = $this->callAPIFailure('uf_join', 'create', $params);
171 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__);
172 }
173
174 /**
175 * create/update uf join
176 */
177 public function testCreateUFJoin() {
178 $params = array(
179 'module' => 'CiviContribute',
180 'entity_table' => 'civicrm_contribution_page',
181 'entity_id' => 1,
182 'weight' => 1,
183 'uf_group_id' => $this->_ufGroupId,
184 'is_active' => 1, 'sequential' => 1,
185 );
186 $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__);
187 $this->assertEquals($ufJoin['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
188 $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
189 $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__);
190
191 $params = array(
192 'id' => $ufJoin['id'],
193 'module' => 'CiviContribute',
194 'entity_table' => 'civicrm_contribution_page',
195 'entity_id' => 1,
196 'weight' => 1,
197 'uf_group_id' => $this->_ufGroupId,
198 'is_active' => 0, 'sequential' => 1,
199 );
200 $ufJoinUpdated = $this->callAPISuccess('uf_join', 'create', $params);
201 $this->assertEquals($ufJoinUpdated['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
202 $this->assertEquals($ufJoinUpdated['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
203 $this->assertEquals($ufJoinUpdated['values'][0]['is_active'], $params['is_active'], 'In line ' . __LINE__);
204 }
205
206
207 public function testFindUFJoinWrongParamsType() {
208 $params = 'a string';
209 $result = $this->callAPIFailure('uf_join', 'create', $params);
210 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array', 'In line ' . __LINE__);
211 }
212
213 public function testFindUFJoinEmptyParams() {
214 $result = $this->callAPIFailure('uf_join', 'create', array());
215 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: module, weight, uf_group_id', 'In line ' . __LINE__);
216 }
217
218 public function testFindUFJoinWithoutUFGroupId() {
219 $params = array(
220 'module' => 'CiviContribute',
221 'entity_table' => 'civicrm_contribution_page',
222 'entity_id' => 1,
223 'weight' => 1,
224 'is_active' => 1,
225 );
226 $result = $this->callAPIFailure('uf_join', 'create', $params);
227 $this->assertEquals($result['error_message'], 'Mandatory key(s) missing from params array: uf_group_id', 'In line ' . __LINE__);
228 }
229
230 /**
231 * find uf join id
232 */
233 public function testGetUFJoinId() {
234 $params = array(
235 'module' => 'CiviContribute',
236 'entity_table' => 'civicrm_contribution_page',
237 'entity_id' => 1,
238 'weight' => 1,
239 'uf_group_id' => $this->_ufGroupId,
240 'is_active' => 1, );
241
242 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
243 $searchParams = array(
244 'entity_table' => 'civicrm_contribution_page',
245 'entity_id' => 1, 'sequential' => 1,
246 );
247
248 $result = $this->callAPIAndDocument('uf_join', 'get', $searchParams, __FUNCTION__, __FILE__);
249 $this->assertEquals($result['values'][0]['module'], $params['module'], 'In line ' . __LINE__);
250 $this->assertEquals($result['values'][0]['uf_group_id'], $params['uf_group_id'], 'In line ' . __LINE__);
251 $this->assertEquals($result['values'][0]['entity_id'], $params['entity_id'], 'In line ' . __LINE__);
252 }
253
254 /**
255 * Test civicrm_activity_create() using example code
256 */
257 function testUFJoinCreateExample() {
258 require_once 'api/v3/examples/UFJoinCreate.php';
259 $result = UF_join_create_example();
260 $expectedResult = UF_join_create_expectedresult();
261 $this->assertEquals($result, $expectedResult);
262 }
263 }
264