]> BookStack Code Mirror - bookstack/blob - tests/HomepageTest.php
Applied StyleCI changes
[bookstack] / tests / HomepageTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Auth\Role;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Page;
9
10 class HomepageTest extends TestCase
11 {
12     public function test_default_homepage_visible()
13     {
14         $this->asEditor();
15         $homeVisit = $this->get('/');
16         $homeVisit->assertSee('My Recently Viewed');
17         $homeVisit->assertSee('Recently Updated Pages');
18         $homeVisit->assertSee('Recent Activity');
19         $homeVisit->assertSee('home-default');
20     }
21
22     public function test_custom_homepage()
23     {
24         $this->asEditor();
25         $name = 'My custom homepage';
26         $content = str_repeat('This is the body content of my custom homepage.', 20);
27         $customPage = $this->newPage(['name' => $name, 'html' => $content]);
28         $this->setSettings(['app-homepage' => $customPage->id]);
29         $this->setSettings(['app-homepage-type' => 'page']);
30
31         $homeVisit = $this->get('/');
32         $homeVisit->assertSee($name);
33         $homeVisit->assertSee($content);
34         $homeVisit->assertSee('My Recently Viewed');
35         $homeVisit->assertSee('Recently Updated Pages');
36         $homeVisit->assertSee('Recent Activity');
37     }
38
39     public function test_delete_custom_homepage()
40     {
41         $this->asEditor();
42         $name = 'My custom homepage';
43         $content = str_repeat('This is the body content of my custom homepage.', 20);
44         $customPage = $this->newPage(['name' => $name, 'html' => $content]);
45         $this->setSettings([
46             'app-homepage'      => $customPage->id,
47             'app-homepage-type' => 'page',
48         ]);
49
50         $homeVisit = $this->get('/');
51         $homeVisit->assertSee($name);
52         $homeVisit->assertElementNotExists('#home-default');
53
54         $pageDeleteReq = $this->delete($customPage->getUrl());
55         $pageDeleteReq->assertStatus(302);
56         $pageDeleteReq->assertRedirect($customPage->getUrl());
57         $pageDeleteReq->assertSessionHas('error');
58         $pageDeleteReq->assertSessionMissing('success');
59
60         $homeVisit = $this->get('/');
61         $homeVisit->assertSee($name);
62         $homeVisit->assertStatus(200);
63     }
64
65     public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
66     {
67         $this->asEditor();
68         $name = 'My custom homepage';
69         $content = str_repeat('This is the body content of my custom homepage.', 20);
70         $customPage = $this->newPage(['name' => $name, 'html' => $content]);
71         $this->setSettings([
72             'app-homepage'      => $customPage->id,
73             'app-homepage-type' => 'default',
74         ]);
75
76         $pageDeleteReq = $this->delete($customPage->getUrl());
77         $pageDeleteReq->assertStatus(302);
78         $pageDeleteReq->assertSessionHas('success');
79         $pageDeleteReq->assertSessionMissing('error');
80     }
81
82     public function test_custom_homepage_cannot_be_deleted_from_parent_deletion()
83     {
84         /** @var Page $page */
85         $page = Page::query()->first();
86         $this->setSettings([
87             'app-homepage'      => $page->id,
88             'app-homepage-type' => 'page',
89         ]);
90
91         $this->asEditor()->delete($page->book->getUrl());
92         $this->assertSessionError('Cannot delete a page while it is set as a homepage');
93         $this->assertDatabaseMissing('deletions', ['deletable_id' => $page->book->id]);
94
95         $page->refresh();
96         $this->assertNull($page->deleted_at);
97         $this->assertNull($page->book->deleted_at);
98     }
99
100     public function test_custom_homepage_renders_includes()
101     {
102         $this->asEditor();
103         /** @var Page $included */
104         $included = Page::query()->first();
105         $content = str_repeat('This is the body content of my custom homepage.', 20);
106         $included->html = $content;
107         $included->save();
108
109         $name = 'My custom homepage';
110         $customPage = $this->newPage(['name' => $name, 'html' => '{{@' . $included->id . '}}']);
111         $this->setSettings(['app-homepage' => $customPage->id]);
112         $this->setSettings(['app-homepage-type' => 'page']);
113
114         $homeVisit = $this->get('/');
115         $homeVisit->assertSee($name);
116         $homeVisit->assertSee($content);
117     }
118
119     public function test_set_book_homepage()
120     {
121         $editor = $this->getEditor();
122         setting()->putUser($editor, 'books_view_type', 'grid');
123
124         $this->setSettings(['app-homepage-type' => 'books']);
125
126         $this->asEditor();
127         $homeVisit = $this->get('/');
128         $homeVisit->assertSee('Books');
129         $homeVisit->assertSee('grid-card');
130         $homeVisit->assertSee('grid-card-content');
131         $homeVisit->assertSee('grid-card-footer');
132         $homeVisit->assertSee('featured-image-container');
133
134         $this->setSettings(['app-homepage-type' => false]);
135         $this->test_default_homepage_visible();
136     }
137
138     public function test_set_bookshelves_homepage()
139     {
140         $editor = $this->getEditor();
141         setting()->putUser($editor, 'bookshelves_view_type', 'grid');
142         $shelf = Bookshelf::query()->firstOrFail();
143
144         $this->setSettings(['app-homepage-type' => 'bookshelves']);
145
146         $this->asEditor();
147         $homeVisit = $this->get('/');
148         $homeVisit->assertSee('Shelves');
149         $homeVisit->assertSee('grid-card-content');
150         $homeVisit->assertSee('featured-image-container');
151         $homeVisit->assertElementContains('.grid-card', $shelf->name);
152
153         $this->setSettings(['app-homepage-type' => false]);
154         $this->test_default_homepage_visible();
155     }
156
157     public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
158     {
159         $editor = $this->getEditor();
160         setting()->putUser($editor, 'bookshelves_view_type', 'list');
161         $this->setSettings(['app-homepage-type' => 'bookshelves']);
162         $this->asEditor();
163
164         $shelf = Bookshelf::query()->first();
165         $book = $shelf->books()->first();
166
167         // Ensure initially visible
168         $homeVisit = $this->get('/');
169         $homeVisit->assertElementContains('.content-wrap', $shelf->name);
170         $homeVisit->assertElementContains('.content-wrap', $book->name);
171
172         // Ensure book no longer visible without view permission
173         $editor->roles()->detach();
174         $this->giveUserPermissions($editor, ['bookshelf-view-all']);
175         $homeVisit = $this->get('/');
176         $homeVisit->assertElementContains('.content-wrap', $shelf->name);
177         $homeVisit->assertElementNotContains('.content-wrap', $book->name);
178
179         // Ensure is visible again with entity-level view permission
180         $this->setEntityRestrictions($book, ['view'], [$editor->roles()->first()]);
181         $homeVisit = $this->get('/');
182         $homeVisit->assertElementContains('.content-wrap', $shelf->name);
183         $homeVisit->assertElementContains('.content-wrap', $book->name);
184     }
185
186     public function test_new_users_dont_have_any_recently_viewed()
187     {
188         $user = User::factory()->create();
189         $viewRole = Role::getRole('Viewer');
190         $user->attachRole($viewRole);
191
192         $homeVisit = $this->actingAs($user)->get('/');
193         $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');
194     }
195 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.