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