Merge pull request #21294 from eileenmcnaughton/cont_form
[civicrm-core.git] / tests / phpunit / api / v4 / Action / GetFieldsTest.php
CommitLineData
88c31c05
CW
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
20namespace api\v4\Action;
21
22use api\v4\UnitTestCase;
2bf220fd 23use Civi\Api4\Contact;
b1b7d409 24use Civi\Api4\Contribution;
88c31c05
CW
25
26/**
27 * @group headless
28 */
29class GetFieldsTest extends UnitTestCase {
30
2bf220fd
CW
31 public function testOptionsAreReturned() {
32 $fields = Contact::getFields(FALSE)
33 ->execute()
34 ->indexBy('name');
35 $this->assertTrue($fields['gender_id']['options']);
36 $this->assertFalse($fields['first_name']['options']);
37
38 $fields = Contact::getFields(FALSE)
39 ->setLoadOptions(TRUE)
40 ->execute()
41 ->indexBy('name');
42 $this->assertTrue(is_array($fields['gender_id']['options']));
43 $this->assertFalse($fields['first_name']['options']);
44 }
45
e60149f3
CW
46 public function testTableAndColumnReturned() {
47 $fields = Contact::getFields(FALSE)
48 ->execute()
49 ->indexBy('name');
50 $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']);
51 $this->assertEquals('display_name', $fields['display_name']['column_name']);
52 }
53
88c31c05
CW
54 public function testComponentFields() {
55 \CRM_Core_BAO_ConfigSetting::disableComponent('CiviCampaign');
56 $fields = \Civi\Api4\Event::getFields()
57 ->addWhere('name', 'CONTAINS', 'campaign')
58 ->execute();
59 $this->assertCount(0, $fields);
60 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
61 $fields = \Civi\Api4\Event::getFields()
62 ->addWhere('name', 'CONTAINS', 'campaign')
63 ->execute();
64 $this->assertCount(1, $fields);
65 }
66
4b3b32e5
CW
67 public function testInternalPropsAreHidden() {
68 // Public getFields should not contain @internal props
69 $fields = Contact::getFields(FALSE)
b1b7d409 70 ->execute();
4b3b32e5
CW
71 foreach ($fields as $field) {
72 $this->assertArrayNotHasKey('output_formatters', $field);
73 }
74 // Internal entityFields should contain @internal props
75 $fields = Contact::get(FALSE)
76 ->entityFields();
77 foreach ($fields as $field) {
78 $this->assertArrayHasKey('output_formatters', $field);
79 }
80 }
81
b1b7d409
CW
82 public function testPreloadFalse() {
83 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
84 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
85 // The campaign_id field has preload = false in the schema,
86 // Which means the options will NOT load but suffixes are still available
87 $fields = Contribution::getFields(FALSE)
88 ->setLoadOptions(['name', 'label'])
89 ->execute()->indexBy('name');
90 $this->assertFalse($fields['campaign_id']['options']);
91 $this->assertEquals(['name', 'label'], $fields['campaign_id']['suffixes']);
92 }
93
88c31c05 94}