Merge pull request #2744 from pratik-joshi/CRM-13992_temp_fix
[civicrm-core.git] / tests / phpunit / CRM / Core / Payment / PayPalProIPNTest.php
index 29019146a8255ed4b733d0b0ec06246b859871f2..918636b84f9a42c6c213d5c9683930ae4d9473fe 100644 (file)
@@ -1,9 +1,9 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.3                                                |
+ | CiviCRM version 4.5                                                |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2013                                |
+ | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -26,7 +26,7 @@
 */
 
 require_once 'CiviTest/CiviUnitTestCase.php';
-class CRM_Core_Payment_PaypalProIPNTest extends CiviUnitTestCase {
+class CRM_Core_Payment_PayPalProIPNTest extends CiviUnitTestCase {
   protected $_contributionID;
   protected $_invoiceID = 'c2r9c15f7be20b4f3fef1f77e4c37424';
   protected $_financialTypeID = 1;
@@ -65,23 +65,11 @@ class CRM_Core_Payment_PaypalProIPNTest extends CiviUnitTestCase {
   }
 
   function tearDown() {
-  // $this->paymentProcessor->delete($this->processorParams->id);
-    $tablesToTruncate = array(
-      'civicrm_contribution',
-      'civicrm_financial_trxn',
-      'civicrm_contribution_recur',
-      'civicrm_line_item',
-      'civicrm_contribution_page',
-      'civicrm_payment_processor',
-      'civicrm_entity_financial_trxn',
-    );
-    $this->quickCleanup($tablesToTruncate);
+    $this->quickCleanUpFinancialEntities();
   }
 
   /**
    * test IPN response updates contribution_recur & contribution for first & second contribution
-   * @todo I was unable to get this to finish without a fatal error (trying to insert the same line items twice)
-   * If that is fixed then the try catch should be removed & tests added to check that
    */
   function testIPNPaymentRecurSuccess() {
     $this->setupPaymentProcessorTransaction();
@@ -94,18 +82,40 @@ class CRM_Core_Payment_PaypalProIPNTest extends CiviUnitTestCase {
     $this->assertTrue(substr($contribution['contribution_source'], 0, 20) == "Online Contribution:");
     $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $this->_contributionRecurID));
     $this->assertEquals(5, $contributionRecur['contribution_status_id']);
-    try{
-      $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction());
-      $paypalIPN->main();
-    }
-    catch(Exception $e) {
-      // expected failure as code currently appears to fail when the line items are created for the second time
-    }
+    $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction());
+    $paypalIPN->main();
     $contribution = $this->callAPISuccess('contribution', 'get', array('contribution_recur_id' => $this->_contributionRecurID, 'sequential' => 1));
     $this->assertEquals(2, $contribution['count']);
     $this->assertEquals('secondone', $contribution['values'][1]['trxn_id']);
   }
 
+  /**
+   * CRM-13743 test IPN edge case where the first transaction fails and the second succeeds
+   * We are checking that the created contribution has the same date as IPN says it should
+   * Note that only one contribution will be created (no evidence of the failed contribution is left)
+   * It seems likely that may change in future & this test will start failing (I point this out in the hope it
+   * will help future debuggers)
+   */
+  function testIPNPaymentCRM13743() {
+    $this->setupPaymentProcessorTransaction();
+    $firstPaymentParams = $this->getPaypalProRecurTransaction();
+    $firstPaymentParams['txn_type'] = 'recurring_payment_failed';
+    $paypalIPN = new CRM_Core_Payment_PayPalProIPN($firstPaymentParams);
+    $paypalIPN->main();
+
+    $contribution = $this->callAPISuccess('contribution', 'getsingle', array('id' => $this->_contributionID));
+    $this->assertEquals(2, $contribution['contribution_status_id']);
+    $this->assertEquals('', $contribution['trxn_id']);
+    $contributionRecur = $this->callAPISuccess('contribution_recur', 'getsingle', array('id' => $this->_contributionRecurID));
+    $this->assertEquals(2, $contributionRecur['contribution_status_id']);
+    $paypalIPN = new CRM_Core_Payment_PayPalProIPN($this->getPaypalProRecurSubsequentTransaction());
+    $paypalIPN->main();
+    $contribution = $this->callAPISuccess('contribution', 'get', array('contribution_recur_id' => $this->_contributionRecurID, 'sequential' => 1));
+    $this->assertEquals(1, $contribution['count']);
+    $this->assertEquals('secondone', $contribution['values'][0]['trxn_id']);
+    $this->assertEquals(strtotime('03:59:05 Jul 14, 2013 PDT'), strtotime($contribution['values'][0]['receive_date']));
+  }
+
   /**
    * check a payment express IPN call does not throw any errors
    * At this stage nothing it supposed to happen so it's a pretty blunt test