]> BookStack Code Mirror - bookstack/blob - tests/HomepageTest.php
Merge branch 'create-content-meta-tags' of https://github.com/james-geiger/BookStack...
[bookstack] / tests / HomepageTest.php
1 <?php namespace Tests;
2
3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Models\Bookshelf;
6 use BookStack\Entities\Models\Page;
7
8 class HomepageTest extends TestCase
9 {
10
11     public function test_default_homepage_visible()
12     {
13         $this->asEditor();
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');
19     }
20
21     public function test_custom_homepage()
22     {
23         $this->asEditor();
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']);
29
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');
36     }
37
38     public function test_delete_custom_homepage()
39     {
40         $this->asEditor();
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]);
44         $this->setSettings([
45             'app-homepage' => $customPage->id,
46             'app-homepage-type' => 'page'
47         ]);
48
49         $homeVisit = $this->get('/');
50         $homeVisit->assertSee($name);
51         $homeVisit->assertElementNotExists('#home-default');
52
53         $pageDeleteReq = $this->delete($customPage->getUrl());
54         $pageDeleteReq->assertStatus(302);
55         $pageDeleteReq->assertRedirect($customPage->getUrl());
56         $pageDeleteReq->assertSessionHas('error');
57         $pageDeleteReq->assertSessionMissing('success');
58
59         $homeVisit = $this->get('/');
60         $homeVisit->assertSee($name);
61         $homeVisit->assertStatus(200);
62     }
63
64     public function test_custom_homepage_can_be_deleted_once_custom_homepage_no_longer_used()
65     {
66         $this->asEditor();
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]);
70         $this->setSettings([
71             'app-homepage' => $customPage->id,
72             'app-homepage-type' => 'default'
73         ]);
74
75         $pageDeleteReq = $this->delete($customPage->getUrl());
76         $pageDeleteReq->assertStatus(302);
77         $pageDeleteReq->assertSessionHas('success');
78         $pageDeleteReq->assertSessionMissing('error');
79     }
80
81     public function test_set_book_homepage()
82     {
83         $editor = $this->getEditor();
84         setting()->putUser($editor, 'books_view_type', 'grid');
85
86         $this->setSettings(['app-homepage-type' => 'books']);
87
88         $this->asEditor();
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');
95
96         $this->setSettings(['app-homepage-type' => false]);
97         $this->test_default_homepage_visible();
98     }
99
100     public function test_set_bookshelves_homepage()
101     {
102         $editor = $this->getEditor();
103         setting()->putUser($editor, 'bookshelves_view_type', 'grid');
104         $shelf = Bookshelf::query()->firstOrFail();
105
106         $this->setSettings(['app-homepage-type' => 'bookshelves']);
107
108         $this->asEditor();
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);
114
115         $this->setSettings(['app-homepage-type' => false]);
116         $this->test_default_homepage_visible();
117     }
118
119     public function test_shelves_list_homepage_adheres_to_book_visibility_permissions()
120     {
121         $editor = $this->getEditor();
122         setting()->putUser($editor, 'bookshelves_view_type', 'list');
123         $this->setSettings(['app-homepage-type' => 'bookshelves']);
124         $this->asEditor();
125
126         $shelf = Bookshelf::query()->first();
127         $book = $shelf->books()->first();
128
129         // Ensure initially visible
130         $homeVisit = $this->get('/');
131         $homeVisit->assertElementContains('.content-wrap', $shelf->name);
132         $homeVisit->assertElementContains('.content-wrap', $book->name);
133
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);
140
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);
146     }
147
148     public function test_new_users_dont_have_any_recently_viewed()
149     {
150         $user = factory(User::class)->create();
151         $viewRole = Role::getRole('Viewer');
152         $user->attachRole($viewRole);
153
154         $homeVisit = $this->actingAs($user)->get('/');
155         $homeVisit->assertElementContains('#recently-viewed', 'You have not viewed any pages');
156     }
157 }