3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 |
9 +--------------------------------------------------------------------+
14 * @package CiviCRM_APIv3
16 class api_v3_FinancialTypeTest
extends CiviUnitTestCase
{
19 * Test Create, Read, Update Financial type with custom field.
21 public function testCreateUpdateFinancialTypeCustomField() {
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',
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,
36 $financialTypeData = [
37 'Financial Type' . substr(sha1(rand()), 0, 4) => [
38 ['Test-1', 'Test-2', NULL],
41 'Financial Type' . substr(sha1(rand()), 0, 4) => [
43 ['Test_1', NULL, 'Test_3'],
46 foreach ($financialTypeData as $financialTypeName => $data) {
48 'name' => $financialTypeName,
49 'is_deductible' => '1',
54 foreach ($data[0] as $key => $value) {
55 $customFields['custom_' . $customFieldIds['custom_field_id'][$key]] = $value;
58 // create financial type with custom field
59 $financialType = $this->callAPISuccess('FinancialType', 'create', array_merge($params, $customFields));
60 $this->callAPISuccessGetSingle('FinancialType', ['name' => $financialTypeName]);
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 != '');
66 $this->callAPISuccessGetSingle('FinancialType', [
67 'id' => $financialType['id'],
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;
78 $this->callAPISuccess('FinancialType', 'create', array_merge([
79 'id' => $financialType['id'],
80 ], $updateCustomFields));
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 != '');
86 $this->callAPISuccessGetSingle('FinancialType', [
87 'id' => $financialType['id'],
89 $this->callAPISuccess('FinancialType', 'delete', ['id' => $financialType['id']]);