]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Add git to the apt-get install packages.
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php namespace Tests;
2
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;
8
9 class BookShelfTest extends TestCase
10 {
11
12     public function test_shelves_shows_in_header_if_have_view_permissions()
13     {
14         $viewer = $this->getViewer();
15         $resp = $this->actingAs($viewer)->get('/');
16         $resp->assertElementContains('header', 'Shelves');
17
18         $viewer->roles()->delete();
19         $this->giveUserPermissions($viewer);
20         $resp = $this->actingAs($viewer)->get('/');
21         $resp->assertElementNotContains('header', 'Shelves');
22
23         $this->giveUserPermissions($viewer, ['bookshelf-view-all']);
24         $resp = $this->actingAs($viewer)->get('/');
25         $resp->assertElementContains('header', 'Shelves');
26
27         $viewer->roles()->delete();
28         $this->giveUserPermissions($viewer, ['bookshelf-view-own']);
29         $resp = $this->actingAs($viewer)->get('/');
30         $resp->assertElementContains('header', 'Shelves');
31     }
32
33     public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
34     {
35         $user = factory(User::class)->create();
36         $this->giveUserPermissions($user, ['image-create-all']);
37         $shelf = Bookshelf::first();
38         $userRole = $user->roles()->first();
39
40         $resp = $this->actingAs($user)->get('/');
41         $resp->assertElementNotContains('header', 'Shelves');
42
43         $this->setEntityRestrictions($shelf, ['view'], [$userRole]);
44
45         $resp = $this->get('/');
46         $resp->assertElementContains('header', 'Shelves');
47     }
48
49     public function test_shelves_page_contains_create_link()
50     {
51         $resp = $this->asEditor()->get('/shelves');
52         $resp->assertElementContains('a', 'New Shelf');
53     }
54
55     public function test_shelves_create()
56     {
57         $booksToInclude = Book::take(2)->get();
58         $shelfInfo = [
59             'name' => 'My test book' . Str::random(4),
60             'description' => 'Test book description ' . Str::random(10)
61         ];
62         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
63             'books' => $booksToInclude->implode('id', ','),
64             'tags' => [
65                 [
66                     'name' => 'Test Category',
67                     'value' => 'Test Tag Value',
68                 ]
69             ],
70         ]));
71         $resp->assertRedirect();
72         $editorId = $this->getEditor()->id;
73         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
74
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');
81
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]);
84     }
85
86     public function test_shelf_view()
87     {
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);
93
94         foreach ($shelf->books as $book) {
95             $resp->assertSee($book->name);
96         }
97     }
98
99     public function test_shelf_view_shows_action_buttons()
100     {
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');
111
112         $resp = $this->asEditor()->get($shelf->getUrl());
113         $resp->assertDontSee($shelf->getUrl('/permissions'));
114     }
115
116     public function test_shelf_edit()
117     {
118         $shelf = Bookshelf::first();
119         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
120         $resp->assertSeeText('Edit Bookshelf');
121
122         $booksToInclude = Book::take(2)->get();
123         $shelfInfo = [
124             'name' => 'My test book' . Str::random(4),
125             'description' => 'Test book description ' . Str::random(10)
126         ];
127
128         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
129             'books' => $booksToInclude->implode('id', ','),
130             'tags' => [
131                 [
132                     'name' => 'Test Category',
133                     'value' => 'Test Tag Value',
134                 ]
135             ],
136         ]));
137         $shelf = Bookshelf::find($shelf->id);
138         $resp->assertRedirect($shelf->getUrl());
139         $this->assertSessionHas('success');
140
141         $editorId = $this->getEditor()->id;
142         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
143
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');
149
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]);
152     }
153
154     public function test_shelf_create_new_book()
155     {
156         $shelf = Bookshelf::first();
157         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
158
159         $resp->assertSee('Create New Book');
160         $resp->assertSee($shelf->getShortName());
161
162         $testName = 'Test Book in Shelf Name';
163
164         $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
165             'name' => $testName,
166             'description' => 'Book in shelf description'
167         ]);
168         $createBookResp->assertRedirect();
169
170         $newBook = Book::query()->orderBy('id', 'desc')->first();
171         $this->assertDatabaseHas('bookshelves_books', [
172             'bookshelf_id' => $shelf->id,
173             'book_id' => $newBook->id,
174         ]);
175
176         $resp = $this->asEditor()->get($shelf->getUrl());
177         $resp->assertSee($testName);
178     }
179
180     public function test_shelf_delete()
181     {
182         $shelf = Bookshelf::first();
183         $resp = $this->asEditor()->get($shelf->getUrl('/delete'));
184         $resp->assertSeeText('Delete Bookshelf');
185         $resp->assertSee("action=\"{$shelf->getUrl()}\"");
186
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');
192     }
193
194     public function test_shelf_copy_permissions()
195     {
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')}\"");
200
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");
205
206         $this->setEntityRestrictions($shelf, ['view', 'update'], [$editorRole]);
207         $resp = $this->post($shelf->getUrl('/copy-permissions'));
208         $child = $shelf->books()->first();
209
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]);
215     }
216
217     public function test_bookshelves_show_in_breadcrumbs_if_in_context()
218     {
219         $shelf = Bookshelf::first();
220         $shelfBook = $shelf->books()->first();
221         $shelfPage = $shelfBook->pages()->first();
222         $this->asAdmin();
223
224         $bookVisit = $this->get($shelfBook->getUrl());
225         $bookVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
226         $bookVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
227
228         $this->get($shelf->getUrl());
229         $bookVisit = $this->get($shelfBook->getUrl());
230         $bookVisit->assertElementContains('.breadcrumbs', 'Shelves');
231         $bookVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
232
233         $pageVisit = $this->get($shelfPage->getUrl());
234         $pageVisit->assertElementContains('.breadcrumbs', 'Shelves');
235         $pageVisit->assertElementContains('.breadcrumbs', $shelf->getShortName());
236
237         $this->get('/books');
238         $pageVisit = $this->get($shelfPage->getUrl());
239         $pageVisit->assertElementNotContains('.breadcrumbs', 'Shelves');
240         $pageVisit->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
241     }
242
243 }