Merge pull request #17294 from agh1/sr-rel-perms
[civicrm-core.git] / tests / phpunit / api / v3 / FinancialTypeTest.php
CommitLineData
5c2a4a7b
PN
1<?php
2/*
3 +--------------------------------------------------------------------+
7d61e75f 4 | Copyright CiviCRM LLC. All rights reserved. |
5c2a4a7b 5 | |
7d61e75f
TO
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
5c2a4a7b
PN
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CiviCRM_APIv3
15 */
16class api_v3_FinancialTypeTest extends CiviUnitTestCase {
17
18 /**
19 * Test Create, Read, Update Financial type with custom field.
20 */
21 public function testCreateUpdateFinancialTypeCustomField() {
bbfa037b
PN
22 $this->callAPISuccess('OptionValue', 'create', [
23 'label' => ts('Financial Type'),
24 'name' => 'civicrm_financial_type',
25 'value' => 'FinancialType',
26 'option_group_id' => 'cg_extend_objects',
27 'is_active' => 1,
28 ]);
5c2a4a7b
PN
29 // create custom group and custom field
30 $customFieldIds = $this->CustomGroupMultipleCreateWithFields([
31 'name' => 'Test_Group_Financial_type',
32 'title' => 'Test_Group_Financial_type',
33 'extends' => 'FinancialType',
34 'is_multiple' => FALSE,
35 ]);
36 $financialTypeData = [
37 'Financial Type' . substr(sha1(rand()), 0, 4) => [
38 ['Test-1', 'Test-2', NULL],
39 [NULL, '', 'Test_3'],
40 ],
41 'Financial Type' . substr(sha1(rand()), 0, 4) => [
42 [NULL, NULL, NULL],
43 ['Test_1', NULL, 'Test_3'],
44 ],
45 ];
46 foreach ($financialTypeData as $financialTypeName => $data) {
47 $params = [
48 'name' => $financialTypeName,
49 'is_deductible' => '1',
50 'is_reserved' => '0',
51 'is_active' => '1',
52 ];
53 $customFields = [];
54 foreach ($data[0] as $key => $value) {
55 $customFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
56 }
57
58 // create financial type with custom field
59 $financialType = $this->callAPISuccess('FinancialType', 'create', array_merge($params, $customFields));
906c7498 60 $this->callAPISuccessGetSingle('FinancialType', ['name' => $financialTypeName]);
5c2a4a7b
PN
61
62 // get financial type to check custom field value
63 $expectedResult = array_filter(array_merge($params, $customFields), function($var) {
64 return (!is_null($var) && $var != '');
65 });
906c7498 66 $this->callAPISuccessGetSingle('FinancialType', [
5c2a4a7b
PN
67 'id' => $financialType['id'],
68 ], $expectedResult);
69
70 // updated financial type with custom field
71 $updateCustomFields = [];
72 foreach ($data[1] as $key => $value) {
73 $updateCustomFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
74 if (!is_null($value)) {
75 $customFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
76 }
77 }
78 $this->callAPISuccess('FinancialType', 'create', array_merge([
79 'id' => $financialType['id'],
80 ], $updateCustomFields));
81
82 // get financial type to check custom field value
83 $expectedResult = array_filter(array_merge($params, $customFields), function($var) {
84 return (!is_null($var) && $var != '');
85 });
906c7498 86 $this->callAPISuccessGetSingle('FinancialType', [
5c2a4a7b
PN
87 'id' => $financialType['id'],
88 ], $expectedResult);
06a2050e 89 $this->callAPISuccess('FinancialType', 'delete', ['id' => $financialType['id']]);
5c2a4a7b
PN
90 }
91 }
92
93}