5 use BookStack\Auth\Role;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Page;
10 class HomepageTest extends TestCase
12 public function test_default_homepage_visible()
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');
22 public function test_custom_homepage()
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']);
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');
39 public function test_delete_custom_homepage()
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]);
46 'app-homepage' => $customPage->id,
47 'app-homepage-type' => 'page',
50 $homeVisit = $this->get('/');
51 $homeVisit->assertSee($name);
52 $homeVisit->assertElementNotExists('#home-default');
54 $pageDeleteReq = $this->delete($customPage->getUrl());
55 $pageDeleteReq->assertStatus(302);
56 $pageDeleteReq->assertRedirect($customPage->getUrl());
57 $pageDeleteReq->assertSessionHas('error');
58 $pageDeleteReq->assertSessionMissing('success');
60 $homeVisit = $this->get('/');
61 $homeVisit->assertSee($name);
62 $homeVisit->assertStatus(200);
65 public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
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]);
72 'app-homepage' => $customPage->id,
73 'app-homepage-type' => 'default',
76 $pageDeleteReq = $this->delete($customPage->getUrl());
77 $pageDeleteReq->assertStatus(302);
78 $pageDeleteReq->assertSessionHas('success');
79 $pageDeleteReq->assertSessionMissing('error');
82 public function test_custom_homepage_cannot_be_deleted_from_parent_deletion()
84 /** @var Page $page */
85 $page = Page::query()->first();
87 'app-homepage' => $page->id,
88 'app-homepage-type' => 'page',
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]);
96 $this->assertNull($page->deleted_at);
97 $this->assertNull($page->book->deleted_at);
100 public function test_custom_homepage_renders_includes()
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;
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']);
114 $homeVisit = $this->get('/');
115 $homeVisit->assertSee($name);
116 $homeVisit->assertSee($content);
119 public function test_set_book_homepage()
121 $editor = $this->getEditor();
122 setting()->putUser($editor, 'books_view_type', 'grid');
124 $this->setSettings(['app-homepage-type' => 'books']);
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');
134 $this->setSettings(['app-homepage-type' => false]);
135 $this->test_default_homepage_visible();
138 public function test_set_bookshelves_homepage()
140 $editor = $this->getEditor();
141 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
142 $shelf = Bookshelf::query()->firstOrFail();
144 $this->setSettings(['app-homepage-type' => 'bookshelves']);
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);
153 $this->setSettings(['app-homepage-type' => false]);
154 $this->test_default_homepage_visible();
157 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
159 $editor = $this->getEditor();
160 setting()->putUser($editor, 'bookshelves_view_type', 'list');
161 $this->setSettings(['app-homepage-type' => 'bookshelves']);
164 $shelf = Bookshelf::query()->first();
165 $book = $shelf->books()->first();
167 // Ensure initially visible
168 $homeVisit = $this->get('/');
169 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
170 $homeVisit->assertElementContains('.content-wrap', $book->name);
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);
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);
186 public function test_new_users_dont_have_any_recently_viewed()
188 $user = User::factory()->create();
189 $viewRole = Role::getRole('Viewer');
190 $user->attachRole($viewRole);
192 $homeVisit = $this->actingAs($user)->get('/');
193 $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');