]> BookStack Code Mirror - bookstack/blob - tests/User/UserPreferencesTest.php
Remove Dumps
[bookstack] / tests / User / UserPreferencesTest.php
1 <?php
2
3 namespace Tests\User;
4
5 use BookStack\Activity\Tools\UserEntityWatchOptions;
6 use BookStack\Activity\WatchLevels;
7 use Tests\TestCase;
8
9 class UserPreferencesTest extends TestCase
10 {
11     public function test_index_view()
12     {
13         $resp = $this->asEditor()->get('/preferences');
14         $resp->assertOk();
15         $resp->assertSee('Interface Keyboard Shortcuts');
16         $resp->assertSee('Edit Profile');
17     }
18
19     public function test_index_view_accessible_but_without_profile_and_notifications_for_guest_user()
20     {
21         $this->setSettings(['app-public' => 'true']);
22         $this->permissions->grantUserRolePermissions($this->users->guest(), ['receive-notifications']);
23         $resp = $this->get('/preferences');
24         $resp->assertOk();
25         $resp->assertSee('Interface Keyboard Shortcuts');
26         $resp->assertDontSee('Edit Profile');
27         $resp->assertDontSee('Notification');
28     }
29     public function test_interface_shortcuts_updating()
30     {
31         $this->asEditor();
32
33         // View preferences with defaults
34         $resp = $this->get('/preferences/shortcuts');
35         $resp->assertSee('Interface Keyboard Shortcuts');
36
37         $html = $this->withHtml($resp);
38         $html->assertFieldHasValue('enabled', 'false');
39         $html->assertFieldHasValue('shortcut[home_view]', '1');
40
41         // Update preferences
42         $resp = $this->put('/preferences/shortcuts', [
43             'enabled' => 'true',
44             'shortcut' => ['home_view' => 'Ctrl + 1'],
45         ]);
46
47         $resp->assertRedirect('/preferences/shortcuts');
48         $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
49
50         // View updates to preferences page
51         $resp = $this->get('/preferences/shortcuts');
52         $html = $this->withHtml($resp);
53         $html->assertFieldHasValue('enabled', 'true');
54         $html->assertFieldHasValue('shortcut[home_view]', 'Ctrl + 1');
55     }
56
57     public function test_body_has_shortcuts_component_when_active()
58     {
59         $editor = $this->users->editor();
60         $this->actingAs($editor);
61
62         $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
63
64         setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
65         $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
66     }
67
68     public function test_notification_routes_requires_notification_permission()
69     {
70         $viewer = $this->users->viewer();
71         $resp = $this->actingAs($viewer)->get('/preferences/notifications');
72         $this->assertPermissionError($resp);
73
74         $resp = $this->put('/preferences/notifications');
75         $this->assertPermissionError($resp);
76
77         $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
78         $resp = $this->get('/preferences/notifications');
79         $resp->assertOk();
80         $resp->assertSee('Notification Preferences');
81     }
82
83     public function test_notification_preferences_updating()
84     {
85         $editor = $this->users->editor();
86
87         // View preferences with defaults
88         $resp = $this->actingAs($editor)->get('/preferences/notifications');
89         $resp->assertSee('Notification Preferences');
90
91         $html = $this->withHtml($resp);
92         $html->assertFieldHasValue('preferences[comment-replies]', 'false');
93
94         // Update preferences
95         $resp = $this->put('/preferences/notifications', [
96             'preferences' => ['comment-replies' => 'true'],
97         ]);
98
99         $resp->assertRedirect('/preferences/notifications');
100         $resp->assertSessionHas('success', 'Notification preferences have been updated!');
101
102         // View updates to preferences page
103         $resp = $this->get('/preferences/notifications');
104         $html = $this->withHtml($resp);
105         $html->assertFieldHasValue('preferences[comment-replies]', 'true');
106     }
107
108     public function test_notification_preferences_show_watches()
109     {
110         $editor = $this->users->editor();
111         $book = $this->entities->book();
112
113         $options = new UserEntityWatchOptions($editor, $book);
114         $options->updateLevelByValue(WatchLevels::COMMENTS);
115
116         $resp = $this->actingAs($editor)->get('/preferences/notifications');
117         $resp->assertSee($book->name);
118         $resp->assertSee('All Page Updates & Comments');
119
120         $options->updateLevelByValue(WatchLevels::DEFAULT);
121
122         $resp = $this->actingAs($editor)->get('/preferences/notifications');
123         $resp->assertDontSee($book->name);
124         $resp->assertDontSee('All Page Updates & Comments');
125     }
126
127     public function test_notification_preferences_dont_error_on_deleted_items()
128     {
129         $editor = $this->users->editor();
130         $book = $this->entities->book();
131
132         $options = new UserEntityWatchOptions($editor, $book);
133         $options->updateLevelByValue(WatchLevels::COMMENTS);
134
135         $this->actingAs($editor)->delete($book->getUrl());
136         $book->refresh();
137         $this->assertNotNull($book->deleted_at);
138
139         $resp = $this->actingAs($editor)->get('/preferences/notifications');
140         $resp->assertOk();
141         $resp->assertDontSee($book->name);
142     }
143
144     public function test_notification_preferences_not_accessible_to_guest()
145     {
146         $this->setSettings(['app-public' => 'true']);
147         $guest = $this->users->guest();
148         $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
149
150         $resp = $this->get('/preferences/notifications');
151         $this->assertPermissionError($resp);
152
153         $resp = $this->put('/preferences/notifications', [
154             'preferences' => ['comment-replies' => 'true'],
155         ]);
156         $this->assertPermissionError($resp);
157     }
158
159     public function test_notification_comment_options_only_exist_if_comments_active()
160     {
161         $resp = $this->asEditor()->get('/preferences/notifications');
162         $resp->assertSee('Notify upon comments');
163         $resp->assertSee('Notify upon replies');
164
165         setting()->put('app-disable-comments', true);
166
167         $resp = $this->get('/preferences/notifications');
168         $resp->assertDontSee('Notify upon comments');
169         $resp->assertDontSee('Notify upon replies');
170     }
171
172     public function test_update_sort_preference()
173     {
174         $editor = $this->users->editor();
175         $this->actingAs($editor);
176
177         $updateRequest = $this->patch('/preferences/change-sort/books', [
178             'sort'  => 'created_at',
179             'order' => 'desc',
180         ]);
181         $updateRequest->assertStatus(302);
182
183         $this->assertDatabaseHas('settings', [
184             'setting_key' => 'user:' . $editor->id . ':books_sort',
185             'value'       => 'created_at',
186         ]);
187         $this->assertDatabaseHas('settings', [
188             'setting_key' => 'user:' . $editor->id . ':books_sort_order',
189             'value'       => 'desc',
190         ]);
191         $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
192         $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
193     }
194
195     public function test_update_sort_bad_entity_type_handled()
196     {
197         $editor = $this->users->editor();
198         $this->actingAs($editor);
199
200         $updateRequest = $this->patch('/preferences/change-sort/dogs', [
201             'sort'  => 'name',
202             'order' => 'asc',
203         ]);
204         $updateRequest->assertStatus(500);
205
206         $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
207         $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
208     }
209
210     public function test_update_expansion_preference()
211     {
212         $editor = $this->users->editor();
213         $this->actingAs($editor);
214
215         $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
216         $updateRequest->assertStatus(204);
217
218         $this->assertDatabaseHas('settings', [
219             'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
220             'value'       => 'true',
221         ]);
222         $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
223
224         $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
225         $invalidKeyRequest->assertStatus(500);
226     }
227
228     public function test_toggle_dark_mode()
229     {
230         $home = $this->actingAs($this->users->editor())->get('/');
231         $home->assertSee('Dark Mode');
232         $this->withHtml($home)->assertElementNotExists('.dark-mode');
233
234         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
235         $prefChange = $this->patch('/preferences/toggle-dark-mode');
236         $prefChange->assertRedirect();
237         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
238
239         $home = $this->actingAs($this->users->editor())->get('/');
240         $this->withHtml($home)->assertElementExists('.dark-mode');
241         $home->assertDontSee('Dark Mode');
242         $home->assertSee('Light Mode');
243     }
244
245     public function test_dark_mode_defaults_to_config_option()
246     {
247         config()->set('setting-defaults.user.dark-mode-enabled', false);
248         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
249         $home = $this->get('/login');
250         $this->withHtml($home)->assertElementNotExists('.dark-mode');
251
252         config()->set('setting-defaults.user.dark-mode-enabled', true);
253         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
254         $home = $this->get('/login');
255         $this->withHtml($home)->assertElementExists('.dark-mode');
256     }
257
258     public function test_dark_mode_toggle_endpoint_changes_to_light_when_dark_by_default()
259     {
260         config()->set('setting-defaults.user.dark-mode-enabled', true);
261         $editor = $this->users->editor();
262
263         $this->assertEquals(true, setting()->getUser($editor, 'dark-mode-enabled'));
264         $prefChange = $this->actingAs($editor)->patch('/preferences/toggle-dark-mode');
265         $prefChange->assertRedirect();
266         $this->assertEquals(false, setting()->getUser($editor, 'dark-mode-enabled'));
267
268         $home = $this->get('/');
269         $this->withHtml($home)->assertElementNotExists('.dark-mode');
270         $home->assertDontSee('Light Mode');
271         $home->assertSee('Dark Mode');
272     }
273
274     public function test_books_view_type_preferences_when_list()
275     {
276         $editor = $this->users->editor();
277         setting()->putUser($editor, 'books_view_type', 'list');
278
279         $resp = $this->actingAs($editor)->get('/books');
280         $this->withHtml($resp)
281             ->assertElementNotExists('.featured-image-container')
282             ->assertElementExists('.content-wrap .entity-list-item');
283     }
284
285     public function test_books_view_type_preferences_when_grid()
286     {
287         $editor = $this->users->editor();
288         setting()->putUser($editor, 'books_view_type', 'grid');
289
290         $resp = $this->actingAs($editor)->get('/books');
291         $this->withHtml($resp)->assertElementExists('.featured-image-container');
292     }
293
294     public function test_shelf_view_type_change()
295     {
296         $editor = $this->users->editor();
297         $shelf = $this->entities->shelf();
298         setting()->putUser($editor, 'bookshelf_view_type', 'list');
299
300         $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
301         $this->withHtml($resp)
302             ->assertElementNotExists('.featured-image-container')
303             ->assertElementExists('.content-wrap .entity-list-item');
304
305         $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
306         $req->assertRedirect($shelf->getUrl());
307
308         $resp = $this->actingAs($editor)->get($shelf->getUrl())
309             ->assertSee('List View');
310
311         $this->withHtml($resp)
312             ->assertElementExists('.featured-image-container')
313             ->assertElementNotExists('.content-wrap .entity-list-item');
314     }
315
316     public function test_update_code_language_favourite()
317     {
318         $editor = $this->users->editor();
319         $page = $this->entities->page();
320         $this->actingAs($editor);
321
322         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
323         $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
324
325         $resp = $this->get($page->getUrl('/edit'));
326         $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
327
328         $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
329         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
330
331         $resp = $this->get($page->getUrl('/edit'));
332         $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);
333     }
334 }