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