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_update_sort_preference()
126 $editor = $this->users->editor();
127 $this->actingAs($editor);
129 $updateRequest = $this->patch('/preferences/change-sort/books', [
130 'sort' => 'created_at',
133 $updateRequest->assertStatus(302);
135 $this->assertDatabaseHas('settings', [
136 'setting_key' => 'user:' . $editor->id . ':books_sort',
137 'value' => 'created_at',
139 $this->assertDatabaseHas('settings', [
140 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
143 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
144 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
147 public function test_update_sort_bad_entity_type_handled()
149 $editor = $this->users->editor();
150 $this->actingAs($editor);
152 $updateRequest = $this->patch('/preferences/change-sort/dogs', [
156 $updateRequest->assertStatus(500);
158 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
159 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
162 public function test_update_expansion_preference()
164 $editor = $this->users->editor();
165 $this->actingAs($editor);
167 $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
168 $updateRequest->assertStatus(204);
170 $this->assertDatabaseHas('settings', [
171 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
174 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
176 $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
177 $invalidKeyRequest->assertStatus(500);
180 public function test_toggle_dark_mode()
182 $home = $this->actingAs($this->users->editor())->get('/');
183 $home->assertSee('Dark Mode');
184 $this->withHtml($home)->assertElementNotExists('.dark-mode');
186 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
187 $prefChange = $this->patch('/preferences/toggle-dark-mode');
188 $prefChange->assertRedirect();
189 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
191 $home = $this->actingAs($this->users->editor())->get('/');
192 $this->withHtml($home)->assertElementExists('.dark-mode');
193 $home->assertDontSee('Dark Mode');
194 $home->assertSee('Light Mode');
197 public function test_dark_mode_defaults_to_config_option()
199 config()->set('setting-defaults.user.dark-mode-enabled', false);
200 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
201 $home = $this->get('/login');
202 $this->withHtml($home)->assertElementNotExists('.dark-mode');
204 config()->set('setting-defaults.user.dark-mode-enabled', true);
205 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
206 $home = $this->get('/login');
207 $this->withHtml($home)->assertElementExists('.dark-mode');
210 public function test_books_view_type_preferences_when_list()
212 $editor = $this->users->editor();
213 setting()->putUser($editor, 'books_view_type', 'list');
215 $resp = $this->actingAs($editor)->get('/books');
216 $this->withHtml($resp)
217 ->assertElementNotExists('.featured-image-container')
218 ->assertElementExists('.content-wrap .entity-list-item');
221 public function test_books_view_type_preferences_when_grid()
223 $editor = $this->users->editor();
224 setting()->putUser($editor, 'books_view_type', 'grid');
226 $resp = $this->actingAs($editor)->get('/books');
227 $this->withHtml($resp)->assertElementExists('.featured-image-container');
230 public function test_shelf_view_type_change()
232 $editor = $this->users->editor();
233 $shelf = $this->entities->shelf();
234 setting()->putUser($editor, 'bookshelf_view_type', 'list');
236 $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
237 $this->withHtml($resp)
238 ->assertElementNotExists('.featured-image-container')
239 ->assertElementExists('.content-wrap .entity-list-item');
241 $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
242 $req->assertRedirect($shelf->getUrl());
244 $resp = $this->actingAs($editor)->get($shelf->getUrl())
245 ->assertSee('List View');
247 $this->withHtml($resp)
248 ->assertElementExists('.featured-image-container')
249 ->assertElementNotExists('.content-wrap .entity-list-item');
252 public function test_update_code_language_favourite()
254 $editor = $this->users->editor();
255 $page = $this->entities->page();
256 $this->actingAs($editor);
258 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
259 $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
261 $resp = $this->get($page->getUrl('/edit'));
262 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
264 $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
265 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
267 $resp = $this->get($page->getUrl('/edit'));
268 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);