3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Bookshelf;
8 class BookShelfTest extends TestCase
11 public function test_shelves_shows_in_header_if_have_view_permissions()
13 $viewer = $this->getViewer();
14 $resp = $this->actingAs($viewer)->get('/');
15 $resp->assertElementContains('header', 'Shelves');
17 $viewer->roles()->delete();
18 $this->giveUserPermissions($viewer);
19 $resp = $this->actingAs($viewer)->get('/');
20 $resp->assertElementNotContains('header', 'Shelves');
22 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
23 $resp = $this->actingAs($viewer)->get('/');
24 $resp->assertElementContains('header', 'Shelves');
26 $viewer->roles()->delete();
27 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
28 $resp = $this->actingAs($viewer)->get('/');
29 $resp->assertElementContains('header', 'Shelves');
32 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
34 $user = factory(User::class)->create();
35 $this->giveUserPermissions($user, ['image-create-all']);
36 $shelf = Bookshelf::first();
37 $userRole = $user->roles()->first();
39 $resp = $this->actingAs($user)->get('/');
40 $resp->assertElementNotContains('header', 'Shelves');
42 $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
44 $resp = $this->get('/');
45 $resp->assertElementContains('header', 'Shelves');
48 public function test_shelves_page_contains_create_link()
50 $resp = $this->asEditor()->get('/shelves');
51 $resp->assertElementContains('a', 'New Shelf');
54 public function test_shelves_create()
56 $booksToInclude = Book::take(2)->get();
58 'name' => 'My test book' . str_random(4),
59 'description' => 'Test book description ' . str_random(10)
61 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
62 'books' => $booksToInclude->implode('id', ','),
65 'name' => 'Test Category',
66 'value' => 'Test Tag Value',
70 $resp->assertRedirect();
71 $editorId = $this->getEditor()->id;
72 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
74 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
75 $shelfPage = $this->get($shelf->getUrl());
76 $shelfPage->assertSee($shelfInfo['name']);
77 $shelfPage->assertSee($shelfInfo['description']);
78 $shelfPage->assertElementContains('.tag-item', 'Test Category');
79 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
81 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
82 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
85 public function test_shelf_view()
87 $shelf = Bookshelf::first();
88 $resp = $this->asEditor()->get($shelf->getUrl());
89 $resp->assertStatus(200);
90 $resp->assertSeeText($shelf->name);
91 $resp->assertSeeText($shelf->description);
93 foreach ($shelf->books as $book) {
94 $resp->assertSee($book->name);
98 public function test_shelf_view_shows_action_buttons()
100 $shelf = Bookshelf::first();
101 $resp = $this->asAdmin()->get($shelf->getUrl());
102 $resp->assertSee($shelf->getUrl('/create-book'));
103 $resp->assertSee($shelf->getUrl('/edit'));
104 $resp->assertSee($shelf->getUrl('/permissions'));
105 $resp->assertSee($shelf->getUrl('/delete'));
106 $resp->assertElementContains('a', 'New Book');
107 $resp->assertElementContains('a', 'Edit');
108 $resp->assertElementContains('a', 'Permissions');
109 $resp->assertElementContains('a', 'Delete');
111 $resp = $this->asEditor()->get($shelf->getUrl());
112 $resp->assertDontSee($shelf->getUrl('/permissions'));
115 public function test_shelf_edit()
117 $shelf = Bookshelf::first();
118 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
119 $resp->assertSeeText('Edit Bookshelf');
121 $booksToInclude = Book::take(2)->get();
123 'name' => 'My test book' . str_random(4),
124 'description' => 'Test book description ' . str_random(10)
127 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
128 'books' => $booksToInclude->implode('id', ','),
131 'name' => 'Test Category',
132 'value' => 'Test Tag Value',
136 $shelf = Bookshelf::find($shelf->id);
137 $resp->assertRedirect($shelf->getUrl());
138 $this->assertSessionHas('success');
140 $editorId = $this->getEditor()->id;
141 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
143 $shelfPage = $this->get($shelf->getUrl());
144 $shelfPage->assertSee($shelfInfo['name']);
145 $shelfPage->assertSee($shelfInfo['description']);
146 $shelfPage->assertElementContains('.tag-item', 'Test Category');
147 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
149 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
150 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
153 public function test_shelf_create_new_book()
155 $shelf = Bookshelf::first();
156 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
158 $resp->assertSee('Create New Book');
159 $resp->assertSee($shelf->getShortName());
161 $testName = 'Test Book in Shelf Name';
163 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
165 'description' => 'Book in shelf description'
167 $createBookResp->assertRedirect();
169 $newBook = Book::query()->orderBy('id', 'desc')->first();
170 $this->assertDatabaseHas('bookshelves_books', [
171 'bookshelf_id' => $shelf->id,
172 'book_id' => $newBook->id,
175 $resp = $this->asEditor()->get($shelf->getUrl());
176 $resp->assertSee($testName);
179 public function test_shelf_delete()
181 $shelf = Bookshelf::first();
182 $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
183 $resp->assertSeeText('Delete Bookshelf');
184 $resp->assertSee("action=\"{$shelf->getUrl()}\"");
186 $resp = $this->delete($shelf->getUrl());
187 $resp->assertRedirect('/shelves');
188 $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
189 $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
190 $this->assertSessionHas('success');
193 public function test_shelf_copy_permissions()
195 $shelf = Bookshelf::first();
196 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
197 $resp->assertSeeText('Copy Permissions');
198 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
200 $child = $shelf->books()->first();
201 $editorRole = $this->getEditor()->roles()->first();
202 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
203 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
205 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
206 $resp = $this->post($shelf->getUrl('/copy-permissions'));
207 $child = $shelf->books()->first();
209 $resp->assertRedirect($shelf->getUrl());
210 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
211 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
212 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
213 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
216 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
218 $shelf = Bookshelf::first();
219 $shelfBook = $shelf->books()->first();
220 $shelfPage = $shelfBook->pages()->first();
223 $bookVisit = $this->get($shelfBook->getUrl());
224 $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
225 $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
227 $this->get($shelf->getUrl());
228 $bookVisit = $this->get($shelfBook->getUrl());
229 $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
230 $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
232 $pageVisit = $this->get($shelfPage->getUrl());
233 $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
234 $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
236 $this->get('/books');
237 $pageVisit = $this->get($shelfPage->getUrl());
238 $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
239 $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());