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\FieldSpec; | |
23 | use Civi\Api4\Service\Spec\Provider\Generic\SpecProviderInterface; | |
24 | use Civi\Api4\Service\Spec\SpecGatherer; | |
25 | use api\v4\Traits\OptionCleanupTrait; | |
26 | use api\v4\UnitTestCase; | |
27 | use Civi\Api4\CustomField; | |
28 | use Civi\Api4\CustomGroup; | |
29 | use api\v4\Traits\TableDropperTrait; | |
30 | use Prophecy\Argument; | |
31 | ||
32 | /** | |
33 | * @group headless | |
34 | */ | |
35 | class SpecGathererTest extends UnitTestCase { | |
36 | ||
37 | use TableDropperTrait; | |
38 | use OptionCleanupTrait; | |
39 | ||
40 | public function setUpHeadless() { | |
41 | $this->dropByPrefix('civicrm_value_favorite'); | |
42 | $this->cleanup([ | |
43 | 'tablesToTruncate' => [ | |
44 | 'civicrm_custom_group', | |
45 | 'civicrm_custom_field', | |
46 | ], | |
47 | ]); | |
48 | return parent::setUpHeadless(); | |
49 | } | |
50 | ||
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'); | |
58 | ||
59 | $this->assertEmpty(array_diff_key($contactFieldNames, $specFieldNames)); | |
60 | } | |
61 | ||
62 | public function testWithSpecProvider() { | |
63 | $gather = new SpecGatherer(); | |
64 | ||
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 */ | |
69 | $spec = $args[0]; | |
70 | $spec->addFieldSpec(new FieldSpec('foo', 'Contact')); | |
71 | }); | |
72 | $gather->addSpecProvider($provider->reveal()); | |
73 | ||
74 | $spec = $gather->getSpec('Contact', 'create', FALSE); | |
75 | $fieldNames = $spec->getFieldNames(); | |
76 | ||
77 | $this->assertContains('foo', $fieldNames); | |
78 | } | |
79 | ||
80 | public function testPseudoConstantOptionsWillBeAdded() { | |
fe806431 | 81 | $customGroupId = CustomGroup::create(FALSE) |
19b53e5b C |
82 | ->addValue('name', 'FavoriteThings') |
83 | ->addValue('extends', 'Contact') | |
84 | ->execute() | |
85 | ->first()['id']; | |
86 | ||
87 | $options = ['r' => 'Red', 'g' => 'Green', 'p' => 'Pink']; | |
88 | ||
fe806431 | 89 | CustomField::create(FALSE) |
19b53e5b C |
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') | |
95 | ->execute(); | |
96 | ||
97 | $gatherer = new SpecGatherer(); | |
98 | $spec = $gatherer->getSpec('Contact', 'get', TRUE); | |
99 | ||
100 | $regularField = $spec->getFieldByName('contact_type'); | |
101 | $this->assertNotEmpty($regularField->getOptions()); | |
bb6bfd68 | 102 | $this->assertContains('Individual', array_column($regularField->getOptions([], ['id', 'label']), 'label', 'id')); |
19b53e5b C |
103 | |
104 | $customField = $spec->getFieldByName('FavoriteThings.FavColor'); | |
bb6bfd68 CW |
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']); | |
19b53e5b C |
108 | } |
109 | ||
110 | } |