Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
380f3545 TO |
3 | /* |
4 | +--------------------------------------------------------------------+ | |
7d61e75f | 5 | | Copyright CiviCRM LLC. All rights reserved. | |
380f3545 | 6 | | | |
7d61e75f TO |
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 | | |
380f3545 TO |
10 | +--------------------------------------------------------------------+ |
11 | */ | |
12 | ||
13 | /** | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
380f3545 TO |
17 | */ |
18 | ||
19 | ||
19b53e5b C |
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', | |
b751bdc9 | 65 | 'custom_group.title' => 'My Group', |
19b53e5b C |
66 | 'id' => $customFieldId, |
67 | 'name' => $name, | |
b751bdc9 | 68 | 'label' => $name, |
19b53e5b | 69 | 'data_type' => 'String', |
08a06697 | 70 | 'html_type' => 'Select', |
a689294c | 71 | 'column_name' => $name, |
08a06697 | 72 | 'serialize' => 1, |
6e18e781 | 73 | 'is_view' => FALSE, |
19b53e5b C |
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 | } |