]> BookStack Code Mirror - bookstack/blob - tests/User/UserPreferencesTest.php
Notifications: Added testing to cover controls
[bookstack] / tests / User / UserPreferencesTest.php
1 <?php
2
3 namespace Tests\User;
4
5 use BookStack\Activity\Tools\UserEntityWatchOptions;
6 use Tests\TestCase;
7
8 class UserPreferencesTest extends TestCase
9 {
10     public function test_index_view()
11     {
12         $resp = $this->asEditor()->get('/preferences');
13         $resp->assertOk();
14         $resp->assertSee('Interface Keyboard Shortcuts');
15         $resp->assertSee('Edit Profile');
16     }
17
18     public function test_index_view_accessible_but_without_profile_for_guest_user()
19     {
20         $this->setSettings(['app-public' => 'true']);
21         $resp = $this->get('/preferences');
22         $resp->assertOk();
23         $resp->assertSee('Interface Keyboard Shortcuts');
24         $resp->assertDontSee('Edit Profile');
25     }
26     public function test_interface_shortcuts_updating()
27     {
28         $this->asEditor();
29
30         // View preferences with defaults
31         $resp = $this->get('/preferences/shortcuts');
32         $resp->assertSee('Interface Keyboard Shortcuts');
33
34         $html = $this->withHtml($resp);
35         $html->assertFieldHasValue('enabled', 'false');
36         $html->assertFieldHasValue('shortcut[home_view]', '1');
37
38         // Update preferences
39         $resp = $this->put('/preferences/shortcuts', [
40             'enabled' => 'true',
41             'shortcut' => ['home_view' => 'Ctrl + 1'],
42         ]);
43
44         $resp->assertRedirect('/preferences/shortcuts');
45         $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
46
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');
52     }
53
54     public function test_body_has_shortcuts_component_when_active()
55     {
56         $editor = $this->users->editor();
57         $this->actingAs($editor);
58
59         $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
60
61         setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
62         $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
63     }
64
65     public function test_notification_routes_requires_notification_permission()
66     {
67         $viewer = $this->users->viewer();
68         $resp = $this->actingAs($viewer)->get('/preferences/notifications');
69         $this->assertPermissionError($resp);
70
71         $resp = $this->put('/preferences/notifications');
72         $this->assertPermissionError($resp);
73
74         $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
75         $resp = $this->get('/preferences/notifications');
76         $resp->assertOk();
77         $resp->assertSee('Notification Preferences');
78     }
79
80     public function test_notification_preferences_updating()
81     {
82         $editor = $this->users->editor();
83
84         // View preferences with defaults
85         $resp = $this->actingAs($editor)->get('/preferences/notifications');
86         $resp->assertSee('Notification Preferences');
87
88         $html = $this->withHtml($resp);
89         $html->assertFieldHasValue('preferences[comment-replies]', 'false');
90
91         // Update preferences
92         $resp = $this->put('/preferences/notifications', [
93             'preferences' => ['comment-replies' => 'true'],
94         ]);
95
96         $resp->assertRedirect('/preferences/notifications');
97         $resp->assertSessionHas('success', 'Notification preferences have been updated!');
98
99         // View updates to preferences page
100         $resp = $this->get('/preferences/notifications');
101         $html = $this->withHtml($resp);
102         $html->assertFieldHasValue('preferences[comment-replies]', 'true');
103     }
104
105     public function test_notification_preferences_show_watches()
106     {
107         $editor = $this->users->editor();
108         $book = $this->entities->book();
109
110         $options = new UserEntityWatchOptions($editor, $book);
111         $options->updateWatchLevel('comments');
112
113         $resp = $this->actingAs($editor)->get('/preferences/notifications');
114         $resp->assertSee($book->name);
115         $resp->assertSee('All Page Updates & Comments');
116
117         $options->updateWatchLevel('default');
118
119         $resp = $this->actingAs($editor)->get('/preferences/notifications');
120         $resp->assertDontSee($book->name);
121         $resp->assertDontSee('All Page Updates & Comments');
122     }
123
124     public function test_update_sort_preference()
125     {
126         $editor = $this->users->editor();
127         $this->actingAs($editor);
128
129         $updateRequest = $this->patch('/preferences/change-sort/books', [
130             'sort'  => 'created_at',
131             'order' => 'desc',
132         ]);
133         $updateRequest->assertStatus(302);
134
135         $this->assertDatabaseHas('settings', [
136             'setting_key' => 'user:' . $editor->id . ':books_sort',
137             'value'       => 'created_at',
138         ]);
139         $this->assertDatabaseHas('settings', [
140             'setting_key' => 'user:' . $editor->id . ':books_sort_order',
141             'value'       => 'desc',
142         ]);
143         $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
144         $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
145     }
146
147     public function test_update_sort_bad_entity_type_handled()
148     {
149         $editor = $this->users->editor();
150         $this->actingAs($editor);
151
152         $updateRequest = $this->patch('/preferences/change-sort/dogs', [
153             'sort'  => 'name',
154             'order' => 'asc',
155         ]);
156         $updateRequest->assertStatus(500);
157
158         $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
159         $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
160     }
161
162     public function test_update_expansion_preference()
163     {
164         $editor = $this->users->editor();
165         $this->actingAs($editor);
166
167         $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
168         $updateRequest->assertStatus(204);
169
170         $this->assertDatabaseHas('settings', [
171             'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
172             'value'       => 'true',
173         ]);
174         $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
175
176         $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
177         $invalidKeyRequest->assertStatus(500);
178     }
179
180     public function test_toggle_dark_mode()
181     {
182         $home = $this->actingAs($this->users->editor())->get('/');
183         $home->assertSee('Dark Mode');
184         $this->withHtml($home)->assertElementNotExists('.dark-mode');
185
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'));
190
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');
195     }
196
197     public function test_dark_mode_defaults_to_config_option()
198     {
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');
203
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');
208     }
209
210     public function test_books_view_type_preferences_when_list()
211     {
212         $editor = $this->users->editor();
213         setting()->putUser($editor, 'books_view_type', 'list');
214
215         $resp = $this->actingAs($editor)->get('/books');
216         $this->withHtml($resp)
217             ->assertElementNotExists('.featured-image-container')
218             ->assertElementExists('.content-wrap .entity-list-item');
219     }
220
221     public function test_books_view_type_preferences_when_grid()
222     {
223         $editor = $this->users->editor();
224         setting()->putUser($editor, 'books_view_type', 'grid');
225
226         $resp = $this->actingAs($editor)->get('/books');
227         $this->withHtml($resp)->assertElementExists('.featured-image-container');
228     }
229
230     public function test_shelf_view_type_change()
231     {
232         $editor = $this->users->editor();
233         $shelf = $this->entities->shelf();
234         setting()->putUser($editor, 'bookshelf_view_type', 'list');
235
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');
240
241         $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
242         $req->assertRedirect($shelf->getUrl());
243
244         $resp = $this->actingAs($editor)->get($shelf->getUrl())
245             ->assertSee('List View');
246
247         $this->withHtml($resp)
248             ->assertElementExists('.featured-image-container')
249             ->assertElementNotExists('.content-wrap .entity-list-item');
250     }
251
252     public function test_update_code_language_favourite()
253     {
254         $editor = $this->users->editor();
255         $page = $this->entities->page();
256         $this->actingAs($editor);
257
258         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
259         $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
260
261         $resp = $this->get($page->getUrl('/edit'));
262         $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
263
264         $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
265         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
266
267         $resp = $this->get($page->getUrl('/edit'));
268         $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);
269     }
270 }