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->entities->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->entities->newPage(['name' => $name, 'html' => $content]);
46 'app-homepage' => $customPage->id,
47 'app-homepage-type' => 'page',
50 $homeVisit = $this->get('/');
51 $homeVisit->assertSee($name);
52 $this->withHtml($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->entities->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 $page = $this->entities->page();
86 'app-homepage' => $page->id,
87 'app-homepage-type' => 'page',
90 $this->asEditor()->delete($page->book->getUrl());
91 $this->assertSessionError('Cannot delete a page while it is set as a homepage');
92 $this->assertDatabaseMissing('deletions', ['deletable_id' => $page->book->id]);
95 $this->assertNull($page->deleted_at);
96 $this->assertNull($page->book->deleted_at);
99 public function test_custom_homepage_renders_includes()
102 /** @var Page $included */
103 $included = Page::query()->first();
104 $content = str_repeat('This is the body content of my custom homepage.', 20);
105 $included->html = $content;
108 $name = 'My custom homepage';
109 $customPage = $this->entities->newPage(['name' => $name, 'html' => '{{@' . $included->id . '}}']);
110 $this->setSettings(['app-homepage' => $customPage->id]);
111 $this->setSettings(['app-homepage-type' => 'page']);
113 $homeVisit = $this->get('/');
114 $homeVisit->assertSee($name);
115 $homeVisit->assertSee($content);
118 public function test_set_book_homepage()
120 $editor = $this->getEditor();
121 setting()->putUser($editor, 'books_view_type', 'grid');
123 $this->setSettings(['app-homepage-type' => 'books']);
126 $homeVisit = $this->get('/');
127 $homeVisit->assertSee('Books');
128 $homeVisit->assertSee('grid-card');
129 $homeVisit->assertSee('grid-card-content');
130 $homeVisit->assertSee('grid-card-footer');
131 $homeVisit->assertSee('featured-image-container');
133 $this->setSettings(['app-homepage-type' => false]);
134 $this->test_default_homepage_visible();
137 public function test_set_bookshelves_homepage()
139 $editor = $this->getEditor();
140 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
141 $shelf = Bookshelf::query()->firstOrFail();
143 $this->setSettings(['app-homepage-type' => 'bookshelves']);
146 $homeVisit = $this->get('/');
147 $homeVisit->assertSee('Shelves');
148 $homeVisit->assertSee('grid-card-content');
149 $homeVisit->assertSee('featured-image-container');
150 $this->withHtml($homeVisit)->assertElementContains('.grid-card', $shelf->name);
152 $this->setSettings(['app-homepage-type' => false]);
153 $this->test_default_homepage_visible();
156 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
158 $editor = $this->getEditor();
159 setting()->putUser($editor, 'bookshelves_view_type', 'list');
160 $this->setSettings(['app-homepage-type' => 'bookshelves']);
163 $shelf = $this->entities->shelf();
164 $book = $shelf->books()->first();
166 // Ensure initially visible
167 $homeVisit = $this->get('/');
168 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
169 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $book->name);
171 // Ensure book no longer visible without view permission
172 $editor->roles()->detach();
173 $this->giveUserPermissions($editor, ['bookshelf-view-all']);
174 $homeVisit = $this->get('/');
175 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
176 $this->withHtml($homeVisit)->assertElementNotContains('.content-wrap', $book->name);
178 // Ensure is visible again with entity-level view permission
179 $this->entities->setPermissions($book, ['view'], [$editor->roles()->first()]);
180 $homeVisit = $this->get('/');
181 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
182 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $book->name);
185 public function test_new_users_dont_have_any_recently_viewed()
187 $user = User::factory()->create();
188 $viewRole = Role::getRole('Viewer');
189 $user->attachRole($viewRole);
191 $homeVisit = $this->actingAs($user)->get('/');
192 $this->withHtml($homeVisit)->assertElementContains('#recently-viewed', 'You have not viewed any pages');