Merge pull request #14894 from eileenmcnaughton/date_fisc
[civicrm-core.git] / tests / phpunit / api / v3 / UFJoinTest.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_UFJoinTest extends CiviUnitTestCase {
36 /**
37 * ids from the uf_group_test.xml fixture
38 * @var int
39 */
40 protected $_ufGroupId = 11;
41 protected $_ufFieldId;
42 protected $_contactId = 69;
43
44 protected function setUp() {
45 parent::setUp();
46 // Truncate the tables
47 $this->quickCleanup(
48 [
49 'civicrm_group',
50 'civicrm_contact',
51 'civicrm_uf_group',
52 'civicrm_uf_join',
53 'civicrm_uf_match',
54 ]
55 );
56 $this->loadXMLDataSet(dirname(__FILE__) . '/dataset/uf_group_test.xml');
57 }
58
59 public function tearDown() {
60 // Truncate the tables
61 $this->quickCleanup(
62 [
63 'civicrm_group',
64 'civicrm_contact',
65 'civicrm_uf_group',
66 'civicrm_uf_join',
67 'civicrm_uf_match',
68 ]
69 );
70 }
71
72 /**
73 * Find uf join group id.
74 * @param int $version
75 * @dataProvider versionThreeAndFour
76 */
77 public function testFindUFGroupId($version) {
78 $this->_apiversion = $version;
79 $params = [
80 'module' => 'CiviContribute',
81 'entity_table' => 'civicrm_contribution_page',
82 'entity_id' => 1,
83 'weight' => 1,
84 'uf_group_id' => $this->_ufGroupId,
85 'is_active' => 1,
86 ];
87 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
88
89 $searchParams = [
90 'entity_table' => 'civicrm_contribution_page',
91 'entity_id' => 1,
92 ];
93 $result = $this->callAPISuccess('uf_join', 'get', $searchParams);
94
95 foreach ($result['values'] as $key => $value) {
96 $this->assertEquals($value['uf_group_id'], $this->_ufGroupId);
97 }
98 }
99
100 /**
101 * @param int $version
102 * @dataProvider versionThreeAndFour
103 */
104 public function testUFJoinEditWrongParamsType($version) {
105 $this->_apiversion = $version;
106 $params = 'a string';
107 $result = $this->callAPIFailure('uf_join', 'create', $params);
108 $this->assertEquals($result['error_message'], 'Input variable `params` is not an array');
109 }
110
111 /**
112 * @param int $version
113 * @dataProvider versionThreeAndFour
114 */
115 public function testUFJoinEditWithoutUFGroupId($version) {
116 $this->_apiversion = $version;
117 $params = [
118 'module' => 'CiviContribute',
119 'entity_table' => 'civicrm_contribution_page',
120 'entity_id' => 1,
121 'weight' => 1,
122 'is_active' => 1,
123 ];
124 $result = $this->callAPIFailure('uf_join', 'create', $params);
125 $this->assertContains('Mandatory', $result['error_message']);
126 $this->assertContains('missing', $result['error_message']);
127 $this->assertContains('uf_group_id', $result['error_message']);
128 }
129
130 /**
131 * Create/update uf join
132 * @param int $version
133 * @dataProvider versionThreeAndFour
134 */
135 public function testCreateUFJoin($version) {
136 $this->_apiversion = $version;
137 $params = [
138 'module' => 'CiviContribute',
139 'entity_table' => 'civicrm_contribution_page',
140 'entity_id' => 1,
141 'weight' => 1,
142 'uf_group_id' => $this->_ufGroupId,
143 'is_active' => 1,
144 'sequential' => 1,
145 ];
146 $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__);
147 $this->assertEquals($ufJoin['values'][0]['module'], $params['module']);
148 $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id']);
149 $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active']);
150
151 $params = [
152 'id' => $ufJoin['id'],
153 'module' => 'CiviContribute',
154 'entity_table' => 'civicrm_contribution_page',
155 'entity_id' => 1,
156 'weight' => 1,
157 'uf_group_id' => $this->_ufGroupId,
158 'is_active' => 0,
159 'sequential' => 1,
160 ];
161 $ufJoinUpdated = $this->callAPISuccess('uf_join', 'create', $params);
162 $this->assertEquals($ufJoinUpdated['values'][0]['module'], $params['module']);
163 $this->assertEquals($ufJoinUpdated['values'][0]['uf_group_id'], $params['uf_group_id']);
164 $this->assertEquals($ufJoinUpdated['values'][0]['is_active'], $params['is_active']);
165 }
166
167 /**
168 * Ensure we can create a survey join which is less common than event or contribution
169 * joins.
170 * @param int $version
171 * @dataProvider versionThreeAndFour
172 */
173 public function testCreateSurveyUFJoin($version) {
174 $this->_apiversion = $version;
175 $params = [
176 'module' => 'CiviCampaign',
177 'entity_table' => 'civicrm_survey',
178 'entity_id' => 1,
179 'weight' => 1,
180 'uf_group_id' => $this->_ufGroupId,
181 'is_active' => 1,
182 'sequential' => 1,
183 ];
184 $ufJoin = $this->callAPIAndDocument('uf_join', 'create', $params, __FUNCTION__, __FILE__);
185 $this->assertEquals($ufJoin['values'][0]['module'], $params['module']);
186 $this->assertEquals($ufJoin['values'][0]['uf_group_id'], $params['uf_group_id']);
187 $this->assertEquals($ufJoin['values'][0]['is_active'], $params['is_active']);
188 }
189
190 /**
191 * @param int $version
192 * @dataProvider versionThreeAndFour
193 */
194 public function testFindUFJoinEmptyParams($version) {
195 $this->_apiversion = $version;
196 $result = $this->callAPIFailure('uf_join', 'create', []);
197 $this->assertContains('Mandatory', $result['error_message']);
198 $this->assertContains('missing', $result['error_message']);
199 $this->assertContains('module', $result['error_message']);
200 $this->assertContains('uf_group_id', $result['error_message']);
201 }
202
203 /**
204 * @param int $version
205 * @dataProvider versionThreeAndFour
206 */
207 public function testCreateUFJoinWithoutUFGroupId($version) {
208 $this->_apiversion = $version;
209 $params = [
210 'module' => 'CiviContribute',
211 'entity_table' => 'civicrm_contribution_page',
212 'entity_id' => 1,
213 'weight' => 1,
214 'is_active' => 1,
215 ];
216 $result = $this->callAPIFailure('uf_join', 'create', $params);
217 $this->assertContains('Mandatory', $result['error_message']);
218 $this->assertContains('missing', $result['error_message']);
219 $this->assertContains('uf_group_id', $result['error_message']);
220 }
221
222 /**
223 * Find uf join id.
224 * @param int $version
225 * @dataProvider versionThreeAndFour
226 */
227 public function testGetUFJoinId($version) {
228 $this->_apiversion = $version;
229 $params = [
230 'module' => 'CiviContribute',
231 'entity_table' => 'civicrm_contribution_page',
232 'entity_id' => 1,
233 'weight' => 1,
234 'uf_group_id' => $this->_ufGroupId,
235 'is_active' => 1,
236 ];
237
238 $ufJoin = $this->callAPISuccess('uf_join', 'create', $params);
239 $searchParams = [
240 'entity_table' => 'civicrm_contribution_page',
241 'entity_id' => 1,
242 'sequential' => 1,
243 ];
244
245 $result = $this->callAPIAndDocument('uf_join', 'get', $searchParams, __FUNCTION__, __FILE__);
246 $this->assertEquals($result['values'][0]['module'], $params['module']);
247 $this->assertEquals($result['values'][0]['uf_group_id'], $params['uf_group_id']);
248 $this->assertEquals($result['values'][0]['entity_id'], $params['entity_id']);
249 }
250
251 /**
252 * Test civicrm_activity_create() using example code.
253 * @param int $version
254 * @dataProvider versionThreeAndFour
255 */
256 public function testUFJoinCreateExample($version) {
257 $this->_apiversion = $version;
258 require_once 'api/v3/examples/UFJoin/Create.ex.php';
259 $result = UF_join_create_example();
260 $expectedResult = UF_join_create_expectedresult();
261 $this->assertEquals($result, $expectedResult);
262 }
263
264 }