minor refactors toward getting membership_recur test to pass
[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 the submit function of the membership form.
153 */
154 public function testSubmit() {
155 $form = $this->getForm();
156 $this->createLoggedInUser();
157 $params = array(
158 'cid' => $this->_individualId,
159 'join_date' => date('m/d/Y', time()),
160 'start_date' => '',
161 'end_date' => '',
162 // This format reflects the 23 being the organisation & the 25 being the type.
163 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
164 'auto_renew' => '0',
165 'max_related' => '',
166 'num_terms' => '1',
167 'source' => '',
168 'total_amount' => '50.00',
169 //Member dues, see data.xml
170 'financial_type_id' => '2',
171 'soft_credit_type_id' => '',
172 'soft_credit_contact_id' => '',
173 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
174 'receipt_text_signup' => 'Thank you text',
175 'payment_processor_id' => $this->_paymentProcessorID,
176 'credit_card_number' => '4111111111111111',
177 'cvv2' => '123',
178 'credit_card_exp_date' => array(
179 'M' => '9',
180 'Y' => '2024', // TODO: Future proof
181 ),
182 'credit_card_type' => 'Visa',
183 'billing_first_name' => 'Test',
184 'billing_middlename' => 'Last',
185 'billing_street_address-5' => '10 Test St',
186 'billing_city-5' => 'Test',
187 'billing_state_province_id-5' => '1003',
188 'billing_postal_code-5' => '90210',
189 'billing_country_id-5' => '1228',
190 );
191 $form->_contactID = $this->_individualId;
192
193 $form->testSubmit($params);
194 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
195 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 0);
196 $contribution = $this->callAPISuccess('Contribution', 'get', array(
197 'contact_id' => $this->_individualId,
198 'is_test' => TRUE,
199 ));
200
201 $this->callAPISuccessGetCount('LineItem', array(
202 'entity_id' => $membership['id'],
203 'entity_table' => 'civicrm_membership',
204 'contribution_id' => $contribution['id'],
205 ), 1);
206 }
207
208 /**
209 * Test the submit function of the membership form.
210 */
211 public function testSubmitRecur() {
212 $form = $this->getForm();
213
214 $this->callAPISuccess('MembershipType', 'create', array(
215 'id' => $this->membershipTypeAnnualFixedID,
216 'duration_unit' => 'month',
217 'duration_interval' => 1,
218 'auto_renew' => TRUE,
219 ));
220 $form->preProcess();
221 $this->createLoggedInUser();
222 $params = array(
223 'cid' => $this->_individualId,
224 'price_set_id' => 0,
225 'join_date' => date('m/d/Y', time()),
226 'start_date' => '',
227 'end_date' => '',
228 'campaign_id' => '',
229 // This format reflects the 23 being the organisation & the 25 being the type.
230 'membership_type_id' => array(23, $this->membershipTypeAnnualFixedID),
231 'auto_renew' => '1',
232 'is_recur' => 1,
233 'max_related' => 0,
234 'num_terms' => '1',
235 'source' => '',
236 'total_amount' => '77.00',
237 'financial_type_id' => '2', //Member dues, see data.xml
238 'soft_credit_type_id' => 11,
239 'soft_credit_contact_id' => '',
240 'from_email_address' => '"Demonstrators Anonymous" <info@example.org>',
241 'receipt_text' => 'Thank you text',
242 'payment_processor_id' => $this->_paymentProcessorID,
243 'credit_card_number' => '4111111111111111',
244 'cvv2' => '123',
245 'credit_card_exp_date' => array(
246 'M' => '9',
247 'Y' => '2019', // TODO: Future proof
248 ),
249 'credit_card_type' => 'Visa',
250 'billing_first_name' => 'Test',
251 'billing_middlename' => 'Last',
252 'billing_street_address-5' => '10 Test St',
253 'billing_city-5' => 'Test',
254 'billing_state_province_id-5' => '1003',
255 'billing_postal_code-5' => '90210',
256 'billing_country_id-5' => '1228',
257 );
258 $form->_mode = 'test';
259 $form->_contactID = $this->_individualId;
260
261 $form->testSubmit($params);
262 $membership = $this->callAPISuccessGetSingle('Membership', array('contact_id' => $this->_individualId));
263 $this->callAPISuccessGetCount('ContributionRecur', array('contact_id' => $this->_individualId), 1);
264 $contribution = $this->callAPISuccess('Contribution', 'get', array(
265 'contact_id' => $this->_individualId,
266 'is_test' => TRUE,
267 ));
268
269 $this->callAPISuccessGetCount('LineItem', array(
270 'entity_id' => $membership['id'],
271 'entity_table' => 'civicrm_membership',
272 'contribution_id' => $contribution['id'],
273 ), 1);
274
275 }
276
277 /**
278 * Get a membership form object.
279 *
280 * We need to instantiate the form to run preprocess, which means we have to trick it about the request method.
281 *
282 * @return \CRM_Member_Form_MembershipRenewal
283 */
284 protected function getForm() {
285 $form = new CRM_Member_Form_MembershipRenewal();
286 $_SERVER['REQUEST_METHOD'] = 'GET';
287 $form->controller = new CRM_Core_Controller();
288 $form->_bltID = 5;
289 $form->_mode = 'test';
290 $form->_id = $this->_membershipID;
291 $form->preProcess();
292 return $form;
293 }
294
295 }