Autoformat /tests directory with php short array syntax
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Task / InvoiceTest.php
index 9db2dc8d41d7c82e046becc791e0b5dd63dd953a..dedc0a997cbbd51dc76cbcd9ab3fb88252588058 100644 (file)
@@ -3,7 +3,7 @@
  +--------------------------------------------------------------------+
  | CiviCRM version 5                                                  |
  +--------------------------------------------------------------------+
- | Copyright CiviCRM LLC (c) 2004-2018                                |
+ | Copyright CiviCRM LLC (c) 2004-2019                                |
  +--------------------------------------------------------------------+
  | This file is a part of CiviCRM.                                    |
  |                                                                    |
@@ -33,9 +33,7 @@
  * @group headless
  */
 class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
-  /**
-   * Assume empty database with just civicrm_data.
-   */
+
   protected $_individualId;
 
   /**
@@ -51,18 +49,18 @@ class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
    * invoice pdf for pending and completed contributions
    */
   public function testInvoiceForDueDate() {
-    $contactIds = array();
-    $params = array(
+    $contactIds = [];
+    $params = [
       'output' => 'pdf_invoice',
       'forPage' => 1,
-    );
+    ];
 
     $this->_individualId = $this->individualCreate();
-    $contributionParams = array(
+    $contributionParams = [
       'contact_id' => $this->_individualId,
       'total_amount' => 100,
       'financial_type_id' => 'Donation',
-    );
+    ];
     $result = $this->callAPISuccess('Contribution', 'create', $contributionParams);
 
     $contributionParams['contribution_status_id'] = 2;
@@ -70,15 +68,15 @@ class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
     $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
 
     $contribution3 = $this->callAPISuccess('Contribution', 'create', $contributionParams);
-    $this->callAPISuccess('Payment', 'create', array('total_amount' => 8, 'contribution_id' => $contribution3['id']));
+    $this->callAPISuccess('Payment', 'create', ['total_amount' => 8, 'contribution_id' => $contribution3['id']]);
 
-    $this->callAPISuccess('Contribution', 'create', array('id' => $contribution3['id'], 'is_pay_later' => 0));
+    $this->callAPISuccess('Contribution', 'create', ['id' => $contribution3['id'], 'is_pay_later' => 0]);
 
-    $contributionIDs = array(
-      array($result['id']),
-      array($contribution['id']),
-      array($contribution3['id']),
-    );
+    $contributionIDs = [
+      [$result['id']],
+      [$contribution['id']],
+      [$contribution3['id']],
+    ];
 
     $contactIds[] = $this->_individualId;
     foreach ($contributionIDs as $contributionID) {
@@ -96,4 +94,86 @@ class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
 
   }
 
+  /**
+   * PR 13477 - Fix incorrect display of Line Items created via API
+   * when printing invoice (for Participants).
+   */
+  public function testInvoiceForLineItems() {
+
+    $this->enableTaxAndInvoicing();
+
+    $event = $this->eventCreatePaid([]);
+
+    $individualOneId = $this->individualCreate();
+    $individualTwoId = $this->individualCreate();
+    $contactIds = [$individualOneId, $individualTwoId];
+
+    $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $event['id']);
+    $priceField = $this->callAPISuccess('PriceField', 'get', ['price_set_id' => $priceSetId]);
+    $priceFieldValues = $this->callAPISuccess('PriceFieldValue', 'get', [
+      'sequential' => 1,
+      'price_field_id' => $priceField['id'],
+    ]);
+
+    $lineItemParams = [];
+    foreach ($priceFieldValues['values'] as $key => $priceFieldValue) {
+      $lineItemParams[] = [
+        'line_item' => [
+          $priceFieldValue['id'] => [
+            'price_field_id' => $priceField['id'],
+            'label' => $priceFieldValue['label'],
+            'financial_type_id' => $priceFieldValue['financial_type_id'],
+            'price_field_value_id' => $priceFieldValue['id'],
+            'qty' => 1,
+            'field_title' => $priceFieldValue['label'],
+            'unit_price' => $priceFieldValue['amount'],
+            'line_total' => $priceFieldValue['amount'],
+            'entity_table' => 'civicrm_participant',
+          ],
+        ],
+        // participant params
+        'params' => [
+          'contact_id' => $contactIds[$key],
+          'event_id' => $event['id'],
+          'status_id' => 1,
+          'price_set_id' => $priceSetId,
+          'participant_fee_amount' => $priceFieldValue['amount'],
+          'participant_fee_level' => $priceFieldValue['label'],
+        ],
+      ];
+    }
+
+    $orderParams = [
+      'contact_id' => $individualOneId,
+      'total_amount' => array_reduce($priceFieldValues['values'], function($total, $priceFieldValue) {
+        $total += $priceFieldValue['amount'];
+        return $total;
+      }),
+      'financial_type_id' => $priceFieldValues['values'][0]['financial_type_id'],
+      'contribution_status_id' => 'Completed',
+      'currency' => 'USD',
+      'line_items' => $lineItemParams,
+    ];
+
+    $order = $this->callAPISuccess('Order', 'create', $orderParams);
+
+    $pdfParams = [
+      'output' => 'pdf_invoice',
+      'forPage' => 1,
+    ];
+
+    $invoiceHTML = CRM_Contribute_Form_Task_Invoice::printPDF([$order['id']], $pdfParams, [$individualOneId]);
+
+    $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $order['id']]);
+
+    foreach ($lineItems['values'] as $lineItem) {
+      $this->assertContains("<font size = \"1\">$ {$lineItem['line_total']}</font>", $invoiceHTML);
+    }
+
+    $totalAmount = $this->formatMoneyInput($order['values'][$order['id']]['total_amount']);
+    $this->assertContains("TOTAL USD</font></b></td>
+                <td style = \"padding-left:34px;text-align:right;\"><font size = \"1\">$ $totalAmount</font>", $invoiceHTML);
+
+  }
+
 }