5 use BookStack\Activity\Tools\UserEntityWatchOptions;
6 use BookStack\Activity\WatchLevels;
9 class UserMyAccountTest extends TestCase
11 public function test_index_view()
13 $resp = $this->asEditor()->get('/my-account');
14 $resp->assertRedirect('/my-account/profile');
17 public function test_views_not_accessible_to_guest_user()
19 $categories = ['profile', 'auth', 'shortcuts', 'notifications', ''];
20 $this->setSettings(['app-public' => 'true']);
22 $this->permissions->grantUserRolePermissions($this->users->guest(), ['receive-notifications']);
24 foreach ($categories as $category) {
25 $resp = $this->get('/my-account/' . $category);
26 $resp->assertRedirect('/');
29 public function test_interface_shortcuts_updating()
33 // View preferences with defaults
34 $resp = $this->get('/my-account/shortcuts');
35 $resp->assertSee('UI Shortcut Preferences');
37 $html = $this->withHtml($resp);
38 $html->assertFieldHasValue('enabled', 'false');
39 $html->assertFieldHasValue('shortcut[home_view]', '1');
42 $resp = $this->put('/my-account/shortcuts', [
44 'shortcut' => ['home_view' => 'Ctrl + 1'],
47 $resp->assertRedirect('/my-account/shortcuts');
48 $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
50 // View updates to preferences page
51 $resp = $this->get('/my-account/shortcuts');
52 $html = $this->withHtml($resp);
53 $html->assertFieldHasValue('enabled', 'true');
54 $html->assertFieldHasValue('shortcut[home_view]', 'Ctrl + 1');
57 public function test_body_has_shortcuts_component_when_active()
59 $editor = $this->users->editor();
60 $this->actingAs($editor);
62 $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
64 setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
65 $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
68 public function test_notification_routes_requires_notification_permission()
70 $viewer = $this->users->viewer();
71 $resp = $this->actingAs($viewer)->get('/my-account/notifications');
72 $this->assertPermissionError($resp);
74 $resp = $this->actingAs($viewer)->get('/my-account/profile');
75 $resp->assertDontSeeText('Notification Preferences');
77 $resp = $this->put('/my-account/notifications');
78 $this->assertPermissionError($resp);
80 $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
81 $resp = $this->get('/my-account/notifications');
83 $resp->assertSee('Notification Preferences');
86 public function test_notification_preferences_updating()
88 $editor = $this->users->editor();
90 // View preferences with defaults
91 $resp = $this->actingAs($editor)->get('/my-account/notifications');
92 $resp->assertSee('Notification Preferences');
94 $html = $this->withHtml($resp);
95 $html->assertFieldHasValue('preferences[comment-replies]', 'false');
98 $resp = $this->put('/my-account/notifications', [
99 'preferences' => ['comment-replies' => 'true'],
102 $resp->assertRedirect('/my-account/notifications');
103 $resp->assertSessionHas('success', 'Notification preferences have been updated!');
105 // View updates to preferences page
106 $resp = $this->get('/my-account/notifications');
107 $html = $this->withHtml($resp);
108 $html->assertFieldHasValue('preferences[comment-replies]', 'true');
111 public function test_notification_preferences_show_watches()
113 $editor = $this->users->editor();
114 $book = $this->entities->book();
116 $options = new UserEntityWatchOptions($editor, $book);
117 $options->updateLevelByValue(WatchLevels::COMMENTS);
119 $resp = $this->actingAs($editor)->get('/my-account/notifications');
120 $resp->assertSee($book->name);
121 $resp->assertSee('All Page Updates & Comments');
123 $options->updateLevelByValue(WatchLevels::DEFAULT);
125 $resp = $this->actingAs($editor)->get('/my-account/notifications');
126 $resp->assertDontSee($book->name);
127 $resp->assertDontSee('All Page Updates & Comments');
130 public function test_notification_preferences_dont_error_on_deleted_items()
132 $editor = $this->users->editor();
133 $book = $this->entities->book();
135 $options = new UserEntityWatchOptions($editor, $book);
136 $options->updateLevelByValue(WatchLevels::COMMENTS);
138 $this->actingAs($editor)->delete($book->getUrl());
140 $this->assertNotNull($book->deleted_at);
142 $resp = $this->actingAs($editor)->get('/my-account/notifications');
144 $resp->assertDontSee($book->name);
147 public function test_notification_preferences_not_accessible_to_guest()
149 $this->setSettings(['app-public' => 'true']);
150 $guest = $this->users->guest();
151 $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
153 $resp = $this->get('/my-account/notifications');
154 $this->assertPermissionError($resp);
156 $resp = $this->put('/my-account/notifications', [
157 'preferences' => ['comment-replies' => 'true'],
159 $this->assertPermissionError($resp);
162 public function test_notification_comment_options_only_exist_if_comments_active()
164 $resp = $this->asEditor()->get('/my-account/notifications');
165 $resp->assertSee('Notify upon comments');
166 $resp->assertSee('Notify upon replies');
168 setting()->put('app-disable-comments', true);
170 $resp = $this->get('/my-account/notifications');
171 $resp->assertDontSee('Notify upon comments');
172 $resp->assertDontSee('Notify upon replies');