APIv4 - Reorganize test classes, don't use transactions for custom value tests
[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
46f571dd 22use api\v4\Api4TestBase;
f8d39baf 23use Civi\Api4\Activity;
3d9604f0 24use Civi\Api4\Campaign;
2bf220fd 25use Civi\Api4\Contact;
b1b7d409 26use Civi\Api4\Contribution;
46f571dd 27use Civi\Test\TransactionalInterface;
88c31c05
CW
28
29/**
30 * @group headless
31 */
46f571dd 32class GetFieldsTest extends Api4TestBase implements TransactionalInterface {
88c31c05 33
2bf220fd
CW
34 public function testOptionsAreReturned() {
35 $fields = Contact::getFields(FALSE)
36 ->execute()
37 ->indexBy('name');
38 $this->assertTrue($fields['gender_id']['options']);
39 $this->assertFalse($fields['first_name']['options']);
40
41 $fields = Contact::getFields(FALSE)
42 ->setLoadOptions(TRUE)
43 ->execute()
44 ->indexBy('name');
45 $this->assertTrue(is_array($fields['gender_id']['options']));
46 $this->assertFalse($fields['first_name']['options']);
47 }
48
e60149f3
CW
49 public function testTableAndColumnReturned() {
50 $fields = Contact::getFields(FALSE)
51 ->execute()
52 ->indexBy('name');
53 $this->assertEquals('civicrm_contact', $fields['display_name']['table_name']);
54 $this->assertEquals('display_name', $fields['display_name']['column_name']);
55 }
56
88c31c05
CW
57 public function testComponentFields() {
58 \CRM_Core_BAO_ConfigSetting::disableComponent('CiviCampaign');
59 $fields = \Civi\Api4\Event::getFields()
60 ->addWhere('name', 'CONTAINS', 'campaign')
61 ->execute();
62 $this->assertCount(0, $fields);
63 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
64 $fields = \Civi\Api4\Event::getFields()
65 ->addWhere('name', 'CONTAINS', 'campaign')
66 ->execute();
67 $this->assertCount(1, $fields);
68 }
69
4b3b32e5
CW
70 public function testInternalPropsAreHidden() {
71 // Public getFields should not contain @internal props
72 $fields = Contact::getFields(FALSE)
b1b7d409 73 ->execute();
4b3b32e5
CW
74 foreach ($fields as $field) {
75 $this->assertArrayNotHasKey('output_formatters', $field);
76 }
77 // Internal entityFields should contain @internal props
78 $fields = Contact::get(FALSE)
79 ->entityFields();
80 foreach ($fields as $field) {
81 $this->assertArrayHasKey('output_formatters', $field);
82 }
83 }
84
b1b7d409
CW
85 public function testPreloadFalse() {
86 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviContribute');
87 \CRM_Core_BAO_ConfigSetting::enableComponent('CiviCampaign');
3d9604f0 88 Campaign::create()->setValues(['name' => 'Big Campaign', 'title' => 'Biggie'])->execute();
b1b7d409
CW
89 // The campaign_id field has preload = false in the schema,
90 // Which means the options will NOT load but suffixes are still available
91 $fields = Contribution::getFields(FALSE)
92 ->setLoadOptions(['name', 'label'])
93 ->execute()->indexBy('name');
94 $this->assertFalse($fields['campaign_id']['options']);
95 $this->assertEquals(['name', 'label'], $fields['campaign_id']['suffixes']);
96 }
97
f8d39baf
CW
98 public function testRequiredAndNullable() {
99 $actFields = Activity::getFields(FALSE)
100 ->setAction('create')
101 ->execute()->indexBy('name');
102
103 $this->assertTrue($actFields['activity_type_id']['required']);
104 $this->assertFalse($actFields['activity_type_id']['nullable']);
105 $this->assertFalse($actFields['subject']['required']);
106 $this->assertTrue($actFields['subject']['nullable']);
107 }
108
c9ae8a39
CW
109 public function testGetSuffixes() {
110 $actFields = Activity::getFields(FALSE)
111 ->execute()->indexBy('name');
112
113 $this->assertEquals(['name', 'label', 'description'], $actFields['engagement_level']['suffixes']);
114 $this->assertEquals(['name', 'label', 'description', 'icon'], $actFields['activity_type_id']['suffixes']);
115 $this->assertEquals(['name', 'label', 'description', 'color'], $actFields['status_id']['suffixes']);
116 $this->assertEquals(['name', 'label', 'description', 'color'], $actFields['tags']['suffixes']);
117 }
118
88c31c05 119}