Merge pull request #11336 from eileenmcnaughton/live
[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 * @var String
37 */
38 protected $_membershipTypeName = NULL;
39
40 /**
41 * Membership type id used in test function.
42 * @var String
43 */
44 protected $_membershipTypeID = NULL;
45
46 public function setUp() {
47 parent::setUp();
48
49 $params = array(
50 'contact_type_a' => 'Individual',
51 'contact_type_b' => 'Organization',
52 'name_a_b' => 'Test Employee of',
53 'name_b_a' => 'Test Employer of',
54 );
55 $this->_relationshipTypeId = $this->relationshipTypeCreate($params);
56 $this->_orgContactID = $this->organizationCreate();
57 $this->_financialTypeId = 1;
58 $this->_membershipTypeName = 'Mickey Mouse Club Member';
59 $params = array(
60 'name' => $this->_membershipTypeName,
61 'description' => NULL,
62 'minimum_fee' => 10,
63 'duration_unit' => 'year',
64 'member_of_contact_id' => $this->_orgContactID,
65 'period_type' => 'fixed',
66 'duration_interval' => 1,
67 'financial_type_id' => $this->_financialTypeId,
68 'relationship_type_id' => $this->_relationshipTypeId,
69 'visibility' => 'Public',
70 'is_active' => 1,
71 'fixed_period_start_day' => 101,
72 'fixed_period_rollover_day' => 1231,
73 );
74 $ids = array();
75 $membershipType = CRM_Member_BAO_MembershipType::add($params, $ids);
76 $this->_membershipTypeID = $membershipType->id;
77
78 $this->_mebershipStatusID = $this->membershipStatusCreate('test status');
79 $session = CRM_Core_Session::singleton();
80 $session->set('dateTypes', 1);
81 }
82
83 /**
84 * Tears down the fixture, for example, closes a network connection.
85 * This method is called after a test is executed.
86 */
87 public function tearDown() {
88 $tablesToTruncate = array(
89 'civicrm_membership',
90 'civicrm_membership_log',
91 'civicrm_contribution',
92 'civicrm_membership_payment',
93 );
94 $this->quickCleanup($tablesToTruncate);
95 $this->relationshipTypeDelete($this->_relationshipTypeId);
96 $this->membershipTypeDelete(array('id' => $this->_membershipTypeID));
97 $this->membershipStatusDelete($this->_mebershipStatusID);
98 $this->contactDelete($this->_orgContactID);
99 }
100
101 /**
102 * Test Import.
103 */
104 public function testImport() {
105 $this->individualCreate();
106 $contact2Params = array(
107 'first_name' => 'Anthonita',
108 'middle_name' => 'J.',
109 'last_name' => 'Anderson',
110 'prefix_id' => 3,
111 'suffix_id' => 3,
112 'email' => 'b@c.com',
113 'contact_type' => 'Individual',
114 );
115
116 $this->individualCreate($contact2Params);
117 $year = date('Y') - 1;
118 $startDate2 = date('Y-m-d', mktime(0, 0, 0, 9, 10, $year));
119 $params = array(
120 array(
121 'anthony_anderson@civicrm.org',
122 $this->_membershipTypeID,
123 date('Y-m-d'),
124 ),
125 array(
126 $contact2Params['email'],
127 $this->_membershipTypeName,
128 $startDate2,
129 ),
130 );
131 $fieldMapper = array(
132 'mapper[0][0]' => 'email',
133 'mapper[1][0]' => 'membership_type_id',
134 'mapper[2][0]' => 'membership_start_date',
135 );
136
137 $importObject = new CRM_Member_Import_Parser_Membership($fieldMapper);
138 $importObject->init();
139 $importObject->_contactType = 'Individual';
140 foreach ($params as $values) {
141 $this->assertEquals(CRM_Import_Parser::VALID, $importObject->import(CRM_Import_Parser::DUPLICATE_UPDATE, $values), $values[0]);
142 }
143 $result = $this->callAPISuccess('membership', 'get', array());
144 $this->assertEquals(2, $result['count']);
145 }
146
147 public function testImportOverriddenMembershipButWithoutStatus() {
148 $this->individualCreate(array('email' => 'anthony_anderson2@civicrm.org'));
149
150 $fieldMapper = array(
151 'mapper[0][0]' => 'email',
152 'mapper[1][0]' => 'membership_type_id',
153 'mapper[2][0]' => 'membership_start_date',
154 'mapper[3][0]' => 'is_override',
155 );
156 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
157 $membershipImporter->init();
158 $membershipImporter->_contactType = 'Individual';
159
160 $importValues = array(
161 'anthony_anderson2@civicrm.org',
162 $this->_membershipTypeID,
163 date('Y-m-d'),
164 TRUE,
165 );
166
167 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
168 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
169 $this->assertContains('Required parameter missing: Status', $importValues);
170 }
171
172 public function testImportOverriddenMembershipWithStatus() {
173 $this->individualCreate(array('email' => 'anthony_anderson3@civicrm.org'));
174
175 $fieldMapper = array(
176 'mapper[0][0]' => 'email',
177 'mapper[1][0]' => 'membership_type_id',
178 'mapper[2][0]' => 'membership_start_date',
179 'mapper[3][0]' => 'is_override',
180 'mapper[4][0]' => 'status_id',
181 );
182 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
183 $membershipImporter->init();
184 $membershipImporter->_contactType = 'Individual';
185
186 $importValues = array(
187 'anthony_anderson3@civicrm.org',
188 $this->_membershipTypeID,
189 date('Y-m-d'),
190 TRUE,
191 'New',
192 );
193
194 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
195 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
196 }
197
198 public function testImportOverriddenMembershipWithValidOverrideEndDate() {
199 $this->individualCreate(array('email' => 'anthony_anderson4@civicrm.org'));
200
201 $fieldMapper = array(
202 'mapper[0][0]' => 'email',
203 'mapper[1][0]' => 'membership_type_id',
204 'mapper[2][0]' => 'membership_start_date',
205 'mapper[3][0]' => 'is_override',
206 'mapper[4][0]' => 'status_id',
207 'mapper[5][0]' => 'status_override_end_date',
208 );
209 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
210 $membershipImporter->init();
211 $membershipImporter->_contactType = 'Individual';
212
213 $importValues = array(
214 'anthony_anderson4@civicrm.org',
215 $this->_membershipTypeID,
216 date('Y-m-d'),
217 TRUE,
218 'New',
219 date('Y-m-d'),
220 );
221
222 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
223 $this->assertEquals(CRM_Import_Parser::VALID, $importResponse);
224 }
225
226 public function testImportOverriddenMembershipWithInvalidOverrideEndDate() {
227 $this->individualCreate(array('email' => 'anthony_anderson5@civicrm.org'));
228
229 $fieldMapper = array(
230 'mapper[0][0]' => 'email',
231 'mapper[1][0]' => 'membership_type_id',
232 'mapper[2][0]' => 'membership_start_date',
233 'mapper[3][0]' => 'is_override',
234 'mapper[4][0]' => 'status_id',
235 'mapper[5][0]' => 'status_override_end_date',
236 );
237 $membershipImporter = new CRM_Member_Import_Parser_Membership($fieldMapper);
238 $membershipImporter->init();
239 $membershipImporter->_contactType = 'Individual';
240
241 $importValues = array(
242 'anthony_anderson5@civicrm.org',
243 'New',
244 date('Y-m-d'),
245 TRUE,
246 $this->_mebershipStatusID,
247 'abc',
248 );
249
250 $importResponse = $membershipImporter->import(CRM_Import_Parser::DUPLICATE_UPDATE, $importValues);
251 $this->assertEquals(CRM_Import_Parser::ERROR, $importResponse);
252 $this->assertContains('Required parameter missing: Status', $importValues);
253 }
254
255 }