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_renders_includes()
85 /** @var Page $included */
86 $included = Page::query()->first();
87 $content = str_repeat('This is the body content of my custom homepage.', 20);
88 $included->html = $content;
91 $name = 'My custom homepage';
92 $customPage = $this->newPage(['name' => $name, 'html' => '{{@' . $included->id . '}}']);
93 $this->setSettings(['app-homepage' => $customPage->id]);
94 $this->setSettings(['app-homepage-type' => 'page']);
96 $homeVisit = $this->get('/');
97 $homeVisit->assertSee($name);
98 $homeVisit->assertSee($content);
101 public function test_set_book_homepage()
103 $editor = $this->getEditor();
104 setting()->putUser($editor, 'books_view_type', 'grid');
106 $this->setSettings(['app-homepage-type' => 'books']);
109 $homeVisit = $this->get('/');
110 $homeVisit->assertSee('Books');
111 $homeVisit->assertSee('grid-card');
112 $homeVisit->assertSee('grid-card-content');
113 $homeVisit->assertSee('grid-card-footer');
114 $homeVisit->assertSee('featured-image-container');
116 $this->setSettings(['app-homepage-type' => false]);
117 $this->test_default_homepage_visible();
120 public function test_set_bookshelves_homepage()
122 $editor = $this->getEditor();
123 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
124 $shelf = Bookshelf::query()->firstOrFail();
126 $this->setSettings(['app-homepage-type' => 'bookshelves']);
129 $homeVisit = $this->get('/');
130 $homeVisit->assertSee('Shelves');
131 $homeVisit->assertSee('grid-card-content');
132 $homeVisit->assertSee('featured-image-container');
133 $homeVisit->assertElementContains('.grid-card', $shelf->name);
135 $this->setSettings(['app-homepage-type' => false]);
136 $this->test_default_homepage_visible();
139 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
141 $editor = $this->getEditor();
142 setting()->putUser($editor, 'bookshelves_view_type', 'list');
143 $this->setSettings(['app-homepage-type' => 'bookshelves']);
146 $shelf = Bookshelf::query()->first();
147 $book = $shelf->books()->first();
149 // Ensure initially visible
150 $homeVisit = $this->get('/');
151 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
152 $homeVisit->assertElementContains('.content-wrap', $book->name);
154 // Ensure book no longer visible without view permission
155 $editor->roles()->detach();
156 $this->giveUserPermissions($editor, ['bookshelf-view-all']);
157 $homeVisit = $this->get('/');
158 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
159 $homeVisit->assertElementNotContains('.content-wrap', $book->name);
161 // Ensure is visible again with entity-level view permission
162 $this->setEntityRestrictions($book, ['view'], [$editor->roles()->first()]);
163 $homeVisit = $this->get('/');
164 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
165 $homeVisit->assertElementContains('.content-wrap', $book->name);
168 public function test_new_users_dont_have_any_recently_viewed()
170 $user = User::factory()->create();
171 $viewRole = Role::getRole('Viewer');
172 $user->attachRole($viewRole);
174 $homeVisit = $this->actingAs($user)->get('/');
175 $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');