]> BookStack Code Mirror - bookstack/blob - tests/HomepageTest.php
Applied StyleCI changes, added php/larastan to attribution
[bookstack] / tests / HomepageTest.php
1 <?php
2
3 namespace Tests;
4
5 use BookStack\Auth\Role;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Models\Page;
9
10 class HomepageTest extends TestCase
11 {
12     public function test_default_homepage_visible()
13     {
14         $this->asEditor();
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');
20     }
21
22     public function test_custom_homepage()
23     {
24         $this->asEditor();
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']);
30
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');
37     }
38
39     public function test_delete_custom_homepage()
40     {
41         $this->asEditor();
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]);
45         $this->setSettings([
46             'app-homepage'      => $customPage->id,
47             'app-homepage-type' => 'page',
48         ]);
49
50         $homeVisit = $this->get('/');
51         $homeVisit->assertSee($name);
52         $homeVisit->assertElementNotExists('#home-default');
53
54         $pageDeleteReq = $this->delete($customPage->getUrl());
55         $pageDeleteReq->assertStatus(302);
56         $pageDeleteReq->assertRedirect($customPage->getUrl());
57         $pageDeleteReq->assertSessionHas('error');
58         $pageDeleteReq->assertSessionMissing('success');
59
60         $homeVisit = $this->get('/');
61         $homeVisit->assertSee($name);
62         $homeVisit->assertStatus(200);
63     }
64
65     public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
66     {
67         $this->asEditor();
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]);
71         $this->setSettings([
72             'app-homepage'      => $customPage->id,
73             'app-homepage-type' => 'default',
74         ]);
75
76         $pageDeleteReq = $this->delete($customPage->getUrl());
77         $pageDeleteReq->assertStatus(302);
78         $pageDeleteReq->assertSessionHas('success');
79         $pageDeleteReq->assertSessionMissing('error');
80     }
81
82     public function test_custom_homepage_renders_includes()
83     {
84         $this->asEditor();
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;
89         $included->save();
90
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']);
95
96         $homeVisit = $this->get('/');
97         $homeVisit->assertSee($name);
98         $homeVisit->assertSee($content);
99     }
100
101     public function test_set_book_homepage()
102     {
103         $editor = $this->getEditor();
104         setting()->putUser($editor, 'books_view_type', 'grid');
105
106         $this->setSettings(['app-homepage-type' => 'books']);
107
108         $this->asEditor();
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');
115
116         $this->setSettings(['app-homepage-type' => false]);
117         $this->test_default_homepage_visible();
118     }
119
120     public function test_set_bookshelves_homepage()
121     {
122         $editor = $this->getEditor();
123         setting()->putUser($editor, 'bookshelves_view_type', 'grid');
124         $shelf = Bookshelf::query()->firstOrFail();
125
126         $this->setSettings(['app-homepage-type' => 'bookshelves']);
127
128         $this->asEditor();
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);
134
135         $this->setSettings(['app-homepage-type' => false]);
136         $this->test_default_homepage_visible();
137     }
138
139     public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
140     {
141         $editor = $this->getEditor();
142         setting()->putUser($editor, 'bookshelves_view_type', 'list');
143         $this->setSettings(['app-homepage-type' => 'bookshelves']);
144         $this->asEditor();
145
146         $shelf = Bookshelf::query()->first();
147         $book = $shelf->books()->first();
148
149         // Ensure initially visible
150         $homeVisit = $this->get('/');
151         $homeVisit->assertElementContains('.content-wrap', $shelf->name);
152         $homeVisit->assertElementContains('.content-wrap', $book->name);
153
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);
160
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);
166     }
167
168     public function test_new_users_dont_have_any_recently_viewed()
169     {
170         $user = User::factory()->create();
171         $viewRole = Role::getRole('Viewer');
172         $user->attachRole($viewRole);
173
174         $homeVisit = $this->actingAs($user)->get('/');
175         $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');
176     }
177 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.