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