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\FieldSpec
;
23 use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface
;
24 use Civi\Api4\Service\Spec\SpecGatherer
;
25 use api\v
4\Traits\OptionCleanupTrait
;
26 use api\v
4\UnitTestCase
;
27 use Civi\Api4\CustomField
;
28 use Civi\Api4\CustomGroup
;
29 use api\v
4\Traits\TableDropperTrait
;
30 use Prophecy\Argument
;
35 class SpecGathererTest
extends UnitTestCase
{
37 use TableDropperTrait
;
38 use OptionCleanupTrait
;
40 public function setUpHeadless() {
41 $this->dropByPrefix('civicrm_value_favorite');
43 'tablesToTruncate' => [
44 'civicrm_custom_group',
45 'civicrm_custom_field',
48 return parent
::setUpHeadless();
51 public function testBasicFieldsGathering() {
52 $gatherer = new SpecGatherer();
53 $specs = $gatherer->getSpec('Contact', 'get', FALSE);
54 $contactDAO = _civicrm_api3_get_DAO('Contact');
55 $contactFields = $contactDAO::fields();
56 $specFieldNames = $specs->getFieldNames();
57 $contactFieldNames = array_column($contactFields, 'name');
59 $this->assertEmpty(array_diff_key($contactFieldNames, $specFieldNames));
62 public function testWithSpecProvider() {
63 $gather = new SpecGatherer();
65 $provider = $this->prophesize(SpecProviderInterface
::class);
66 $provider->applies('Contact', 'create')->willReturn(TRUE);
67 $provider->modifySpec(Argument
::any())->will(function ($args) {
68 /** @var \Civi\Api4\Service\Spec\RequestSpec $spec */
70 $spec->addFieldSpec(new FieldSpec('foo', 'Contact'));
72 $gather->addSpecProvider($provider->reveal());
74 $spec = $gather->getSpec('Contact', 'create', FALSE);
75 $fieldNames = $spec->getFieldNames();
77 $this->assertContains('foo', $fieldNames);
80 public function testPseudoConstantOptionsWillBeAdded() {
81 $customGroupId = CustomGroup
::create(FALSE)
82 ->addValue('name', 'FavoriteThings')
83 ->addValue('extends', 'Contact')
87 $options = ['r' => 'Red', 'g' => 'Green', 'p' => 'Pink'];
89 CustomField
::create(FALSE)
90 ->addValue('label', 'FavColor')
91 ->addValue('custom_group_id', $customGroupId)
92 ->addValue('option_values', $options)
93 ->addValue('html_type', 'Select')
94 ->addValue('data_type', 'String')
97 $gatherer = new SpecGatherer();
98 $spec = $gatherer->getSpec('Contact', 'get', TRUE);
100 $regularField = $spec->getFieldByName('contact_type');
101 $this->assertNotEmpty($regularField->getOptions());
102 $this->assertContains('Individual', array_column($regularField->getOptions([], ['id', 'label']), 'label', 'id'));
104 $customField = $spec->getFieldByName('FavoriteThings.FavColor');
105 $options = array_column($customField->getOptions([], ['id', 'name', 'label']), NULL, 'id');
106 $this->assertEquals('Green', $options['g']['name']);
107 $this->assertEquals('Pink', $options['p']['label']);