]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookShelfTest.php
Added method for using enity ownership in relation queries
[bookstack] / tests / Entity / BookShelfTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Auth\User;
6 use BookStack\Entities\Models\Book;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Uploads\Image;
9 use Illuminate\Support\Str;
10 use Tests\TestCase;
11 use Tests\Uploads\UsesImages;
12
13 class BookShelfTest extends TestCase
14 {
15     use UsesImages;
16
17     public function test_shelves_shows_in_header_if_have_view_permissions()
18     {
19         $viewer = $this->users->viewer();
20         $resp = $this->actingAs($viewer)->get('/');
21         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
22
23         $viewer->roles()->delete();
24         $resp = $this->actingAs($viewer)->get('/');
25         $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
26
27         $this->permissions->grantUserRolePermissions($viewer, ['bookshelf-view-all']);
28         $resp = $this->actingAs($viewer)->get('/');
29         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
30
31         $viewer->roles()->delete();
32         $this->permissions->grantUserRolePermissions($viewer, ['bookshelf-view-own']);
33         $resp = $this->actingAs($viewer)->get('/');
34         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
35     }
36
37     public function test_shelves_shows_in_header_if_have_any_shelve_view_permission()
38     {
39         $user = User::factory()->create();
40         $this->permissions->grantUserRolePermissions($user, ['image-create-all']);
41         $shelf = $this->entities->shelf();
42         $userRole = $user->roles()->first();
43
44         $resp = $this->actingAs($user)->get('/');
45         $this->withHtml($resp)->assertElementNotContains('header', 'Shelves');
46
47         $this->permissions->setEntityPermissions($shelf, ['view'], [$userRole]);
48
49         $resp = $this->get('/');
50         $this->withHtml($resp)->assertElementContains('header', 'Shelves');
51     }
52
53     public function test_shelves_page_contains_create_link()
54     {
55         $resp = $this->asEditor()->get('/shelves');
56         $this->withHtml($resp)->assertElementContains('a', 'New Shelf');
57     }
58
59     public function test_book_not_visible_in_shelf_list_view_if_user_cant_view_shelf()
60     {
61         config()->set([
62             'setting-defaults.user.bookshelves_view_type' => 'list',
63         ]);
64         $shelf = $this->entities->shelf();
65         $book = $shelf->books()->first();
66
67         $resp = $this->asEditor()->get('/shelves');
68         $resp->assertSee($book->name);
69         $resp->assertSee($book->getUrl());
70
71         $this->permissions->setEntityPermissions($book, []);
72
73         $resp = $this->asEditor()->get('/shelves');
74         $resp->assertDontSee($book->name);
75         $resp->assertDontSee($book->getUrl());
76     }
77
78     public function test_shelves_create()
79     {
80         $booksToInclude = Book::take(2)->get();
81         $shelfInfo = [
82             'name'        => 'My test book' . Str::random(4),
83             'description' => 'Test book description ' . Str::random(10),
84         ];
85         $resp = $this->asEditor()->post('/shelves', array_merge($shelfInfo, [
86             'books' => $booksToInclude->implode('id', ','),
87             'tags'  => [
88                 [
89                     'name'  => 'Test Category',
90                     'value' => 'Test Tag Value',
91                 ],
92             ],
93         ]));
94         $resp->assertRedirect();
95         $editorId = $this->users->editor()->id;
96         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['created_by' => $editorId, 'updated_by' => $editorId]));
97
98         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
99         $shelfPage = $this->get($shelf->getUrl());
100         $shelfPage->assertSee($shelfInfo['name']);
101         $shelfPage->assertSee($shelfInfo['description']);
102         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Category');
103         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Tag Value');
104
105         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
106         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
107     }
108
109     public function test_shelves_create_sets_cover_image()
110     {
111         $shelfInfo = [
112             'name'        => 'My test book' . Str::random(4),
113             'description' => 'Test book description ' . Str::random(10),
114         ];
115
116         $imageFile = $this->getTestImage('shelf-test.png');
117         $resp = $this->asEditor()->call('POST', '/shelves', $shelfInfo, [], ['image' => $imageFile]);
118         $resp->assertRedirect();
119
120         $lastImage = Image::query()->orderByDesc('id')->firstOrFail();
121         $shelf = Bookshelf::query()->where('name', '=', $shelfInfo['name'])->first();
122         $this->assertDatabaseHas('bookshelves', [
123             'id'       => $shelf->id,
124             'image_id' => $lastImage->id,
125         ]);
126         $this->assertEquals($lastImage->id, $shelf->cover->id);
127         $this->assertEquals('cover_bookshelf', $lastImage->type);
128     }
129
130     public function test_shelf_view()
131     {
132         $shelf = $this->entities->shelf();
133         $resp = $this->asEditor()->get($shelf->getUrl());
134         $resp->assertStatus(200);
135         $resp->assertSeeText($shelf->name);
136         $resp->assertSeeText($shelf->description);
137
138         foreach ($shelf->books as $book) {
139             $resp->assertSee($book->name);
140         }
141     }
142
143     public function test_shelf_view_shows_action_buttons()
144     {
145         $shelf = $this->entities->shelf();
146         $resp = $this->asAdmin()->get($shelf->getUrl());
147         $resp->assertSee($shelf->getUrl('/create-book'));
148         $resp->assertSee($shelf->getUrl('/edit'));
149         $resp->assertSee($shelf->getUrl('/permissions'));
150         $resp->assertSee($shelf->getUrl('/delete'));
151         $this->withHtml($resp)->assertElementContains('a', 'New Book');
152         $this->withHtml($resp)->assertElementContains('a', 'Edit');
153         $this->withHtml($resp)->assertElementContains('a', 'Permissions');
154         $this->withHtml($resp)->assertElementContains('a', 'Delete');
155
156         $resp = $this->asEditor()->get($shelf->getUrl());
157         $resp->assertDontSee($shelf->getUrl('/permissions'));
158     }
159
160     public function test_shelf_view_has_sort_control_that_defaults_to_default()
161     {
162         $shelf = $this->entities->shelf();
163         $resp = $this->asAdmin()->get($shelf->getUrl());
164         $this->withHtml($resp)->assertElementExists('form[action$="change-sort/shelf_books"]');
165         $this->withHtml($resp)->assertElementContains('form[action$="change-sort/shelf_books"] [aria-haspopup="true"]', 'Default');
166     }
167
168     public function test_shelf_view_sort_takes_action()
169     {
170         $shelf = Bookshelf::query()->whereHas('books')->with('books')->first();
171         $books = Book::query()->take(3)->get(['id', 'name']);
172         $books[0]->fill(['name' => 'bsfsdfsdfsd'])->save();
173         $books[1]->fill(['name' => 'adsfsdfsdfsd'])->save();
174         $books[2]->fill(['name' => 'hdgfgdfg'])->save();
175
176         // Set book ordering
177         $this->asAdmin()->put($shelf->getUrl(), [
178             'books' => $books->implode('id', ','),
179             'tags'  => [], 'description' => 'abc', 'name' => 'abc',
180         ]);
181         $this->assertEquals(3, $shelf->books()->count());
182         $shelf->refresh();
183
184         $resp = $this->asEditor()->get($shelf->getUrl());
185         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
186         $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
187
188         setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
189         $resp = $this->asEditor()->get($shelf->getUrl());
190         $this->withHtml($resp)->assertElementNotContains('.book-content a.grid-card:nth-child(1)', $books[0]->name);
191         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', $books[0]->name);
192
193         setting()->putUser($this->users->editor(), 'shelf_books_sort_order', 'desc');
194         setting()->putUser($this->users->editor(), 'shelf_books_sort', 'name');
195         $resp = $this->asEditor()->get($shelf->getUrl());
196         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(1)', 'hdgfgdfg');
197         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(2)', 'bsfsdfsdfsd');
198         $this->withHtml($resp)->assertElementContains('.book-content a.grid-card:nth-child(3)', 'adsfsdfsdfsd');
199     }
200
201     public function test_shelf_edit()
202     {
203         $shelf = $this->entities->shelf();
204         $resp = $this->asEditor()->get($shelf->getUrl('/edit'));
205         $resp->assertSeeText('Edit Shelf');
206
207         $booksToInclude = Book::take(2)->get();
208         $shelfInfo = [
209             'name'        => 'My test book' . Str::random(4),
210             'description' => 'Test book description ' . Str::random(10),
211         ];
212
213         $resp = $this->asEditor()->put($shelf->getUrl(), array_merge($shelfInfo, [
214             'books' => $booksToInclude->implode('id', ','),
215             'tags'  => [
216                 [
217                     'name'  => 'Test Category',
218                     'value' => 'Test Tag Value',
219                 ],
220             ],
221         ]));
222         $shelf = Bookshelf::find($shelf->id);
223         $resp->assertRedirect($shelf->getUrl());
224         $this->assertSessionHas('success');
225
226         $editorId = $this->users->editor()->id;
227         $this->assertDatabaseHas('bookshelves', array_merge($shelfInfo, ['id' => $shelf->id, 'created_by' => $editorId, 'updated_by' => $editorId]));
228
229         $shelfPage = $this->get($shelf->getUrl());
230         $shelfPage->assertSee($shelfInfo['name']);
231         $shelfPage->assertSee($shelfInfo['description']);
232         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Category');
233         $this->withHtml($shelfPage)->assertElementContains('.tag-item', 'Test Tag Value');
234
235         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[0]->id]);
236         $this->assertDatabaseHas('bookshelves_books', ['bookshelf_id' => $shelf->id, 'book_id' => $booksToInclude[1]->id]);
237     }
238
239     public function test_shelf_create_new_book()
240     {
241         $shelf = $this->entities->shelf();
242         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
243
244         $resp->assertSee('Create New Book');
245         $resp->assertSee($shelf->getShortName());
246
247         $testName = 'Test Book in Shelf Name';
248
249         $createBookResp = $this->asEditor()->post($shelf->getUrl('/create-book'), [
250             'name'        => $testName,
251             'description' => 'Book in shelf description',
252         ]);
253         $createBookResp->assertRedirect();
254
255         $newBook = Book::query()->orderBy('id', 'desc')->first();
256         $this->assertDatabaseHas('bookshelves_books', [
257             'bookshelf_id' => $shelf->id,
258             'book_id'      => $newBook->id,
259         ]);
260
261         $resp = $this->asEditor()->get($shelf->getUrl());
262         $resp->assertSee($testName);
263     }
264
265     public function test_shelf_delete()
266     {
267         $shelf = Bookshelf::query()->whereHas('books')->first();
268         $this->assertNull($shelf->deleted_at);
269         $bookCount = $shelf->books()->count();
270
271         $deleteViewReq = $this->asEditor()->get($shelf->getUrl('/delete'));
272         $deleteViewReq->assertSeeText('Are you sure you want to delete this shelf?');
273
274         $deleteReq = $this->delete($shelf->getUrl());
275         $deleteReq->assertRedirect(url('/shelves'));
276         $this->assertActivityExists('bookshelf_delete', $shelf);
277
278         $shelf->refresh();
279         $this->assertNotNull($shelf->deleted_at);
280
281         $this->assertTrue($shelf->books()->count() === $bookCount);
282         $this->assertTrue($shelf->deletions()->count() === 1);
283
284         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
285         $this->assertNotificationContains($redirectReq, 'Shelf Successfully Deleted');
286     }
287
288     public function test_shelf_copy_permissions()
289     {
290         $shelf = $this->entities->shelf();
291         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
292         $resp->assertSeeText('Copy Permissions');
293         $resp->assertSee("action=\"{$shelf->getUrl('/copy-permissions')}\"", false);
294
295         $child = $shelf->books()->first();
296         $editorRole = $this->users->editor()->roles()->first();
297         $this->assertFalse($child->hasPermissions(), 'Child book should not be restricted by default');
298         $this->assertTrue($child->permissions()->count() === 0, 'Child book should have no permissions by default');
299
300         $this->permissions->setEntityPermissions($shelf, ['view', 'update'], [$editorRole]);
301         $resp = $this->post($shelf->getUrl('/copy-permissions'));
302         $child = $shelf->books()->first();
303
304         $resp->assertRedirect($shelf->getUrl());
305         $this->assertTrue($child->hasPermissions(), 'Child book should now be restricted');
306         $this->assertTrue($child->permissions()->count() === 2, 'Child book should have copied permissions');
307         $this->assertDatabaseHas('entity_permissions', [
308             'entity_type' => 'book',
309             'entity_id' => $child->id,
310             'role_id' => $editorRole->id,
311             'view' => true, 'update' => true, 'create' => false, 'delete' => false,
312         ]);
313     }
314
315     public function test_permission_page_has_a_warning_about_no_cascading()
316     {
317         $shelf = $this->entities->shelf();
318         $resp = $this->asAdmin()->get($shelf->getUrl('/permissions'));
319         $resp->assertSeeText('Permissions on shelves do not automatically cascade to contained books.');
320     }
321
322     public function test_bookshelves_show_in_breadcrumbs_if_in_context()
323     {
324         $shelf = $this->entities->shelf();
325         $shelfBook = $shelf->books()->first();
326         $shelfPage = $shelfBook->pages()->first();
327         $this->asAdmin();
328
329         $bookVisit = $this->get($shelfBook->getUrl());
330         $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
331         $this->withHtml($bookVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
332
333         $this->get($shelf->getUrl());
334         $bookVisit = $this->get($shelfBook->getUrl());
335         $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', 'Shelves');
336         $this->withHtml($bookVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
337
338         $pageVisit = $this->get($shelfPage->getUrl());
339         $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', 'Shelves');
340         $this->withHtml($pageVisit)->assertElementContains('.breadcrumbs', $shelf->getShortName());
341
342         $this->get('/books');
343         $pageVisit = $this->get($shelfPage->getUrl());
344         $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', 'Shelves');
345         $this->withHtml($pageVisit)->assertElementNotContains('.breadcrumbs', $shelf->getShortName());
346     }
347
348     public function test_bookshelves_show_on_book()
349     {
350         // Create shelf
351         $shelfInfo = [
352             'name'        => 'My test shelf' . Str::random(4),
353             'description' => 'Test shelf description ' . Str::random(10),
354         ];
355
356         $this->asEditor()->post('/shelves', $shelfInfo);
357         $shelf = Bookshelf::where('name', '=', $shelfInfo['name'])->first();
358
359         // Create book and add to shelf
360         $this->asEditor()->post($shelf->getUrl('/create-book'), [
361             'name'        => 'Test book name',
362             'description' => 'Book in shelf description',
363         ]);
364
365         $newBook = Book::query()->orderBy('id', 'desc')->first();
366
367         $resp = $this->asEditor()->get($newBook->getUrl());
368         $this->withHtml($resp)->assertElementContains('.tri-layout-left-contents', $shelfInfo['name']);
369
370         // Remove shelf
371         $this->delete($shelf->getUrl());
372
373         $resp = $this->asEditor()->get($newBook->getUrl());
374         $resp->assertDontSee($shelfInfo['name']);
375     }
376
377     public function test_cancel_on_child_book_creation_returns_to_original_shelf()
378     {
379         $shelf = $this->entities->shelf();
380         $resp = $this->asEditor()->get($shelf->getUrl('/create-book'));
381         $this->withHtml($resp)->assertElementContains('form a[href="' . $shelf->getUrl() . '"]', 'Cancel');
382     }
383 }