]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Merge branch 'feature/#1598' of git://github.com/cw1998/BookStack into cw1998-feature...
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php namespace Tests\Entity;
2
3 use BookStack\Auth\User;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Bookshelf;
6 use BookStack\Uploads\Image;
7 use Illuminate\Support\Str;
8 use Tests\TestCase;
9 use Tests\Uploads\UsesImages;
10
11 class BookShelfTest extends TestCase
12 {
13
14     use UsesImages;
15
16     public function test_shelves_shows_in_header_if_have_view_permissions()
17     {
18         $viewer = $this->getViewer();
19         $resp = $this->actingAs($viewer)->get('/');
20         $resp->assertElementContains('header', 'Shelves');
21
22         $viewer->roles()->delete();
23         $this->giveUserPermissions($viewer);
24         $resp = $this->actingAs($viewer)->get('/');
25         $resp->assertElementNotContains('header', 'Shelves');
26
27         $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
28         $resp = $this->actingAs($viewer)->get('/');
29         $resp->assertElementContains('header', 'Shelves');
30
31         $viewer->roles()->delete();
32         $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
33         $resp = $this->actingAs($viewer)->get('/');
34         $resp->assertElementContains('header', 'Shelves');
35     }
36
37     public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
38     {
39         $user = factory(User::class)->create();
40         $this->giveUserPermissions($user, ['image-create-all']);
41         $shelf = Bookshelf::first();
42         $userRole = $user->roles()->first();
43
44         $resp = $this->actingAs($user)->get('/');
45         $resp->assertElementNotContains('header', 'Shelves');
46
47         $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
48
49         $resp = $this->get('/');
50         $resp->assertElementContains('header', 'Shelves');
51     }
52
53     public function test_shelves_page_contains_create_link()
54     {
55         $resp = $this->asEditor()->get('/shelves');
56         $resp->assertElementContains('a', 'New Shelf');
57     }
58
59     public function test_shelves_create()
60     {
61         $booksToInclude = Book::take(2)->get();
62         $shelfInfo = [
63             'name' => 'My test book' . Str::random(4),
64             'description' => 'Test book description ' . Str::random(10)
65         ];
66         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
67             'books' => $booksToInclude->implode('id', ','),
68             'tags' => [
69                 [
70                     'name' => 'Test Category',
71                     'value' => 'Test Tag Value',
72                 ]
73             ],
74         ]));
75         $resp->assertRedirect();
76         $editorId = $this->getEditor()->id;
77         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
78
79         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
80         $shelfPage = $this->get($shelf->getUrl());
81         $shelfPage->assertSee($shelfInfo['name']);
82         $shelfPage->assertSee($shelfInfo['description']);
83         $shelfPage->assertElementContains('.tag-item', 'Test Category');
84         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
85
86         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
87         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
88     }
89
90     public function test_shelves_create_sets_cover_image()
91     {
92         $shelfInfo = [
93             'name' => 'My test book' . Str::random(4),
94             'description' => 'Test book description ' . Str::random(10)
95         ];
96
97         $imageFile = $this->getTestImage('shelf-test.png');
98         $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
99         $resp->assertRedirect();
100
101         $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
102         $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
103         $this->assertDatabaseHas('bookshelves', [
104             'id' => $shelf->id,
105             'image_id' => $lastImage->id,
106         ]);
107         $this->assertEquals($lastImage->id, $shelf->cover->id);
108     }
109
110     public function test_shelf_view()
111     {
112         $shelf = Bookshelf::first();
113         $resp = $this->asEditor()->get($shelf->getUrl());
114         $resp->assertStatus(200);
115         $resp->assertSeeText($shelf->name);
116         $resp->assertSeeText($shelf->description);
117
118         foreach ($shelf->books as $book) {
119             $resp->assertSee($book->name);
120         }
121     }
122
123     public function test_shelf_view_shows_action_buttons()
124     {
125         $shelf = Bookshelf::first();
126         $resp = $this->asAdmin()->get($shelf->getUrl());
127         $resp->assertSee($shelf->getUrl('/create-book'));
128         $resp->assertSee($shelf->getUrl('/edit'));
129         $resp->assertSee($shelf->getUrl('/permissions'));
130         $resp->assertSee($shelf->getUrl('/delete'));
131         $resp->assertElementContains('a', 'New Book');
132         $resp->assertElementContains('a', 'Edit');
133         $resp->assertElementContains('a', 'Permissions');
134         $resp->assertElementContains('a', 'Delete');
135
136         $resp = $this->asEditor()->get($shelf->getUrl());
137         $resp->assertDontSee($shelf->getUrl('/permissions'));
138     }
139
140     public function test_shelf_edit()
141     {
142         $shelf = Bookshelf::first();
143         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
144         $resp->assertSeeText('Edit Bookshelf');
145
146         $booksToInclude = Book::take(2)->get();
147         $shelfInfo = [
148             'name' => 'My test book' . Str::random(4),
149             'description' => 'Test book description ' . Str::random(10)
150         ];
151
152         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
153             'books' => $booksToInclude->implode('id', ','),
154             'tags' => [
155                 [
156                     'name' => 'Test Category',
157                     'value' => 'Test Tag Value',
158                 ]
159             ],
160         ]));
161         $shelf = Bookshelf::find($shelf->id);
162         $resp->assertRedirect($shelf->getUrl());
163         $this->assertSessionHas('success');
164
165         $editorId = $this->getEditor()->id;
166         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
167
168         $shelfPage = $this->get($shelf->getUrl());
169         $shelfPage->assertSee($shelfInfo['name']);
170         $shelfPage->assertSee($shelfInfo['description']);
171         $shelfPage->assertElementContains('.tag-item', 'Test Category');
172         $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
173
174         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
175         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
176     }
177
178     public function test_shelf_create_new_book()
179     {
180         $shelf = Bookshelf::first();
181         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
182
183         $resp->assertSee('Create New Book');
184         $resp->assertSee($shelf->getShortName());
185
186         $testName = 'Test Book in Shelf Name';
187
188         $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
189             'name' => $testName,
190             'description' => 'Book in shelf description'
191         ]);
192         $createBookResp->assertRedirect();
193
194         $newBook = Book::query()->orderBy('id', 'desc')->first();
195         $this->assertDatabaseHas('bookshelves_books', [
196             'bookshelf_id' => $shelf->id,
197             'book_id' => $newBook->id,
198         ]);
199
200         $resp = $this->asEditor()->get($shelf->getUrl());
201         $resp->assertSee($testName);
202     }
203
204     public function test_shelf_delete()
205     {
206         $shelf = Bookshelf::first();
207         $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
208         $resp->assertSeeText('Delete Bookshelf');
209         $resp->assertSee("action=\"{$shelf->getUrl()}\"");
210
211         $resp = $this->delete($shelf->getUrl());
212         $resp->assertRedirect('/shelves');
213         $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
214         $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
215         $this->assertSessionHas('success');
216     }
217
218     public function test_shelf_copy_permissions()
219     {
220         $shelf = Bookshelf::first();
221         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
222         $resp->assertSeeText('Copy Permissions');
223         $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
224
225         $child = $shelf->books()->first();
226         $editorRole = $this->getEditor()->roles()->first();
227         $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
228         $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
229
230         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
231         $resp = $this->post($shelf->getUrl('/copy-permissions'));
232         $child = $shelf->books()->first();
233
234         $resp->assertRedirect($shelf->getUrl());
235         $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
236         $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
237         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
238         $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
239     }
240
241     public function test_bookshelves_show_in_breadcrumbs_if_in_context()
242     {
243         $shelf = Bookshelf::first();
244         $shelfBook = $shelf->books()->first();
245         $shelfPage = $shelfBook->pages()->first();
246         $this->asAdmin();
247
248         $bookVisit = $this->get($shelfBook->getUrl());
249         $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
250         $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
251
252         $this->get($shelf->getUrl());
253         $bookVisit = $this->get($shelfBook->getUrl());
254         $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
255         $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
256
257         $pageVisit = $this->get($shelfPage->getUrl());
258         $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
259         $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
260
261         $this->get('/books');
262         $pageVisit = $this->get($shelfPage->getUrl());
263         $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
264         $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
265     }
266
267     public function test_bookshelves_show_on_book()
268     {
269         // Create shelf
270         $shelfInfo = [
271             'name' => 'My test shelf' . Str::random(4),
272             'description' => 'Test shelf description ' . Str::random(10)
273         ];
274
275         $this->asEditor()->post('/shelves', $shelfInfo);
276         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
277
278         // Create book and add to shelf
279         $this->asEditor()->post($shelf->getUrl('/create-book'), [
280             'name' => 'Test book name',
281             'description' => 'Book in shelf description'
282         ]);
283
284         $newBook = Book::query()->orderBy('id', 'desc')->first();
285
286         $resp = $this->asEditor()->get($newBook->getUrl());
287         $resp->assertSee($shelfInfo['name']);
288
289         // Remove shelf
290         $this->delete($shelf->getUrl());
291
292         $resp = $this->asEditor()->get($newBook->getUrl());
293         $resp->assertDontSee($shelfInfo['name']);
294     }
295 }