api4 - Import CRM/, Civi/, templates/, ang/, css/, js/, xml/menu
[civicrm-core.git] / tests / phpunit / api / v4 / Spec / SpecGathererTest.php
1 <?php
2
3 namespace api\v4\Spec;
4
5 use Civi\Api4\Service\Spec\FieldSpec;
6 use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface;
7 use Civi\Api4\Service\Spec\SpecGatherer;
8 use api\v4\Traits\OptionCleanupTrait;
9 use api\v4\UnitTestCase;
10 use Civi\Api4\CustomField;
11 use Civi\Api4\CustomGroup;
12 use api\v4\Traits\TableDropperTrait;
13 use Prophecy\Argument;
14
15 /**
16 * @group headless
17 */
18 class SpecGathererTest extends UnitTestCase {
19
20 use TableDropperTrait;
21 use OptionCleanupTrait;
22
23 public function setUpHeadless() {
24 $this->dropByPrefix('civicrm_value_favorite');
25 $this->cleanup([
26 'tablesToTruncate' => [
27 'civicrm_custom_group',
28 'civicrm_custom_field',
29 ],
30 ]);
31 return parent::setUpHeadless();
32 }
33
34 public function testBasicFieldsGathering() {
35 $gatherer = new SpecGatherer();
36 $specs = $gatherer->getSpec('Contact', 'get', FALSE);
37 $contactDAO = _civicrm_api3_get_DAO('Contact');
38 $contactFields = $contactDAO::fields();
39 $specFieldNames = $specs->getFieldNames();
40 $contactFieldNames = array_column($contactFields, 'name');
41
42 $this->assertEmpty(array_diff_key($contactFieldNames, $specFieldNames));
43 }
44
45 public function testWithSpecProvider() {
46 $gather = new SpecGatherer();
47
48 $provider = $this->prophesize(SpecProviderInterface::class);
49 $provider->applies('Contact', 'create')->willReturn(TRUE);
50 $provider->modifySpec(Argument::any())->will(function ($args) {
51 /** @var \Civi\Api4\Service\Spec\RequestSpec $spec */
52 $spec = $args[0];
53 $spec->addFieldSpec(new FieldSpec('foo', 'Contact'));
54 });
55 $gather->addSpecProvider($provider->reveal());
56
57 $spec = $gather->getSpec('Contact', 'create', FALSE);
58 $fieldNames = $spec->getFieldNames();
59
60 $this->assertContains('foo', $fieldNames);
61 }
62
63 public function testPseudoConstantOptionsWillBeAdded() {
64 $customGroupId = CustomGroup::create()
65 ->setCheckPermissions(FALSE)
66 ->addValue('name', 'FavoriteThings')
67 ->addValue('extends', 'Contact')
68 ->execute()
69 ->first()['id'];
70
71 $options = ['r' => 'Red', 'g' => 'Green', 'p' => 'Pink'];
72
73 CustomField::create()
74 ->setCheckPermissions(FALSE)
75 ->addValue('label', 'FavColor')
76 ->addValue('custom_group_id', $customGroupId)
77 ->addValue('option_values', $options)
78 ->addValue('html_type', 'Select')
79 ->addValue('data_type', 'String')
80 ->execute();
81
82 $gatherer = new SpecGatherer();
83 $spec = $gatherer->getSpec('Contact', 'get', TRUE);
84
85 $regularField = $spec->getFieldByName('contact_type');
86 $this->assertNotEmpty($regularField->getOptions());
87 $this->assertContains('Individual', $regularField->getOptions());
88
89 $customField = $spec->getFieldByName('FavoriteThings.FavColor');
90 $this->assertNotEmpty($customField->getOptions());
91 $this->assertContains('Green', $customField->getOptions());
92 $this->assertEquals('Pink', $customField->getOptions()['p']);
93 }
94
95 }