4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
20 namespace api\v
4\Spec
;
22 use Civi\Api4\Service\Spec\CustomFieldSpec
;
23 use Civi\Api4\Service\Spec\FieldSpec
;
24 use Civi\Api4\Service\Spec\RequestSpec
;
25 use Civi\Api4\Service\Spec\SpecFormatter
;
26 use api\v
4\UnitTestCase
;
31 class SpecFormatterTest
extends UnitTestCase
{
33 public function testSpecToArray() {
34 $spec = new RequestSpec('Contact', 'get');
35 $fieldName = 'last_name';
36 $field = new FieldSpec($fieldName, 'Contact');
37 $spec->addFieldSpec($field);
38 $arraySpec = SpecFormatter
::specToArray($spec->getFields());
40 $this->assertEquals('String', $arraySpec[$fieldName]['data_type']);
44 * @dataProvider arrayFieldSpecProvider
46 * @param array $fieldData
47 * @param string $expectedName
48 * @param string $expectedType
50 public function testArrayToField($fieldData, $expectedName, $expectedType) {
51 $field = SpecFormatter
::arrayToField($fieldData, 'TestEntity');
53 $this->assertEquals($expectedName, $field->getName());
54 $this->assertEquals($expectedType, $field->getDataType());
57 public function testCustomFieldWillBeReturned() {
58 $customGroupId = 1432;
59 $customFieldId = 3333;
60 $name = 'MyFancyField';
63 'custom_group_id' => $customGroupId,
64 'custom_group.name' => 'my_group',
65 'custom_group.title' => 'My Group',
66 'id' => $customFieldId,
69 'data_type' => 'String',
70 'html_type' => 'Select',
71 'column_name' => $name,
76 /** @var \Civi\Api4\Service\Spec\CustomFieldSpec $field */
77 $field = SpecFormatter
::arrayToField($data, 'TestEntity');
79 $this->assertInstanceOf(CustomFieldSpec
::class, $field);
80 $this->assertEquals('my_group', $field->getCustomGroupName());
81 $this->assertEquals($customFieldId, $field->getCustomFieldId());
82 $this->assertEquals(\CRM_Core_DAO
::SERIALIZE_SEPARATOR_BOOKEND
, $field->getSerialize());
83 $this->assertEquals('Select', $field->getInputType());
84 $this->assertTrue($field->getInputAttrs()['multiple']);
90 public function arrayFieldSpecProvider() {
96 'type' => \CRM_Utils_Type
::T_STRING
,
105 'type' => \CRM_Utils_Type
::T_STRING
,
106 // this should take precedence
107 'data_type' => 'Boolean',