Fix metadata on member export
[civicrm-core.git] / tests / phpunit / CRM / Member / Import / Parser / MembershipTest.php
1 <?php
2
3 /**
4 * File for the TestActivityType class
5 *
6 * (PHP 5)
7 *
8 * @package CiviCRM
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
27 /**
28 * Test CRM/Member/BAO Membership Log add , delete functions
29 *
30 * @package CiviCRM
31 * @group headless
32 */
33 class CRM_Member_Import_Parser_MembershipTest extends CiviUnitTestCase {
34 /**
35 * Membership type name used in test function.
36 *
37 * @var string
38 */
39 protected $_membershipTypeName = NULL;
40
41 /**
42 * Membership type id used in test function.
43 *
44 * @var string
45 */
46 protected $_membershipTypeID = NULL;
47
48 public function setUp() {
49 parent::setUp();
50
51 $params = [
52 'contact_type_a' => 'Individual',
53 'contact_type_b' => 'Organization',
54 'name_a_b' => 'Test Employee of',
55 'name_b_a' => 'Test Employer of',
56 ];
57 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
58 $this->_orgContactID = $this->organizationCreate();
59 $this->_financialTypeId = 1;
60 $this->_membershipTypeName = 'Mickey Mouse Club Member';
61 $params = [
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,
69 'financial_type_id' => $this->_financialTypeId,
70 'relationship_type_id' => $this->_relationshipTypeId,
71 'visibility' => 'Public',
72 'is_active' => 1,
73 'fixed_period_start_day' => 101,
74 'fixed_period_rollover_day' => 1231,
75 ];
76 $ids = [];
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.
88 *
89 * @throws \CRM_Core_Exception
90 */
91 public function tearDown() {
92 $tablesToTruncate = [
93 'civicrm_membership',
94 'civicrm_membership_log',
95 'civicrm_contribution',
96 'civicrm_membership_payment',
97 'civicrm_contact',
98 ];
99 $this->quickCleanup($tablesToTruncate, TRUE);
100 $this->relationshipTypeDelete($this->_relationshipTypeId);
101 $this->membershipTypeDelete(['id' => $this->_membershipTypeID]);
102 $this->membershipStatusDelete($this->_mebershipStatusID);
103 }
104
105 /**
106 * Test Import.
107 */
108 public function testImport() {
109 $this->individualCreate();
110 $contact2Params = [
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',
118 ];
119
120 $this->individualCreate($contact2Params);
121 $year = date('Y') - 1;
122 $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year));
123 $params = [
124 [
125 'anthony_anderson@civicrm.org',
126 $this->_membershipTypeID,
127 date('Y-m-d'),
128 ],
129 [
130 $contact2Params['email'],
131 $this->_membershipTypeName,
132 $startDate2,
133 ],
134 ];
135 $fieldMapper = [
136 'mapper[0][0]' => 'email',
137 'mapper[1][0]' => 'membership_type_id',
138 'mapper[2][0]' => 'membership_start_date',
139 ];
140
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 }
147 $result = $this->callAPISuccess('membership', 'get', []);
148 $this->assertEquals(2, $result['count']);
149 }
150
151 /**
152 * Test overriding a membership but not providing status.
153 *
154 * @throws \CRM_Core_Exception
155 */
156 public function testImportOverriddenMembershipButWithoutStatus() {
157 $this->individualCreate(['email' => 'anthony_anderson2@civicrm.org']);
158
159 $fieldMapper = [
160 'mapper[0][0]' => 'email',
161 'mapper[1][0]' => 'membership_type_id',
162 'mapper[2][0]' => 'membership_start_date',
163 'mapper[3][0]' => 'member_is_override',
164 ];
165 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
166 $membershipImporter->init();
167 $membershipImporter->_contactType = 'Individual';
168
169 $importValues = [
170 'anthony_anderson2@civicrm.org',
171 $this->_membershipTypeID,
172 date('Y-m-d'),
173 TRUE,
174 ];
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
181 /**
182 * Test that the passed in status is respected.
183 *
184 * @throws \CRM_Core_Exception
185 */
186 public function testImportOverriddenMembershipWithStatus() {
187 $this->individualCreate(['email' => 'anthony_anderson3@civicrm.org']);
188 $membershipImporter = $this->createImportObject([
189 'email',
190 'membership_type_id',
191 'membership_start_date',
192 'member_is_override',
193 'status_id',
194 ]);
195
196 $importValues = [
197 'anthony_anderson3@civicrm.org',
198 $this->_membershipTypeID,
199 date('Y-m-d'),
200 TRUE,
201 'New',
202 ];
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() {
209 $this->individualCreate(['email' => 'anthony_anderson4@civicrm.org']);
210
211 $fieldMapper = [
212 'mapper[0][0]' => 'email',
213 'mapper[1][0]' => 'membership_type_id',
214 'mapper[2][0]' => 'membership_start_date',
215 'mapper[3][0]' => 'member_is_override',
216 'mapper[4][0]' => 'status_id',
217 'mapper[5][0]' => 'status_override_end_date',
218 ];
219 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
220 $membershipImporter->init();
221 $membershipImporter->_contactType = 'Individual';
222
223 $importValues = [
224 'anthony_anderson4@civicrm.org',
225 $this->_membershipTypeID,
226 date('Y-m-d'),
227 TRUE,
228 'New',
229 date('Y-m-d'),
230 ];
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() {
237 $this->individualCreate(['email' => 'anthony_anderson5@civicrm.org']);
238
239 $fieldMapper = [
240 'mapper[0][0]' => 'email',
241 'mapper[1][0]' => 'membership_type_id',
242 'mapper[2][0]' => 'membership_start_date',
243 'mapper[3][0]' => 'member_is_override',
244 'mapper[4][0]' => 'status_id',
245 'mapper[5][0]' => 'status_override_end_date',
246 ];
247 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
248 $membershipImporter->init();
249 $membershipImporter->_contactType = 'Individual';
250
251 $importValues = [
252 'anthony_anderson5@civicrm.org',
253 'New',
254 date('Y-m-d'),
255 TRUE,
256 $this->_mebershipStatusID,
257 'abc',
258 ];
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
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',
283 'member_is_override',
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
325 }