Fix for new prefetch key
[civicrm-core.git] / tests / phpunit / api / v4 / Action / GetFieldsTest.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 */
18
19
20 namespace api\v4\Action;
21
22 use api\v4\UnitTestCase;
23 use Civi\Api4\Campaign;
24 use Civi\Api4\Contact;
25 use Civi\Api4\Contribution;
26
27 /**
28 * @group headless
29 */
30 class GetFieldsTest extends UnitTestCase {
31
32 public function testOptionsAreReturned() {
33 $fields = Contact::getFields(FALSE)
34 ->execute()
35 ->indexBy('name');
36 $this->assertTrue($fields['gender_id']['options']);
37 $this->assertFalse($fields['first_name']['options']);
38
39 $fields = Contact::getFields(FALSE)
40 ->setLoadOptions(TRUE)
41 ->execute()
42 ->indexBy('name');
43 $this->assertTrue(is_array($fields['gender_id']['options']));
44 $this->assertFalse($fields['first_name']['options']);
45 }
46
47 public function testTableAndColumnReturned() {
48 $fields = Contact::getFields(FALSE)
49 ->execute()
50 ->indexBy('name');
51 $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']);
52 $this->assertEquals('display_name', $fields['display_name']['column_name']);
53 }
54
55 public function testComponentFields() {
56 \CRM_Core_BAO_ConfigSetting::disableComponent('CiviCampaign');
57 $fields = \Civi\Api4\Event::getFields()
58 ->addWhere('name', 'CONTAINS', 'campaign')
59 ->execute();
60 $this->assertCount(0, $fields);
61 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
62 $fields = \Civi\Api4\Event::getFields()
63 ->addWhere('name', 'CONTAINS', 'campaign')
64 ->execute();
65 $this->assertCount(1, $fields);
66 }
67
68 public function testInternalPropsAreHidden() {
69 // Public getFields should not contain @internal props
70 $fields = Contact::getFields(FALSE)
71 ->execute();
72 foreach ($fields as $field) {
73 $this->assertArrayNotHasKey('output_formatters', $field);
74 }
75 // Internal entityFields should contain @internal props
76 $fields = Contact::get(FALSE)
77 ->entityFields();
78 foreach ($fields as $field) {
79 $this->assertArrayHasKey('output_formatters', $field);
80 }
81 }
82
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']);
94 }
95
96 }