issue 1522
[civicrm-core.git] / tests / phpunit / api / v3 / ContributionTest.php
index c2a60c169ed0c7e17b96a14de379716b5de0aede..8a86f1233e12d0aedd96867648af5639563d826a 100644 (file)
@@ -19,6 +19,7 @@
 class api_v3_ContributionTest extends CiviUnitTestCase {
 
   use CRMTraits_Profile_ProfileTrait;
+  use CRMTraits_Custom_CustomDataTrait;
 
   protected $_individualId;
   protected $_contribution;
@@ -57,6 +58,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Setup function.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function setUp() {
     parent::setUp();
@@ -97,6 +100,7 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
       'min_amount' => 10,
       'max_amount' => 1000,
     ];
+    $this->entity = $this->_entity;
   }
 
   /**
@@ -124,6 +128,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Test Get.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testGetContribution() {
     $contributionSettings = $this->enableTaxAndInvoicing();
@@ -209,6 +215,8 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
 
   /**
    * Test that test contributions can be retrieved.
+   *
+   * @throws \CRM_Core_Exception
    */
   public function testGetTestContribution() {
     $this->callAPISuccess('Contribution', 'create', array_merge($this->_params, ['is_test' => 1]));
@@ -697,6 +705,93 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->callAPISuccessGetCount('Contribution', [], 2);
   }
 
+  /**
+   * @dataProvider createLocalizedContributionDataProvider
+   * @param $inputData
+   * @param $decimalPoint
+   * @param $thousandSeparator
+   * @param $currency
+   * @param $expectedResult
+   * @throws \Exception
+   */
+  public function testCreateLocalizedContribution($totalAmount, $decimalPoint, $thousandSeparator, $currency, $expectedResult) {
+    $this->setDefaultCurrency($currency);
+    $this->setMonetaryDecimalPoint($decimalPoint);
+    $this->setMonetaryThousandSeparator($thousandSeparator);
+
+    $_params = [
+      'contact_id' => $this->_individualId,
+      'receive_date' => '20120511',
+      'total_amount' => $totalAmount,
+      'financial_type_id' => $this->_financialTypeId,
+      'contribution_status_id' => 1,
+    ];
+
+    if ($expectedResult) {
+      $this->callAPISuccess('Contribution', 'create', $_params);
+    } else {
+      $this->callAPIFailure('Contribution', 'create', $_params);
+    }
+  }
+
+  /**
+   * @return array
+   */
+  public function createLocalizedContributionDataProvider() {
+    return [
+      [10, '.', ',', 'USD', TRUE],
+      ['145.0E+3', '.', ',', 'USD', FALSE],
+      ['10', '.', ',', 'USD', TRUE],
+      [-10, '.', ',', 'USD', TRUE],
+      ['-10', '.', ',', 'USD', TRUE],
+      ['-10foo', '.', ',', 'USD', FALSE],
+      ['-10.0345619', '.', ',', 'USD', TRUE],
+      ['-10.010,4345619', '.', ',', 'USD', TRUE],
+      ['10.0104345619', '.', ',', 'USD', TRUE],
+      ['-0', '.', ',', 'USD', TRUE],
+      ['-.1', '.', ',', 'USD', TRUE],
+      ['.1', '.', ',', 'USD', TRUE],
+      // Test currency symbols too, default locale uses $, so if we wanted to test others we'd need to reconfigure locale
+      ['$1,234,567.89', '.', ',', 'USD', TRUE],
+      ['-$1,234,567.89', '.', ',', 'USD', TRUE],
+      ['$-1,234,567.89', '.', ',', 'USD', TRUE],
+      ['1234567.89', '.', ',', 'USD', TRUE], // This is the float format. Encapsulated in strings
+      [1234567.89, '.', ',', 'USD', TRUE], // This is the float format.
+      // Test EURO currency
+      ['€1,234,567.89', '.', ',', 'EUR', TRUE],
+      ['-€1,234,567.89', '.', ',', 'EUR', TRUE],
+      ['€-1,234,567.89', '.', ',', 'EUR', TRUE],
+      ['1234567.89', '.', ',', 'EUR', TRUE], // This is the float format. Encapsulated in strings
+      [1234567.89, '.', ',', 'EUR', TRUE], // This is the float format.
+      // Test Norwegian KR currency
+      ['kr1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['kr 1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['-kr1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['-kr 1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['kr-1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['kr -1,234,567.89', '.', ',', 'NOK', TRUE],
+      ['1234567.89', '.', ',', 'NOK', TRUE], // This is the float format. Encapsulated in strings
+      [1234567.89, '.', ',', 'NOK', TRUE], // This is the float format.
+      // Test different localization options: , as decimal separator and dot as thousand separator
+      ['$1.234.567,89', ',', '.', 'USD', TRUE],
+      ['-$1.234.567,89', ',', '.', 'USD', TRUE],
+      ['$-1.234.567,89', ',', '.', 'USD', TRUE],
+      ['1.234.567,89', ',', '.', 'USD', TRUE],
+      ['1234567.89', ',', '.', 'USD', TRUE], // This is the float format. Encapsulated in strings
+      [1234567.89, ',', '.', 'USD', TRUE], // This is the float format.
+      ['$1,234,567.89', ',', '.', 'USD', FALSE],
+      ['-$1,234,567.89', ',', '.', 'USD', FALSE],
+      ['$-1,234,567.89', ',', '.', 'USD', FALSE],
+      // Now with a space as thousand separator
+      ['$1 234 567,89', ',', ' ', 'USD', TRUE],
+      ['-$1 234 567,89', ',', ' ', 'USD', TRUE],
+      ['$-1 234 567,89', ',', ' ', 'USD', TRUE],
+      ['1 234 567,89', ',', ' ', 'USD', TRUE],
+      ['1234567.89', ',', ' ', 'USD', TRUE], // This is the float format. Encapsulated in strings
+      [1234567.89, ',', ' ', 'USD', TRUE], // This is the float format.
+    ];
+  }
+
   /**
    * Create test with unique field name on source.
    */
@@ -4393,4 +4488,30 @@ class api_v3_ContributionTest extends CiviUnitTestCase {
     $this->assertNotContains('US Dollar', $result['values']);
   }
 
+  public function testSetCustomDataInCreateAndHook() {
+    $this->createCustomGroupWithFieldsOfAllTypes();
+    $this->hookClass->setHook('civicrm_post', [
+      $this,
+      'civicrmPostContributionCustom',
+    ]);
+    $params = $this->_params;
+    $params['custom_' . $this->ids['CustomField']['text']] = 'Some Text';
+    $contribution = $this->callAPISuccess('Contribution', 'create', $params);
+    $getContribution = $this->callAPISuccess('Contribution', 'get', [
+      'id' => $contribution['id'],
+      'return' => ['id', 'custom_' . $this->ids['CustomField']['text'], 'custom_' . $this->ids['CustomField']['int']],
+    ]);
+    $this->assertEquals(5, $getContribution['values'][$contribution['id']]['custom_' . $this->ids['CustomField']['int']]);
+    $this->assertEquals('Some Text', $getContribution['values'][$contribution['id']]['custom_' . $this->ids['CustomField']['text']]);
+  }
+
+  public function civicrmPostContributionCustom($op, $objectName, $objectId, &$objectRef) {
+    if ($objectName === 'Contribution' && $op === 'create') {
+      $this->callAPISuccess('Contribution', 'create', [
+        'id' => $objectId,
+        'custom_' . $this->ids['CustomField']['int'] => 5,
+      ]);
+    }
+  }
+
 }