Merge pull request #19800 from eileenmcnaughton/gettypes
[civicrm-core.git] / tests / phpunit / api / v4 / Spec / SpecFormatterTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Spec;
21
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\v4\UnitTestCase;
27
28 /**
29 * @group headless
30 */
31 class SpecFormatterTest extends UnitTestCase {
32
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());
39
40 $this->assertEquals('String', $arraySpec[$fieldName]['data_type']);
41 }
42
43 /**
44 * @dataProvider arrayFieldSpecProvider
45 *
46 * @param array $fieldData
47 * @param string $expectedName
48 * @param string $expectedType
49 */
50 public function testArrayToField($fieldData, $expectedName, $expectedType) {
51 $field = SpecFormatter::arrayToField($fieldData, 'TestEntity');
52
53 $this->assertEquals($expectedName, $field->getName());
54 $this->assertEquals($expectedType, $field->getDataType());
55 }
56
57 public function testCustomFieldWillBeReturned() {
58 $customGroupId = 1432;
59 $customFieldId = 3333;
60 $name = 'MyFancyField';
61
62 $data = [
63 'custom_group_id' => $customGroupId,
64 'custom_group.name' => 'my_group',
65 'custom_group.title' => 'My Group',
66 'id' => $customFieldId,
67 'name' => $name,
68 'label' => $name,
69 'data_type' => 'String',
70 'html_type' => 'Select',
71 'column_name' => $name,
72 'serialize' => 1,
73 'is_view' => FALSE,
74 ];
75
76 /** @var \Civi\Api4\Service\Spec\CustomFieldSpec $field */
77 $field = SpecFormatter::arrayToField($data, 'TestEntity');
78
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']);
85 }
86
87 /**
88 * @return array
89 */
90 public function arrayFieldSpecProvider() {
91 return [
92 [
93 [
94 'name' => 'Foo',
95 'title' => 'Bar',
96 'type' => \CRM_Utils_Type::T_STRING,
97 ],
98 'Foo',
99 'String',
100 ],
101 [
102 [
103 'name' => 'MyField',
104 'title' => 'Bar',
105 'type' => \CRM_Utils_Type::T_STRING,
106 // this should take precedence
107 'data_type' => 'Boolean',
108 ],
109 'MyField',
110 'Boolean',
111 ],
112 ];
113 }
114
115 }