]> BookStack Code Mirror - bookstack/blob - tests/HomepageTest.php
Migrated much test entity usage via find/replace
[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->entities->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->entities->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         $this->withHtml($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->entities->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_cannot_be_deleted_from_parent_deletion()
83     {
84         $page = $this->entities->page();
85         $this->setSettings([
86             'app-homepage'      => $page->id,
87             'app-homepage-type' => 'page',
88         ]);
89
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]);
93
94         $page->refresh();
95         $this->assertNull($page->deleted_at);
96         $this->assertNull($page->book->deleted_at);
97     }
98
99     public function test_custom_homepage_renders_includes()
100     {
101         $this->asEditor();
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;
106         $included->save();
107
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']);
112
113         $homeVisit = $this->get('/');
114         $homeVisit->assertSee($name);
115         $homeVisit->assertSee($content);
116     }
117
118     public function test_set_book_homepage()
119     {
120         $editor = $this->getEditor();
121         setting()->putUser($editor, 'books_view_type', 'grid');
122
123         $this->setSettings(['app-homepage-type' => 'books']);
124
125         $this->asEditor();
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');
132
133         $this->setSettings(['app-homepage-type' => false]);
134         $this->test_default_homepage_visible();
135     }
136
137     public function test_set_bookshelves_homepage()
138     {
139         $editor = $this->getEditor();
140         setting()->putUser($editor, 'bookshelves_view_type', 'grid');
141         $shelf = Bookshelf::query()->firstOrFail();
142
143         $this->setSettings(['app-homepage-type' => 'bookshelves']);
144
145         $this->asEditor();
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);
151
152         $this->setSettings(['app-homepage-type' => false]);
153         $this->test_default_homepage_visible();
154     }
155
156     public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
157     {
158         $editor = $this->getEditor();
159         setting()->putUser($editor, 'bookshelves_view_type', 'list');
160         $this->setSettings(['app-homepage-type' => 'bookshelves']);
161         $this->asEditor();
162
163         $shelf = $this->entities->shelf();
164         $book = $shelf->books()->first();
165
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);
170
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);
177
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);
183     }
184
185     public function test_new_users_dont_have_any_recently_viewed()
186     {
187         $user = User::factory()->create();
188         $viewRole = Role::getRole('Viewer');
189         $user->attachRole($viewRole);
190
191         $homeVisit = $this->actingAs($user)->get('/');
192         $this->withHtml($homeVisit)->assertElementContains('#recently-viewed', 'You have not viewed any pages');
193     }
194 }