]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookTest.php
Input WYSIWYG: Updated tests, Added simple html limiting
[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_html'));
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_html'));
40         $this->asEditor()->post('/books', $book->only('name', 'description_html'));
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_html' => '<p>A book with tags</p>',
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_html' => $book->description_html]);
83         $book->refresh();
84
85         $newName = $book->name . ' Updated';
86         $newDesc = $book->description_html . '<p>with more content</p>';
87
88         $resp = $this->get($book->getUrl('/edit'));
89         $resp->assertSee($book->name);
90         $resp->assertSee($book->description_html);
91         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
92
93         $resp = $this->put($book->getUrl(), ['name' => $newName, 'description_html' => $newDesc]);
94         $resp->assertRedirect($book->getUrl() . '-updated');
95
96         $resp = $this->get($book->getUrl() . '-updated');
97         $resp->assertSee($newName);
98         $resp->assertSee($newDesc, false);
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::take(2)->get();
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::take(2)->get();
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_description_limited_to_specific_html()
266     {
267         $book = $this->entities->book();
268
269         $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" data-a="b">a</a><section>Hello</section></p>';
270         $expected = '<p>Content<a href="#cat">a</a></p>';
271
272         $this->asEditor()->put($book->getUrl(), [
273             'name' => $book->name,
274             'description_html' => $input
275         ]);
276
277         $book->refresh();
278         $this->assertEquals($expected, $book->description_html);
279     }
280
281     public function test_show_view_has_copy_button()
282     {
283         $book = $this->entities->book();
284         $resp = $this->asEditor()->get($book->getUrl());
285
286         $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
287     }
288
289     public function test_copy_view()
290     {
291         $book = $this->entities->book();
292         $resp = $this->asEditor()->get($book->getUrl('/copy'));
293
294         $resp->assertOk();
295         $resp->assertSee('Copy Book');
296         $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
297     }
298
299     public function test_copy()
300     {
301         /** @var Book $book */
302         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
303         $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
304
305         /** @var Book $copy */
306         $copy = Book::query()->where('name', '=', 'My copy book')->first();
307
308         $resp->assertRedirect($copy->getUrl());
309         $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
310     }
311
312     public function test_copy_does_not_copy_non_visible_content()
313     {
314         /** @var Book $book */
315         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
316
317         // Hide child content
318         /** @var BookChild $page */
319         foreach ($book->getDirectChildren() as $child) {
320             $this->permissions->setEntityPermissions($child, [], []);
321         }
322
323         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
324         /** @var Book $copy */
325         $copy = Book::query()->where('name', '=', 'My copy book')->first();
326
327         $this->assertEquals(0, $copy->getDirectChildren()->count());
328     }
329
330     public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
331     {
332         /** @var Book $book */
333         $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
334         $viewer = $this->users->viewer();
335         $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
336
337         $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
338         /** @var Book $copy */
339         $copy = Book::query()->where('name', '=', 'My copy book')->first();
340
341         $this->assertEquals(0, $copy->pages()->count());
342         $this->assertEquals(0, $copy->chapters()->count());
343     }
344
345     public function test_copy_clones_cover_image_if_existing()
346     {
347         $book = $this->entities->book();
348         $bookRepo = $this->app->make(BookRepo::class);
349         $coverImageFile = $this->files->uploadedImage('cover.png');
350         $bookRepo->updateCoverImage($book, $coverImageFile);
351
352         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
353         /** @var Book $copy */
354         $copy = Book::query()->where('name', '=', 'My copy book')->first();
355
356         $this->assertNotNull($copy->cover);
357         $this->assertNotEquals($book->cover->id, $copy->cover->id);
358     }
359
360     public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
361     {
362         /** @var Bookshelf $shelfA */
363         /** @var Bookshelf $shelfB */
364         [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
365         $book = $this->entities->book();
366
367         $shelfA->appendBook($book);
368         $shelfB->appendBook($book);
369
370         $viewer = $this->users->viewer();
371         $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
372         $this->permissions->setEntityPermissions($shelfB);
373
374
375         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
376         /** @var Book $copy */
377         $copy = Book::query()->where('name', '=', 'My copy book')->first();
378
379         $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
380         $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());
381     }
382 }