]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookTest.php
Extracted test file handling to its own class
[bookstack] / tests / Entity / BookTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Repos\BookRepo;
9 use Tests\TestCase;
10
11 class BookTest extends TestCase
12 {
13     public function test_create()
14     {
15         $book = Book::factory()->make([
16             'name' => 'My First Book',
17         ]);
18
19         $resp = $this->asEditor()->get('/books');
20         $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
21
22         $resp = $this->get('/create-book');
23         $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
24
25         $resp = $this->post('/books', $book->only('name', 'description'));
26         $resp->assertRedirect('/books/my-first-book');
27
28         $resp = $this->get('/books/my-first-book');
29         $resp->assertSee($book->name);
30         $resp->assertSee($book->description);
31     }
32
33     public function test_create_uses_different_slugs_when_name_reused()
34     {
35         $book = Book::factory()->make([
36             'name' => 'My First Book',
37         ]);
38
39         $this->asEditor()->post('/books', $book->only('name', 'description'));
40         $this->asEditor()->post('/books', $book->only('name', 'description'));
41
42         $books = Book::query()->where('name', '=', $book->name)
43             ->orderBy('id', 'desc')
44             ->take(2)
45             ->get();
46
47         $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
48         $this->assertEquals('my-first-book', $books[1]->slug);
49     }
50
51     public function test_create_sets_tags()
52     {
53         // Cheeky initial update to refresh slug
54         $this->asEditor()->post('books', [
55             'name'        => 'My book with tags',
56             'description' => 'A book with tags',
57             'tags'        => [
58                 [
59                     'name'  => 'Category',
60                     'value' => 'Donkey Content',
61                 ],
62                 [
63                     'name'  => 'Level',
64                     'value' => '5',
65                 ],
66             ],
67         ]);
68
69         /** @var Book $book */
70         $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
71         $tags = $book->tags()->get();
72
73         $this->assertEquals(2, $tags->count());
74         $this->assertEquals('Donkey Content', $tags[0]->value);
75         $this->assertEquals('Level', $tags[1]->name);
76     }
77
78     public function test_update()
79     {
80         $book = $this->entities->book();
81         // Cheeky initial update to refresh slug
82         $this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description' => $book->description]);
83         $book->refresh();
84
85         $newName = $book->name . ' Updated';
86         $newDesc = $book->description . ' with more content';
87
88         $resp = $this->get($book->getUrl('/edit'));
89         $resp->assertSee($book->name);
90         $resp->assertSee($book->description);
91         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
92
93         $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
94         $resp->assertRedirect($book->getUrl() . '-updated');
95
96         $resp = $this->get($book->getUrl() . '-updated');
97         $resp->assertSee($newName);
98         $resp->assertSee($newDesc);
99     }
100
101     public function test_update_sets_tags()
102     {
103         $book = $this->entities->book();
104
105         $this->assertEquals(0, $book->tags()->count());
106
107         // Cheeky initial update to refresh slug
108         $this->asEditor()->put($book->getUrl(), [
109             'name' => $book->name,
110             'tags' => [
111                 [
112                     'name'  => 'Category',
113                     'value' => 'Dolphin Content',
114                 ],
115                 [
116                     'name'  => 'Level',
117                     'value' => '5',
118                 ],
119             ],
120         ]);
121
122         $book->refresh();
123         $tags = $book->tags()->get();
124
125         $this->assertEquals(2, $tags->count());
126         $this->assertEquals('Dolphin Content', $tags[0]->value);
127         $this->assertEquals('Level', $tags[1]->name);
128     }
129
130     public function test_delete()
131     {
132         $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
133         $this->assertNull($book->deleted_at);
134         $pageCount = $book->pages()->count();
135         $chapterCount = $book->chapters()->count();
136
137         $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
138         $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
139
140         $deleteReq = $this->delete($book->getUrl());
141         $deleteReq->assertRedirect(url('/books'));
142         $this->assertActivityExists('book_delete', $book);
143
144         $book->refresh();
145         $this->assertNotNull($book->deleted_at);
146
147         $this->assertTrue($book->pages()->count() === 0);
148         $this->assertTrue($book->chapters()->count() === 0);
149         $this->assertTrue($book->pages()->withTrashed()->count() === $pageCount);
150         $this->assertTrue($book->chapters()->withTrashed()->count() === $chapterCount);
151         $this->assertTrue($book->deletions()->count() === 1);
152
153         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
154         $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
155     }
156
157     public function test_cancel_on_create_page_leads_back_to_books_listing()
158     {
159         $resp = $this->asEditor()->get('/create-book');
160         $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
161     }
162
163     public function test_cancel_on_edit_book_page_leads_back_to_book()
164     {
165         $book = $this->entities->book();
166         $resp = $this->asEditor()->get($book->getUrl('/edit'));
167         $this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
168     }
169
170     public function test_next_previous_navigation_controls_show_within_book_content()
171     {
172         $book = $this->entities->book();
173         $chapter = $book->chapters->first();
174
175         $resp = $this->asEditor()->get($chapter->getUrl());
176         $this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Next');
177         $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20));
178
179         $resp = $this->get($chapter->pages[0]->getUrl());
180         $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[1]->name, 0, 20));
181         $this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Previous');
182         $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->name, 0, 20));
183     }
184
185     public function test_recently_viewed_books_updates_as_expected()
186     {
187         $books = Book::all()->take(2);
188
189         $resp = $this->asAdmin()->get('/books');
190         $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
191             ->assertElementNotContains('#recents', $books[1]->name);
192
193         $this->get($books[0]->getUrl());
194         $this->get($books[1]->getUrl());
195
196         $resp = $this->get('/books');
197         $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
198             ->assertElementContains('#recents', $books[1]->name);
199     }
200
201     public function test_popular_books_updates_upon_visits()
202     {
203         $books = Book::all()->take(2);
204
205         $resp = $this->asAdmin()->get('/books');
206         $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
207             ->assertElementNotContains('#popular', $books[1]->name);
208
209         $this->get($books[0]->getUrl());
210         $this->get($books[1]->getUrl());
211         $this->get($books[0]->getUrl());
212
213         $resp = $this->get('/books');
214         $this->withHtml($resp)->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
215             ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
216     }
217
218     public function test_books_view_shows_view_toggle_option()
219     {
220         /** @var Book $book */
221         $editor = $this->users->editor();
222         setting()->putUser($editor, 'books_view_type', 'list');
223
224         $resp = $this->actingAs($editor)->get('/books');
225         $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'Grid View');
226         $this->withHtml($resp)->assertElementExists('button[name="view"][value="grid"]');
227
228         $resp = $this->patch("/preferences/change-view/books", ['view' => 'grid']);
229         $resp->assertRedirect();
230         $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
231
232         $resp = $this->actingAs($editor)->get('/books');
233         $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'List View');
234         $this->withHtml($resp)->assertElementExists('button[name="view"][value="list"]');
235
236         $resp = $this->patch("/preferences/change-view/books", ['view_type' => 'list']);
237         $resp->assertRedirect();
238         $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
239     }
240
241     public function test_slug_multi_byte_url_safe()
242     {
243         $book = $this->entities->newBook([
244             'name' => 'информация',
245         ]);
246
247         $this->assertEquals('informaciia', $book->slug);
248
249         $book = $this->entities->newBook([
250             'name' => '¿Qué?',
251         ]);
252
253         $this->assertEquals('que', $book->slug);
254     }
255
256     public function test_slug_format()
257     {
258         $book = $this->entities->newBook([
259             'name' => 'PartA / PartB / PartC',
260         ]);
261
262         $this->assertEquals('parta-partb-partc', $book->slug);
263     }
264
265     public function test_show_view_has_copy_button()
266     {
267         $book = $this->entities->book();
268         $resp = $this->asEditor()->get($book->getUrl());
269
270         $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
271     }
272
273     public function test_copy_view()
274     {
275         $book = $this->entities->book();
276         $resp = $this->asEditor()->get($book->getUrl('/copy'));
277
278         $resp->assertOk();
279         $resp->assertSee('Copy Book');
280         $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
281     }
282
283     public function test_copy()
284     {
285         /** @var Book $book */
286         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
287         $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
288
289         /** @var Book $copy */
290         $copy = Book::query()->where('name', '=', 'My copy book')->first();
291
292         $resp->assertRedirect($copy->getUrl());
293         $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
294     }
295
296     public function test_copy_does_not_copy_non_visible_content()
297     {
298         /** @var Book $book */
299         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
300
301         // Hide child content
302         /** @var BookChild $page */
303         foreach ($book->getDirectChildren() as $child) {
304             $this->permissions->setEntityPermissions($child, [], []);
305         }
306
307         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
308         /** @var Book $copy */
309         $copy = Book::query()->where('name', '=', 'My copy book')->first();
310
311         $this->assertEquals(0, $copy->getDirectChildren()->count());
312     }
313
314     public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
315     {
316         /** @var Book $book */
317         $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
318         $viewer = $this->users->viewer();
319         $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
320
321         $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
322         /** @var Book $copy */
323         $copy = Book::query()->where('name', '=', 'My copy book')->first();
324
325         $this->assertEquals(0, $copy->pages()->count());
326         $this->assertEquals(0, $copy->chapters()->count());
327     }
328
329     public function test_copy_clones_cover_image_if_existing()
330     {
331         $book = $this->entities->book();
332         $bookRepo = $this->app->make(BookRepo::class);
333         $coverImageFile = $this->files->uploadedImage('cover.png');
334         $bookRepo->updateCoverImage($book, $coverImageFile);
335
336         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
337         /** @var Book $copy */
338         $copy = Book::query()->where('name', '=', 'My copy book')->first();
339
340         $this->assertNotNull($copy->cover);
341         $this->assertNotEquals($book->cover->id, $copy->cover->id);
342     }
343
344     public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
345     {
346         /** @var Bookshelf $shelfA */
347         /** @var Bookshelf $shelfB */
348         [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
349         $book = $this->entities->book();
350
351         $shelfA->appendBook($book);
352         $shelfB->appendBook($book);
353
354         $viewer = $this->users->viewer();
355         $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
356         $this->permissions->setEntityPermissions($shelfB);
357
358
359         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
360         /** @var Book $copy */
361         $copy = Book::query()->where('name', '=', 'My copy book')->first();
362
363         $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
364         $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());
365     }
366 }