class api_v3_ContributionTest extends CiviUnitTestCase {
use CRMTraits_Profile_ProfileTrait;
+ use CRMTraits_Custom_CustomDataTrait;
protected $_individualId;
protected $_contribution;
/**
* Setup function.
+ *
+ * @throws \CRM_Core_Exception
*/
public function setUp() {
parent::setUp();
'min_amount' => 10,
'max_amount' => 1000,
];
+ $this->entity = $this->_entity;
}
/**
/**
* Test Get.
+ *
+ * @throws \CRM_Core_Exception
*/
public function testGetContribution() {
$contributionSettings = $this->enableTaxAndInvoicing();
/**
* 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]));
$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.
*/
$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,
+ ]);
+ }
+ }
+
}