]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Create additional test helper classes
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Uploads\Image;
9 use Illuminate\Support\Str;
10 use Tests\TestCase;
11 use Tests\Uploads\UsesImages;
12
13 class BookShelfTest extends TestCase
14 {
15     use UsesImages;
16
17     public function test_shelves_shows_in_header_if_have_view_permissions()
18     {
19         $viewer = $this->users->viewer();
20         $resp = $this->actingAs($viewer)->get('/');
21         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
22
23         $viewer->roles()->delete();
24         $this->permissions->grantUserRolePermissions($viewer);
25         $resp = $this->actingAs($viewer)->get('/');
26         $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
27
28         $this->permissions->grantUserRolePermissions($viewer, ['bookshelf-view-all']);
29         $resp = $this->actingAs($viewer)->get('/');
30         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
31
32         $viewer->roles()->delete();
33         $this->permissions->grantUserRolePermissions($viewer, ['bookshelf-view-own']);
34         $resp = $this->actingAs($viewer)->get('/');
35         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
36     }
37
38     public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
39     {
40         $user = User::factory()->create();
41         $this->permissions->grantUserRolePermissions($user, ['image-create-all']);
42         $shelf = $this->entities->shelf();
43         $userRole = $user->roles()->first();
44
45         $resp = $this->actingAs($user)->get('/');
46         $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
47
48         $this->permissions->setEntityPermissions($shelf, ['view'], [$userRole]);
49
50         $resp = $this->get('/');
51         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
52     }
53
54     public function test_shelves_page_contains_create_link()
55     {
56         $resp = $this->asEditor()->get('/shelves');
57         $this->withHtml($resp)->assertElementContains('a', 'New Shelf');
58     }
59
60     public function test_book_not_visible_in_shelf_list_view_if_user_cant_view_shelf()
61     {
62         config()->set([
63             'setting-defaults.user.bookshelves_view_type' => 'list',
64         ]);
65         $shelf = $this->entities->shelf();
66         $book = $shelf->books()->first();
67
68         $resp = $this->asEditor()->get('/shelves');
69         $resp->assertSee($book->name);
70         $resp->assertSee($book->getUrl());
71
72         $this->permissions->setEntityPermissions($book, []);
73
74         $resp = $this->asEditor()->get('/shelves');
75         $resp->assertDontSee($book->name);
76         $resp->assertDontSee($book->getUrl());
77     }
78
79     public function test_shelves_create()
80     {
81         $booksToInclude = Book::take(2)->get();
82         $shelfInfo = [
83             'name'        => 'My test book' . Str::random(4),
84             'description' => 'Test book description ' . Str::random(10),
85         ];
86         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
87             'books' => $booksToInclude->implode('id', ','),
88             'tags'  => [
89                 [
90                     'name'  => 'Test Category',
91                     'value' => 'Test Tag Value',
92                 ],
93             ],
94         ]));
95         $resp->assertRedirect();
96         $editorId = $this->users->editor()->id;
97         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
98
99         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
100         $shelfPage = $this->get($shelf->getUrl());
101         $shelfPage->assertSee($shelfInfo['name']);
102         $shelfPage->assertSee($shelfInfo['description']);
103         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Category');
104         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Tag Value');
105
106         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
107         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
108     }
109
110     public function test_shelves_create_sets_cover_image()
111     {
112         $shelfInfo = [
113             'name'        => 'My test book' . Str::random(4),
114             'description' => 'Test book description ' . Str::random(10),
115         ];
116
117         $imageFile = $this->getTestImage('shelf-test.png');
118         $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
119         $resp->assertRedirect();
120
121         $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
122         $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
123         $this->assertDatabaseHas('bookshelves', [
124             'id'       => $shelf->id,
125             'image_id' => $lastImage->id,
126         ]);
127         $this->assertEquals($lastImage->id, $shelf->cover->id);
128         $this->assertEquals('cover_bookshelf', $lastImage->type);
129     }
130
131     public function test_shelf_view()
132     {
133         $shelf = $this->entities->shelf();
134         $resp = $this->asEditor()->get($shelf->getUrl());
135         $resp->assertStatus(200);
136         $resp->assertSeeText($shelf->name);
137         $resp->assertSeeText($shelf->description);
138
139         foreach ($shelf->books as $book) {
140             $resp->assertSee($book->name);
141         }
142     }
143
144     public function test_shelf_view_shows_action_buttons()
145     {
146         $shelf = $this->entities->shelf();
147         $resp = $this->asAdmin()->get($shelf->getUrl());
148         $resp->assertSee($shelf->getUrl('/create-book'));
149         $resp->assertSee($shelf->getUrl('/edit'));
150         $resp->assertSee($shelf->getUrl('/permissions'));
151         $resp->assertSee($shelf->getUrl('/delete'));
152         $this->withHtml($resp)->assertElementContains('a', 'New Book');
153         $this->withHtml($resp)->assertElementContains('a', 'Edit');
154         $this->withHtml($resp)->assertElementContains('a', 'Permissions');
155         $this->withHtml($resp)->assertElementContains('a', 'Delete');
156
157         $resp = $this->asEditor()->get($shelf->getUrl());
158         $resp->assertDontSee($shelf->getUrl('/permissions'));
159     }
160
161     public function test_shelf_view_has_sort_control_that_defaults_to_default()
162     {
163         $shelf = $this->entities->shelf();
164         $resp = $this->asAdmin()->get($shelf->getUrl());
165         $this->withHtml($resp)->assertElementExists('form[action$="change-sort/shelf_books"]');
166         $this->withHtml($resp)->assertElementContains('form[action$="change-sort/shelf_books"] [aria-haspopup="true"]', 'Default');
167     }
168
169     public function test_shelf_view_sort_takes_action()
170     {
171         $shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
172         $books = Book::query()->take(3)->get(['id', 'name']);
173         $books[0]->fill(['name' => 'bsfsdfsdfsd'])->save();
174         $books[1]->fill(['name' => 'adsfsdfsdfsd'])->save();
175         $books[2]->fill(['name' => 'hdgfgdfg'])->save();
176
177         // Set book ordering
178         $this->asAdmin()->put($shelf->getUrl(), [
179             'books' => $books->implode('id', ','),
180             'tags'  => [], 'description' => 'abc', 'name' => 'abc',
181         ]);
182         $this->assertEquals(3, $shelf->books()->count());
183         $shelf->refresh();
184
185         $resp = $this->asEditor()->get($shelf->getUrl());
186         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
187         $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
188
189         setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
190         $resp = $this->asEditor()->get($shelf->getUrl());
191         $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
192         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
193
194         setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
195         setting()->putUser($this->users->editor(), 'shelf_books_sort', 'name');
196         $resp = $this->asEditor()->get($shelf->getUrl());
197         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', 'hdgfgdfg');
198         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(2)', 'bsfsdfsdfsd');
199         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', 'adsfsdfsdfsd');
200     }
201
202     public function test_shelf_edit()
203     {
204         $shelf = $this->entities->shelf();
205         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
206         $resp->assertSeeText('Edit Shelf');
207
208         $booksToInclude = Book::take(2)->get();
209         $shelfInfo = [
210             'name'        => 'My test book' . Str::random(4),
211             'description' => 'Test book description ' . Str::random(10),
212         ];
213
214         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
215             'books' => $booksToInclude->implode('id', ','),
216             'tags'  => [
217                 [
218                     'name'  => 'Test Category',
219                     'value' => 'Test Tag Value',
220                 ],
221             ],
222         ]));
223         $shelf = Bookshelf::find($shelf->id);
224         $resp->assertRedirect($shelf->getUrl());
225         $this->assertSessionHas('success');
226
227         $editorId = $this->users->editor()->id;
228         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
229
230         $shelfPage = $this->get($shelf->getUrl());
231         $shelfPage->assertSee($shelfInfo['name']);
232         $shelfPage->assertSee($shelfInfo['description']);
233         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Category');
234         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Tag Value');
235
236         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
237         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
238     }
239
240     public function test_shelf_create_new_book()
241     {
242         $shelf = $this->entities->shelf();
243         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
244
245         $resp->assertSee('Create New Book');
246         $resp->assertSee($shelf->getShortName());
247
248         $testName = 'Test Book in Shelf Name';
249
250         $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
251             'name'        => $testName,
252             'description' => 'Book in shelf description',
253         ]);
254         $createBookResp->assertRedirect();
255
256         $newBook = Book::query()->orderBy('id', 'desc')->first();
257         $this->assertDatabaseHas('bookshelves_books', [
258             'bookshelf_id' => $shelf->id,
259             'book_id'      => $newBook->id,
260         ]);
261
262         $resp = $this->asEditor()->get($shelf->getUrl());
263         $resp->assertSee($testName);
264     }
265
266     public function test_shelf_delete()
267     {
268         $shelf = Bookshelf::query()->whereHas('books')->first();
269         $this->assertNull($shelf->deleted_at);
270         $bookCount = $shelf->books()->count();
271
272         $deleteViewReq = $this->asEditor()->get($shelf->getUrl('/delete'));
273         $deleteViewReq->assertSeeText('Are you sure you want to delete this shelf?');
274
275         $deleteReq = $this->delete($shelf->getUrl());
276         $deleteReq->assertRedirect(url('/shelves'));
277         $this->assertActivityExists('bookshelf_delete', $shelf);
278
279         $shelf->refresh();
280         $this->assertNotNull($shelf->deleted_at);
281
282         $this->assertTrue($shelf->books()->count() === $bookCount);
283         $this->assertTrue($shelf->deletions()->count() === 1);
284
285         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
286         $this->assertNotificationContains($redirectReq, 'Shelf Successfully Deleted');
287     }
288
289     public function test_shelf_copy_permissions()
290     {
291         $shelf = $this->entities->shelf();
292         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
293         $resp->assertSeeText('Copy Permissions');
294         $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"", false);
295
296         $child = $shelf->books()->first();
297         $editorRole = $this->users->editor()->roles()->first();
298         $this->assertFalse($child->hasPermissions(), 'Child book should not be restricted by default');
299         $this->assertTrue($child->permissions()->count() === 0, 'Child book should have no permissions by default');
300
301         $this->permissions->setEntityPermissions($shelf, ['view', 'update'], [$editorRole]);
302         $resp = $this->post($shelf->getUrl('/copy-permissions'));
303         $child = $shelf->books()->first();
304
305         $resp->assertRedirect($shelf->getUrl());
306         $this->assertTrue($child->hasPermissions(), 'Child book should now be restricted');
307         $this->assertTrue($child->permissions()->count() === 2, 'Child book should have copied permissions');
308         $this->assertDatabaseHas('entity_permissions', [
309             'entity_type' => 'book',
310             'entity_id' => $child->id,
311             'role_id' => $editorRole->id,
312             'view' => true, 'update' => true, 'create' => false, 'delete' => false,
313         ]);
314     }
315
316     public function test_permission_page_has_a_warning_about_no_cascading()
317     {
318         $shelf = $this->entities->shelf();
319         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
320         $resp->assertSeeText('Permissions on shelves do not automatically cascade to contained books.');
321     }
322
323     public function test_bookshelves_show_in_breadcrumbs_if_in_context()
324     {
325         $shelf = $this->entities->shelf();
326         $shelfBook = $shelf->books()->first();
327         $shelfPage = $shelfBook->pages()->first();
328         $this->asAdmin();
329
330         $bookVisit = $this->get($shelfBook->getUrl());
331         $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
332         $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
333
334         $this->get($shelf->getUrl());
335         $bookVisit = $this->get($shelfBook->getUrl());
336         $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', 'Shelves');
337         $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
338
339         $pageVisit = $this->get($shelfPage->getUrl());
340         $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', 'Shelves');
341         $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
342
343         $this->get('/books');
344         $pageVisit = $this->get($shelfPage->getUrl());
345         $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
346         $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
347     }
348
349     public function test_bookshelves_show_on_book()
350     {
351         // Create shelf
352         $shelfInfo = [
353             'name'        => 'My test shelf' . Str::random(4),
354             'description' => 'Test shelf description ' . Str::random(10),
355         ];
356
357         $this->asEditor()->post('/shelves', $shelfInfo);
358         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
359
360         // Create book and add to shelf
361         $this->asEditor()->post($shelf->getUrl('/create-book'), [
362             'name'        => 'Test book name',
363             'description' => 'Book in shelf description',
364         ]);
365
366         $newBook = Book::query()->orderBy('id', 'desc')->first();
367
368         $resp = $this->asEditor()->get($newBook->getUrl());
369         $this->withHtml($resp)->assertElementContains('.tri-layout-left-contents', $shelfInfo['name']);
370
371         // Remove shelf
372         $this->delete($shelf->getUrl());
373
374         $resp = $this->asEditor()->get($newBook->getUrl());
375         $resp->assertDontSee($shelfInfo['name']);
376     }
377
378     public function test_cancel_on_child_book_creation_returns_to_original_shelf()
379     {
380         $shelf = $this->entities->shelf();
381         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
382         $this->withHtml($resp)->assertElementContains('form a[href="' . $shelf->getUrl() . '"]', 'Cancel');
383     }
384 }