7 class UserPreferencesTest extends TestCase
9 public function test_interface_shortcuts_updating()
13 // View preferences with defaults
14 $resp = $this->get('/preferences/shortcuts');
15 $resp->assertSee('Interface Keyboard Shortcuts');
17 $html = $this->withHtml($resp);
18 $html->assertFieldHasValue('enabled', 'false');
19 $html->assertFieldHasValue('shortcut[home_view]', '1');
22 $resp = $this->put('/preferences/shortcuts', [
24 'shortcut' => ['home_view' => 'Ctrl + 1'],
27 $resp->assertRedirect('/preferences/shortcuts');
28 $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
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');
37 public function test_body_has_shortcuts_component_when_active()
39 $editor = $this->users->editor();
40 $this->actingAs($editor);
42 $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
44 setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
45 $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
48 public function test_notification_preferences_updating()
52 // View preferences with defaults
53 $resp = $this->get('/preferences/notifications');
54 $resp->assertSee('Notification Preferences');
56 $html = $this->withHtml($resp);
57 $html->assertFieldHasValue('preferences[comment-replies]', 'false');
60 $resp = $this->put('/preferences/notifications', [
61 'preferences' => ['comment-replies' => 'true'],
64 $resp->assertRedirect('/preferences/notifications');
65 $resp->assertSessionHas('success', 'Notification preferences have been updated!');
67 // View updates to preferences page
68 $resp = $this->get('/preferences/notifications');
69 $html = $this->withHtml($resp);
70 $html->assertFieldHasValue('preferences[comment-replies]', 'true');
73 public function test_update_sort_preference()
75 $editor = $this->users->editor();
76 $this->actingAs($editor);
78 $updateRequest = $this->patch('/preferences/change-sort/books', [
79 'sort' => 'created_at',
82 $updateRequest->assertStatus(302);
84 $this->assertDatabaseHas('settings', [
85 'setting_key' => 'user:' . $editor->id . ':books_sort',
86 'value' => 'created_at',
88 $this->assertDatabaseHas('settings', [
89 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
92 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
93 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
96 public function test_update_sort_bad_entity_type_handled()
98 $editor = $this->users->editor();
99 $this->actingAs($editor);
101 $updateRequest = $this->patch('/preferences/change-sort/dogs', [
105 $updateRequest->assertStatus(500);
107 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
108 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
111 public function test_update_expansion_preference()
113 $editor = $this->users->editor();
114 $this->actingAs($editor);
116 $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
117 $updateRequest->assertStatus(204);
119 $this->assertDatabaseHas('settings', [
120 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
123 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
125 $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
126 $invalidKeyRequest->assertStatus(500);
129 public function test_toggle_dark_mode()
131 $home = $this->actingAs($this->users->editor())->get('/');
132 $home->assertSee('Dark Mode');
133 $this->withHtml($home)->assertElementNotExists('.dark-mode');
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'));
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');
146 public function test_dark_mode_defaults_to_config_option()
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');
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');
159 public function test_books_view_type_preferences_when_list()
161 $editor = $this->users->editor();
162 setting()->putUser($editor, 'books_view_type', 'list');
164 $resp = $this->actingAs($editor)->get('/books');
165 $this->withHtml($resp)
166 ->assertElementNotExists('.featured-image-container')
167 ->assertElementExists('.content-wrap .entity-list-item');
170 public function test_books_view_type_preferences_when_grid()
172 $editor = $this->users->editor();
173 setting()->putUser($editor, 'books_view_type', 'grid');
175 $resp = $this->actingAs($editor)->get('/books');
176 $this->withHtml($resp)->assertElementExists('.featured-image-container');
179 public function test_shelf_view_type_change()
181 $editor = $this->users->editor();
182 $shelf = $this->entities->shelf();
183 setting()->putUser($editor, 'bookshelf_view_type', 'list');
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');
190 $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
191 $req->assertRedirect($shelf->getUrl());
193 $resp = $this->actingAs($editor)->get($shelf->getUrl())
194 ->assertSee('List View');
196 $this->withHtml($resp)
197 ->assertElementExists('.featured-image-container')
198 ->assertElementNotExists('.content-wrap .entity-list-item');
201 public function test_update_code_language_favourite()
203 $editor = $this->users->editor();
204 $page = $this->entities->page();
205 $this->actingAs($editor);
207 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
208 $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
210 $resp = $this->get($page->getUrl('/edit'));
211 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
213 $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
214 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
216 $resp = $this->get($page->getUrl('/edit'));
217 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);