5 use BookStack\Users\Models\Role;
6 use BookStack\Users\Models\User;
8 class HomepageTest extends TestCase
10 public function test_default_homepage_visible()
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');
20 public function test_custom_homepage()
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']);
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');
37 public function test_delete_custom_homepage()
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]);
44 'app-homepage' => $customPage->id,
45 'app-homepage-type' => 'page',
48 $homeVisit = $this->get('/');
49 $homeVisit->assertSee($name);
50 $this->withHtml($homeVisit)->assertElementNotExists('#home-default');
52 $pageDeleteReq = $this->delete($customPage->getUrl());
53 $pageDeleteReq->assertStatus(302);
54 $pageDeleteReq->assertRedirect($customPage->getUrl());
55 $pageDeleteReq->assertSessionHas('error');
56 $pageDeleteReq->assertSessionMissing('success');
58 $homeVisit = $this->get('/');
59 $homeVisit->assertSee($name);
60 $homeVisit->assertStatus(200);
63 public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
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]);
70 'app-homepage' => $customPage->id,
71 'app-homepage-type' => 'default',
74 $pageDeleteReq = $this->delete($customPage->getUrl());
75 $pageDeleteReq->assertStatus(302);
76 $pageDeleteReq->assertSessionHas('success');
77 $pageDeleteReq->assertSessionMissing('error');
80 public function test_custom_homepage_cannot_be_deleted_from_parent_deletion()
82 $page = $this->entities->page();
84 'app-homepage' => $page->id,
85 'app-homepage-type' => 'page',
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]);
93 $this->assertNull($page->deleted_at);
94 $this->assertNull($page->book->deleted_at);
97 public function test_custom_homepage_renders_includes()
100 $included = $this->entities->page();
101 $content = str_repeat('This is the body content of my custom homepage.', 20);
102 $included->html = $content;
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']);
110 $homeVisit = $this->get('/');
111 $homeVisit->assertSee($name);
112 $homeVisit->assertSee($content);
115 public function test_set_book_homepage()
117 $editor = $this->users->editor();
118 setting()->putUser($editor, 'books_view_type', 'grid');
120 $this->setSettings(['app-homepage-type' => 'books']);
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');
131 public function test_set_bookshelves_homepage()
133 $editor = $this->users->editor();
134 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
135 $shelf = $this->entities->shelf();
137 $this->setSettings(['app-homepage-type' => 'bookshelves']);
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);
147 public function test_books_and_bookshelves_homepage_has_expected_actions()
151 foreach (['bookshelves', 'books'] as $homepageType) {
152 $this->setSettings(['app-homepage-type' => $homepageType]);
154 $html = $this->withHtml($this->get('/'));
155 $html->assertElementContains('.actions button', 'Dark Mode');
156 $html->assertElementContains('.actions a[href$="/tags"]', 'View Tags');
160 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
162 $editor = $this->users->editor();
163 setting()->putUser($editor, 'bookshelves_view_type', 'list');
164 $this->setSettings(['app-homepage-type' => 'bookshelves']);
167 $shelf = $this->entities->shelf();
168 $book = $shelf->books()->first();
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);
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);
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);
189 public function test_new_users_dont_have_any_recently_viewed()
191 $user = User::factory()->create();
192 $viewRole = Role::getRole('Viewer');
193 $user->attachRole($viewRole);
195 $homeVisit = $this->actingAs($user)->get('/');
196 $this->withHtml($homeVisit)->assertElementContains('#recently-viewed', 'You have not viewed any pages');