1 <?php namespace Tests\Entity;
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;
9 use Tests\Uploads\UsesImages;
11 class BookShelfTest extends TestCase
16 public function test_shelves_shows_in_header_if_have_view_permissions()
18 $viewer = $this->getViewer();
19 $resp = $this->actingAs($viewer)->get('/');
20 $resp->assertElementContains('header', 'Shelves');
22 $viewer->roles()->delete();
23 $this->giveUserPermissions($viewer);
24 $resp = $this->actingAs($viewer)->get('/');
25 $resp->assertElementNotContains('header', 'Shelves');
27 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
28 $resp = $this->actingAs($viewer)->get('/');
29 $resp->assertElementContains('header', 'Shelves');
31 $viewer->roles()->delete();
32 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
33 $resp = $this->actingAs($viewer)->get('/');
34 $resp->assertElementContains('header', 'Shelves');
37 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
39 $user = factory(User::class)->create();
40 $this->giveUserPermissions($user, ['image-create-all']);
41 $shelf = Bookshelf::first();
42 $userRole = $user->roles()->first();
44 $resp = $this->actingAs($user)->get('/');
45 $resp->assertElementNotContains('header', 'Shelves');
47 $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
49 $resp = $this->get('/');
50 $resp->assertElementContains('header', 'Shelves');
53 public function test_shelves_page_contains_create_link()
55 $resp = $this->asEditor()->get('/shelves');
56 $resp->assertElementContains('a', 'New Shelf');
59 public function test_shelves_create()
61 $booksToInclude = Book::take(2)->get();
63 'name' => 'My test book' . Str::random(4),
64 'description' => 'Test book description ' . Str::random(10)
66 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
67 'books' => $booksToInclude->implode('id', ','),
70 'name' => 'Test Category',
71 'value' => 'Test Tag Value',
75 $resp->assertRedirect();
76 $editorId = $this->getEditor()->id;
77 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
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');
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]);
90 public function test_shelves_create_sets_cover_image()
93 'name' => 'My test book' . Str::random(4),
94 'description' => 'Test book description ' . Str::random(10)
97 $imageFile = $this->getTestImage('shelf-test.png');
98 $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
99 $resp->assertRedirect();
101 $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
102 $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
103 $this->assertDatabaseHas('bookshelves', [
105 'image_id' => $lastImage->id,
107 $this->assertEquals($lastImage->id, $shelf->cover->id);
110 public function test_shelf_view()
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);
118 foreach ($shelf->books as $book) {
119 $resp->assertSee($book->name);
123 public function test_shelf_view_shows_action_buttons()
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');
136 $resp = $this->asEditor()->get($shelf->getUrl());
137 $resp->assertDontSee($shelf->getUrl('/permissions'));
140 public function test_shelf_edit()
142 $shelf = Bookshelf::first();
143 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
144 $resp->assertSeeText('Edit Bookshelf');
146 $booksToInclude = Book::take(2)->get();
148 'name' => 'My test book' . Str::random(4),
149 'description' => 'Test book description ' . Str::random(10)
152 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
153 'books' => $booksToInclude->implode('id', ','),
156 'name' => 'Test Category',
157 'value' => 'Test Tag Value',
161 $shelf = Bookshelf::find($shelf->id);
162 $resp->assertRedirect($shelf->getUrl());
163 $this->assertSessionHas('success');
165 $editorId = $this->getEditor()->id;
166 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
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');
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]);
178 public function test_shelf_create_new_book()
180 $shelf = Bookshelf::first();
181 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
183 $resp->assertSee('Create New Book');
184 $resp->assertSee($shelf->getShortName());
186 $testName = 'Test Book in Shelf Name';
188 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
190 'description' => 'Book in shelf description'
192 $createBookResp->assertRedirect();
194 $newBook = Book::query()->orderBy('id', 'desc')->first();
195 $this->assertDatabaseHas('bookshelves_books', [
196 'bookshelf_id' => $shelf->id,
197 'book_id' => $newBook->id,
200 $resp = $this->asEditor()->get($shelf->getUrl());
201 $resp->assertSee($testName);
204 public function test_shelf_delete()
206 $shelf = Bookshelf::first();
207 $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
208 $resp->assertSeeText('Delete Bookshelf');
209 $resp->assertSee("action=\"{$shelf->getUrl()}\"");
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');
218 public function test_shelf_copy_permissions()
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')}\"");
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");
230 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
231 $resp = $this->post($shelf->getUrl('/copy-permissions'));
232 $child = $shelf->books()->first();
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]);
241 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
243 $shelf = Bookshelf::first();
244 $shelfBook = $shelf->books()->first();
245 $shelfPage = $shelfBook->pages()->first();
248 $bookVisit = $this->get($shelfBook->getUrl());
249 $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
250 $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
252 $this->get($shelf->getUrl());
253 $bookVisit = $this->get($shelfBook->getUrl());
254 $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
255 $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
257 $pageVisit = $this->get($shelfPage->getUrl());
258 $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
259 $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
261 $this->get('/books');
262 $pageVisit = $this->get($shelfPage->getUrl());
263 $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
264 $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
267 public function test_bookshelves_show_on_book()
271 'name' => 'My test shelf' . Str::random(4),
272 'description' => 'Test shelf description ' . Str::random(10)
275 $this->asEditor()->post('/shelves', $shelfInfo);
276 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
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'
284 $newBook = Book::query()->orderBy('id', 'desc')->first();
286 $resp = $this->asEditor()->get($newBook->getUrl());
287 $resp->assertSee($shelfInfo['name']);
290 $this->delete($shelf->getUrl());
292 $resp = $this->asEditor()->get($newBook->getUrl());
293 $resp->assertDontSee($shelfInfo['name']);