Merge pull request #6170 from colemanw/CRM-16354
[civicrm-core.git] / tests / phpunit / CRM / Member / Form / MembershipRenewalTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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 * File for the MembershipTest class
30 *
31 * (PHP 5)
32 *
33 * @author Walt Haas <walt@dharmatech.org> (801) 534-1262
34 */
35
36 /**
37 * Include class definitions
38 */
39 require_once 'CiviTest/CiviUnitTestCase.php';
40
41 require_once 'HTML/QuickForm/Page.php';
42
43 /**
44 * Test CRM_Member_Form_Membership functions.
45 *
46 * @package CiviCRM
47 */
48 class CRM_Member_Form_MembershipRenewalTest extends CiviUnitTestCase {
49
50 /**
51 * Assume empty database with just civicrm_data.
52 */
53 protected $_individualId;
54 protected $_contribution;
55 protected $_financialTypeId = 1;
56 protected $_apiversion;
57 protected $_entity = 'Membership';
58 protected $_params;
59 protected $_ids = array();
60 protected $_paymentProcessorID;
61
62 /**
63 * Membership type ID for annual fixed membership.
64 *
65 * @var int
66 */
67 protected $membershipTypeAnnualFixedID;
68
69 /**
70 * Parameters to create payment processor.
71 *
72 * @var array
73 */
74 protected $_processorParams = array();
75
76 /**
77 * ID of created membership.
78 *
79 * @var int
80 */
81 protected $_membershipID;
82
83 /**
84 * Payment instrument mapping.
85 *
86 * @var array
87 */
88 protected $paymentInstruments = array();
89
90 /**
91 * Test setup for every test.
92 *
93 * Connect to the database, truncate the tables that will be used
94 * and redirect stdin to a temporary file.
95 */
96 public function setUp() {
97 $this->_apiversion = 3;
98 parent::setUp();
99
100 $this->_individualId = $this->individualCreate();
101 $processor = $this->processorCreate();
102 $this->_paymentProcessorID = $processor->id;
103 // Insert test data.
104 $op = new PHPUnit_Extensions_Database_Operation_Insert();
105 $op->execute($this->_dbconn,
106 $this->createFlatXMLDataSet(
107 dirname(__FILE__) . '/dataset/data.xml'
108 )
109 );
110 $membershipTypeAnnualFixed = $this->callAPISuccess('membership_type', 'create', array(
111 'domain_id' => 1,
112 'name' => "AnnualFixed",
113 'member_of_contact_id' => 23,
114 'duration_unit' => "year",
115 'duration_interval' => 1,
116 'period_type' => "fixed",
117 'fixed_period_start_day' => "101",
118 'fixed_period_rollover_day' => "1231",
119 'relationship_type_id' => 20,
120 'financial_type_id' => 2,
121 ));
122 $this->membershipTypeAnnualFixedID = $membershipTypeAnnualFixed['id'];
123 $membership = $this->callAPISuccess('Membership', 'create', array(
124 'contact_id' => $this->_individualId,
125 'membership_type_id' => $this->membershipTypeAnnualFixedID,
126 ));
127 $this->_membershipID = $membership['id'];
128
129 $instruments = $this->callAPISuccess('contribution', 'getoptions', array('field' => 'payment_instrument_id'));
130 $this->paymentInstruments = $instruments['values'];
131 }
132
133 /**
134 * Clean up after each test.
135 */
136 public function tearDown() {
137 $this->quickCleanUpFinancialEntities();
138 $this->quickCleanup(
139 array(
140 'civicrm_relationship',
141 'civicrm_membership_type',
142 'civicrm_membership',
143 'civicrm_uf_match',
144 )
145 );
146 $this->callAPISuccess('contact', 'delete', array('id' => 17, 'skip_undelete' => TRUE));
147 $this->callAPISuccess('contact', 'delete', array('id' => 23, 'skip_undelete' => TRUE));
148 $this->callAPISuccess('relationship_type', 'delete', array('id' => 20));
149 }
150
151 /**
152 * Test CRM_Member_Form_Membership::buildQuickForm()
153 */
154 //function testCRMMemberFormMembershipBuildQuickForm()
155 //{
156 // throw new PHPUnit_Framework_IncompleteTestError( "not implemented" );
157 //}
158
159 /**
160 * Test the submit function of the membership form.
161 */
162 public function testSubmit() {
163 $form = $this->getForm();
164 $this->createLoggedInUser();
165 $params = array(
166 'cid' => $this->_individualId,
167 'join_date' => date('m/d/Y', time()),
168 'start_date' => '',
169 'end_date' => '',
170 // This format reflects the 23 being the organisation & the 25 being the type.
171 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
172 'auto_renew' => '0',
173 'max_related' => '',
174 'num_terms' => '1',
175 'source' => '',
176 'total_amount' => '50.00',
177 'financial_type_id' => '2', //Member dues, see data.xml
178 'soft_credit_type_id' => '',
179 'soft_credit_contact_id' => '',
180 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
181 'receipt_text_signup' => 'Thank you text',
182 'payment_processor_id' => $this->_paymentProcessorID,
183 'credit_card_number' => '4111111111111111',
184 'cvv2' => '123',
185 'credit_card_exp_date' => array(
186 'M' => '9',
187 'Y' => '2024', // TODO: Future proof
188 ),
189 'credit_card_type' => 'Visa',
190 'billing_first_name' => 'Test',
191 'billing_middlename' => 'Last',
192 'billing_street_address-5' => '10 Test St',
193 'billing_city-5' => 'Test',
194 'billing_state_province_id-5' => '1003',
195 'billing_postal_code-5' => '90210',
196 'billing_country_id-5' => '1228',
197 );
198 $form->_contactID = $this->_individualId;
199
200 $form->testSubmit($params);
201 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
202 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 0);
203 $contribution = $this->callAPISuccess('Contribution', 'get', array(
204 'contact_id' => $this->_individualId,
205 'is_test' => TRUE,
206 ));
207
208 $this->callAPISuccessGetCount('LineItem', array(
209 'entity_id' => $membership['id'],
210 'entity_table' => 'civicrm_membership',
211 'contribution_id' => $contribution['id'],
212 ), 1);
213 }
214
215 /**
216 * Test the submit function of the membership form.
217 */
218 public function testSubmitRecur() {
219 $form = $this->getForm();
220
221 $this->callAPISuccess('MembershipType', 'create', array(
222 'id' => $this->membershipTypeAnnualFixedID,
223 'duration_unit' => 'month',
224 'duration_interval' => 1,
225 'auto_renew' => TRUE,
226 ));
227 $form->preProcess();
228 $this->createLoggedInUser();
229 $params = array(
230 'cid' => $this->_individualId,
231 'price_set_id' => 0,
232 'join_date' => date('m/d/Y', time()),
233 'start_date' => '',
234 'end_date' => '',
235 'campaign_id' => '',
236 // This format reflects the 23 being the organisation & the 25 being the type.
237 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
238 'auto_renew' => '1',
239 'is_recur' => 1,
240 'max_related' => 0,
241 'num_terms' => '1',
242 'source' => '',
243 'total_amount' => '77.00',
244 'financial_type_id' => '2', //Member dues, see data.xml
245 'soft_credit_type_id' => 11,
246 'soft_credit_contact_id' => '',
247 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
248 'receipt_text' => 'Thank you text',
249 'payment_processor_id' => $this->_paymentProcessorID,
250 'credit_card_number' => '4111111111111111',
251 'cvv2' => '123',
252 'credit_card_exp_date' => array(
253 'M' => '9',
254 'Y' => '2019', // TODO: Future proof
255 ),
256 'credit_card_type' => 'Visa',
257 'billing_first_name' => 'Test',
258 'billing_middlename' => 'Last',
259 'billing_street_address-5' => '10 Test St',
260 'billing_city-5' => 'Test',
261 'billing_state_province_id-5' => '1003',
262 'billing_postal_code-5' => '90210',
263 'billing_country_id-5' => '1228',
264 );
265 $form->_mode = 'test';
266 $form->_contactID = $this->_individualId;
267
268 $form->testSubmit($params);
269 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
270 //$this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1);
271 $contribution = $this->callAPISuccess('Contribution', 'get', array(
272 'contact_id' => $this->_individualId,
273 'is_test' => TRUE,
274 ));
275
276 $this->callAPISuccessGetCount('LineItem', array(
277 'entity_id' => $membership['id'],
278 'entity_table' => 'civicrm_membership',
279 'contribution_id' => $contribution['id'],
280 ), 1);
281
282 }
283
284 /**
285 * Get a membership form object.
286 *
287 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
288 *
289 * @return \CRM_Member_Form_MembershipRenewal
290 */
291 protected function getForm() {
292 $form = new CRM_Member_Form_MembershipRenewal();
293 $_SERVER['REQUEST_METHOD'] = 'GET';
294 $form->controller = new CRM_Core_Controller();
295 $form->_bltID = 5;
296 $form->_mode = 'test';
297 $form->_id = $this->_membershipID;
298 $form->preProcess();
299 return $form;
300 }
301
302 }