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