3 namespace Tests\Entity;
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;
12 class BookShelfTest extends TestCase
14 public function test_shelves_shows_in_header_if_have_view_permissions()
16 $viewer = $this->users->viewer();
17 $resp = $this->actingAs($viewer)->get('/');
18 $this->withHtml($resp)->assertElementContains('header', 'Shelves');
20 $viewer->roles()->delete();
21 $this->permissions->grantUserRolePermissions($viewer, []);
22 $resp = $this->actingAs($viewer)->get('/');
23 $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
25 $this->permissions->grantUserRolePermissions($viewer, ['bookshelf-view-all']);
26 $resp = $this->actingAs($viewer)->get('/');
27 $this->withHtml($resp)->assertElementContains('header', 'Shelves');
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');
35 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
37 $user = User::factory()->create();
38 $this->permissions->grantUserRolePermissions($user, ['image-create-all']);
39 $shelf = $this->entities->shelf();
40 $userRole = $user->roles()->first();
42 $resp = $this->actingAs($user)->get('/');
43 $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
45 $this->permissions->setEntityPermissions($shelf, ['view'], [$userRole]);
47 $resp = $this->get('/');
48 $this->withHtml($resp)->assertElementContains('header', 'Shelves');
51 public function test_shelves_page_contains_create_link()
53 $resp = $this->asEditor()->get('/shelves');
54 $this->withHtml($resp)->assertElementContains('a', 'New Shelf');
57 public function test_book_not_visible_in_shelf_list_view_if_user_cant_view_shelf()
60 'setting-defaults.user.bookshelves_view_type' => 'list',
62 $shelf = $this->entities->shelf();
63 $book = $shelf->books()->first();
65 $resp = $this->asEditor()->get('/shelves');
66 $resp->assertSee($book->name);
67 $resp->assertSee($book->getUrl());
69 $this->permissions->setEntityPermissions($book, []);
71 $resp = $this->asEditor()->get('/shelves');
72 $resp->assertDontSee($book->name);
73 $resp->assertDontSee($book->getUrl());
76 public function test_shelves_create()
78 $booksToInclude = Book::take(2)->get();
80 'name' => 'My test shelf' . Str::random(4),
81 'description_html' => '<p>Test book description ' . Str::random(10) . '</p>',
83 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
84 'books' => $booksToInclude->implode('id', ','),
87 'name' => 'Test Category',
88 'value' => 'Test Tag Value',
92 $resp->assertRedirect();
93 $editorId = $this->users->editor()->id;
94 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
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');
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]);
107 public function test_shelves_create_sets_cover_image()
110 'name' => 'My test shelf' . Str::random(4),
111 'description_html' => '<p>Test book description ' . Str::random(10) . '</p>',
114 $imageFile = $this->files->uploadedImage('shelf-test.png');
115 $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
116 $resp->assertRedirect();
118 $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
119 $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
120 $this->assertDatabaseHas('bookshelves', [
122 'image_id' => $lastImage->id,
124 $this->assertEquals($lastImage->id, $shelf->cover->id);
125 $this->assertEquals('cover_bookshelf', $lastImage->type);
128 public function test_shelf_view()
130 $shelf = $this->entities->shelf();
131 $resp = $this->asEditor()->get($shelf->getUrl());
132 $resp->assertStatus(200);
133 $resp->assertSeeText($shelf->name);
134 $resp->assertSeeText($shelf->description);
136 foreach ($shelf->books as $book) {
137 $resp->assertSee($book->name);
141 public function test_shelf_view_shows_action_buttons()
143 $shelf = $this->entities->shelf();
144 $resp = $this->asAdmin()->get($shelf->getUrl());
145 $resp->assertSee($shelf->getUrl('/create-book'));
146 $resp->assertSee($shelf->getUrl('/edit'));
147 $resp->assertSee($shelf->getUrl('/permissions'));
148 $resp->assertSee($shelf->getUrl('/delete'));
149 $this->withHtml($resp)->assertElementContains('a', 'New Book');
150 $this->withHtml($resp)->assertElementContains('a', 'Edit');
151 $this->withHtml($resp)->assertElementContains('a', 'Permissions');
152 $this->withHtml($resp)->assertElementContains('a', 'Delete');
154 $resp = $this->asEditor()->get($shelf->getUrl());
155 $resp->assertDontSee($shelf->getUrl('/permissions'));
158 public function test_shelf_view_has_sort_control_that_defaults_to_default()
160 $shelf = $this->entities->shelf();
161 $resp = $this->asAdmin()->get($shelf->getUrl());
162 $this->withHtml($resp)->assertElementExists('form[action$="change-sort/shelf_books"]');
163 $this->withHtml($resp)->assertElementContains('form[action$="change-sort/shelf_books"] [aria-haspopup="true"]', 'Default');
166 public function test_shelf_view_sort_takes_action()
168 $shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
169 $books = Book::query()->take(3)->get(['id', 'name']);
170 $books[0]->fill(['name' => 'bsfsdfsdfsd'])->save();
171 $books[1]->fill(['name' => 'adsfsdfsdfsd'])->save();
172 $books[2]->fill(['name' => 'hdgfgdfg'])->save();
175 $this->asAdmin()->put($shelf->getUrl(), [
176 'books' => $books->implode('id', ','),
177 'tags' => [], 'description_html' => 'abc', 'name' => 'abc',
179 $this->assertEquals(3, $shelf->books()->count());
182 $resp = $this->asEditor()->get($shelf->getUrl());
183 $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
184 $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
186 setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
187 $resp = $this->asEditor()->get($shelf->getUrl());
188 $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
189 $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
191 setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
192 setting()->putUser($this->users->editor(), 'shelf_books_sort', 'name');
193 $resp = $this->asEditor()->get($shelf->getUrl());
194 $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', 'hdgfgdfg');
195 $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(2)', 'bsfsdfsdfsd');
196 $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', 'adsfsdfsdfsd');
199 public function test_shelf_view_sorts_by_name_case_insensitively()
201 $shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
202 $books = Book::query()->take(3)->get(['id', 'name']);
203 $books[0]->fill(['name' => 'Book Ab'])->save();
204 $books[1]->fill(['name' => 'Book ac'])->save();
205 $books[2]->fill(['name' => 'Book AD'])->save();
208 $this->asAdmin()->put($shelf->getUrl(), [
209 'books' => $books->implode('id', ','),
210 'tags' => [], 'description_html' => 'abc', 'name' => 'abc',
212 $this->assertEquals(3, $shelf->books()->count());
215 setting()->putUser($this->users->editor(), 'shelf_books_sort', 'name');
216 setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'asc');
217 $html = $this->withHtml($this->asEditor()->get($shelf->getUrl()));
219 $html->assertElementContains('.book-content a.grid-card:nth-child(1)', 'Book Ab');
220 $html->assertElementContains('.book-content a.grid-card:nth-child(2)', 'Book ac');
221 $html->assertElementContains('.book-content a.grid-card:nth-child(3)', 'Book AD');
224 public function test_shelf_edit()
226 $shelf = $this->entities->shelf();
227 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
228 $resp->assertSeeText('Edit Shelf');
230 $booksToInclude = Book::take(2)->get();
232 'name' => 'My test shelf' . Str::random(4),
233 'description_html' => '<p>Test book description ' . Str::random(10) . '</p>',
236 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
237 'books' => $booksToInclude->implode('id', ','),
240 'name' => 'Test Category',
241 'value' => 'Test Tag Value',
245 $shelf = Bookshelf::find($shelf->id);
246 $resp->assertRedirect($shelf->getUrl());
247 $this->assertSessionHas('success');
249 $editorId = $this->users->editor()->id;
250 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
252 $shelfPage = $this->get($shelf->getUrl());
253 $shelfPage->assertSee($shelfInfo['name']);
254 $shelfPage->assertSee($shelfInfo['description_html'], false);
255 $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Category');
256 $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Tag Value');
258 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
259 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
262 public function test_shelf_create_new_book()
264 $shelf = $this->entities->shelf();
265 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
267 $resp->assertSee('Create New Book');
268 $resp->assertSee($shelf->getShortName());
270 $testName = 'Test Book in Shelf Name';
272 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
274 'description_html' => 'Book in shelf description',
276 $createBookResp->assertRedirect();
278 $newBook = Book::query()->orderBy('id', 'desc')->first();
279 $this->assertDatabaseHas('bookshelves_books', [
280 'bookshelf_id' => $shelf->id,
281 'book_id' => $newBook->id,
284 $resp = $this->asEditor()->get($shelf->getUrl());
285 $resp->assertSee($testName);
288 public function test_shelf_delete()
290 $shelf = Bookshelf::query()->whereHas('books')->first();
291 $this->assertNull($shelf->deleted_at);
292 $bookCount = $shelf->books()->count();
294 $deleteViewReq = $this->asEditor()->get($shelf->getUrl('/delete'));
295 $deleteViewReq->assertSeeText('Are you sure you want to delete this shelf?');
297 $deleteReq = $this->delete($shelf->getUrl());
298 $deleteReq->assertRedirect(url('/shelves'));
299 $this->assertActivityExists('bookshelf_delete', $shelf);
302 $this->assertNotNull($shelf->deleted_at);
304 $this->assertTrue($shelf->books()->count() === $bookCount);
305 $this->assertTrue($shelf->deletions()->count() === 1);
307 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
308 $this->assertNotificationContains($redirectReq, 'Shelf Successfully Deleted');
311 public function test_shelf_copy_permissions()
313 $shelf = $this->entities->shelf();
314 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
315 $resp->assertSeeText('Copy Permissions');
316 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"", false);
318 $child = $shelf->books()->first();
319 $editorRole = $this->users->editor()->roles()->first();
320 $this->assertFalse($child->hasPermissions(), 'Child book should not be restricted by default');
321 $this->assertTrue($child->permissions()->count() === 0, 'Child book should have no permissions by default');
323 $this->permissions->setEntityPermissions($shelf, ['view', 'update'], [$editorRole]);
324 $resp = $this->post($shelf->getUrl('/copy-permissions'));
325 $child = $shelf->books()->first();
327 $resp->assertRedirect($shelf->getUrl());
328 $this->assertTrue($child->hasPermissions(), 'Child book should now be restricted');
329 $this->assertTrue($child->permissions()->count() === 2, 'Child book should have copied permissions');
330 $this->assertDatabaseHas('entity_permissions', [
331 'entity_type' => 'book',
332 'entity_id' => $child->id,
333 'role_id' => $editorRole->id,
334 'view' => true, 'update' => true, 'create' => false, 'delete' => false,
338 public function test_permission_page_has_a_warning_about_no_cascading()
340 $shelf = $this->entities->shelf();
341 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
342 $resp->assertSeeText('Permissions on shelves do not automatically cascade to contained books.');
345 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
347 $shelf = $this->entities->shelf();
348 $shelfBook = $shelf->books()->first();
349 $shelfPage = $shelfBook->pages()->first();
352 $bookVisit = $this->get($shelfBook->getUrl());
353 $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
354 $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
356 $this->get($shelf->getUrl());
357 $bookVisit = $this->get($shelfBook->getUrl());
358 $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', 'Shelves');
359 $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
361 $pageVisit = $this->get($shelfPage->getUrl());
362 $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', 'Shelves');
363 $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
365 $this->get('/books');
366 $pageVisit = $this->get($shelfPage->getUrl());
367 $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
368 $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
371 public function test_bookshelves_show_on_book()
375 'name' => 'My test shelf' . Str::random(4),
376 'description_html' => '<p>Test shelf description ' . Str::random(10) . '</p>',
379 $this->asEditor()->post('/shelves', $shelfInfo);
380 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
382 // Create book and add to shelf
383 $this->asEditor()->post($shelf->getUrl('/create-book'), [
384 'name' => 'Test book name',
385 'description_html' => '<p>Book in shelf description</p>',
388 $newBook = Book::query()->orderBy('id', 'desc')->first();
390 $resp = $this->asEditor()->get($newBook->getUrl());
391 $this->withHtml($resp)->assertElementContains('.tri-layout-left-contents', $shelfInfo['name']);
394 $this->delete($shelf->getUrl());
396 $resp = $this->asEditor()->get($newBook->getUrl());
397 $resp->assertDontSee($shelfInfo['name']);
400 public function test_cancel_on_child_book_creation_returns_to_original_shelf()
402 $shelf = $this->entities->shelf();
403 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
404 $this->withHtml($resp)->assertElementContains('form a[href="' . $shelf->getUrl() . '"]', 'Cancel');
407 public function test_show_view_displays_description_if_no_description_html_set()
409 $shelf = $this->entities->shelf();
410 $shelf->description_html = '';
411 $shelf->description = "My great\ndescription\n\nwith newlines";
414 $resp = $this->asEditor()->get($shelf->getUrl());
415 $resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false);