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;
8 use Tests\Uploads\UsesImages;
10 class BookShelfTest extends TestCase
15 public function test_shelves_shows_in_header_if_have_view_permissions()
17 $viewer = $this->getViewer();
18 $resp = $this->actingAs($viewer)->get('/');
19 $resp->assertElementContains('header', 'Shelves');
21 $viewer->roles()->delete();
22 $this->giveUserPermissions($viewer);
23 $resp = $this->actingAs($viewer)->get('/');
24 $resp->assertElementNotContains('header', 'Shelves');
26 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
27 $resp = $this->actingAs($viewer)->get('/');
28 $resp->assertElementContains('header', 'Shelves');
30 $viewer->roles()->delete();
31 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
32 $resp = $this->actingAs($viewer)->get('/');
33 $resp->assertElementContains('header', 'Shelves');
36 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
38 $user = factory(User::class)->create();
39 $this->giveUserPermissions($user, ['image-create-all']);
40 $shelf = Bookshelf::first();
41 $userRole = $user->roles()->first();
43 $resp = $this->actingAs($user)->get('/');
44 $resp->assertElementNotContains('header', 'Shelves');
46 $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
48 $resp = $this->get('/');
49 $resp->assertElementContains('header', 'Shelves');
52 public function test_shelves_page_contains_create_link()
54 $resp = $this->asEditor()->get('/shelves');
55 $resp->assertElementContains('a', 'New Shelf');
58 public function test_shelves_create()
60 $booksToInclude = Book::take(2)->get();
62 'name' => 'My test book' . Str::random(4),
63 'description' => 'Test book description ' . Str::random(10)
65 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
66 'books' => $booksToInclude->implode('id', ','),
69 'name' => 'Test Category',
70 'value' => 'Test Tag Value',
74 $resp->assertRedirect();
75 $editorId = $this->getEditor()->id;
76 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
78 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
79 $shelfPage = $this->get($shelf->getUrl());
80 $shelfPage->assertSee($shelfInfo['name']);
81 $shelfPage->assertSee($shelfInfo['description']);
82 $shelfPage->assertElementContains('.tag-item', 'Test Category');
83 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
85 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
86 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
89 public function test_shelves_create_sets_cover_image()
92 'name' => 'My test book' . Str::random(4),
93 'description' => 'Test book description ' . Str::random(10)
96 $imageFile = $this->getTestImage('shelf-test.png');
97 $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
98 $resp->assertRedirect();
100 $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
101 $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
102 $this->assertDatabaseHas('bookshelves', [
104 'image_id' => $lastImage->id,
106 $this->assertEquals($lastImage->id, $shelf->cover->id);
109 public function test_shelf_view()
111 $shelf = Bookshelf::first();
112 $resp = $this->asEditor()->get($shelf->getUrl());
113 $resp->assertStatus(200);
114 $resp->assertSeeText($shelf->name);
115 $resp->assertSeeText($shelf->description);
117 foreach ($shelf->books as $book) {
118 $resp->assertSee($book->name);
122 public function test_shelf_view_shows_action_buttons()
124 $shelf = Bookshelf::first();
125 $resp = $this->asAdmin()->get($shelf->getUrl());
126 $resp->assertSee($shelf->getUrl('/create-book'));
127 $resp->assertSee($shelf->getUrl('/edit'));
128 $resp->assertSee($shelf->getUrl('/permissions'));
129 $resp->assertSee($shelf->getUrl('/delete'));
130 $resp->assertElementContains('a', 'New Book');
131 $resp->assertElementContains('a', 'Edit');
132 $resp->assertElementContains('a', 'Permissions');
133 $resp->assertElementContains('a', 'Delete');
135 $resp = $this->asEditor()->get($shelf->getUrl());
136 $resp->assertDontSee($shelf->getUrl('/permissions'));
139 public function test_shelf_edit()
141 $shelf = Bookshelf::first();
142 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
143 $resp->assertSeeText('Edit Bookshelf');
145 $booksToInclude = Book::take(2)->get();
147 'name' => 'My test book' . Str::random(4),
148 'description' => 'Test book description ' . Str::random(10)
151 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
152 'books' => $booksToInclude->implode('id', ','),
155 'name' => 'Test Category',
156 'value' => 'Test Tag Value',
160 $shelf = Bookshelf::find($shelf->id);
161 $resp->assertRedirect($shelf->getUrl());
162 $this->assertSessionHas('success');
164 $editorId = $this->getEditor()->id;
165 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
167 $shelfPage = $this->get($shelf->getUrl());
168 $shelfPage->assertSee($shelfInfo['name']);
169 $shelfPage->assertSee($shelfInfo['description']);
170 $shelfPage->assertElementContains('.tag-item', 'Test Category');
171 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
173 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
174 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
177 public function test_shelf_create_new_book()
179 $shelf = Bookshelf::first();
180 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
182 $resp->assertSee('Create New Book');
183 $resp->assertSee($shelf->getShortName());
185 $testName = 'Test Book in Shelf Name';
187 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
189 'description' => 'Book in shelf description'
191 $createBookResp->assertRedirect();
193 $newBook = Book::query()->orderBy('id', 'desc')->first();
194 $this->assertDatabaseHas('bookshelves_books', [
195 'bookshelf_id' => $shelf->id,
196 'book_id' => $newBook->id,
199 $resp = $this->asEditor()->get($shelf->getUrl());
200 $resp->assertSee($testName);
203 public function test_shelf_delete()
205 $shelf = Bookshelf::first();
206 $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
207 $resp->assertSeeText('Delete Bookshelf');
208 $resp->assertSee("action=\"{$shelf->getUrl()}\"");
210 $resp = $this->delete($shelf->getUrl());
211 $resp->assertRedirect('/shelves');
212 $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
213 $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
214 $this->assertSessionHas('success');
217 public function test_shelf_copy_permissions()
219 $shelf = Bookshelf::first();
220 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
221 $resp->assertSeeText('Copy Permissions');
222 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
224 $child = $shelf->books()->first();
225 $editorRole = $this->getEditor()->roles()->first();
226 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
227 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
229 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
230 $resp = $this->post($shelf->getUrl('/copy-permissions'));
231 $child = $shelf->books()->first();
233 $resp->assertRedirect($shelf->getUrl());
234 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
235 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
236 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
237 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
240 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
242 $shelf = Bookshelf::first();
243 $shelfBook = $shelf->books()->first();
244 $shelfPage = $shelfBook->pages()->first();
247 $bookVisit = $this->get($shelfBook->getUrl());
248 $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
249 $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
251 $this->get($shelf->getUrl());
252 $bookVisit = $this->get($shelfBook->getUrl());
253 $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
254 $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
256 $pageVisit = $this->get($shelfPage->getUrl());
257 $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
258 $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
260 $this->get('/books');
261 $pageVisit = $this->get($shelfPage->getUrl());
262 $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
263 $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());