Merge pull request #23101 from MegaphoneJon/core-1836-js
[civicrm-core.git] / tests / phpunit / api / v4 / Entity / SettingTest.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\Entity;
21
22 use Civi\Api4\Setting;
23 use api\v4\UnitTestCase;
24
25 /**
26 * @group headless
27 */
28 class SettingTest extends UnitTestCase {
29
30 public function testSettingASetting() {
31 $setting = Setting::set()->addValue('menubar_position', 'above-crm-container')->setCheckPermissions(FALSE)->execute()->first();
32 $this->assertEquals('above-crm-container', $setting['value']);
33 $setting = Setting::get()->addSelect('menubar_position')->setCheckPermissions(FALSE)->execute()->first();
34 $this->assertEquals('above-crm-container', $setting['value']);
35
36 $setting = Setting::revert()->addSelect('menubar_position')->setCheckPermissions(FALSE)->execute()->indexBy('name')->column('value');
37 $this->assertEquals(['menubar_position' => 'over-cms-menu'], $setting);
38 $setting = civicrm_api4('Setting', 'get', ['select' => ['menubar_position'], 'checkPermissions' => FALSE], 0);
39 $this->assertEquals('over-cms-menu', $setting['value']);
40 }
41
42 public function testInvalidSetting() {
43 $message = '';
44 try {
45 Setting::set()->addValue('not_a_real_setting!', 'hello')->setCheckPermissions(FALSE)->execute();
46 }
47 catch (\API_Exception $e) {
48 $message = $e->getMessage();
49 }
50 $this->assertStringContainsString('setting', $message);
51 }
52
53 public function testSerailizedSetting() {
54 $set = \Civi\Api4\Setting::set(FALSE)
55 ->addValue('contact_edit_options:name', [
56 'CommunicationPreferences',
57 'CustomData',
58 ])
59 ->execute();
60
61 $setting = \Civi\Api4\Setting::get(FALSE)
62 ->addSelect('contact_edit_options')
63 ->execute()->first();
64 $this->assertTrue(is_array($setting['value']));
65 $this->assertTrue(is_numeric($setting['value'][0]));
66
67 $setting = \Civi\Api4\Setting::get(FALSE)
68 ->addSelect('contact_edit_options:label')
69 ->execute()->first();
70 $this->assertEquals(['Communication Preferences', 'Custom Data'], $setting['value']);
71 $this->assertEquals('contact_edit_options:label', $setting['name']);
72 }
73
74 /**
75 * Ensure settings work with the "index" mode.
76 */
77 public function testSettingsWithIndexParam() {
78 $settings = civicrm_api4('Setting', 'get', [], ['name' => 'value']);
79 $stringValues = FALSE;
80 $arrayValues = FALSE;
81 // With indexing by [name => value], keys should be string and values should be string/array
82 foreach ($settings as $name => $value) {
83 $this->assertTrue(is_string($name) && !is_numeric($name));
84 if (is_string($value)) {
85 $stringValues = TRUE;
86 }
87 elseif (is_array($value)) {
88 $arrayValues = TRUE;
89 }
90 }
91 $this->assertTrue($stringValues);
92 $this->assertTrue($arrayValues);
93 }
94
95 /**
96 * Make sure options load from getFields.
97 */
98 public function testSettingGetFieldsOptions() {
99 $setting = civicrm_api4('Setting', 'getFields', [
100 'select' => ['options'],
101 'loadOptions' => FALSE,
102 ], 'name');
103 $this->assertTrue($setting['contact_edit_options']['options']);
104
105 $setting = civicrm_api4('Setting', 'getFields', [
106 'select' => ['options'],
107 'where' => [['name', '=', 'contact_edit_options']],
108 'loadOptions' => TRUE,
109 ], 'name');
110 $this->assertContains('Custom Data', $setting['contact_edit_options']['options']);
111
112 $setting = civicrm_api4('Setting', 'getFields', [
113 'select' => ['options'],
114 'loadOptions' => ['id', 'name', 'label'],
115 ], 'name');
116 $this->assertTrue(is_array($setting['contact_edit_options']['options'][0]));
117 }
118
119 /**
120 * Ensure settings default values unserialize.
121 */
122 public function testSettingUnserializeDefaults() {
123 $setting = civicrm_api4('Setting', 'getFields', ['where' => [['name', '=', 'contact_view_options']]], 0);
124 $this->assertTrue(is_array($setting['default']));
125 }
126
127 }