3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 require_once 'CiviTest/CiviUnitTestCase.php';
32 * Class CRM_Core_Payment_BaseIPNTest
34 class CRM_Core_Payment_BaseIPNTest
extends CiviUnitTestCase
{
36 protected $_contributionTypeId;
37 protected $_contributionParams;
38 protected $_contactId;
39 protected $_contributionId;
40 protected $_participantId;
43 protected $_processorId;
44 protected $_contributionRecurParams;
45 protected $_paymentProcessor;
48 protected $_membershipId;
52 public $DBResetRequired = FALSE;
54 public function setUp() {
56 $this->_processorId
= $this->paymentProcessorAuthorizeNetCreate();
57 $this->input
= $this->ids
= $this->objects
= array();
58 $this->IPN
= new CRM_Core_Payment_AuthorizeNetIPN($this->input
);
60 $this->_contactId
= $this->individualCreate();
61 $this->ids
['contact'] = $this->_contactId
;
62 $this->_contributionTypeId
= 1;
64 $this->_contributionParams
= array(
65 'contact_id' => $this->_contactId
,
67 'financial_type_id' => $this->_contributionTypeId
,
68 'receive_date' => date('Ymd'),
69 'total_amount' => 150.00,
70 'invoice_id' => 'c8acb91e080ad7bd8a2adc119c192885',
72 'contribution_recur_id' => $this->_recurId
,
74 'contribution_status_id' => 2,
76 $contribution = civicrm_api('contribution', 'create', $this->_contributionParams
);
77 $this->assertAPISuccess($contribution, ' set-up of contribution ');
78 $this->_contributionId
= $contribution['id'];
80 $contribution = new CRM_Contribute_BAO_Contribution();
81 $contribution->id
= $this->_contributionId
;
82 $contribution->find(TRUE);
83 $this->objects
['contribution'] = $contribution;
86 public function tearDown() {
87 $this->quickCleanUpFinancialEntities();
88 CRM_Member_PseudoConstant
::membershipType(NULL, TRUE);
89 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
93 * Test the LoadObjects function with recurring membership data.
95 public function testLoadMembershipObjects() {
96 $this->_setUpMembershipObjects();
97 $this->_setUpRecurringContribution();
98 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
99 $this->assertFalse(empty($this->objects
['membership']));
100 $this->assertArrayHasKey($this->_membershipTypeID
, $this->objects
['membership']);
101 $this->assertTrue(is_a($this->objects
['membership'][$this->_membershipTypeID
], 'CRM_Member_BAO_Membership'));
102 $this->assertTrue(is_a($this->objects
['contributionType'], 'CRM_Financial_BAO_FinancialType'));
103 $this->assertFalse(empty($this->objects
['contributionRecur']));
104 $this->assertFalse(empty($this->objects
['paymentProcessor']));
108 * Test the LoadObjects function with recurring membership data.
110 public function testLoadMembershipObjectsLoadAll() {
111 $this->_setUpMembershipObjects();
112 $this->_setUpRecurringContribution();
113 unset($this->ids
['membership']);
114 $contribution = new CRM_Contribute_BAO_Contribution();
115 $contribution->id
= $this->_contributionId
;
116 $contribution->find(TRUE);
117 $contribution->loadRelatedObjects($this->input
, $this->ids
, FALSE, TRUE);
118 $this->assertFalse(empty($contribution->_relatedObjects
['membership']));
119 $this->assertArrayHasKey($this->_membershipTypeID
, $contribution->_relatedObjects
['membership']);
120 $this->assertTrue(is_a($contribution->_relatedObjects
['membership'][$this->_membershipTypeID
], 'CRM_Member_BAO_Membership'));
121 $this->assertTrue(is_a($contribution->_relatedObjects
['contributionType'], 'CRM_Financial_BAO_FinancialType'));
122 $this->assertFalse(empty($contribution->_relatedObjects
['contributionRecur']));
123 $this->assertFalse(empty($contribution->_relatedObjects
['paymentProcessor']));
127 * Test the LoadObjects function with recurring membership data.
129 public function testsendMailMembershipObjects() {
130 $this->_setUpMembershipObjects();
132 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
133 $msg = $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, TRUE);
134 $this->assertTrue(is_array($msg), "Message returned as an array in line");
135 $this->assertEquals('Mr. Anthony Anderson II', $msg['to']);
136 $this->assertContains('<p>Please print this confirmation for your records.</p>', $msg['html']);
137 $this->assertContains('Membership Type: General', $msg['body']);
141 * Test the LoadObjects function with recurring membership data.
143 public function testsendMailMembershipWithoutLoadObjects() {
144 $this->_setUpMembershipObjects();
146 $msg = $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, TRUE);
147 $this->assertTrue(is_array($msg), "Message returned as an array in line" . __LINE__
);
148 $this->assertEquals('Mr. Anthony Anderson II', $msg['to']);
149 $this->assertContains('<p>Please print this confirmation for your records.</p>', $msg['html']);
150 $this->assertContains('Membership Type: General', $msg['body']);
154 * Test that loadObjects works with participant values.
156 public function testLoadParticipantObjects() {
157 $this->_setUpParticipantObjects();
158 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
159 $this->assertFalse(empty($this->objects
['participant']));
160 $this->assertTrue(is_a($this->objects
['participant'], 'CRM_Event_BAO_Participant'));
161 $this->assertTrue(is_a($this->objects
['contributionType'], 'CRM_Financial_BAO_FinancialType'));
162 $this->assertFalse(empty($this->objects
['event']));
163 $this->assertTrue(is_a($this->objects
['event'], 'CRM_Event_BAO_Event'));
164 $this->assertTrue(is_a($this->objects
['contribution'], 'CRM_Contribute_BAO_Contribution'));
165 $this->assertFalse(empty($this->objects
['event']->id
));
169 * Test the LoadObjects function with a participant.
171 public function testComposeMailParticipant() {
172 $this->_setUpParticipantObjects();
173 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
175 $this->assertFalse(empty($this->objects
['event']));
176 $msg = $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, TRUE);
177 $this->assertContains('registration has been received and your status has been updated to Attended.', $msg['body']);
178 $this->assertContains('Annual CiviCRM meet', $msg['html']);
183 public function testComposeMailParticipantObjects() {
184 $this->_setUpParticipantObjects();
186 $msg = $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, TRUE);
187 $this->assertTrue(is_array($msg), "Message returned as an array in line" . __LINE__
);
188 $this->assertEquals('Mr. Anthony Anderson II', $msg['to']);
189 $this->assertContains('<p>Please print this confirmation for your records.</p>', $msg['html']);
190 $this->assertContains('Thank you for your participation', $msg['body']);
194 * Test the LoadObjects function with recurring membership data.
196 public function testsendMailParticipantObjectsCheckLog() {
197 $this->_setUpParticipantObjects();
199 require_once 'CiviTest/CiviMailUtils.php';
200 $mut = new CiviMailUtils($this, TRUE);
201 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
202 $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, FALSE);
203 $mut->checkMailLog(array(
204 'Thank you for your participation',
205 'Annual CiviCRM meet',
206 'Mr. Anthony Anderson II',
213 * Test the LoadObjects function with recurring membership data.
215 public function testsendMailParticipantObjectsNoMail() {
216 $this->_setUpParticipantObjects();
217 $event = new CRM_Event_BAO_Event();
218 $event->id
= $this->_eventId
;
219 $event->is_email_confirm
= FALSE;
222 $tablesToTruncate = array(
223 'civicrm_mailing_spool',
225 $this->quickCleanup($tablesToTruncate, FALSE);
226 require_once 'CiviTest/CiviMailUtils.php';
227 $mut = new CiviMailUtils($this, TRUE);
228 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
229 $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, FALSE);
230 $mut->assertMailLogEmpty('no mail should have been send as event set to no confirm');
235 * Test that loadObjects works with participant values.
237 public function testLoadPledgeObjects() {
238 $this->_setUpPledgeObjects();
239 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, $this->_processorId
);
240 $this->assertFalse(empty($this->objects
['pledge_payment'][0]));
241 $this->assertTrue(is_a($this->objects
['contributionType'], 'CRM_Financial_BAO_FinancialType'));
242 $this->assertTrue(is_a($this->objects
['contribution'], 'CRM_Contribute_BAO_Contribution'));
243 $this->assertTrue(is_a($this->objects
['pledge_payment'][0], 'CRM_Pledge_BAO_PledgePayment'));
244 $this->assertFalse(empty($this->objects
['pledge_payment'][0]->id
));
245 $this->assertEquals($this->_contributionTypeId
, $this->objects
['contributionType']->id
);
246 $this->assertEquals($this->_processorId
, $this->objects
['paymentProcessor']['id']);
247 $this->assertEquals($this->_contributionId
, $this->objects
['contribution']->id
);
248 $this->assertEquals($this->_contactId
, $this->objects
['contact']->id
);
249 $this->assertEquals($this->_pledgeId
, $this->objects
['pledge_payment'][0]->pledge_id
);
253 * Test that loadObjects works with participant values.
255 public function testLoadPledgeObjectsInvalidPledgeID() {
256 $this->_setUpPledgeObjects();
257 $this->ids
['pledge_payment'][0] = 0;
258 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
259 $this->assertArrayHasKey('error_message', $result);
260 $this->assertArrayNotHasKey('pledge_payment', $this->objects
);
261 $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']);
262 $this->ids
['pledge_payment'][0] = NULL;
263 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
264 $this->assertArrayHasKey('error_message', $result);
265 $this->assertArrayNotHasKey('pledge_payment', $this->objects
);
266 $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']);
267 $this->ids
['pledge_payment'][0] = '';
268 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
269 $this->assertArrayHasKey('error_message', $result);
270 $this->assertArrayNotHasKey('pledge_payment', $this->objects
);
271 $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']);
273 $this->ids
['pledge_payment'][0] = 999;
274 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, $this->_processorId
, array('return_error' => 1));
275 $this->assertArrayHasKey('error_message', $result);
276 $this->assertEquals('Could not find pledge payment record: 999', $result['error_message']);
280 * Test the LoadObjects function with a pledge.
282 public function testsendMailPledge() {
283 $this->_setUpPledgeObjects();
285 $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, NULL);
286 $msg = $this->IPN
->sendMail($this->input
, $this->ids
, $this->objects
, $values, FALSE, TRUE);
287 $this->assertContains('Contribution Information', $msg['html']);
291 * Test that an error is returned if required set & no contribution page
293 public function testRequiredWithoutProcessorID() {
294 $this->_setUpPledgeObjects();
296 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
297 $this->assertArrayHasKey('error_message', $result);
298 $this->assertEquals('Could not find payment processor for contribution record: 1', $result['error_message']);
299 // error is only returned if $required set to True
300 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, NULL, array('return_error' => 1));
301 $this->assertFalse(is_array($result));
302 //check that error is not returned if error checking not set
303 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('log_error' => 1));
304 $this->assertFalse(is_array($result));
309 * Test that an error is not if required set & no processor ID
311 public function testRequiredWithContributionPage() {
312 $this->_setUpContributionObjects(TRUE);
313 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
314 $this->assertFalse(is_array($result), $result['error_message']);
318 * Test that an error is returned if required set & contribution page exists
320 public function testRequiredWithContributionPageError() {
321 $this->_setUpContributionObjects();
322 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('return_error' => 1));
323 $this->assertArrayHasKey('error_message', $result);
324 $this->assertEquals('Could not find contribution page for contribution record: 1', $result['error_message']);
325 // error is only returned if $required set to True
326 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, FALSE, NULL, array('return_error' => 1));
327 $this->assertFalse(is_array($result));
328 //check that error is not returned if error checking not set
329 $result = $this->IPN
->loadObjects($this->input
, $this->ids
, $this->objects
, TRUE, NULL, array('log_error' => 1));
330 $this->assertFalse(is_array($result));
333 /* @codingStandardsIgnoreStart
334 * Test calls main functions in sequence per 'main' - I had hoped to test the functions more
335 * fully but the calls to the POST happen in more than one function
336 * keeping this as good example of data to bring back to life later
338 public function testMainFunctionActions() {
339 $ids = $objects = array( );
340 $input['component'] = 'Contribute';
341 $postedParams = array(
342 'x_response_code' => 1,
343 'x_response_reason_code' => 1,
344 'x_response_reason_text' => 'This transaction has been approved.',
346 'x_auth_code' => 140454,
347 'x_trans_id' => 4353599599,
349 'x_card_type' => 'American Express',
350 'x_account_number' => 'XXXX2701',
351 'x_first_name' => 'Arthur',
352 'x_last_name' => 'Jacobs',
354 'x_address' => '866 2166th St SN',
355 'x_city' => 'Edwardstown',
362 'x_invoice_num' => 'a9fb56c24576lk4c9490f6',
363 'x_description' => 'my desc',
364 'x_type' => 'auth_capture',
366 'x_ship_to_first_name' => null,
367 'x_ship_to_last_name' => null,
368 'x_ship_to_company' => null,
369 'x_ship_to_address' => null,
370 'x_ship_to_city' => null,
371 'x_ship_to_state' => null,
372 'x_ship_to_zip' => null,
373 'x_ship_to_country' => null,
378 'x_tax_exempt' => FALSE,
380 'x_MD5_Hash' => '069ECAD13C8E15AC205CDF92B8B58CC7',
381 'x_cvv2_resp_code' => null,
382 'x_cavv_response' => null,
383 'x_test_request' => false,
384 'description' => 'my description'
386 $this->IPN->getInput( $input, $ids );
387 $this->IPN->getIDs( $ids, $input );
389 CRM_Core_Error::debug_var( '$ids', $ids );
390 CRM_Core_Error::debug_var( '$input', $input );
392 $paymentProcessorID = CRM_Core_DAO::getFieldValue( 'CRM_Financial_DAO_PaymentProcessorType',
393 'AuthNet', 'id', 'name' );
395 if ( ! $this->IPN->validateData( $input, $ids, $objects, true, $paymentProcessorID ) ) {
399 if ( $component == 'contribute' && $ids['contributionRecur'] ) {
400 // check if first contribution is completed, else complete first contribution
402 if ( $objects['contribution']->contribution_status_id == 1 ) {
405 return $this->IPN->recur( $input, $ids, $objects, $first );
408 @codingStandardsIgnoreEnd */
411 * Prepare for contribution Test - involving only contribution objects
413 * @param bool $contributionPage
415 public function _setUpContributionObjects($contributionPage = FALSE) {
417 $contribution = new CRM_Contribute_BAO_Contribution();
418 $contribution->id
= $this->_contributionId
;
419 $contribution->find(TRUE);
420 $contributionPageID = NULL;
422 if (!empty($contributionPage)) {
423 $dao = new CRM_Core_DAO();
424 $contribution_page = $dao->createTestObject('CRM_Contribute_DAO_ContributionPage');
425 $contribution_page->payment_processor
= 1;
426 $contribution_page->save();
427 $contribution->contribution_page_id
= $contributionPageID = $contribution_page->id
;
428 //for unknown reasons trying to do a find & save on a contribution with a receive_date set
429 // doesn't work. This seems of minimal relevance to this test so ignoring
430 // note that in tests it worked sometimes & not others - dependent on which other tests run.
431 // running all CRM_Core tests caused failure as did just the single failing test. But running
432 // just this class succeeds - because it actually doesn't do a mysql update on the following save
434 unset($contribution->receive_date
);
435 $contribution->save();
438 $this->objects
['contribution'] = $contribution;
439 $this->input
= array(
440 'component' => 'contribute',
441 'contribution_page_id' => $contributionPageID,
442 'total_amount' => 110.00,
443 'invoiceID' => "c8acb91e080ad7777a2adc119c192885",
444 'contactID' => $this->_contactId
,
445 'contributionID' => $this->objects
['contribution']->id
,
450 * Prepare for membership test.
452 public function _setUpMembershipObjects() {
454 $this->_membershipTypeID
= $this->membershipTypeCreate();
455 $this->_membershipStatusID
= $this->membershipStatusCreate('test status');
457 catch (Exception
$e) {
458 echo $e->getMessage();
460 CRM_Member_PseudoConstant
::membershipType($this->_membershipTypeID
, TRUE);
461 CRM_Member_PseudoConstant
::membershipStatus(NULL, NULL, 'name', TRUE);
462 $this->_membershipParams
= array(
463 'contact_id' => $this->_contactId
,
464 'membership_type_id' => $this->_membershipTypeID
,
465 'join_date' => '2009-01-21',
466 'start_date' => '2009-01-21',
467 'end_date' => '2009-12-21',
468 'source' => 'Payment',
470 'status_id' => $this->_membershipStatusID
,
474 $membership = $this->callAPISuccess('membership', 'create', $this->_membershipParams
);
476 $this->_membershipId
= $membership['id'];
477 //we'll create membership payment here because to make setup more re-usable
478 civicrm_api('membership_payment', 'create', array(
480 'contribution_id' => $this->_contributionId
,
481 'membership_id' => $this->_membershipId
,
484 $this->input
= array(
485 'component' => 'contribute',
486 'total_amount' => 150.00,
487 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885",
488 'contactID' => $this->_contactId
,
489 'contributionID' => $this->objects
['contribution']->id
,
490 'membershipID' => $this->_membershipId
,
493 $this->ids
['membership'] = $this->_membershipId
;
496 public function _setUpRecurringContribution() {
497 $this->_contributionRecurParams
= array(
498 'contact_id' => $this->_contactId
,
501 'frequency_unit' => 'week',
502 'frequency_interval' => 1,
504 'start_date' => date('Ymd'),
505 'create_date' => date('Ymd'),
506 'invoice_id' => 'c8acb91e080ad7bd8a2adc119c192885',
507 'contribution_status_id' => 2,
509 'financial_type_id' => $this->_contributionTypeId
,
511 'payment_processor_id' => $this->_processorId
,
513 $this->_recurId
= civicrm_api('contribution_recur', 'create', $this->_contributionRecurParams
);
514 $this->assertAPISuccess($this->_recurId
, 'line ' . __LINE__
. ' set-up of recurring contrib');
515 $this->_recurId
= $this->_recurId
['id'];
516 $this->input
['contributionRecurId'] = $this->_recurId
;
517 $this->ids
['contributionRecur'] = $this->_recurId
;
521 * Set up participant requirements for test.
523 public function _setUpParticipantObjects() {
524 $event = $this->eventCreate(array('is_email_confirm' => 1));
525 $this->assertAPISuccess($event, 'line ' . __LINE__
. ' set-up of event');
526 $this->_eventId
= $event['id'];
527 $this->_participantId
= $this->participantCreate(array(
528 'event_id' => $this->_eventId
,
529 'contact_id' => $this->_contactId
,
531 //we'll create membership payment here because to make setup more re-usable
532 $participantPayment = civicrm_api('participant_payment', 'create', array(
534 'contribution_id' => $this->_contributionId
,
535 'participant_id' => $this->_participantId
,
537 $this->assertAPISuccess($participantPayment, 'line ' . __LINE__
. ' set-up of event');
538 $contribution = new CRM_Contribute_BAO_Contribution();
539 $contribution->id
= $this->_contributionId
;
540 $contribution->find();
541 $this->objects
['contribution'] = $contribution;
542 $this->input
= array(
543 'component' => 'event',
544 'total_amount' => 150.00,
545 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885",
546 'contactID' => $this->_contactId
,
547 'contributionID' => $contribution->id
,
548 'participantID' => $this->_participantId
,
551 $this->ids
['participant'] = $this->_participantId
;
552 $this->ids
['event'] = $this->_eventId
;
556 * Set up participant requirements for test.
558 public function _setUpPledgeObjects() {
559 $this->_pledgeId
= $this->pledgeCreate($this->_contactId
);
560 //we'll create membership payment here because to make setup more re-usable
561 $pledgePayment = civicrm_api('pledge_payment', 'create', array(
563 'pledge_id' => $this->_pledgeId
,
564 'contribution_id' => $this->_contributionId
,
566 'actual_amount' => 50,
568 $this->assertAPISuccess($pledgePayment, 'line ' . __LINE__
. ' set-up of pledge payment');
569 $this->input
= array(
570 'component' => 'contribute',
571 'total_amount' => 150.00,
572 'invoiceID' => "c8acb91e080ad7bd8a2adc119c192885",
573 'contactID' => $this->_contactId
,
574 'contributionID' => $this->_contributionId
,
575 'pledgeID' => $this->_pledgeId
,
578 $this->ids
['pledge_payment'][] = $pledgePayment['id'];