-
Notifications
You must be signed in to change notification settings - Fork 244
Expand file tree
/
Copy pathSettingsTest.php
More file actions
232 lines (216 loc) · 7.83 KB
/
Copy pathSettingsTest.php
File metadata and controls
232 lines (216 loc) · 7.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<?php
namespace Tests\Feature\Api;
use DB;
use Illuminate\Foundation\Testing\WithFaker;
use ProcessMaker\Models\Setting;
use ProcessMaker\Models\SettingsMenus;
use Tests\Feature\Shared\RequestHelper;
use Tests\Feature\Shared\ResourceAssertionsTrait;
use Tests\TestCase;
class SettingsTest extends TestCase
{
use ResourceAssertionsTrait;
use WithFaker;
use RequestHelper;
protected $resource = 'setting';
protected $structure = [
'config',
'process_request_id',
'user_id',
'element_id',
'element_type',
'element_name',
'status',
'completed_at',
'due_at',
'initiated_at',
'riskchanges_at',
'updated_at',
'created_at',
];
/**
* Test get settings menus
*/
public function testGetSettingsMenus()
{
SettingsMenus::query()->delete();
// Create
SettingsMenus::factory()->create([
'menu_group' => SettingsMenus::EMAIL_MENU_GROUP,
]);
SettingsMenus::factory()->create([
'menu_group' => SettingsMenus::LOG_IN_AUTH_MENU_GROUP,
]);
SettingsMenus::factory()->create([
'menu_group' => SettingsMenus::USER_SETTINGS_MENU_GROUP,
]);
SettingsMenus::factory()->create([
'menu_group' => SettingsMenus::INTEGRATIONS_MENU_GROUP,
]);
$route = route('api.settings.menu_groups');
$response = $this->apiCall('GET', $route);
// Verify the status
$response->assertStatus(200);
$this->assertCount(4, $response['data']);
}
/**
* Test get settings menus group related
*/
public function testGetSettingsMenusGroup()
{
// Get setting menus
$menus = SettingsMenus::factory()->create();
Setting::factory()->create([
'key' => 'test.properties',
'name' => 'test',
'format' => 'test',
'group' => 'UserTest',
'group_id' => $menus->id,
]);
$route = route('api.settings.menu_groups');
$response = $this->apiCall('GET', $route);
// Verify the status
$response->assertStatus(200);
$this->assertNotEmpty($response['data']);
$this->assertNotEmpty($response['data'][0]['groups']);
}
/**
* Test update settings for specific group
*/
public function testUpdateSettingsForSpecificGroup()
{
$menus = SettingsMenus::factory()->create();
$group = 'Custom group';
Setting::factory()->create([
'key' => 'test.properties',
'name' => 'test',
'format' => 'test',
'group' => $group,
'group_id' => null,
]);
// Update
Setting::updateSettingsGroup($group, $menus->id);
$matches = Setting::where('group', $group)->where('group_id', $menus->id)->get()->toArray();
$this->assertNotEmpty($matches);
}
/**
* Test extended properties variable valid name validation
*/
public function testUpdateExtendedPropertiesWithValidVariableName()
{
$this->markTestSkipped('Not using validation in backend yet, because there are some config data that should not be validated as LDAP config...');
$setting = Setting::factory()->create(['key' => 'users.properties']);
$params = [
// Test data different valid variable names
'config' => [
'_myVar' => 'This is my variable 1',
'myVar' => 'This is my variable 2',
'myVar1' => 'This is my variable 3',
],
'key' => $setting->key,
'id' => $setting->id,
];
//Update setting config
$route = route('api.settings.update', [$setting->id]);
$response = $this->apiCall('PUT', $route, $params);
//Verify the status
$response->assertStatus(204);
//Verify variables were updated
$this->assertDatabaseHas('settings', ['config' => '{"_myVar":"This is my variable 1","myVar":"This is my variable 2","myVar1":"This is my variable 3"}']);
}
/**
* Test extended properties variable invalid name validation
*/
public function testUpdateExtendedPropertiesWithInvalidVariableName()
{
$this->markTestSkipped('Not using validation in backend yet, because there are some config data that should not be validated as LDAP config...');
$setting = Setting::factory()->create(['key' => 'users.properties']);
$params = [
// Test data different valid variable names
'config' => [
'1myVar' => 'This is my variable 1',
'myVar space' => 'This is my variable 2',
'my-Var' => 'This is my variable 3',
],
'key' => $setting->key,
'id' => $setting->id,
];
//Update setting config
$route = route('api.settings.update', [$setting->id]);
$response = $this->apiCall('PUT', $route, $params);
//Verify the status
$response->assertStatus(422);
//Verify response error
$response->assertJson(
[
'message' => 'The given data was invalid.',
'errors' => [
'config.1myVar' => [
'Name has to start with a letter and can contain only letters, numbers, and underscores (_).',
],
'config.myVar space' => [
'Name has to start with a letter and can contain only letters, numbers, and underscores (_).',
],
'config.my-Var' => [
'Name has to start with a letter and can contain only letters, numbers, and underscores (_).',
],
],
]
);
//Verify variable were not updated
$this->assertDatabaseMissing('settings', ['config' => '{"1myVar":"This is my variable 1","myVar space":"This is my variable 2"}']);
}
public function test_it_can_create_a_setting()
{
$menu = SettingsMenus::create([
'menu_group' => 'Log-In & Auth',
]);
$data = [
'key' => 'white_list.drive',
'format' => 'text',
'config' => 'https://drive.google.com',
'name' => 'Drive',
'group' => 'IFrame Whitelist Config',
'group_id' => $menu->id,
'hidden' => false,
'ui' => null,
];
$route = route('api.settings.store');
$response = $this->apiCall('POST', $route, $data);
//Verify the status
$response->assertStatus(201);
}
public function test_it_returns_error_for_duplicate_entry()
{
$menu = SettingsMenus::create([
'menu_group' => 'Log-In & Auth',
]);
// Create a setting first
Setting::create([
'key' => 'white_list.drive',
'format' => 'text',
'config' => 'https://drive.google.com',
'name' => 'Drive',
'group' => 'IFrame Whitelist Config',
'group_id' => $menu->id,
'hidden' => false,
'ui' => null,
]);
// Data to create
$data = [
'key' => 'white_list.drive',
'format' => 'text',
'config' => 'https://drive.google.com',
'name' => 'Drive',
'group' => 'IFrame Whitelist Config',
'group_id' => $menu->id,
'hidden' => false,
'ui' => null,
];
$route = route('api.settings.store');
$response = $this->apiCall('POST', $route, $data);
//Verify the status
$response->assertStatus(409)
->assertJson(['message' => 'The "Site Name" you\'re trying to add already exists. Please select a different name or review the existing entries to avoid duplication.']);
}
}