5 use BookStack\Activity\Tools\UserEntityWatchOptions;
8 class UserPreferencesTest extends TestCase
10 public function test_index_view()
12 $resp = $this->asEditor()->get('/preferences');
14 $resp->assertSee('Interface Keyboard Shortcuts');
15 $resp->assertSee('Edit Profile');
18 public function test_index_view_accessible_but_without_profile_for_guest_user()
20 $this->setSettings(['app-public' => 'true']);
21 $resp = $this->get('/preferences');
23 $resp->assertSee('Interface Keyboard Shortcuts');
24 $resp->assertDontSee('Edit Profile');
26 public function test_interface_shortcuts_updating()
30 // View preferences with defaults
31 $resp = $this->get('/preferences/shortcuts');
32 $resp->assertSee('Interface Keyboard Shortcuts');
34 $html = $this->withHtml($resp);
35 $html->assertFieldHasValue('enabled', 'false');
36 $html->assertFieldHasValue('shortcut[home_view]', '1');
39 $resp = $this->put('/preferences/shortcuts', [
41 'shortcut' => ['home_view' => 'Ctrl + 1'],
44 $resp->assertRedirect('/preferences/shortcuts');
45 $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
47 // View updates to preferences page
48 $resp = $this->get('/preferences/shortcuts');
49 $html = $this->withHtml($resp);
50 $html->assertFieldHasValue('enabled', 'true');
51 $html->assertFieldHasValue('shortcut[home_view]', 'Ctrl + 1');
54 public function test_body_has_shortcuts_component_when_active()
56 $editor = $this->users->editor();
57 $this->actingAs($editor);
59 $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
61 setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
62 $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
65 public function test_notification_routes_requires_notification_permission()
67 $viewer = $this->users->viewer();
68 $resp = $this->actingAs($viewer)->get('/preferences/notifications');
69 $this->assertPermissionError($resp);
71 $resp = $this->put('/preferences/notifications');
72 $this->assertPermissionError($resp);
74 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
75 $resp = $this->get('/preferences/notifications');
77 $resp->assertSee('Notification Preferences');
80 public function test_notification_preferences_updating()
82 $editor = $this->users->editor();
84 // View preferences with defaults
85 $resp = $this->actingAs($editor)->get('/preferences/notifications');
86 $resp->assertSee('Notification Preferences');
88 $html = $this->withHtml($resp);
89 $html->assertFieldHasValue('preferences[comment-replies]', 'false');
92 $resp = $this->put('/preferences/notifications', [
93 'preferences' => ['comment-replies' => 'true'],
96 $resp->assertRedirect('/preferences/notifications');
97 $resp->assertSessionHas('success', 'Notification preferences have been updated!');
99 // View updates to preferences page
100 $resp = $this->get('/preferences/notifications');
101 $html = $this->withHtml($resp);
102 $html->assertFieldHasValue('preferences[comment-replies]', 'true');
105 public function test_notification_preferences_show_watches()
107 $editor = $this->users->editor();
108 $book = $this->entities->book();
110 $options = new UserEntityWatchOptions($editor, $book);
111 $options->updateWatchLevel('comments');
113 $resp = $this->actingAs($editor)->get('/preferences/notifications');
114 $resp->assertSee($book->name);
115 $resp->assertSee('All Page Updates & Comments');
117 $options->updateWatchLevel('default');
119 $resp = $this->actingAs($editor)->get('/preferences/notifications');
120 $resp->assertDontSee($book->name);
121 $resp->assertDontSee('All Page Updates & Comments');
124 public function test_notification_preferences_not_accessible_to_guest()
126 $this->setSettings(['app-public' => 'true']);
127 $guest = $this->users->guest();
128 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
130 $resp = $this->get('/preferences/notifications');
131 $this->assertPermissionError($resp);
133 $resp = $this->put('/preferences/notifications', [
134 'preferences' => ['comment-replies' => 'true'],
136 $this->assertPermissionError($resp);
139 public function test_update_sort_preference()
141 $editor = $this->users->editor();
142 $this->actingAs($editor);
144 $updateRequest = $this->patch('/preferences/change-sort/books', [
145 'sort' => 'created_at',
148 $updateRequest->assertStatus(302);
150 $this->assertDatabaseHas('settings', [
151 'setting_key' => 'user:' . $editor->id . ':books_sort',
152 'value' => 'created_at',
154 $this->assertDatabaseHas('settings', [
155 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
158 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
159 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
162 public function test_update_sort_bad_entity_type_handled()
164 $editor = $this->users->editor();
165 $this->actingAs($editor);
167 $updateRequest = $this->patch('/preferences/change-sort/dogs', [
171 $updateRequest->assertStatus(500);
173 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
174 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
177 public function test_update_expansion_preference()
179 $editor = $this->users->editor();
180 $this->actingAs($editor);
182 $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
183 $updateRequest->assertStatus(204);
185 $this->assertDatabaseHas('settings', [
186 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
189 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
191 $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
192 $invalidKeyRequest->assertStatus(500);
195 public function test_toggle_dark_mode()
197 $home = $this->actingAs($this->users->editor())->get('/');
198 $home->assertSee('Dark Mode');
199 $this->withHtml($home)->assertElementNotExists('.dark-mode');
201 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
202 $prefChange = $this->patch('/preferences/toggle-dark-mode');
203 $prefChange->assertRedirect();
204 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
206 $home = $this->actingAs($this->users->editor())->get('/');
207 $this->withHtml($home)->assertElementExists('.dark-mode');
208 $home->assertDontSee('Dark Mode');
209 $home->assertSee('Light Mode');
212 public function test_dark_mode_defaults_to_config_option()
214 config()->set('setting-defaults.user.dark-mode-enabled', false);
215 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
216 $home = $this->get('/login');
217 $this->withHtml($home)->assertElementNotExists('.dark-mode');
219 config()->set('setting-defaults.user.dark-mode-enabled', true);
220 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
221 $home = $this->get('/login');
222 $this->withHtml($home)->assertElementExists('.dark-mode');
225 public function test_books_view_type_preferences_when_list()
227 $editor = $this->users->editor();
228 setting()->putUser($editor, 'books_view_type', 'list');
230 $resp = $this->actingAs($editor)->get('/books');
231 $this->withHtml($resp)
232 ->assertElementNotExists('.featured-image-container')
233 ->assertElementExists('.content-wrap .entity-list-item');
236 public function test_books_view_type_preferences_when_grid()
238 $editor = $this->users->editor();
239 setting()->putUser($editor, 'books_view_type', 'grid');
241 $resp = $this->actingAs($editor)->get('/books');
242 $this->withHtml($resp)->assertElementExists('.featured-image-container');
245 public function test_shelf_view_type_change()
247 $editor = $this->users->editor();
248 $shelf = $this->entities->shelf();
249 setting()->putUser($editor, 'bookshelf_view_type', 'list');
251 $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
252 $this->withHtml($resp)
253 ->assertElementNotExists('.featured-image-container')
254 ->assertElementExists('.content-wrap .entity-list-item');
256 $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
257 $req->assertRedirect($shelf->getUrl());
259 $resp = $this->actingAs($editor)->get($shelf->getUrl())
260 ->assertSee('List View');
262 $this->withHtml($resp)
263 ->assertElementExists('.featured-image-container')
264 ->assertElementNotExists('.content-wrap .entity-list-item');
267 public function test_update_code_language_favourite()
269 $editor = $this->users->editor();
270 $page = $this->entities->page();
271 $this->actingAs($editor);
273 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
274 $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
276 $resp = $this->get($page->getUrl('/edit'));
277 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
279 $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
280 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
282 $resp = $this->get($page->getUrl('/edit'));
283 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);