Fix metadata on member export
[civicrm-core.git] / tests / phpunit / CRM / Member / Import / Parser / MembershipTest.php
CommitLineData
5e3757e9 1<?php
2
3/**
4 * File for the TestActivityType class
5 *
6 * (PHP 5)
7 *
6c6e6187 8 * @package CiviCRM
5e3757e9 9 *
10 * This file is part of CiviCRM
11 *
12 * CiviCRM is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Affero General Public License
14 * as published by the Free Software Foundation; either version 3 of
15 * the License, or (at your option) any later version.
16 *
17 * CiviCRM is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Affero General Public License for more details.
21 *
22 * You should have received a copy of the GNU Affero General Public
23 * License along with this program. If not, see
24 * <http://www.gnu.org/licenses/>.
25 */
26
5e3757e9 27/**
28 * Test CRM/Member/BAO Membership Log add , delete functions
29 *
6c6e6187 30 * @package CiviCRM
acb109b7 31 * @group headless
5e3757e9 32 */
33class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
5e3757e9 34 /**
eceb18cc 35 * Membership type name used in test function.
9f266042 36 *
37 * @var string
5e3757e9 38 */
39 protected $_membershipTypeName = NULL;
40
41 /**
eceb18cc 42 * Membership type id used in test function.
9f266042 43 *
44 * @var string
5e3757e9 45 */
46 protected $_membershipTypeID = NULL;
5e3757e9 47
00be9182 48 public function setUp() {
5e3757e9 49 parent::setUp();
50
9099cab3 51 $params = [
5e3757e9 52 'contact_type_a' => 'Individual',
53 'contact_type_b' => 'Organization',
54 'name_a_b' => 'Test Employee of',
55 'name_b_a' => 'Test Employer of',
9099cab3 56 ];
5e3757e9 57 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
58 $this->_orgContactID = $this->organizationCreate();
e6ff1593 59 $this->_financialTypeId = 1;
5e3757e9 60 $this->_membershipTypeName = 'Mickey Mouse Club Member';
9099cab3 61 $params = [
5e3757e9 62 'name' => $this->_membershipTypeName,
63 'description' => NULL,
64 'minimum_fee' => 10,
65 'duration_unit' => 'year',
66 'member_of_contact_id' => $this->_orgContactID,
67 'period_type' => 'fixed',
68 'duration_interval' => 1,
e6ff1593 69 'financial_type_id' => $this->_financialTypeId,
5e3757e9 70 'relationship_type_id' => $this->_relationshipTypeId,
71 'visibility' => 'Public',
72 'is_active' => 1,
73 'fixed_period_start_day' => 101,
21dfd5f5 74 'fixed_period_rollover_day' => 1231,
9099cab3
CW
75 ];
76 $ids = [];
5e3757e9 77 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
78 $this->_membershipTypeID = $membershipType->id;
79
80 $this->_mebershipStatusID = $this->membershipStatusCreate('test status');
81 $session = CRM_Core_Session::singleton();
82 $session->set('dateTypes', 1);
83 }
84
85 /**
86 * Tears down the fixture, for example, closes a network connection.
87 * This method is called after a test is executed.
14b9e069 88 *
89 * @throws \CRM_Core_Exception
5e3757e9 90 */
00be9182 91 public function tearDown() {
9099cab3 92 $tablesToTruncate = [
92915c55
TO
93 'civicrm_membership',
94 'civicrm_membership_log',
95 'civicrm_contribution',
408b79bf 96 'civicrm_membership_payment',
14b9e069 97 'civicrm_contact',
9099cab3 98 ];
14b9e069 99 $this->quickCleanup($tablesToTruncate, TRUE);
5e3757e9 100 $this->relationshipTypeDelete($this->_relationshipTypeId);
9099cab3 101 $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
5e3757e9 102 $this->membershipStatusDelete($this->_mebershipStatusID);
5e3757e9 103 }
104
105 /**
eceb18cc 106 * Test Import.
5e3757e9 107 */
00be9182 108 public function testImport() {
e136f704 109 $this->individualCreate();
9099cab3 110 $contact2Params = [
5e3757e9 111 'first_name' => 'Anthonita',
112 'middle_name' => 'J.',
113 'last_name' => 'Anderson',
114 'prefix_id' => 3,
115 'suffix_id' => 3,
116 'email' => 'b@c.com',
117 'contact_type' => 'Individual',
9099cab3 118 ];
e136f704
O
119
120 $this->individualCreate($contact2Params);
5e3757e9 121 $year = date('Y') - 1;
122 $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year));
9099cab3
CW
123 $params = [
124 [
5e3757e9 125 'anthony_anderson@civicrm.org',
92915c55
TO
126 $this->_membershipTypeID,
127 date('Y-m-d'),
9099cab3
CW
128 ],
129 [
5e3757e9 130 $contact2Params['email'],
131 $this->_membershipTypeName,
132 $startDate2,
9099cab3
CW
133 ],
134 ];
135 $fieldMapper = [
5e3757e9 136 'mapper[0][0]' => 'email',
137 'mapper[1][0]' => 'membership_type_id',
138 'mapper[2][0]' => 'membership_start_date',
9099cab3 139 ];
5e3757e9 140
5e3757e9 141 $importObject = new CRM_Member_Import_Parser_Membership($fieldMapper);
142 $importObject->init();
143 $importObject->_contactType = 'Individual';
144 foreach ($params as $values) {
145 $this->assertEquals(CRM_Import_Parser::VALID, $importObject->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), $values[0]);
146 }
9099cab3 147 $result = $this->callAPISuccess('membership', 'get', []);
5e3757e9 148 $this->assertEquals(2, $result['count']);
149 }
96025800 150
8f67d99a 151 /**
152 * Test overriding a membership but not providing status.
153 *
154 * @throws \CRM_Core_Exception
155 */
e136f704 156 public function testImportOverriddenMembershipButWithoutStatus() {
9099cab3 157 $this->individualCreate(['email' => 'anthony_anderson2@civicrm.org']);
e136f704 158
9099cab3 159 $fieldMapper = [
e136f704
O
160 'mapper[0][0]' => 'email',
161 'mapper[1][0]' => 'membership_type_id',
162 'mapper[2][0]' => 'membership_start_date',
8f67d99a 163 'mapper[3][0]' => 'member_is_override',
9099cab3 164 ];
e136f704
O
165 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
166 $membershipImporter->init();
167 $membershipImporter->_contactType = 'Individual';
168
9099cab3 169 $importValues = [
e136f704
O
170 'anthony_anderson2@civicrm.org',
171 $this->_membershipTypeID,
172 date('Y-m-d'),
173 TRUE,
9099cab3 174 ];
e136f704
O
175
176 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
177 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
178 $this->assertContains('Required parameter missing: Status', $importValues);
179 }
180
14b9e069 181 /**
182 * Test that the passed in status is respected.
183 *
184 * @throws \CRM_Core_Exception
185 */
e136f704 186 public function testImportOverriddenMembershipWithStatus() {
9099cab3 187 $this->individualCreate(['email' => 'anthony_anderson3@civicrm.org']);
14b9e069 188 $membershipImporter = $this->createImportObject([
189 'email',
190 'membership_type_id',
191 'membership_start_date',
8f67d99a 192 'member_is_override',
14b9e069 193 'status_id',
194 ]);
e136f704 195
9099cab3 196 $importValues = [
e136f704
O
197 'anthony_anderson3@civicrm.org',
198 $this->_membershipTypeID,
199 date('Y-m-d'),
200 TRUE,
201 'New',
9099cab3 202 ];
e136f704
O
203
204 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
205 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
206 }
207
208 public function testImportOverriddenMembershipWithValidOverrideEndDate() {
9099cab3 209 $this->individualCreate(['email' => 'anthony_anderson4@civicrm.org']);
e136f704 210
9099cab3 211 $fieldMapper = [
e136f704
O
212 'mapper[0][0]' => 'email',
213 'mapper[1][0]' => 'membership_type_id',
214 'mapper[2][0]' => 'membership_start_date',
8f67d99a 215 'mapper[3][0]' => 'member_is_override',
e136f704
O
216 'mapper[4][0]' => 'status_id',
217 'mapper[5][0]' => 'status_override_end_date',
9099cab3 218 ];
e136f704
O
219 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
220 $membershipImporter->init();
221 $membershipImporter->_contactType = 'Individual';
222
9099cab3 223 $importValues = [
e136f704
O
224 'anthony_anderson4@civicrm.org',
225 $this->_membershipTypeID,
226 date('Y-m-d'),
227 TRUE,
228 'New',
229 date('Y-m-d'),
9099cab3 230 ];
e136f704
O
231
232 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
233 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
234 }
235
236 public function testImportOverriddenMembershipWithInvalidOverrideEndDate() {
9099cab3 237 $this->individualCreate(['email' => 'anthony_anderson5@civicrm.org']);
e136f704 238
9099cab3 239 $fieldMapper = [
e136f704
O
240 'mapper[0][0]' => 'email',
241 'mapper[1][0]' => 'membership_type_id',
242 'mapper[2][0]' => 'membership_start_date',
8f67d99a 243 'mapper[3][0]' => 'member_is_override',
e136f704
O
244 'mapper[4][0]' => 'status_id',
245 'mapper[5][0]' => 'status_override_end_date',
9099cab3 246 ];
e136f704
O
247 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
248 $membershipImporter->init();
249 $membershipImporter->_contactType = 'Individual';
250
9099cab3 251 $importValues = [
e136f704
O
252 'anthony_anderson5@civicrm.org',
253 'New',
254 date('Y-m-d'),
255 TRUE,
256 $this->_mebershipStatusID,
257 'abc',
9099cab3 258 ];
e136f704
O
259
260 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
261 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
262 $this->assertContains('Required parameter missing: Status', $importValues);
263 }
264
14b9e069 265 /**
266 * Test that memberships can still be imported if the status is renamed.
267 *
268 * @throws \CRM_Core_Exception
269 */
270 public function testImportMembershipWithRenamedStatus() {
271 $this->individualCreate(['email' => 'anthony_anderson3@civicrm.org']);
272
273 $this->callAPISuccess('MembershipStatus', 'get', [
274 'name' => 'New',
275 'api.MembershipStatus.create' => [
276 'label' => 'New-renamed',
277 ],
278 ]);
279 $membershipImporter = $this->createImportObject([
280 'email',
281 'membership_type_id',
282 'membership_start_date',
8f67d99a 283 'member_is_override',
14b9e069 284 'status_id',
285 ]);
286
287 $importValues = [
288 'anthony_anderson3@civicrm.org',
289 $this->_membershipTypeID,
290 date('Y-m-d'),
291 TRUE,
292 'New-renamed',
293 ];
294
295 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
296 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
297 $createdStatusID = $this->callAPISuccessGetValue('Membership', ['return' => 'status_id']);
298 $this->assertEquals(CRM_Core_PseudoConstant::getKey('CRM_Member_BAO_Membership', 'status_id', 'New'), $createdStatusID);
299 $this->callAPISuccess('MembershipStatus', 'get', [
300 'name' => 'New',
301 'api.MembershipStatus.create' => [
302 'label' => 'New',
303 ],
304 ]);
305 }
306
307 /**
308 * Create an import object.
309 *
310 * @param array $fields
311 *
312 * @return \CRM_Member_Import_Parser_Membership
313 */
314 protected function createImportObject(array $fields): \CRM_Member_Import_Parser_Membership {
315 $fieldMapper = [];
316 foreach ($fields as $index => $field) {
317 $fieldMapper['mapper[' . $index . '][0]'] = $field;
318 }
319 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
320 $membershipImporter->init();
321 $membershipImporter->_contactType = 'Individual';
322 return $membershipImporter;
323 }
324
5e3757e9 325}