4 use BookStack\Bookshelf;
6 class BookShelfTest extends TestCase
9 public function test_shelves_shows_in_header_if_have_view_permissions()
11 $viewer = $this->getViewer();
12 $resp = $this->actingAs($viewer)->get('/');
13 $resp->assertElementContains('header', 'Shelves');
15 $viewer->roles()->delete();
16 $this->giveUserPermissions($viewer);
17 $resp = $this->actingAs($viewer)->get('/');
18 $resp->assertElementNotContains('header', 'Shelves');
20 $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
21 $resp = $this->actingAs($viewer)->get('/');
22 $resp->assertElementContains('header', 'Shelves');
24 $viewer->roles()->delete();
25 $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
26 $resp = $this->actingAs($viewer)->get('/');
27 $resp->assertElementContains('header', 'Shelves');
30 public function test_shelves_page_contains_create_link()
32 $resp = $this->asEditor()->get('/shelves');
33 $resp->assertElementContains('a', 'Create New Shelf');
36 public function test_shelves_create()
38 $booksToInclude = Book::take(2)->get();
40 'name' => 'My test book' . str_random(4),
41 'description' => 'Test book description ' . str_random(10)
43 $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
44 'books' => $booksToInclude->implode('id', ','),
47 'name' => 'Test Category',
48 'value' => 'Test Tag Value',
52 $resp->assertRedirect();
53 $editorId = $this->getEditor()->id;
54 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
56 $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
57 $shelfPage = $this->get($shelf->getUrl());
58 $shelfPage->assertSee($shelfInfo['name']);
59 $shelfPage->assertSee($shelfInfo['description']);
60 $shelfPage->assertElementContains('.tag-item', 'Test Category');
61 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
63 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
64 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
67 public function test_shelf_view()
69 $shelf = Bookshelf::first();
70 $resp = $this->asEditor()->get($shelf->getUrl());
71 $resp->assertStatus(200);
72 $resp->assertSeeText($shelf->name);
73 $resp->assertSeeText($shelf->description);
75 foreach ($shelf->books as $book) {
76 $resp->assertSee($book->name);
80 public function test_shelf_view_shows_action_buttons()
82 $shelf = Bookshelf::first();
83 $resp = $this->asAdmin()->get($shelf->getUrl());
84 $resp->assertSee($shelf->getUrl('/edit'));
85 $resp->assertSee($shelf->getUrl('/permissions'));
86 $resp->assertSee($shelf->getUrl('/delete'));
87 $resp->assertElementContains('a', 'Edit');
88 $resp->assertElementContains('a', 'Permissions');
89 $resp->assertElementContains('a', 'Delete');
91 $resp = $this->asEditor()->get($shelf->getUrl());
92 $resp->assertDontSee($shelf->getUrl('/permissions'));
95 public function test_shelf_edit()
97 $shelf = Bookshelf::first();
98 $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
99 $resp->assertSeeText('Edit Bookshelf');
101 $booksToInclude = Book::take(2)->get();
103 'name' => 'My test book' . str_random(4),
104 'description' => 'Test book description ' . str_random(10)
107 $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
108 'books' => $booksToInclude->implode('id', ','),
111 'name' => 'Test Category',
112 'value' => 'Test Tag Value',
116 $shelf = Bookshelf::find($shelf->id);
117 $resp->assertRedirect($shelf->getUrl());
118 $this->assertSessionHas('success');
120 $editorId = $this->getEditor()->id;
121 $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
123 $shelfPage = $this->get($shelf->getUrl());
124 $shelfPage->assertSee($shelfInfo['name']);
125 $shelfPage->assertSee($shelfInfo['description']);
126 $shelfPage->assertElementContains('.tag-item', 'Test Category');
127 $shelfPage->assertElementContains('.tag-item', 'Test Tag Value');
129 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
130 $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
133 public function test_shelf_delete()
135 $shelf = Bookshelf::first();
136 $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
137 $resp->assertSeeText('Delete Bookshelf');
138 $resp->assertSee("action=\"{$shelf->getUrl()}\"");
140 $resp = $this->delete($shelf->getUrl());
141 $resp->assertRedirect('/shelves');
142 $this->assertDatabaseMissing('bookshelves', ['id' => $shelf->id]);
143 $this->assertDatabaseMissing('bookshelves_books', ['bookshelf_id' => $shelf->id]);
144 $this->assertSessionHas('success');
147 public function test_shelf_copy_permissions()
149 $shelf = Bookshelf::first();
150 $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
151 $resp->assertSeeText('Copy Permissions');
152 $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"");
154 $child = $shelf->books()->first();
155 $editorRole = $this->getEditor()->roles()->first();
156 $this->assertFalse(boolval($child->restricted), "Child book should not be restricted by default");
157 $this->assertTrue($child->permissions()->count() === 0, "Child book should have no permissions by default");
159 $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
160 $resp = $this->post($shelf->getUrl('/copy-permissions'));
161 $child = $shelf->books()->first();
163 $resp->assertRedirect($shelf->getUrl());
164 $this->assertTrue(boolval($child->restricted), "Child book should now be restricted");
165 $this->assertTrue($child->permissions()->count() === 2, "Child book should have copied permissions");
166 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'view', 'role_id' => $editorRole->id]);
167 $this->assertDatabaseHas('entity_permissions', ['restrictable_id' => $child->id, 'action' => 'update', 'role_id' => $editorRole->id]);