3 use BookStack\Auth\Role;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Book;
6 use BookStack\Entities\Bookshelf;
7 use Illuminate\Support\Str;
9 class BookShelfTest extends TestCase
12 public function test_shelves_shows_in_header_if_have_view_permissions()
14 $viewer = $this->getViewer();
15 $resp = $this->actingAs($viewer)->get('/');
16 $resp->assertElementContains('header', 'Shelves');
18 $viewer->roles()->delete();
19 $this->giveUserPermissions($viewer);
20 $resp = $this->actingAs($viewer)->get('/');
21 $resp->assertElementNotContains('header', 'Shelves');
23 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
24 $resp = $this->actingAs($viewer)->get('/');
25 $resp->assertElementContains('header', 'Shelves');
27 $viewer->roles()->delete();
28 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
29 $resp = $this->actingAs($viewer)->get('/');
30 $resp->assertElementContains('header', 'Shelves');
33 public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
35 $user = factory(User::class)->create();
36 $this->giveUserPermissions($user, ['image-create-all']);
37 $shelf = Bookshelf::first();
38 $userRole = $user->roles()->first();
40 $resp = $this->actingAs($user)->get('/');
41 $resp->assertElementNotContains('header', 'Shelves');
43 $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
45 $resp = $this->get('/');
46 $resp->assertElementContains('header', 'Shelves');
49 public function test_shelves_page_contains_create_link()
51 $resp = $this->asEditor()->get('/shelves');
52 $resp->assertElementContains('a', 'New Shelf');
55 public function test_shelves_create()
57 $booksToInclude = Book::take(2)->get();
59 'name' => 'My test book' . Str::random(4),
60 'description' => 'Test book description ' . Str::random(10)
62 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
63 'books' => $booksToInclude->implode('id', ','),
66 'name' => 'Test Category',
67 'value' => 'Test Tag Value',
71 $resp->assertRedirect();
72 $editorId = $this->getEditor()->id;
73 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
75 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
76 $shelfPage = $this->get($shelf->getUrl());
77 $shelfPage->assertSee($shelfInfo['name']);
78 $shelfPage->assertSee($shelfInfo['description']);
79 $shelfPage->assertElementContains('.tag-item', 'Test Category');
80 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
82 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
83 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
86 public function test_shelf_view()
88 $shelf = Bookshelf::first();
89 $resp = $this->asEditor()->get($shelf->getUrl());
90 $resp->assertStatus(200);
91 $resp->assertSeeText($shelf->name);
92 $resp->assertSeeText($shelf->description);
94 foreach ($shelf->books as $book) {
95 $resp->assertSee($book->name);
99 public function test_shelf_view_shows_action_buttons()
101 $shelf = Bookshelf::first();
102 $resp = $this->asAdmin()->get($shelf->getUrl());
103 $resp->assertSee($shelf->getUrl('/create-book'));
104 $resp->assertSee($shelf->getUrl('/edit'));
105 $resp->assertSee($shelf->getUrl('/permissions'));
106 $resp->assertSee($shelf->getUrl('/delete'));
107 $resp->assertElementContains('a', 'New Book');
108 $resp->assertElementContains('a', 'Edit');
109 $resp->assertElementContains('a', 'Permissions');
110 $resp->assertElementContains('a', 'Delete');
112 $resp = $this->asEditor()->get($shelf->getUrl());
113 $resp->assertDontSee($shelf->getUrl('/permissions'));
116 public function test_shelf_edit()
118 $shelf = Bookshelf::first();
119 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
120 $resp->assertSeeText('Edit Bookshelf');
122 $booksToInclude = Book::take(2)->get();
124 'name' => 'My test book' . Str::random(4),
125 'description' => 'Test book description ' . Str::random(10)
128 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
129 'books' => $booksToInclude->implode('id', ','),
132 'name' => 'Test Category',
133 'value' => 'Test Tag Value',
137 $shelf = Bookshelf::find($shelf->id);
138 $resp->assertRedirect($shelf->getUrl());
139 $this->assertSessionHas('success');
141 $editorId = $this->getEditor()->id;
142 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
144 $shelfPage = $this->get($shelf->getUrl());
145 $shelfPage->assertSee($shelfInfo['name']);
146 $shelfPage->assertSee($shelfInfo['description']);
147 $shelfPage->assertElementContains('.tag-item', 'Test Category');
148 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
150 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
151 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
154 public function test_shelf_create_new_book()
156 $shelf = Bookshelf::first();
157 $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
159 $resp->assertSee('Create New Book');
160 $resp->assertSee($shelf->getShortName());
162 $testName = 'Test Book in Shelf Name';
164 $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
166 'description' => 'Book in shelf description'
168 $createBookResp->assertRedirect();
170 $newBook = Book::query()->orderBy('id', 'desc')->first();
171 $this->assertDatabaseHas('bookshelves_books', [
172 'bookshelf_id' => $shelf->id,
173 'book_id' => $newBook->id,
176 $resp = $this->asEditor()->get($shelf->getUrl());
177 $resp->assertSee($testName);
180 public function test_shelf_delete()
182 $shelf = Bookshelf::first();
183 $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
184 $resp->assertSeeText('Delete Bookshelf');
185 $resp->assertSee("action=\"{$shelf->getUrl()}\"");
187 $resp = $this->delete($shelf->getUrl());
188 $resp->assertRedirect('/shelves');
189 $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
190 $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
191 $this->assertSessionHas('success');
194 public function test_shelf_copy_permissions()
196 $shelf = Bookshelf::first();
197 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
198 $resp->assertSeeText('Copy Permissions');
199 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
201 $child = $shelf->books()->first();
202 $editorRole = $this->getEditor()->roles()->first();
203 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
204 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
206 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
207 $resp = $this->post($shelf->getUrl('/copy-permissions'));
208 $child = $shelf->books()->first();
210 $resp->assertRedirect($shelf->getUrl());
211 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
212 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
213 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
214 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);
217 public function test_bookshelves_show_in_breadcrumbs_if_in_context()
219 $shelf = Bookshelf::first();
220 $shelfBook = $shelf->books()->first();
221 $shelfPage = $shelfBook->pages()->first();
224 $bookVisit = $this->get($shelfBook->getUrl());
225 $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
226 $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
228 $this->get($shelf->getUrl());
229 $bookVisit = $this->get($shelfBook->getUrl());
230 $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
231 $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
233 $pageVisit = $this->get($shelfPage->getUrl());
234 $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
235 $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
237 $this->get('/books');
238 $pageVisit = $this->get($shelfPage->getUrl());
239 $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
240 $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());