Merge pull request #12337 from lcdservices/dev-core-190
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeTest.php
CommitLineData
5c2a4a7b
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
5c2a4a7b
PN
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28/**
29 *
30 * @package CiviCRM_APIv3
31 */
32class api_v3_FinancialTypeTest extends CiviUnitTestCase {
33
34 /**
35 * Test Create, Read, Update Financial type with custom field.
36 */
37 public function testCreateUpdateFinancialTypeCustomField() {
bbfa037b
PN
38 $this->callAPISuccess('OptionValue', 'create', [
39 'label' => ts('Financial Type'),
40 'name' => 'civicrm_financial_type',
41 'value' => 'FinancialType',
42 'option_group_id' => 'cg_extend_objects',
43 'is_active' => 1,
44 ]);
5c2a4a7b
PN
45 // create custom group and custom field
46 $customFieldIds = $this->CustomGroupMultipleCreateWithFields([
47 'name' => 'Test_Group_Financial_type',
48 'title' => 'Test_Group_Financial_type',
49 'extends' => 'FinancialType',
50 'is_multiple' => FALSE,
51 ]);
52 $financialTypeData = [
53 'Financial Type' . substr(sha1(rand()), 0, 4) => [
54 ['Test-1', 'Test-2', NULL],
55 [NULL, '', 'Test_3'],
56 ],
57 'Financial Type' . substr(sha1(rand()), 0, 4) => [
58 [NULL, NULL, NULL],
59 ['Test_1', NULL, 'Test_3'],
60 ],
61 ];
62 foreach ($financialTypeData as $financialTypeName => $data) {
63 $params = [
64 'name' => $financialTypeName,
65 'is_deductible' => '1',
66 'is_reserved' => '0',
67 'is_active' => '1',
68 ];
69 $customFields = [];
70 foreach ($data[0] as $key => $value) {
71 $customFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
72 }
73
74 // create financial type with custom field
75 $financialType = $this->callAPISuccess('FinancialType', 'create', array_merge($params, $customFields));
906c7498 76 $this->callAPISuccessGetSingle('FinancialType', ['name' => $financialTypeName]);
5c2a4a7b
PN
77
78 // get financial type to check custom field value
79 $expectedResult = array_filter(array_merge($params, $customFields), function($var) {
80 return (!is_null($var) && $var != '');
81 });
906c7498 82 $this->callAPISuccessGetSingle('FinancialType', [
5c2a4a7b
PN
83 'id' => $financialType['id'],
84 ], $expectedResult);
85
86 // updated financial type with custom field
87 $updateCustomFields = [];
88 foreach ($data[1] as $key => $value) {
89 $updateCustomFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
90 if (!is_null($value)) {
91 $customFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
92 }
93 }
94 $this->callAPISuccess('FinancialType', 'create', array_merge([
95 'id' => $financialType['id'],
96 ], $updateCustomFields));
97
98 // get financial type to check custom field value
99 $expectedResult = array_filter(array_merge($params, $customFields), function($var) {
100 return (!is_null($var) && $var != '');
101 });
906c7498 102 $this->callAPISuccessGetSingle('FinancialType', [
5c2a4a7b
PN
103 'id' => $financialType['id'],
104 ], $expectedResult);
105 }
106 }
107
108}