3 namespace Tests\Entity;
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;
11 use Tests\Uploads\UsesImages;
13 class BookShelfTest extends TestCase
17 public function test_shelves_shows_in_header_if_have_view_permissions()
19 $viewer = $this->getViewer();
20 $resp = $this->actingAs($viewer)->get('/');
21 $resp->assertElementContains('header', 'Shelves');
23 $viewer->roles()->delete();
24 $this->giveUserPermissions($viewer);
25 $resp = $this->actingAs($viewer)->get('/');
26 $resp->assertElementNotContains('header', 'Shelves');
28 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
29 $resp = $this->actingAs($viewer)->get('/');
30 $resp->assertElementContains('header', 'Shelves');
32 $viewer->roles()->delete();
33 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
34 $resp = $this->actingAs($viewer)->get('/');
35 $resp->assertElementContains('header', 'Shelves');
38 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
40 $user = User::factory()->create();
41 $this->giveUserPermissions($user, ['image-create-all']);
42 $shelf = Bookshelf::first();
43 $userRole = $user->roles()->first();
45 $resp = $this->actingAs($user)->get('/');
46 $resp->assertElementNotContains('header', 'Shelves');
48 $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
50 $resp = $this->get('/');
51 $resp->assertElementContains('header', 'Shelves');
54 public function test_shelves_page_contains_create_link()
56 $resp = $this->asEditor()->get('/shelves');
57 $resp->assertElementContains('a', 'New Shelf');
60 public function test_book_not_visible_in_shelf_list_view_if_user_cant_view_shelf()
63 'setting-defaults.user.bookshelves_view_type' => 'list',
65 $shelf = Bookshelf::query()->first();
66 $book = $shelf->books()->first();
68 $resp = $this->asEditor()->get('/shelves');
69 $resp->assertSee($book->name);
70 $resp->assertSee($book->getUrl());
72 $this->setEntityRestrictions($book, []);
74 $resp = $this->asEditor()->get('/shelves');
75 $resp->assertDontSee($book->name);
76 $resp->assertDontSee($book->getUrl());
79 public function test_shelves_create()
81 $booksToInclude = Book::take(2)->get();
83 'name' => 'My test book' . Str::random(4),
84 'description' => 'Test book description ' . Str::random(10),
86 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
87 'books' => $booksToInclude->implode('id', ','),
90 'name' => 'Test Category',
91 'value' => 'Test Tag Value',
95 $resp->assertRedirect();
96 $editorId = $this->getEditor()->id;
97 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
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 $shelfPage->assertElementContains('.tag-item', 'Test Category');
104 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
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]);
110 public function test_shelves_create_sets_cover_image()
113 'name' => 'My test book' . Str::random(4),
114 'description' => 'Test book description ' . Str::random(10),
117 $imageFile = $this->getTestImage('shelf-test.png');
118 $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
119 $resp->assertRedirect();
121 $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
122 $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
123 $this->assertDatabaseHas('bookshelves', [
125 'image_id' => $lastImage->id,
127 $this->assertEquals($lastImage->id, $shelf->cover->id);
130 public function test_shelf_view()
132 $shelf = Bookshelf::first();
133 $resp = $this->asEditor()->get($shelf->getUrl());
134 $resp->assertStatus(200);
135 $resp->assertSeeText($shelf->name);
136 $resp->assertSeeText($shelf->description);
138 foreach ($shelf->books as $book) {
139 $resp->assertSee($book->name);
143 public function test_shelf_view_shows_action_buttons()
145 $shelf = Bookshelf::first();
146 $resp = $this->asAdmin()->get($shelf->getUrl());
147 $resp->assertSee($shelf->getUrl('/create-book'));
148 $resp->assertSee($shelf->getUrl('/edit'));
149 $resp->assertSee($shelf->getUrl('/permissions'));
150 $resp->assertSee($shelf->getUrl('/delete'));
151 $resp->assertElementContains('a', 'New Book');
152 $resp->assertElementContains('a', 'Edit');
153 $resp->assertElementContains('a', 'Permissions');
154 $resp->assertElementContains('a', 'Delete');
156 $resp = $this->asEditor()->get($shelf->getUrl());
157 $resp->assertDontSee($shelf->getUrl('/permissions'));
160 public function test_shelf_view_has_sort_control_that_defaults_to_default()
162 $shelf = Bookshelf::query()->first();
163 $resp = $this->asAdmin()->get($shelf->getUrl());
164 $resp->assertElementExists('form[action$="change-sort/shelf_books"]');
165 $resp->assertElementContains('form[action$="change-sort/shelf_books"] [aria-haspopup="true"]', 'Default');
168 public function test_shelf_view_sort_takes_action()
170 $shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
171 $books = Book::query()->take(3)->get(['id', 'name']);
172 $books[0]->fill(['name' => 'bsfsdfsdfsd'])->save();
173 $books[1]->fill(['name' => 'adsfsdfsdfsd'])->save();
174 $books[2]->fill(['name' => 'hdgfgdfg'])->save();
177 $this->asAdmin()->put($shelf->getUrl(), [
178 'books' => $books->implode('id', ','),
179 'tags' => [], 'description' => 'abc', 'name' => 'abc',
181 $this->assertEquals(3, $shelf->books()->count());
184 $resp = $this->asEditor()->get($shelf->getUrl());
185 $resp->assertElementContains('.book-content a.grid-card', $books[0]->name, 1);
186 $resp->assertElementNotContains('.book-content a.grid-card', $books[0]->name, 3);
188 setting()->putUser($this->getEditor(), 'shelf_books_sort_order', 'desc');
189 $resp = $this->asEditor()->get($shelf->getUrl());
190 $resp->assertElementNotContains('.book-content a.grid-card', $books[0]->name, 1);
191 $resp->assertElementContains('.book-content a.grid-card', $books[0]->name, 3);
193 setting()->putUser($this->getEditor(), 'shelf_books_sort_order', 'desc');
194 setting()->putUser($this->getEditor(), 'shelf_books_sort', 'name');
195 $resp = $this->asEditor()->get($shelf->getUrl());
196 $resp->assertElementContains('.book-content a.grid-card', 'hdgfgdfg', 1);
197 $resp->assertElementContains('.book-content a.grid-card', 'bsfsdfsdfsd', 2);
198 $resp->assertElementContains('.book-content a.grid-card', 'adsfsdfsdfsd', 3);
201 public function test_shelf_edit()
203 $shelf = Bookshelf::first();
204 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
205 $resp->assertSeeText('Edit Bookshelf');
207 $booksToInclude = Book::take(2)->get();
209 'name' => 'My test book' . Str::random(4),
210 'description' => 'Test book description ' . Str::random(10),
213 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
214 'books' => $booksToInclude->implode('id', ','),
217 'name' => 'Test Category',
218 'value' => 'Test Tag Value',
222 $shelf = Bookshelf::find($shelf->id);
223 $resp->assertRedirect($shelf->getUrl());
224 $this->assertSessionHas('success');
226 $editorId = $this->getEditor()->id;
227 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
229 $shelfPage = $this->get($shelf->getUrl());
230 $shelfPage->assertSee($shelfInfo['name']);
231 $shelfPage->assertSee($shelfInfo['description']);
232 $shelfPage->assertElementContains('.tag-item', 'Test Category');
233 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
235 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
236 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
239 public function test_shelf_create_new_book()
241 $shelf = Bookshelf::first();
242 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
244 $resp->assertSee('Create New Book');
245 $resp->assertSee($shelf->getShortName());
247 $testName = 'Test Book in Shelf Name';
249 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
251 'description' => 'Book in shelf description',
253 $createBookResp->assertRedirect();
255 $newBook = Book::query()->orderBy('id', 'desc')->first();
256 $this->assertDatabaseHas('bookshelves_books', [
257 'bookshelf_id' => $shelf->id,
258 'book_id' => $newBook->id,
261 $resp = $this->asEditor()->get($shelf->getUrl());
262 $resp->assertSee($testName);
265 public function test_shelf_delete()
267 $shelf = Bookshelf::query()->whereHas('books')->first();
268 $this->assertNull($shelf->deleted_at);
269 $bookCount = $shelf->books()->count();
271 $deleteViewReq = $this->asEditor()->get($shelf->getUrl('/delete'));
272 $deleteViewReq->assertSeeText('Are you sure you want to delete this bookshelf?');
274 $deleteReq = $this->delete($shelf->getUrl());
275 $deleteReq->assertRedirect(url('/shelves'));
276 $this->assertActivityExists('bookshelf_delete', $shelf);
279 $this->assertNotNull($shelf->deleted_at);
281 $this->assertTrue($shelf->books()->count() === $bookCount);
282 $this->assertTrue($shelf->deletions()->count() === 1);
284 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
285 $redirectReq->assertNotificationContains('Bookshelf Successfully Deleted');
288 public function test_shelf_copy_permissions()
290 $shelf = Bookshelf::first();
291 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
292 $resp->assertSeeText('Copy Permissions');
293 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"", false);
295 $child = $shelf->books()->first();
296 $editorRole = $this->getEditor()->roles()->first();
297 $this->assertFalse(boolval($child->restricted), 'Child book should not be restricted by default');
298 $this->assertTrue($child->permissions()->count() === 0, 'Child book should have no permissions by default');
300 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
301 $resp = $this->post($shelf->getUrl('/copy-permissions'));
302 $child = $shelf->books()->first();
304 $resp->assertRedirect($shelf->getUrl());
305 $this->assertTrue(boolval($child->restricted), 'Child book should now be restricted');
306 $this->assertTrue($child->permissions()->count() === 2, 'Child book should have copied permissions');
307 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
308 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
311 public function test_permission_page_has_a_warning_about_no_cascading()
313 $shelf = Bookshelf::first();
314 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
315 $resp->assertSeeText('Permissions on bookshelves do not automatically cascade to contained books.');
318 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
320 $shelf = Bookshelf::first();
321 $shelfBook = $shelf->books()->first();
322 $shelfPage = $shelfBook->pages()->first();
325 $bookVisit = $this->get($shelfBook->getUrl());
326 $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
327 $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
329 $this->get($shelf->getUrl());
330 $bookVisit = $this->get($shelfBook->getUrl());
331 $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
332 $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
334 $pageVisit = $this->get($shelfPage->getUrl());
335 $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
336 $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
338 $this->get('/books');
339 $pageVisit = $this->get($shelfPage->getUrl());
340 $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
341 $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
344 public function test_bookshelves_show_on_book()
348 'name' => 'My test shelf' . Str::random(4),
349 'description' => 'Test shelf description ' . Str::random(10),
352 $this->asEditor()->post('/shelves', $shelfInfo);
353 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
355 // Create book and add to shelf
356 $this->asEditor()->post($shelf->getUrl('/create-book'), [
357 'name' => 'Test book name',
358 'description' => 'Book in shelf description',
361 $newBook = Book::query()->orderBy('id', 'desc')->first();
363 $resp = $this->asEditor()->get($newBook->getUrl());
364 $resp->assertElementContains('.tri-layout-left-contents', $shelfInfo['name']);
367 $this->delete($shelf->getUrl());
369 $resp = $this->asEditor()->get($newBook->getUrl());
370 $resp->assertDontSee($shelfInfo['name']);
373 public function test_cancel_on_child_book_creation_returns_to_original_shelf()
375 /** @var Bookshelf $shelf */
376 $shelf = Bookshelf::query()->first();
377 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
378 $resp->assertElementContains('form a[href="' . $shelf->getUrl() . '"]', 'Cancel');