5 use BookStack\Auth\Role;
6 use BookStack\Auth\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->getEditor();
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');
130 $this->setSettings(['app-homepage-type' => false]);
131 $this->test_default_homepage_visible();
134 public function test_set_bookshelves_homepage()
136 $editor = $this->getEditor();
137 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
138 $shelf = $this->entities->shelf();
140 $this->setSettings(['app-homepage-type' => 'bookshelves']);
143 $homeVisit = $this->get('/');
144 $homeVisit->assertSee('Shelves');
145 $homeVisit->assertSee('grid-card-content');
146 $homeVisit->assertSee('featured-image-container');
147 $this->withHtml($homeVisit)->assertElementContains('.grid-card', $shelf->name);
149 $this->setSettings(['app-homepage-type' => false]);
150 $this->test_default_homepage_visible();
153 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
155 $editor = $this->getEditor();
156 setting()->putUser($editor, 'bookshelves_view_type', 'list');
157 $this->setSettings(['app-homepage-type' => 'bookshelves']);
160 $shelf = $this->entities->shelf();
161 $book = $shelf->books()->first();
163 // Ensure initially visible
164 $homeVisit = $this->get('/');
165 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
166 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $book->name);
168 // Ensure book no longer visible without view permission
169 $editor->roles()->detach();
170 $this->giveUserPermissions($editor, ['bookshelf-view-all']);
171 $homeVisit = $this->get('/');
172 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
173 $this->withHtml($homeVisit)->assertElementNotContains('.content-wrap', $book->name);
175 // Ensure is visible again with entity-level view permission
176 $this->entities->setPermissions($book, ['view'], [$editor->roles()->first()]);
177 $homeVisit = $this->get('/');
178 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $shelf->name);
179 $this->withHtml($homeVisit)->assertElementContains('.content-wrap', $book->name);
182 public function test_new_users_dont_have_any_recently_viewed()
184 $user = User::factory()->create();
185 $viewRole = Role::getRole('Viewer');
186 $user->attachRole($viewRole);
188 $homeVisit = $this->actingAs($user)->get('/');
189 $this->withHtml($homeVisit)->assertElementContains('#recently-viewed', 'You have not viewed any pages');