]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookTest.php
Fix Crowdin name in the language_request issue template
[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         $resp->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
23
24         $resp = $this->get('/create-book');
25         $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_update()
54     {
55         /** @var Book $book */
56         $book = Book::query()->first();
57         // Cheeky initial update to refresh slug
58         $this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description' => $book->description]);
59         $book->refresh();
60
61         $newName = $book->name . ' Updated';
62         $newDesc = $book->description . ' with more content';
63
64         $resp = $this->get($book->getUrl('/edit'));
65         $resp->assertSee($book->name);
66         $resp->assertSee($book->description);
67         $resp->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
68
69         $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
70         $resp->assertRedirect($book->getUrl() . '-updated');
71
72         $resp = $this->get($book->getUrl() . '-updated');
73         $resp->assertSee($newName);
74         $resp->assertSee($newDesc);
75     }
76
77     public function test_delete()
78     {
79         $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
80         $this->assertNull($book->deleted_at);
81         $pageCount = $book->pages()->count();
82         $chapterCount = $book->chapters()->count();
83
84         $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
85         $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
86
87         $deleteReq = $this->delete($book->getUrl());
88         $deleteReq->assertRedirect(url('/books'));
89         $this->assertActivityExists('book_delete', $book);
90
91         $book->refresh();
92         $this->assertNotNull($book->deleted_at);
93
94         $this->assertTrue($book->pages()->count() === 0);
95         $this->assertTrue($book->chapters()->count() === 0);
96         $this->assertTrue($book->pages()->withTrashed()->count() === $pageCount);
97         $this->assertTrue($book->chapters()->withTrashed()->count() === $chapterCount);
98         $this->assertTrue($book->deletions()->count() === 1);
99
100         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
101         $redirectReq->assertNotificationContains('Book Successfully Deleted');
102     }
103
104     public function test_cancel_on_create_page_leads_back_to_books_listing()
105     {
106         $resp = $this->asEditor()->get('/create-book');
107         $resp->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
108     }
109
110     public function test_cancel_on_edit_book_page_leads_back_to_book()
111     {
112         /** @var Book $book */
113         $book = Book::query()->first();
114         $resp = $this->asEditor()->get($book->getUrl('/edit'));
115         $resp->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
116     }
117
118     public function test_next_previous_navigation_controls_show_within_book_content()
119     {
120         $book = Book::query()->first();
121         $chapter = $book->chapters->first();
122
123         $resp = $this->asEditor()->get($chapter->getUrl());
124         $resp->assertElementContains('#sibling-navigation', 'Next');
125         $resp->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20));
126
127         $resp = $this->get($chapter->pages[0]->getUrl());
128         $resp->assertElementContains('#sibling-navigation', substr($chapter->pages[1]->name, 0, 20));
129         $resp->assertElementContains('#sibling-navigation', 'Previous');
130         $resp->assertElementContains('#sibling-navigation', substr($chapter->name, 0, 20));
131     }
132
133     public function test_recently_viewed_books_updates_as_expected()
134     {
135         $books = Book::all()->take(2);
136
137         $this->asAdmin()->get('/books')
138             ->assertElementNotContains('#recents', $books[0]->name)
139             ->assertElementNotContains('#recents', $books[1]->name);
140
141         $this->get($books[0]->getUrl());
142         $this->get($books[1]->getUrl());
143
144         $this->get('/books')
145             ->assertElementContains('#recents', $books[0]->name)
146             ->assertElementContains('#recents', $books[1]->name);
147     }
148
149     public function test_popular_books_updates_upon_visits()
150     {
151         $books = Book::all()->take(2);
152
153         $this->asAdmin()->get('/books')
154             ->assertElementNotContains('#popular', $books[0]->name)
155             ->assertElementNotContains('#popular', $books[1]->name);
156
157         $this->get($books[0]->getUrl());
158         $this->get($books[1]->getUrl());
159         $this->get($books[0]->getUrl());
160
161         $this->get('/books')
162             ->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
163             ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
164     }
165
166     public function test_books_view_shows_view_toggle_option()
167     {
168         /** @var Book $book */
169         $editor = $this->getEditor();
170         setting()->putUser($editor, 'books_view_type', 'list');
171
172         $resp = $this->actingAs($editor)->get('/books');
173         $resp->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'Grid View');
174         $resp->assertElementExists('input[name="view_type"][value="grid"]');
175
176         $resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'grid']);
177         $resp->assertRedirect();
178         $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
179
180         $resp = $this->actingAs($editor)->get('/books');
181         $resp->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'List View');
182         $resp->assertElementExists('input[name="view_type"][value="list"]');
183
184         $resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'list']);
185         $resp->assertRedirect();
186         $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
187     }
188
189     public function test_slug_multi_byte_url_safe()
190     {
191         $book = $this->newBook([
192             'name' => 'информация',
193         ]);
194
195         $this->assertEquals('informaciya', $book->slug);
196
197         $book = $this->newBook([
198             'name' => '¿Qué?',
199         ]);
200
201         $this->assertEquals('que', $book->slug);
202     }
203
204     public function test_slug_format()
205     {
206         $book = $this->newBook([
207             'name' => 'PartA / PartB / PartC',
208         ]);
209
210         $this->assertEquals('parta-partb-partc', $book->slug);
211     }
212
213     public function test_show_view_has_copy_button()
214     {
215         /** @var Book $book */
216         $book = Book::query()->first();
217         $resp = $this->asEditor()->get($book->getUrl());
218
219         $resp->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
220     }
221
222     public function test_copy_view()
223     {
224         /** @var Book $book */
225         $book = Book::query()->first();
226         $resp = $this->asEditor()->get($book->getUrl('/copy'));
227
228         $resp->assertOk();
229         $resp->assertSee('Copy Book');
230         $resp->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
231     }
232
233     public function test_copy()
234     {
235         /** @var Book $book */
236         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
237         $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
238
239         /** @var Book $copy */
240         $copy = Book::query()->where('name', '=', 'My copy book')->first();
241
242         $resp->assertRedirect($copy->getUrl());
243         $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
244     }
245
246     public function test_copy_does_not_copy_non_visible_content()
247     {
248         /** @var Book $book */
249         $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
250
251         // Hide child content
252         /** @var BookChild $page */
253         foreach ($book->getDirectChildren() as $child) {
254             $child->restricted = true;
255             $child->save();
256             $this->regenEntityPermissions($child);
257         }
258
259         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
260         /** @var Book $copy */
261         $copy = Book::query()->where('name', '=', 'My copy book')->first();
262
263         $this->assertEquals(0, $copy->getDirectChildren()->count());
264     }
265
266     public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
267     {
268         /** @var Book $book */
269         $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
270         $viewer = $this->getViewer();
271         $this->giveUserPermissions($viewer, ['book-create-all']);
272
273         $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
274         /** @var Book $copy */
275         $copy = Book::query()->where('name', '=', 'My copy book')->first();
276
277         $this->assertEquals(0, $copy->pages()->count());
278         $this->assertEquals(0, $copy->chapters()->count());
279     }
280
281     public function test_copy_clones_cover_image_if_existing()
282     {
283         /** @var Book $book */
284         $book = Book::query()->first();
285         $bookRepo = $this->app->make(BookRepo::class);
286         $coverImageFile = $this->getTestImage('cover.png');
287         $bookRepo->updateCoverImage($book, $coverImageFile);
288
289         $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
290
291         /** @var Book $copy */
292         $copy = Book::query()->where('name', '=', 'My copy book')->first();
293         $this->assertNotNull($copy->cover);
294         $this->assertNotEquals($book->cover->id, $copy->cover->id);
295     }
296 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.