5 use BookStack\Activity\Tools\UserEntityWatchOptions;
6 use BookStack\Activity\WatchLevels;
9 class UserPreferencesTest extends TestCase
11 public function test_index_view()
13 $resp = $this->asEditor()->get('/preferences');
15 $resp->assertSee('Interface Keyboard Shortcuts');
16 $resp->assertSee('Edit Profile');
19 public function test_index_view_accessible_but_without_profile_and_notifications_for_guest_user()
21 $this->setSettings(['app-public' => 'true']);
22 $this->permissions->grantUserRolePermissions($this->users->guest(), ['receive-notifications']);
23 $resp = $this->get('/preferences');
25 $resp->assertSee('Interface Keyboard Shortcuts');
26 $resp->assertDontSee('Edit Profile');
27 $resp->assertDontSee('Notification');
29 public function test_interface_shortcuts_updating()
33 // View preferences with defaults
34 $resp = $this->get('/preferences/shortcuts');
35 $resp->assertSee('Interface Keyboard Shortcuts');
37 $html = $this->withHtml($resp);
38 $html->assertFieldHasValue('enabled', 'false');
39 $html->assertFieldHasValue('shortcut[home_view]', '1');
42 $resp = $this->put('/preferences/shortcuts', [
44 'shortcut' => ['home_view' => 'Ctrl + 1'],
47 $resp->assertRedirect('/preferences/shortcuts');
48 $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
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');
57 public function test_body_has_shortcuts_component_when_active()
59 $editor = $this->users->editor();
60 $this->actingAs($editor);
62 $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
64 setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
65 $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
68 public function test_notification_routes_requires_notification_permission()
70 $viewer = $this->users->viewer();
71 $resp = $this->actingAs($viewer)->get('/preferences/notifications');
72 $this->assertPermissionError($resp);
74 $resp = $this->put('/preferences/notifications');
75 $this->assertPermissionError($resp);
77 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
78 $resp = $this->get('/preferences/notifications');
80 $resp->assertSee('Notification Preferences');
83 public function test_notification_preferences_updating()
85 $editor = $this->users->editor();
87 // View preferences with defaults
88 $resp = $this->actingAs($editor)->get('/preferences/notifications');
89 $resp->assertSee('Notification Preferences');
91 $html = $this->withHtml($resp);
92 $html->assertFieldHasValue('preferences[comment-replies]', 'false');
95 $resp = $this->put('/preferences/notifications', [
96 'preferences' => ['comment-replies' => 'true'],
99 $resp->assertRedirect('/preferences/notifications');
100 $resp->assertSessionHas('success', 'Notification preferences have been updated!');
102 // View updates to preferences page
103 $resp = $this->get('/preferences/notifications');
104 $html = $this->withHtml($resp);
105 $html->assertFieldHasValue('preferences[comment-replies]', 'true');
108 public function test_notification_preferences_show_watches()
110 $editor = $this->users->editor();
111 $book = $this->entities->book();
113 $options = new UserEntityWatchOptions($editor, $book);
114 $options->updateLevelByValue(WatchLevels::COMMENTS);
116 $resp = $this->actingAs($editor)->get('/preferences/notifications');
117 $resp->assertSee($book->name);
118 $resp->assertSee('All Page Updates & Comments');
120 $options->updateLevelByValue(WatchLevels::DEFAULT);
122 $resp = $this->actingAs($editor)->get('/preferences/notifications');
123 $resp->assertDontSee($book->name);
124 $resp->assertDontSee('All Page Updates & Comments');
127 public function test_notification_preferences_dont_error_on_deleted_items()
129 $editor = $this->users->editor();
130 $book = $this->entities->book();
132 $options = new UserEntityWatchOptions($editor, $book);
133 $options->updateLevelByValue(WatchLevels::COMMENTS);
135 $this->actingAs($editor)->delete($book->getUrl());
137 $this->assertNotNull($book->deleted_at);
139 $resp = $this->actingAs($editor)->get('/preferences/notifications');
141 $resp->assertDontSee($book->name);
144 public function test_notification_preferences_not_accessible_to_guest()
146 $this->setSettings(['app-public' => 'true']);
147 $guest = $this->users->guest();
148 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
150 $resp = $this->get('/preferences/notifications');
151 $this->assertPermissionError($resp);
153 $resp = $this->put('/preferences/notifications', [
154 'preferences' => ['comment-replies' => 'true'],
156 $this->assertPermissionError($resp);
159 public function test_update_sort_preference()
161 $editor = $this->users->editor();
162 $this->actingAs($editor);
164 $updateRequest = $this->patch('/preferences/change-sort/books', [
165 'sort' => 'created_at',
168 $updateRequest->assertStatus(302);
170 $this->assertDatabaseHas('settings', [
171 'setting_key' => 'user:' . $editor->id . ':books_sort',
172 'value' => 'created_at',
174 $this->assertDatabaseHas('settings', [
175 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
178 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
179 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
182 public function test_update_sort_bad_entity_type_handled()
184 $editor = $this->users->editor();
185 $this->actingAs($editor);
187 $updateRequest = $this->patch('/preferences/change-sort/dogs', [
191 $updateRequest->assertStatus(500);
193 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
194 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
197 public function test_update_expansion_preference()
199 $editor = $this->users->editor();
200 $this->actingAs($editor);
202 $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
203 $updateRequest->assertStatus(204);
205 $this->assertDatabaseHas('settings', [
206 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
209 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
211 $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
212 $invalidKeyRequest->assertStatus(500);
215 public function test_toggle_dark_mode()
217 $home = $this->actingAs($this->users->editor())->get('/');
218 $home->assertSee('Dark Mode');
219 $this->withHtml($home)->assertElementNotExists('.dark-mode');
221 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
222 $prefChange = $this->patch('/preferences/toggle-dark-mode');
223 $prefChange->assertRedirect();
224 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
226 $home = $this->actingAs($this->users->editor())->get('/');
227 $this->withHtml($home)->assertElementExists('.dark-mode');
228 $home->assertDontSee('Dark Mode');
229 $home->assertSee('Light Mode');
232 public function test_dark_mode_defaults_to_config_option()
234 config()->set('setting-defaults.user.dark-mode-enabled', false);
235 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
236 $home = $this->get('/login');
237 $this->withHtml($home)->assertElementNotExists('.dark-mode');
239 config()->set('setting-defaults.user.dark-mode-enabled', true);
240 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
241 $home = $this->get('/login');
242 $this->withHtml($home)->assertElementExists('.dark-mode');
245 public function test_dark_mode_toggle_endpoint_changes_to_light_when_dark_by_default()
247 config()->set('setting-defaults.user.dark-mode-enabled', true);
248 $editor = $this->users->editor();
250 $this->assertEquals(true, setting()->getUser($editor, 'dark-mode-enabled'));
251 $prefChange = $this->actingAs($editor)->patch('/preferences/toggle-dark-mode');
252 $prefChange->assertRedirect();
253 $this->assertEquals(false, setting()->getUser($editor, 'dark-mode-enabled'));
255 $home = $this->get('/');
256 $this->withHtml($home)->assertElementNotExists('.dark-mode');
257 $home->assertDontSee('Light Mode');
258 $home->assertSee('Dark Mode');
261 public function test_books_view_type_preferences_when_list()
263 $editor = $this->users->editor();
264 setting()->putUser($editor, 'books_view_type', 'list');
266 $resp = $this->actingAs($editor)->get('/books');
267 $this->withHtml($resp)
268 ->assertElementNotExists('.featured-image-container')
269 ->assertElementExists('.content-wrap .entity-list-item');
272 public function test_books_view_type_preferences_when_grid()
274 $editor = $this->users->editor();
275 setting()->putUser($editor, 'books_view_type', 'grid');
277 $resp = $this->actingAs($editor)->get('/books');
278 $this->withHtml($resp)->assertElementExists('.featured-image-container');
281 public function test_shelf_view_type_change()
283 $editor = $this->users->editor();
284 $shelf = $this->entities->shelf();
285 setting()->putUser($editor, 'bookshelf_view_type', 'list');
287 $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
288 $this->withHtml($resp)
289 ->assertElementNotExists('.featured-image-container')
290 ->assertElementExists('.content-wrap .entity-list-item');
292 $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
293 $req->assertRedirect($shelf->getUrl());
295 $resp = $this->actingAs($editor)->get($shelf->getUrl())
296 ->assertSee('List View');
298 $this->withHtml($resp)
299 ->assertElementExists('.featured-image-container')
300 ->assertElementNotExists('.content-wrap .entity-list-item');
303 public function test_update_code_language_favourite()
305 $editor = $this->users->editor();
306 $page = $this->entities->page();
307 $this->actingAs($editor);
309 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
310 $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
312 $resp = $this->get($page->getUrl('/edit'));
313 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
315 $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
316 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
318 $resp = $this->get($page->getUrl('/edit'));
319 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);