]> BookStack Code Mirror - bookstack/blob - tests/User/UserPreferencesTest.php
Notifications: added user preference UI & logic
[bookstack] / tests / User / UserPreferencesTest.php
1 <?php
2
3 namespace Tests\User;
4
5 use Tests\TestCase;
6
7 class UserPreferencesTest extends TestCase
8 {
9     public function test_interface_shortcuts_updating()
10     {
11         $this->asEditor();
12
13         // View preferences with defaults
14         $resp = $this->get('/preferences/shortcuts');
15         $resp->assertSee('Interface Keyboard Shortcuts');
16
17         $html = $this->withHtml($resp);
18         $html->assertFieldHasValue('enabled', 'false');
19         $html->assertFieldHasValue('shortcut[home_view]', '1');
20
21         // Update preferences
22         $resp = $this->put('/preferences/shortcuts', [
23             'enabled' => 'true',
24             'shortcut' => ['home_view' => 'Ctrl + 1'],
25         ]);
26
27         $resp->assertRedirect('/preferences/shortcuts');
28         $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
29
30         // View updates to preferences page
31         $resp = $this->get('/preferences/shortcuts');
32         $html = $this->withHtml($resp);
33         $html->assertFieldHasValue('enabled', 'true');
34         $html->assertFieldHasValue('shortcut[home_view]', 'Ctrl + 1');
35     }
36
37     public function test_body_has_shortcuts_component_when_active()
38     {
39         $editor = $this->users->editor();
40         $this->actingAs($editor);
41
42         $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
43
44         setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
45         $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
46     }
47
48     public function test_notification_preferences_updating()
49     {
50         $this->asEditor();
51
52         // View preferences with defaults
53         $resp = $this->get('/preferences/notifications');
54         $resp->assertSee('Notification Preferences');
55
56         $html = $this->withHtml($resp);
57         $html->assertFieldHasValue('preferences[comment-replies]', 'false');
58
59         // Update preferences
60         $resp = $this->put('/preferences/notifications', [
61             'preferences' => ['comment-replies' => 'true'],
62         ]);
63
64         $resp->assertRedirect('/preferences/notifications');
65         $resp->assertSessionHas('success', 'Notification preferences have been updated!');
66
67         // View updates to preferences page
68         $resp = $this->get('/preferences/notifications');
69         $html = $this->withHtml($resp);
70         $html->assertFieldHasValue('preferences[comment-replies]', 'true');
71     }
72
73     public function test_update_sort_preference()
74     {
75         $editor = $this->users->editor();
76         $this->actingAs($editor);
77
78         $updateRequest = $this->patch('/preferences/change-sort/books', [
79             'sort'  => 'created_at',
80             'order' => 'desc',
81         ]);
82         $updateRequest->assertStatus(302);
83
84         $this->assertDatabaseHas('settings', [
85             'setting_key' => 'user:' . $editor->id . ':books_sort',
86             'value'       => 'created_at',
87         ]);
88         $this->assertDatabaseHas('settings', [
89             'setting_key' => 'user:' . $editor->id . ':books_sort_order',
90             'value'       => 'desc',
91         ]);
92         $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
93         $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
94     }
95
96     public function test_update_sort_bad_entity_type_handled()
97     {
98         $editor = $this->users->editor();
99         $this->actingAs($editor);
100
101         $updateRequest = $this->patch('/preferences/change-sort/dogs', [
102             'sort'  => 'name',
103             'order' => 'asc',
104         ]);
105         $updateRequest->assertStatus(500);
106
107         $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
108         $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
109     }
110
111     public function test_update_expansion_preference()
112     {
113         $editor = $this->users->editor();
114         $this->actingAs($editor);
115
116         $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
117         $updateRequest->assertStatus(204);
118
119         $this->assertDatabaseHas('settings', [
120             'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
121             'value'       => 'true',
122         ]);
123         $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
124
125         $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
126         $invalidKeyRequest->assertStatus(500);
127     }
128
129     public function test_toggle_dark_mode()
130     {
131         $home = $this->actingAs($this->users->editor())->get('/');
132         $home->assertSee('Dark Mode');
133         $this->withHtml($home)->assertElementNotExists('.dark-mode');
134
135         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
136         $prefChange = $this->patch('/preferences/toggle-dark-mode');
137         $prefChange->assertRedirect();
138         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
139
140         $home = $this->actingAs($this->users->editor())->get('/');
141         $this->withHtml($home)->assertElementExists('.dark-mode');
142         $home->assertDontSee('Dark Mode');
143         $home->assertSee('Light Mode');
144     }
145
146     public function test_dark_mode_defaults_to_config_option()
147     {
148         config()->set('setting-defaults.user.dark-mode-enabled', false);
149         $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
150         $home = $this->get('/login');
151         $this->withHtml($home)->assertElementNotExists('.dark-mode');
152
153         config()->set('setting-defaults.user.dark-mode-enabled', true);
154         $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
155         $home = $this->get('/login');
156         $this->withHtml($home)->assertElementExists('.dark-mode');
157     }
158
159     public function test_books_view_type_preferences_when_list()
160     {
161         $editor = $this->users->editor();
162         setting()->putUser($editor, 'books_view_type', 'list');
163
164         $resp = $this->actingAs($editor)->get('/books');
165         $this->withHtml($resp)
166             ->assertElementNotExists('.featured-image-container')
167             ->assertElementExists('.content-wrap .entity-list-item');
168     }
169
170     public function test_books_view_type_preferences_when_grid()
171     {
172         $editor = $this->users->editor();
173         setting()->putUser($editor, 'books_view_type', 'grid');
174
175         $resp = $this->actingAs($editor)->get('/books');
176         $this->withHtml($resp)->assertElementExists('.featured-image-container');
177     }
178
179     public function test_shelf_view_type_change()
180     {
181         $editor = $this->users->editor();
182         $shelf = $this->entities->shelf();
183         setting()->putUser($editor, 'bookshelf_view_type', 'list');
184
185         $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
186         $this->withHtml($resp)
187             ->assertElementNotExists('.featured-image-container')
188             ->assertElementExists('.content-wrap .entity-list-item');
189
190         $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
191         $req->assertRedirect($shelf->getUrl());
192
193         $resp = $this->actingAs($editor)->get($shelf->getUrl())
194             ->assertSee('List View');
195
196         $this->withHtml($resp)
197             ->assertElementExists('.featured-image-container')
198             ->assertElementNotExists('.content-wrap .entity-list-item');
199     }
200
201     public function test_update_code_language_favourite()
202     {
203         $editor = $this->users->editor();
204         $page = $this->entities->page();
205         $this->actingAs($editor);
206
207         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
208         $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
209
210         $resp = $this->get($page->getUrl('/edit'));
211         $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
212
213         $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
214         $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
215
216         $resp = $this->get($page->getUrl('/edit'));
217         $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);
218     }
219 }