]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Replace dots with something else on user create and edit screens
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php namespace Tests;
2
3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Bookshelf;
7
8 class BookShelfTest extends TestCase
9 {
10
11     public function test_shelves_shows_in_header_if_have_view_permissions()
12     {
13         $viewer = $this->getViewer();
14         $resp = $this->actingAs($viewer)->get('/');
15         $resp->assertElementContains('header', 'Shelves');
16
17         $viewer->roles()->delete();
18         $this->giveUserPermissions($viewer);
19         $resp = $this->actingAs($viewer)->get('/');
20         $resp->assertElementNotContains('header', 'Shelves');
21
22         $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
23         $resp = $this->actingAs($viewer)->get('/');
24         $resp->assertElementContains('header', 'Shelves');
25
26         $viewer->roles()->delete();
27         $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
28         $resp = $this->actingAs($viewer)->get('/');
29         $resp->assertElementContains('header', 'Shelves');
30     }
31
32     public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
33     {
34         $user = factory(User::class)->create();
35         $this->giveUserPermissions($user, ['image-create-all']);
36         $shelf = Bookshelf::first();
37         $userRole = $user->roles()->first();
38
39         $resp = $this->actingAs($user)->get('/');
40         $resp->assertElementNotContains('header', 'Shelves');
41
42         $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
43
44         $resp = $this->get('/');
45         $resp->assertElementContains('header', 'Shelves');
46     }
47
48     public function test_shelves_page_contains_create_link()
49     {
50         $resp = $this->asEditor()->get('/shelves');
51         $resp->assertElementContains('a', 'Create New Shelf');
52     }
53
54     public function test_shelves_create()
55     {
56         $booksToInclude = Book::take(2)->get();
57         $shelfInfo = [
58             'name' => 'My test book' . str_random(4),
59             'description' => 'Test book description ' . str_random(10)
60         ];
61         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
62             'books' => $booksToInclude->implode('id', ','),
63             'tags' => [
64                 [
65                     'name' => 'Test Category',
66                     'value' => 'Test Tag Value',
67                 ]
68             ],
69         ]));
70         $resp->assertRedirect();
71         $editorId = $this->getEditor()->id;
72         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
73
74         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
75         $shelfPage = $this->get($shelf->getUrl());
76         $shelfPage->assertSee($shelfInfo['name']);
77         $shelfPage->assertSee($shelfInfo['description']);
78         $shelfPage->assertElementContains('.tag-item', 'Test Category');
79         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
80
81         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
82         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
83     }
84
85     public function test_shelf_view()
86     {
87         $shelf = Bookshelf::first();
88         $resp = $this->asEditor()->get($shelf->getUrl());
89         $resp->assertStatus(200);
90         $resp->assertSeeText($shelf->name);
91         $resp->assertSeeText($shelf->description);
92
93         foreach ($shelf->books as $book) {
94             $resp->assertSee($book->name);
95         }
96     }
97
98     public function test_shelf_view_shows_action_buttons()
99     {
100         $shelf = Bookshelf::first();
101         $resp = $this->asAdmin()->get($shelf->getUrl());
102         $resp->assertSee($shelf->getUrl('/edit'));
103         $resp->assertSee($shelf->getUrl('/permissions'));
104         $resp->assertSee($shelf->getUrl('/delete'));
105         $resp->assertElementContains('a', 'Edit');
106         $resp->assertElementContains('a', 'Permissions');
107         $resp->assertElementContains('a', 'Delete');
108
109         $resp = $this->asEditor()->get($shelf->getUrl());
110         $resp->assertDontSee($shelf->getUrl('/permissions'));
111     }
112
113     public function test_shelf_edit()
114     {
115         $shelf = Bookshelf::first();
116         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
117         $resp->assertSeeText('Edit Bookshelf');
118
119         $booksToInclude = Book::take(2)->get();
120         $shelfInfo = [
121             'name' => 'My test book' . str_random(4),
122             'description' => 'Test book description ' . str_random(10)
123         ];
124
125         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
126             'books' => $booksToInclude->implode('id', ','),
127             'tags' => [
128                 [
129                     'name' => 'Test Category',
130                     'value' => 'Test Tag Value',
131                 ]
132             ],
133         ]));
134         $shelf = Bookshelf::find($shelf->id);
135         $resp->assertRedirect($shelf->getUrl());
136         $this->assertSessionHas('success');
137
138         $editorId = $this->getEditor()->id;
139         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
140
141         $shelfPage = $this->get($shelf->getUrl());
142         $shelfPage->assertSee($shelfInfo['name']);
143         $shelfPage->assertSee($shelfInfo['description']);
144         $shelfPage->assertElementContains('.tag-item', 'Test Category');
145         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
146
147         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
148         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
149     }
150
151     public function test_shelf_delete()
152     {
153         $shelf = Bookshelf::first();
154         $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
155         $resp->assertSeeText('Delete Bookshelf');
156         $resp->assertSee("action=\"{$shelf->getUrl()}\"");
157
158         $resp = $this->delete($shelf->getUrl());
159         $resp->assertRedirect('/shelves');
160         $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
161         $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
162         $this->assertSessionHas('success');
163     }
164
165     public function test_shelf_copy_permissions()
166     {
167         $shelf = Bookshelf::first();
168         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
169         $resp->assertSeeText('Copy Permissions');
170         $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
171
172         $child = $shelf->books()->first();
173         $editorRole = $this->getEditor()->roles()->first();
174         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
175         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
176
177         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
178         $resp = $this->post($shelf->getUrl('/copy-permissions'));
179         $child = $shelf->books()->first();
180
181         $resp->assertRedirect($shelf->getUrl());
182         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
183         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
184         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
185         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
186     }
187
188     public function test_bookshelves_show_in_breadcrumbs_if_in_context()
189     {
190         $shelf = Bookshelf::first();
191         $shelfBook = $shelf->books()->first();
192         $shelfPage = $shelfBook->pages()->first();
193         $this->asAdmin();
194
195         $bookVisit = $this->get($shelfBook->getUrl());
196         $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
197         $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
198
199         $this->get($shelf->getUrl());
200         $bookVisit = $this->get($shelfBook->getUrl());
201         $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
202         $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
203
204         $pageVisit = $this->get($shelfPage->getUrl());
205         $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
206         $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
207
208         $this->get('/books');
209         $pageVisit = $this->get($shelfPage->getUrl());
210         $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
211         $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
212     }
213
214 }