Merge pull request #14326 from civicrm/5.14
[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 = array(
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 = array(
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 = array();
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 public function tearDown() {
90 $tablesToTruncate = array(
91 'civicrm_membership',
92 'civicrm_membership_log',
93 'civicrm_contribution',
94 'civicrm_membership_payment',
95 );
96 $this->quickCleanup($tablesToTruncate);
97 $this->relationshipTypeDelete($this->_relationshipTypeId);
98 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
99 $this->membershipStatusDelete($this->_mebershipStatusID);
100 $this->contactDelete($this->_orgContactID);
101 }
102
103 /**
104 * Test Import.
105 */
106 public function testImport() {
107 $this->individualCreate();
108 $contact2Params = array(
109 'first_name' => 'Anthonita',
110 'middle_name' => 'J.',
111 'last_name' => 'Anderson',
112 'prefix_id' => 3,
113 'suffix_id' => 3,
114 'email' => 'b@c.com',
115 'contact_type' => 'Individual',
116 );
117
118 $this->individualCreate($contact2Params);
119 $year = date('Y') - 1;
120 $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year));
121 $params = array(
122 array(
123 'anthony_anderson@civicrm.org',
124 $this->_membershipTypeID,
125 date('Y-m-d'),
126 ),
127 array(
128 $contact2Params['email'],
129 $this->_membershipTypeName,
130 $startDate2,
131 ),
132 );
133 $fieldMapper = array(
134 'mapper[0][0]' => 'email',
135 'mapper[1][0]' => 'membership_type_id',
136 'mapper[2][0]' => 'membership_start_date',
137 );
138
139 $importObject = new CRM_Member_Import_Parser_Membership($fieldMapper);
140 $importObject->init();
141 $importObject->_contactType = 'Individual';
142 foreach ($params as $values) {
143 $this->assertEquals(CRM_Import_Parser::VALID, $importObject->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), $values[0]);
144 }
145 $result = $this->callAPISuccess('membership', 'get', array());
146 $this->assertEquals(2, $result['count']);
147 }
148
149 public function testImportOverriddenMembershipButWithoutStatus() {
150 $this->individualCreate(array('email' => 'anthony_anderson2@civicrm.org'));
151
152 $fieldMapper = array(
153 'mapper[0][0]' => 'email',
154 'mapper[1][0]' => 'membership_type_id',
155 'mapper[2][0]' => 'membership_start_date',
156 'mapper[3][0]' => 'is_override',
157 );
158 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
159 $membershipImporter->init();
160 $membershipImporter->_contactType = 'Individual';
161
162 $importValues = array(
163 'anthony_anderson2@civicrm.org',
164 $this->_membershipTypeID,
165 date('Y-m-d'),
166 TRUE,
167 );
168
169 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
170 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
171 $this->assertContains('Required parameter missing: Status', $importValues);
172 }
173
174 public function testImportOverriddenMembershipWithStatus() {
175 $this->individualCreate(array('email' => 'anthony_anderson3@civicrm.org'));
176
177 $fieldMapper = array(
178 'mapper[0][0]' => 'email',
179 'mapper[1][0]' => 'membership_type_id',
180 'mapper[2][0]' => 'membership_start_date',
181 'mapper[3][0]' => 'is_override',
182 'mapper[4][0]' => 'status_id',
183 );
184 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
185 $membershipImporter->init();
186 $membershipImporter->_contactType = 'Individual';
187
188 $importValues = array(
189 'anthony_anderson3@civicrm.org',
190 $this->_membershipTypeID,
191 date('Y-m-d'),
192 TRUE,
193 'New',
194 );
195
196 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
197 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
198 }
199
200 public function testImportOverriddenMembershipWithValidOverrideEndDate() {
201 $this->individualCreate(array('email' => 'anthony_anderson4@civicrm.org'));
202
203 $fieldMapper = array(
204 'mapper[0][0]' => 'email',
205 'mapper[1][0]' => 'membership_type_id',
206 'mapper[2][0]' => 'membership_start_date',
207 'mapper[3][0]' => 'is_override',
208 'mapper[4][0]' => 'status_id',
209 'mapper[5][0]' => 'status_override_end_date',
210 );
211 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
212 $membershipImporter->init();
213 $membershipImporter->_contactType = 'Individual';
214
215 $importValues = array(
216 'anthony_anderson4@civicrm.org',
217 $this->_membershipTypeID,
218 date('Y-m-d'),
219 TRUE,
220 'New',
221 date('Y-m-d'),
222 );
223
224 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
225 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
226 }
227
228 public function testImportOverriddenMembershipWithInvalidOverrideEndDate() {
229 $this->individualCreate(array('email' => 'anthony_anderson5@civicrm.org'));
230
231 $fieldMapper = array(
232 'mapper[0][0]' => 'email',
233 'mapper[1][0]' => 'membership_type_id',
234 'mapper[2][0]' => 'membership_start_date',
235 'mapper[3][0]' => 'is_override',
236 'mapper[4][0]' => 'status_id',
237 'mapper[5][0]' => 'status_override_end_date',
238 );
239 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
240 $membershipImporter->init();
241 $membershipImporter->_contactType = 'Individual';
242
243 $importValues = array(
244 'anthony_anderson5@civicrm.org',
245 'New',
246 date('Y-m-d'),
247 TRUE,
248 $this->_mebershipStatusID,
249 'abc',
250 );
251
252 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
253 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
254 $this->assertContains('Required parameter missing: Status', $importValues);
255 }
256
257 }