3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Repos\BookRepo;
9 use Tests\Uploads\UsesImages;
11 class BookTest extends TestCase
15 public function test_create()
17 $book = Book::factory()->make([
18 'name' => 'My First Book',
21 $resp = $this->asEditor()->get('/books');
22 $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
24 $resp = $this->get('/create-book');
25 $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
27 $resp = $this->post('/books', $book->only('name', 'description'));
28 $resp->assertRedirect('/books/my-first-book');
30 $resp = $this->get('/books/my-first-book');
31 $resp->assertSee($book->name);
32 $resp->assertSee($book->description);
35 public function test_create_uses_different_slugs_when_name_reused()
37 $book = Book::factory()->make([
38 'name' => 'My First Book',
41 $this->asEditor()->post('/books', $book->only('name', 'description'));
42 $this->asEditor()->post('/books', $book->only('name', 'description'));
44 $books = Book::query()->where('name', '=', $book->name)
45 ->orderBy('id', 'desc')
49 $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
50 $this->assertEquals('my-first-book', $books[1]->slug);
53 public function test_create_sets_tags()
55 // Cheeky initial update to refresh slug
56 $this->asEditor()->post('books', [
57 'name' => 'My book with tags',
58 'description' => 'A book with tags',
62 'value' => 'Donkey Content',
71 /** @var Book $book */
72 $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
73 $tags = $book->tags()->get();
75 $this->assertEquals(2, $tags->count());
76 $this->assertEquals('Donkey Content', $tags[0]->value);
77 $this->assertEquals('Level', $tags[1]->name);
80 public function test_update()
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]);
88 $newName = $book->name . ' Updated';
89 $newDesc = $book->description . ' with more content';
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');
96 $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
97 $resp->assertRedirect($book->getUrl() . '-updated');
99 $resp = $this->get($book->getUrl() . '-updated');
100 $resp->assertSee($newName);
101 $resp->assertSee($newDesc);
104 public function test_update_sets_tags()
106 /** @var Book $book */
107 $book = Book::query()->first();
109 $this->assertEquals(0, $book->tags()->count());
111 // Cheeky initial update to refresh slug
112 $this->asEditor()->put($book->getUrl(), [
113 'name' => $book->name,
116 'name' => 'Category',
117 'value' => 'Dolphin Content',
127 $tags = $book->tags()->get();
129 $this->assertEquals(2, $tags->count());
130 $this->assertEquals('Dolphin Content', $tags[0]->value);
131 $this->assertEquals('Level', $tags[1]->name);
134 public function test_delete()
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();
141 $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
142 $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
144 $deleteReq = $this->delete($book->getUrl());
145 $deleteReq->assertRedirect(url('/books'));
146 $this->assertActivityExists('book_delete', $book);
149 $this->assertNotNull($book->deleted_at);
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);
157 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
158 $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
161 public function test_cancel_on_create_page_leads_back_to_books_listing()
163 $resp = $this->asEditor()->get('/create-book');
164 $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
167 public function test_cancel_on_edit_book_page_leads_back_to_book()
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');
175 public function test_next_previous_navigation_controls_show_within_book_content()
177 $book = Book::query()->first();
178 $chapter = $book->chapters->first();
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));
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));
190 public function test_recently_viewed_books_updates_as_expected()
192 $books = Book::all()->take(2);
194 $resp = $this->asAdmin()->get('/books');
195 $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
196 ->assertElementNotContains('#recents', $books[1]->name);
198 $this->get($books[0]->getUrl());
199 $this->get($books[1]->getUrl());
201 $resp = $this->get('/books');
202 $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
203 ->assertElementContains('#recents', $books[1]->name);
206 public function test_popular_books_updates_upon_visits()
208 $books = Book::all()->take(2);
210 $resp = $this->asAdmin()->get('/books');
211 $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
212 ->assertElementNotContains('#popular', $books[1]->name);
214 $this->get($books[0]->getUrl());
215 $this->get($books[1]->getUrl());
216 $this->get($books[0]->getUrl());
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);
223 public function test_books_view_shows_view_toggle_option()
225 /** @var Book $book */
226 $editor = $this->getEditor();
227 setting()->putUser($editor, 'books_view_type', 'list');
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"]');
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'));
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"]');
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'));
246 public function test_slug_multi_byte_url_safe()
248 $book = $this->newBook([
249 'name' => 'информация',
252 $this->assertEquals('informaciya', $book->slug);
254 $book = $this->newBook([
258 $this->assertEquals('que', $book->slug);
261 public function test_slug_format()
263 $book = $this->newBook([
264 'name' => 'PartA / PartB / PartC',
267 $this->assertEquals('parta-partb-partc', $book->slug);
270 public function test_show_view_has_copy_button()
272 /** @var Book $book */
273 $book = Book::query()->first();
274 $resp = $this->asEditor()->get($book->getUrl());
276 $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
279 public function test_copy_view()
281 /** @var Book $book */
282 $book = Book::query()->first();
283 $resp = $this->asEditor()->get($book->getUrl('/copy'));
286 $resp->assertSee('Copy Book');
287 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
290 public function test_copy()
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']);
296 /** @var Book $copy */
297 $copy = Book::query()->where('name', '=', 'My copy book')->first();
299 $resp->assertRedirect($copy->getUrl());
300 $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
303 public function test_copy_does_not_copy_non_visible_content()
305 /** @var Book $book */
306 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
308 // Hide child content
309 /** @var BookChild $page */
310 foreach ($book->getDirectChildren() as $child) {
311 $child->restricted = true;
313 $this->regenEntityPermissions($child);
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();
320 $this->assertEquals(0, $copy->getDirectChildren()->count());
323 public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
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']);
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();
334 $this->assertEquals(0, $copy->pages()->count());
335 $this->assertEquals(0, $copy->chapters()->count());
338 public function test_copy_clones_cover_image_if_existing()
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);
346 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
348 /** @var Book $copy */
349 $copy = Book::query()->where('name', '=', 'My copy book')->first();
351 $this->assertNotNull($copy->cover);
352 $this->assertNotEquals($book->cover->id, $copy->cover->id);