5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Page;
9 class UserPreferencesTest extends TestCase
11 public function test_update_sort_preference()
13 $editor = $this->getEditor();
14 $this->actingAs($editor);
16 $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/books', [
17 'sort' => 'created_at',
20 $updateRequest->assertStatus(302);
22 $this->assertDatabaseHas('settings', [
23 'setting_key' => 'user:' . $editor->id . ':books_sort',
24 'value' => 'created_at',
26 $this->assertDatabaseHas('settings', [
27 'setting_key' => 'user:' . $editor->id . ':books_sort_order',
30 $this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
31 $this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
34 public function test_update_sort_preference_defaults()
36 $editor = $this->getEditor();
37 $this->actingAs($editor);
39 $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/bookshelves', [
43 $updateRequest->assertStatus(302);
45 $this->assertEquals('name', setting()->getForCurrentUser('bookshelves_sort'));
46 $this->assertEquals('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
49 public function test_update_sort_bad_entity_type_handled()
51 $editor = $this->getEditor();
52 $this->actingAs($editor);
54 $updateRequest = $this->patch('/settings/users/' . $editor->id . '/change-sort/dogs', [
58 $updateRequest->assertStatus(500);
60 $this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
61 $this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
64 public function test_update_expansion_preference()
66 $editor = $this->getEditor();
67 $this->actingAs($editor);
69 $updateRequest = $this->patch('/settings/users/' . $editor->id . '/update-expansion-preference/home-details', ['expand' => 'true']);
70 $updateRequest->assertStatus(204);
72 $this->assertDatabaseHas('settings', [
73 'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
76 $this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
78 $invalidKeyRequest = $this->patch('/settings/users/' . $editor->id . '/update-expansion-preference/my-home-details', ['expand' => 'true']);
79 $invalidKeyRequest->assertStatus(500);
82 public function test_toggle_dark_mode()
84 $home = $this->actingAs($this->getEditor())->get('/');
85 $home->assertSee('Dark Mode');
86 $this->withHtml($home)->assertElementNotExists('.dark-mode');
88 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled', false));
89 $prefChange = $this->patch('/settings/users/toggle-dark-mode');
90 $prefChange->assertRedirect();
91 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
93 $home = $this->actingAs($this->getEditor())->get('/');
94 $this->withHtml($home)->assertElementExists('.dark-mode');
95 $home->assertDontSee('Dark Mode');
96 $home->assertSee('Light Mode');
99 public function test_dark_mode_defaults_to_config_option()
101 config()->set('setting-defaults.user.dark-mode-enabled', false);
102 $this->assertEquals(false, setting()->getForCurrentUser('dark-mode-enabled'));
103 $home = $this->get('/login');
104 $this->withHtml($home)->assertElementNotExists('.dark-mode');
106 config()->set('setting-defaults.user.dark-mode-enabled', true);
107 $this->assertEquals(true, setting()->getForCurrentUser('dark-mode-enabled'));
108 $home = $this->get('/login');
109 $this->withHtml($home)->assertElementExists('.dark-mode');
112 public function test_books_view_type_preferences_when_list()
114 $editor = $this->getEditor();
115 setting()->putUser($editor, 'books_view_type', 'list');
117 $resp = $this->actingAs($editor)->get('/books');
118 $this->withHtml($resp)
119 ->assertElementNotExists('.featured-image-container')
120 ->assertElementExists('.content-wrap .entity-list-item');
123 public function test_books_view_type_preferences_when_grid()
125 $editor = $this->getEditor();
126 setting()->putUser($editor, 'books_view_type', 'grid');
128 $resp = $this->actingAs($editor)->get('/books');
129 $this->withHtml($resp)->assertElementExists('.featured-image-container');
132 public function test_shelf_view_type_change()
134 $editor = $this->getEditor();
135 /** @var Bookshelf $shelf */
136 $shelf = Bookshelf::query()->first();
137 setting()->putUser($editor, 'bookshelf_view_type', 'list');
139 $resp = $this->actingAs($editor)->get($shelf->getUrl())->assertSee('Grid View');
140 $this->withHtml($resp)
141 ->assertElementNotExists('.featured-image-container')
142 ->assertElementExists('.content-wrap .entity-list-item');
144 $req = $this->patch("/settings/users/{$editor->id}/switch-shelf-view", ['view_type' => 'grid']);
145 $req->assertRedirect($shelf->getUrl());
147 $resp = $this->actingAs($editor)->get($shelf->getUrl())
148 ->assertSee('List View');
150 $this->withHtml($resp)
151 ->assertElementExists('.featured-image-container')
152 ->assertElementNotExists('.content-wrap .entity-list-item');
155 public function test_update_code_language_favourite()
157 $editor = $this->getEditor();
158 $page = Page::query()->first();
159 $this->actingAs($editor);
161 $this->patch('/settings/users/update-code-language-favourite', ['language' => 'php', 'active' => true]);
162 $this->patch('/settings/users/update-code-language-favourite', ['language' => 'javascript', 'active' => true]);
164 $resp = $this->get($page->getUrl('/edit'));
165 $resp->assertSee('option:code-editor:favourites="php,javascript"', false);
167 $this->patch('/settings/users/update-code-language-favourite', ['language' => 'ruby', 'active' => true]);
168 $this->patch('/settings/users/update-code-language-favourite', ['language' => 'php', 'active' => false]);
170 $resp = $this->get($page->getUrl('/edit'));
171 $resp->assertSee('option:code-editor:favourites="javascript,ruby"', false);