3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Page;
8 class HomepageTest extends TestCase
11 public function test_default_homepage_visible()
14 $homeVisit = $this->get('/');
15 $homeVisit->assertSee('My Recently Viewed');
16 $homeVisit->assertSee('Recently Updated Pages');
17 $homeVisit->assertSee('Recent Activity');
18 $homeVisit->assertSee('home-default');
21 public function test_custom_homepage()
24 $name = 'My custom homepage';
25 $content = str_repeat('This is the body content of my custom homepage.', 20);
26 $customPage = $this->newPage(['name' => $name, 'html' => $content]);
27 $this->setSettings(['app-homepage' => $customPage->id]);
28 $this->setSettings(['app-homepage-type' => 'page']);
30 $homeVisit = $this->get('/');
31 $homeVisit->assertSee($name);
32 $homeVisit->assertSee($content);
33 $homeVisit->assertSee('My Recently Viewed');
34 $homeVisit->assertSee('Recently Updated Pages');
35 $homeVisit->assertSee('Recent Activity');
38 public function test_delete_custom_homepage()
41 $name = 'My custom homepage';
42 $content = str_repeat('This is the body content of my custom homepage.', 20);
43 $customPage = $this->newPage(['name' => $name, 'html' => $content]);
45 'app-homepage' => $customPage->id,
46 'app-homepage-type' => 'page'
49 $homeVisit = $this->get('/');
50 $homeVisit->assertSee($name);
51 $homeVisit->assertElementNotExists('#home-default');
53 $pageDeleteReq = $this->delete($customPage->getUrl());
54 $pageDeleteReq->assertStatus(302);
55 $pageDeleteReq->assertRedirect($customPage->getUrl());
56 $pageDeleteReq->assertSessionHas('error');
57 $pageDeleteReq->assertSessionMissing('success');
59 $homeVisit = $this->get('/');
60 $homeVisit->assertSee($name);
61 $homeVisit->assertStatus(200);
64 public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
67 $name = 'My custom homepage';
68 $content = str_repeat('This is the body content of my custom homepage.', 20);
69 $customPage = $this->newPage(['name' => $name, 'html' => $content]);
71 'app-homepage' => $customPage->id,
72 'app-homepage-type' => 'default'
75 $pageDeleteReq = $this->delete($customPage->getUrl());
76 $pageDeleteReq->assertStatus(302);
77 $pageDeleteReq->assertSessionHas('success');
78 $pageDeleteReq->assertSessionMissing('error');
81 public function test_set_book_homepage()
83 $editor = $this->getEditor();
84 setting()->putUser($editor, 'books_view_type', 'grid');
86 $this->setSettings(['app-homepage-type' => 'books']);
89 $homeVisit = $this->get('/');
90 $homeVisit->assertSee('Books');
91 $homeVisit->assertSee('grid-card');
92 $homeVisit->assertSee('grid-card-content');
93 $homeVisit->assertSee('grid-card-footer');
94 $homeVisit->assertSee('featured-image-container');
96 $this->setSettings(['app-homepage-type' => false]);
97 $this->test_default_homepage_visible();
100 public function test_set_bookshelves_homepage()
102 $editor = $this->getEditor();
103 setting()->putUser($editor, 'bookshelves_view_type', 'grid');
104 $shelf = Bookshelf::query()->firstOrFail();
106 $this->setSettings(['app-homepage-type' => 'bookshelves']);
109 $homeVisit = $this->get('/');
110 $homeVisit->assertSee('Shelves');
111 $homeVisit->assertSee('grid-card-content');
112 $homeVisit->assertSee('featured-image-container');
113 $homeVisit->assertElementContains('.grid-card', $shelf->name);
115 $this->setSettings(['app-homepage-type' => false]);
116 $this->test_default_homepage_visible();
119 public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
121 $editor = $this->getEditor();
122 setting()->putUser($editor, 'bookshelves_view_type', 'list');
123 $this->setSettings(['app-homepage-type' => 'bookshelves']);
126 $shelf = Bookshelf::query()->first();
127 $book = $shelf->books()->first();
129 // Ensure initially visible
130 $homeVisit = $this->get('/');
131 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
132 $homeVisit->assertElementContains('.content-wrap', $book->name);
134 // Ensure book no longer visible without view permission
135 $editor->roles()->detach();
136 $this->giveUserPermissions($editor, ['bookshelf-view-all']);
137 $homeVisit = $this->get('/');
138 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
139 $homeVisit->assertElementNotContains('.content-wrap', $book->name);
141 // Ensure is visible again with entity-level view permission
142 $this->setEntityRestrictions($book, ['view'], [$editor->roles()->first()]);
143 $homeVisit = $this->get('/');
144 $homeVisit->assertElementContains('.content-wrap', $shelf->name);
145 $homeVisit->assertElementContains('.content-wrap', $book->name);
148 public function test_new_users_dont_have_any_recently_viewed()
150 $user = factory(User::class)->create();
151 $viewRole = Role::getRole('Viewer');
152 $user->attachRole($viewRole);
154 $homeVisit = $this->actingAs($user)->get('/');
155 $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');