]> BookStack Code Mirror - bookstack/blob - tests/User/UserMyAccountTest.php
My Account: Updated and started adding to tests
[bookstack] / tests / User / UserMyAccountTest.php
1 <?php
2
3 namespace Tests\User;
4
5 use BookStack\Activity\Tools\UserEntityWatchOptions;
6 use BookStack\Activity\WatchLevels;
7 use Tests\TestCase;
8
9 class UserMyAccountTest extends TestCase
10 {
11     public function test_index_view()
12     {
13         $resp = $this->asEditor()->get('/my-account');
14         $resp->assertRedirect('/my-account/profile');
15     }
16
17     public function test_views_not_accessible_to_guest_user()
18     {
19         $categories = ['profile', 'auth', 'shortcuts', 'notifications', ''];
20         $this->setSettings(['app-public' => 'true']);
21
22         $this->permissions->grantUserRolePermissions($this->users->guest(), ['receive-notifications']);
23
24         foreach ($categories as $category) {
25             $resp = $this->get('/my-account/' . $category);
26             $resp->assertRedirect('/');
27         }
28     }
29     public function test_interface_shortcuts_updating()
30     {
31         $this->asEditor();
32
33         // View preferences with defaults
34         $resp = $this->get('/my-account/shortcuts');
35         $resp->assertSee('UI Shortcut Preferences');
36
37         $html = $this->withHtml($resp);
38         $html->assertFieldHasValue('enabled', 'false');
39         $html->assertFieldHasValue('shortcut[home_view]', '1');
40
41         // Update preferences
42         $resp = $this->put('/my-account/shortcuts', [
43             'enabled' => 'true',
44             'shortcut' => ['home_view' => 'Ctrl + 1'],
45         ]);
46
47         $resp->assertRedirect('/my-account/shortcuts');
48         $resp->assertSessionHas('success', 'Shortcut preferences have been updated!');
49
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');
55     }
56
57     public function test_body_has_shortcuts_component_when_active()
58     {
59         $editor = $this->users->editor();
60         $this->actingAs($editor);
61
62         $this->withHtml($this->get('/'))->assertElementNotExists('body[component="shortcuts"]');
63
64         setting()->putUser($editor, 'ui-shortcuts-enabled', 'true');
65         $this->withHtml($this->get('/'))->assertElementExists('body[component="shortcuts"]');
66     }
67
68     public function test_notification_routes_requires_notification_permission()
69     {
70         $viewer = $this->users->viewer();
71         $resp = $this->actingAs($viewer)->get('/my-account/notifications');
72         $this->assertPermissionError($resp);
73
74         $resp = $this->actingAs($viewer)->get('/my-account/profile');
75         $resp->assertDontSeeText('Notification Preferences');
76
77         $resp = $this->put('/my-account/notifications');
78         $this->assertPermissionError($resp);
79
80         $this->permissions->grantUserRolePermissions($viewer, ['receive-notifications']);
81         $resp = $this->get('/my-account/notifications');
82         $resp->assertOk();
83         $resp->assertSee('Notification Preferences');
84     }
85
86     public function test_notification_preferences_updating()
87     {
88         $editor = $this->users->editor();
89
90         // View preferences with defaults
91         $resp = $this->actingAs($editor)->get('/my-account/notifications');
92         $resp->assertSee('Notification Preferences');
93
94         $html = $this->withHtml($resp);
95         $html->assertFieldHasValue('preferences[comment-replies]', 'false');
96
97         // Update preferences
98         $resp = $this->put('/my-account/notifications', [
99             'preferences' => ['comment-replies' => 'true'],
100         ]);
101
102         $resp->assertRedirect('/my-account/notifications');
103         $resp->assertSessionHas('success', 'Notification preferences have been updated!');
104
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');
109     }
110
111     public function test_notification_preferences_show_watches()
112     {
113         $editor = $this->users->editor();
114         $book = $this->entities->book();
115
116         $options = new UserEntityWatchOptions($editor, $book);
117         $options->updateLevelByValue(WatchLevels::COMMENTS);
118
119         $resp = $this->actingAs($editor)->get('/my-account/notifications');
120         $resp->assertSee($book->name);
121         $resp->assertSee('All Page Updates & Comments');
122
123         $options->updateLevelByValue(WatchLevels::DEFAULT);
124
125         $resp = $this->actingAs($editor)->get('/my-account/notifications');
126         $resp->assertDontSee($book->name);
127         $resp->assertDontSee('All Page Updates & Comments');
128     }
129
130     public function test_notification_preferences_dont_error_on_deleted_items()
131     {
132         $editor = $this->users->editor();
133         $book = $this->entities->book();
134
135         $options = new UserEntityWatchOptions($editor, $book);
136         $options->updateLevelByValue(WatchLevels::COMMENTS);
137
138         $this->actingAs($editor)->delete($book->getUrl());
139         $book->refresh();
140         $this->assertNotNull($book->deleted_at);
141
142         $resp = $this->actingAs($editor)->get('/my-account/notifications');
143         $resp->assertOk();
144         $resp->assertDontSee($book->name);
145     }
146
147     public function test_notification_preferences_not_accessible_to_guest()
148     {
149         $this->setSettings(['app-public' => 'true']);
150         $guest = $this->users->guest();
151         $this->permissions->grantUserRolePermissions($guest, ['receive-notifications']);
152
153         $resp = $this->get('/my-account/notifications');
154         $this->assertPermissionError($resp);
155
156         $resp = $this->put('/my-account/notifications', [
157             'preferences' => ['comment-replies' => 'true'],
158         ]);
159         $this->assertPermissionError($resp);
160     }
161
162     public function test_notification_comment_options_only_exist_if_comments_active()
163     {
164         $resp = $this->asEditor()->get('/my-account/notifications');
165         $resp->assertSee('Notify upon comments');
166         $resp->assertSee('Notify upon replies');
167
168         setting()->put('app-disable-comments', true);
169
170         $resp = $this->get('/my-account/notifications');
171         $resp->assertDontSee('Notify upon comments');
172         $resp->assertDontSee('Notify upon replies');
173     }
174 }