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->getEditor();
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_update_sort_preference()
50 $editor = $this->getEditor();
51 $this->actingAs($editor);
53 $updateRequest = $this->patch('/preferences/change-sort/books', [
54 'sort' => 'created_at',
57 $updateRequest->assertStatus(302);
59 $this->assertDatabaseHas('settings', [
60 'setting_key' => 'user:' . $editor->id . ':books_sort',
61 'value' => 'created_at',
63 $this->assertDatabaseHas('settings', [
64 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
67 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
68 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
71 public function test_update_sort_bad_entity_type_handled()
73 $editor = $this->getEditor();
74 $this->actingAs($editor);
76 $updateRequest = $this->patch('/preferences/change-sort/dogs', [
80 $updateRequest->assertStatus(500);
82 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
83 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
86 public function test_update_expansion_preference()
88 $editor = $this->getEditor();
89 $this->actingAs($editor);
91 $updateRequest = $this->patch('/preferences/change-expansion/home-details', ['expand' => 'true']);
92 $updateRequest->assertStatus(204);
94 $this->assertDatabaseHas('settings', [
95 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
98 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
100 $invalidKeyRequest = $this->patch('/preferences/change-expansion/my-home-details', ['expand' => 'true']);
101 $invalidKeyRequest->assertStatus(500);
104 public function test_toggle_dark_mode()
106 $home = $this->actingAs($this->getEditor())->get('/');
107 $home->assertSee('Dark Mode');
108 $this->withHtml($home)->assertElementNotExists('.dark-mode');
110 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
111 $prefChange = $this->patch('/preferences/toggle-dark-mode');
112 $prefChange->assertRedirect();
113 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
115 $home = $this->actingAs($this->getEditor())->get('/');
116 $this->withHtml($home)->assertElementExists('.dark-mode');
117 $home->assertDontSee('Dark Mode');
118 $home->assertSee('Light Mode');
121 public function test_dark_mode_defaults_to_config_option()
123 config()->set('setting-defaults.user.dark-mode-enabled', false);
124 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
125 $home = $this->get('/login');
126 $this->withHtml($home)->assertElementNotExists('.dark-mode');
128 config()->set('setting-defaults.user.dark-mode-enabled', true);
129 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
130 $home = $this->get('/login');
131 $this->withHtml($home)->assertElementExists('.dark-mode');
134 public function test_books_view_type_preferences_when_list()
136 $editor = $this->getEditor();
137 setting()->putUser($editor, 'books_view_type', 'list');
139 $resp = $this->actingAs($editor)->get('/books');
140 $this->withHtml($resp)
141 ->assertElementNotExists('.featured-image-container')
142 ->assertElementExists('.content-wrap .entity-list-item');
145 public function test_books_view_type_preferences_when_grid()
147 $editor = $this->getEditor();
148 setting()->putUser($editor, 'books_view_type', 'grid');
150 $resp = $this->actingAs($editor)->get('/books');
151 $this->withHtml($resp)->assertElementExists('.featured-image-container');
154 public function test_shelf_view_type_change()
156 $editor = $this->getEditor();
157 $shelf = $this->entities->shelf();
158 setting()->putUser($editor, 'bookshelf_view_type', 'list');
160 $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
161 $this->withHtml($resp)
162 ->assertElementNotExists('.featured-image-container')
163 ->assertElementExists('.content-wrap .entity-list-item');
165 $req = $this->patch("/preferences/change-view/bookshelf", ['view' => 'grid']);
166 $req->assertRedirect($shelf->getUrl());
168 $resp = $this->actingAs($editor)->get($shelf->getUrl())
169 ->assertSee('List View');
171 $this->withHtml($resp)
172 ->assertElementExists('.featured-image-container')
173 ->assertElementNotExists('.content-wrap .entity-list-item');
176 public function test_update_code_language_favourite()
178 $editor = $this->getEditor();
179 $page = $this->entities->page();
180 $this->actingAs($editor);
182 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => true]);
183 $this->patch('/preferences/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
185 $resp = $this->get($page->getUrl('/edit'));
186 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
188 $this->patch('/preferences/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
189 $this->patch('/preferences/update-code-language-favourite', ['language' => 'php', 'active' => false]);
191 $resp = $this->get($page->getUrl('/edit'));
192 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);
195 public function test_update_boolean()
197 $editor = $this->getEditor();
199 $this->assertTrue(setting()->getUser($editor, 'md-show-preview'));
201 $resp = $this->actingAs($editor)->patch('/preferences/update-boolean', ['name' => 'md-show-preview', 'value' => 'false']);
202 $resp->assertStatus(204);
204 $this->assertFalse(setting()->getUser($editor, 'md-show-preview'));
207 public function test_update_boolean_rejects_unfamiliar_key()
209 $resp = $this->asEditor()->patch('/preferences/update-boolean', ['name' => 'md-donkey-show', 'value' => 'false']);
210 $resp->assertStatus(500);