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;
10 use Tests\Uploads\UsesImages;
12 class BookTest extends TestCase
16 public function test_create()
18 $book = Book::factory()->make([
19 'name' => 'My First Book',
22 $resp = $this->asEditor()->get('/books');
23 $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
25 $resp = $this->get('/create-book');
26 $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
28 $resp = $this->post('/books', $book->only('name', 'description'));
29 $resp->assertRedirect('/books/my-first-book');
31 $resp = $this->get('/books/my-first-book');
32 $resp->assertSee($book->name);
33 $resp->assertSee($book->description);
36 public function test_create_uses_different_slugs_when_name_reused()
38 $book = Book::factory()->make([
39 'name' => 'My First Book',
42 $this->asEditor()->post('/books', $book->only('name', 'description'));
43 $this->asEditor()->post('/books', $book->only('name', 'description'));
45 $books = Book::query()->where('name', '=', $book->name)
46 ->orderBy('id', 'desc')
50 $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
51 $this->assertEquals('my-first-book', $books[1]->slug);
54 public function test_create_sets_tags()
56 // Cheeky initial update to refresh slug
57 $this->asEditor()->post('books', [
58 'name' => 'My book with tags',
59 'description' => 'A book with tags',
63 'value' => 'Donkey Content',
72 /** @var Book $book */
73 $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
74 $tags = $book->tags()->get();
76 $this->assertEquals(2, $tags->count());
77 $this->assertEquals('Donkey Content', $tags[0]->value);
78 $this->assertEquals('Level', $tags[1]->name);
81 public function test_update()
83 $book = $this->entities->book();
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 $book = $this->entities->book();
108 $this->assertEquals(0, $book->tags()->count());
110 // Cheeky initial update to refresh slug
111 $this->asEditor()->put($book->getUrl(), [
112 'name' => $book->name,
115 'name' => 'Category',
116 'value' => 'Dolphin Content',
126 $tags = $book->tags()->get();
128 $this->assertEquals(2, $tags->count());
129 $this->assertEquals('Dolphin Content', $tags[0]->value);
130 $this->assertEquals('Level', $tags[1]->name);
133 public function test_delete()
135 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
136 $this->assertNull($book->deleted_at);
137 $pageCount = $book->pages()->count();
138 $chapterCount = $book->chapters()->count();
140 $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
141 $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
143 $deleteReq = $this->delete($book->getUrl());
144 $deleteReq->assertRedirect(url('/books'));
145 $this->assertActivityExists('book_delete', $book);
148 $this->assertNotNull($book->deleted_at);
150 $this->assertTrue($book->pages()->count() === 0);
151 $this->assertTrue($book->chapters()->count() === 0);
152 $this->assertTrue($book->pages()->withTrashed()->count() === $pageCount);
153 $this->assertTrue($book->chapters()->withTrashed()->count() === $chapterCount);
154 $this->assertTrue($book->deletions()->count() === 1);
156 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
157 $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
160 public function test_cancel_on_create_page_leads_back_to_books_listing()
162 $resp = $this->asEditor()->get('/create-book');
163 $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
166 public function test_cancel_on_edit_book_page_leads_back_to_book()
168 $book = $this->entities->book();
169 $resp = $this->asEditor()->get($book->getUrl('/edit'));
170 $this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
173 public function test_next_previous_navigation_controls_show_within_book_content()
175 $book = $this->entities->book();
176 $chapter = $book->chapters->first();
178 $resp = $this->asEditor()->get($chapter->getUrl());
179 $this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Next');
180 $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20));
182 $resp = $this->get($chapter->pages[0]->getUrl());
183 $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->pages[1]->name, 0, 20));
184 $this->withHtml($resp)->assertElementContains('#sibling-navigation', 'Previous');
185 $this->withHtml($resp)->assertElementContains('#sibling-navigation', substr($chapter->name, 0, 20));
188 public function test_recently_viewed_books_updates_as_expected()
190 $books = Book::all()->take(2);
192 $resp = $this->asAdmin()->get('/books');
193 $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
194 ->assertElementNotContains('#recents', $books[1]->name);
196 $this->get($books[0]->getUrl());
197 $this->get($books[1]->getUrl());
199 $resp = $this->get('/books');
200 $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
201 ->assertElementContains('#recents', $books[1]->name);
204 public function test_popular_books_updates_upon_visits()
206 $books = Book::all()->take(2);
208 $resp = $this->asAdmin()->get('/books');
209 $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
210 ->assertElementNotContains('#popular', $books[1]->name);
212 $this->get($books[0]->getUrl());
213 $this->get($books[1]->getUrl());
214 $this->get($books[0]->getUrl());
216 $resp = $this->get('/books');
217 $this->withHtml($resp)->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
218 ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
221 public function test_books_view_shows_view_toggle_option()
223 /** @var Book $book */
224 $editor = $this->users->editor();
225 setting()->putUser($editor, 'books_view_type', 'list');
227 $resp = $this->actingAs($editor)->get('/books');
228 $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'Grid View');
229 $this->withHtml($resp)->assertElementExists('button[name="view"][value="grid"]');
231 $resp = $this->patch("/preferences/change-view/books", ['view' => 'grid']);
232 $resp->assertRedirect();
233 $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
235 $resp = $this->actingAs($editor)->get('/books');
236 $this->withHtml($resp)->assertElementContains('form[action$="/preferences/change-view/books"]', 'List View');
237 $this->withHtml($resp)->assertElementExists('button[name="view"][value="list"]');
239 $resp = $this->patch("/preferences/change-view/books", ['view_type' => 'list']);
240 $resp->assertRedirect();
241 $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
244 public function test_slug_multi_byte_url_safe()
246 $book = $this->entities->newBook([
247 'name' => 'информация',
250 $this->assertEquals('informaciia', $book->slug);
252 $book = $this->entities->newBook([
256 $this->assertEquals('que', $book->slug);
259 public function test_slug_format()
261 $book = $this->entities->newBook([
262 'name' => 'PartA / PartB / PartC',
265 $this->assertEquals('parta-partb-partc', $book->slug);
268 public function test_show_view_has_copy_button()
270 $book = $this->entities->book();
271 $resp = $this->asEditor()->get($book->getUrl());
273 $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
276 public function test_copy_view()
278 $book = $this->entities->book();
279 $resp = $this->asEditor()->get($book->getUrl('/copy'));
282 $resp->assertSee('Copy Book');
283 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
286 public function test_copy()
288 /** @var Book $book */
289 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
290 $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
292 /** @var Book $copy */
293 $copy = Book::query()->where('name', '=', 'My copy book')->first();
295 $resp->assertRedirect($copy->getUrl());
296 $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
299 public function test_copy_does_not_copy_non_visible_content()
301 /** @var Book $book */
302 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
304 // Hide child content
305 /** @var BookChild $page */
306 foreach ($book->getDirectChildren() as $child) {
307 $this->permissions->setEntityPermissions($child, [], []);
310 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
311 /** @var Book $copy */
312 $copy = Book::query()->where('name', '=', 'My copy book')->first();
314 $this->assertEquals(0, $copy->getDirectChildren()->count());
317 public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
319 /** @var Book $book */
320 $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
321 $viewer = $this->users->viewer();
322 $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
324 $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
325 /** @var Book $copy */
326 $copy = Book::query()->where('name', '=', 'My copy book')->first();
328 $this->assertEquals(0, $copy->pages()->count());
329 $this->assertEquals(0, $copy->chapters()->count());
332 public function test_copy_clones_cover_image_if_existing()
334 $book = $this->entities->book();
335 $bookRepo = $this->app->make(BookRepo::class);
336 $coverImageFile = $this->getTestImage('cover.png');
337 $bookRepo->updateCoverImage($book, $coverImageFile);
339 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
340 /** @var Book $copy */
341 $copy = Book::query()->where('name', '=', 'My copy book')->first();
343 $this->assertNotNull($copy->cover);
344 $this->assertNotEquals($book->cover->id, $copy->cover->id);
347 public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
349 /** @var Bookshelf $shelfA */
350 /** @var Bookshelf $shelfB */
351 [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
352 $book = $this->entities->book();
354 $shelfA->appendBook($book);
355 $shelfB->appendBook($book);
357 $viewer = $this->users->viewer();
358 $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
359 $this->permissions->setEntityPermissions($shelfB);
362 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
363 /** @var Book $copy */
364 $copy = Book::query()->where('name', '=', 'My copy book')->first();
366 $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
367 $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());