3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Repos\BookRepo;
11 class BookTest extends TestCase
13 public function test_create()
15 $book = Book::factory()->make([
16 'name' => 'My First Book',
19 $resp = $this->asEditor()->get('/books');
20 $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
22 $resp = $this->get('/create-book');
23 $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
25 $resp = $this->post('/books', $book->only('name', 'description_html'));
26 $resp->assertRedirect('/books/my-first-book');
28 $resp = $this->get('/books/my-first-book');
29 $resp->assertSee($book->name);
30 $resp->assertSee($book->description);
33 public function test_create_uses_different_slugs_when_name_reused()
35 $book = Book::factory()->make([
36 'name' => 'My First Book',
39 $this->asEditor()->post('/books', $book->only('name', 'description_html'));
40 $this->asEditor()->post('/books', $book->only('name', 'description_html'));
42 $books = Book::query()->where('name', '=', $book->name)
43 ->orderBy('id', 'desc')
47 $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
48 $this->assertEquals('my-first-book', $books[1]->slug);
51 public function test_create_sets_tags()
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>',
60 'value' => 'Donkey Content',
69 /** @var Book $book */
70 $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
71 $tags = $book->tags()->get();
73 $this->assertEquals(2, $tags->count());
74 $this->assertEquals('Donkey Content', $tags[0]->value);
75 $this->assertEquals('Level', $tags[1]->name);
78 public function test_update()
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]);
85 $newName = $book->name . ' Updated';
86 $newDesc = $book->description_html . '<p>with more content</p>';
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');
93 $resp = $this->put($book->getUrl(), ['name' => $newName, 'description_html' => $newDesc]);
94 $resp->assertRedirect($book->getUrl() . '-updated');
96 $resp = $this->get($book->getUrl() . '-updated');
97 $resp->assertSee($newName);
98 $resp->assertSee($newDesc, false);
101 public function test_update_sets_tags()
103 $book = $this->entities->book();
105 $this->assertEquals(0, $book->tags()->count());
107 // Cheeky initial update to refresh slug
108 $this->asEditor()->put($book->getUrl(), [
109 'name' => $book->name,
112 'name' => 'Category',
113 'value' => 'Dolphin Content',
123 $tags = $book->tags()->get();
125 $this->assertEquals(2, $tags->count());
126 $this->assertEquals('Dolphin Content', $tags[0]->value);
127 $this->assertEquals('Level', $tags[1]->name);
130 public function test_delete()
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();
137 $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
138 $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
140 $deleteReq = $this->delete($book->getUrl());
141 $deleteReq->assertRedirect(url('/books'));
142 $this->assertActivityExists('book_delete', $book);
145 $this->assertNotNull($book->deleted_at);
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);
153 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
154 $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
157 public function test_cancel_on_create_page_leads_back_to_books_listing()
159 $resp = $this->asEditor()->get('/create-book');
160 $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
163 public function test_cancel_on_edit_book_page_leads_back_to_book()
165 $book = $this->entities->book();
166 $resp = $this->asEditor()->get($book->getUrl('/edit'));
167 $this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
170 public function test_next_previous_navigation_controls_show_within_book_content()
172 $book = $this->entities->book();
173 $chapter = $book->chapters->first();
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));
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));
185 public function test_recently_viewed_books_updates_as_expected()
187 $books = Book::take(2)->get();
189 $resp = $this->asAdmin()->get('/books');
190 $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
191 ->assertElementNotContains('#recents', $books[1]->name);
193 $this->get($books[0]->getUrl());
194 $this->get($books[1]->getUrl());
196 $resp = $this->get('/books');
197 $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
198 ->assertElementContains('#recents', $books[1]->name);
201 public function test_popular_books_updates_upon_visits()
203 $books = Book::take(2)->get();
205 $resp = $this->asAdmin()->get('/books');
206 $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
207 ->assertElementNotContains('#popular', $books[1]->name);
209 $this->get($books[0]->getUrl());
210 $this->get($books[1]->getUrl());
211 $this->get($books[0]->getUrl());
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);
218 public function test_books_view_shows_view_toggle_option()
220 /** @var Book $book */
221 $editor = $this->users->editor();
222 setting()->putUser($editor, 'books_view_type', 'list');
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"]');
228 $resp = $this->patch("/preferences/change-view/books", ['view' => 'grid']);
229 $resp->assertRedirect();
230 $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
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"]');
236 $resp = $this->patch("/preferences/change-view/books", ['view_type' => 'list']);
237 $resp->assertRedirect();
238 $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
241 public function test_slug_multi_byte_url_safe()
243 $book = $this->entities->newBook([
244 'name' => 'информация',
247 $this->assertEquals('informaciia', $book->slug);
249 $book = $this->entities->newBook([
253 $this->assertEquals('que', $book->slug);
256 public function test_slug_format()
258 $book = $this->entities->newBook([
259 'name' => 'PartA / PartB / PartC',
262 $this->assertEquals('parta-partb-partc', $book->slug);
265 public function test_description_limited_to_specific_html()
267 $book = $this->entities->book();
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>';
272 $this->asEditor()->put($book->getUrl(), [
273 'name' => $book->name,
274 'description_html' => $input
278 $this->assertEquals($expected, $book->description_html);
281 public function test_show_view_has_copy_button()
283 $book = $this->entities->book();
284 $resp = $this->asEditor()->get($book->getUrl());
286 $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
289 public function test_copy_view()
291 $book = $this->entities->book();
292 $resp = $this->asEditor()->get($book->getUrl('/copy'));
295 $resp->assertSee('Copy Book');
296 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
299 public function test_copy()
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']);
305 /** @var Book $copy */
306 $copy = Book::query()->where('name', '=', 'My copy book')->first();
308 $resp->assertRedirect($copy->getUrl());
309 $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
312 public function test_copy_does_not_copy_non_visible_content()
314 /** @var Book $book */
315 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
317 // Hide child content
318 /** @var BookChild $page */
319 foreach ($book->getDirectChildren() as $child) {
320 $this->permissions->setEntityPermissions($child, [], []);
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();
327 $this->assertEquals(0, $copy->getDirectChildren()->count());
330 public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
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']);
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();
341 $this->assertEquals(0, $copy->pages()->count());
342 $this->assertEquals(0, $copy->chapters()->count());
345 public function test_copy_clones_cover_image_if_existing()
347 $book = $this->entities->book();
348 $bookRepo = $this->app->make(BookRepo::class);
349 $coverImageFile = $this->files->uploadedImage('cover.png');
350 $bookRepo->updateCoverImage($book, $coverImageFile);
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();
356 $this->assertNotNull($copy->cover);
357 $this->assertNotEquals($book->cover->id, $copy->cover->id);
360 public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
362 /** @var Bookshelf $shelfA */
363 /** @var Bookshelf $shelfB */
364 [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
365 $book = $this->entities->book();
367 $shelfA->appendBook($book);
368 $shelfB->appendBook($book);
370 $viewer = $this->users->viewer();
371 $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
372 $this->permissions->setEntityPermissions($shelfB);
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();
379 $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
380 $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());