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\Action
;
22 use api\v
4\UnitTestCase
;
23 use Civi\Api4\Campaign
;
24 use Civi\Api4\Contact
;
25 use Civi\Api4\Contribution
;
30 class GetFieldsTest
extends UnitTestCase
{
32 public function testOptionsAreReturned() {
33 $fields = Contact
::getFields(FALSE)
36 $this->assertTrue($fields['gender_id']['options']);
37 $this->assertFalse($fields['first_name']['options']);
39 $fields = Contact
::getFields(FALSE)
40 ->setLoadOptions(TRUE)
43 $this->assertTrue(is_array($fields['gender_id']['options']));
44 $this->assertFalse($fields['first_name']['options']);
47 public function testTableAndColumnReturned() {
48 $fields = Contact
::getFields(FALSE)
51 $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']);
52 $this->assertEquals('display_name', $fields['display_name']['column_name']);
55 public function testComponentFields() {
56 \CRM_Core_BAO_ConfigSetting
::disableComponent('CiviCampaign');
57 $fields = \Civi\Api4\Event
::getFields()
58 ->addWhere('name', 'CONTAINS', 'campaign')
60 $this->assertCount(0, $fields);
61 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
62 $fields = \Civi\Api4\Event
::getFields()
63 ->addWhere('name', 'CONTAINS', 'campaign')
65 $this->assertCount(1, $fields);
68 public function testInternalPropsAreHidden() {
69 // Public getFields should not contain @internal props
70 $fields = Contact
::getFields(FALSE)
72 foreach ($fields as $field) {
73 $this->assertArrayNotHasKey('output_formatters', $field);
75 // Internal entityFields should contain @internal props
76 $fields = Contact
::get(FALSE)
78 foreach ($fields as $field) {
79 $this->assertArrayHasKey('output_formatters', $field);
83 public function testPreloadFalse() {
84 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviContribute');
85 \CRM_Core_BAO_ConfigSetting
::enableComponent('CiviCampaign');
86 Campaign
::create()->setValues(['name' => 'Big Campaign', 'title' => 'Biggie'])->execute();
87 // The campaign_id field has preload = false in the schema,
88 // Which means the options will NOT load but suffixes are still available
89 $fields = Contribution
::getFields(FALSE)
90 ->setLoadOptions(['name', 'label'])
91 ->execute()->indexBy('name');
92 $this->assertFalse($fields['campaign_id']['options']);
93 $this->assertEquals(['name', 'label'], $fields['campaign_id']['suffixes']);