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'));
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'));
40 $this->asEditor()->post('/books', $book->only('name', 'description'));
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' => 'A book with tags',
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' => $book->description]);
85 $newName = $book->name . ' Updated';
86 $newDesc = $book->description . ' with more content';
88 $resp = $this->get($book->getUrl('/edit'));
89 $resp->assertSee($book->name);
90 $resp->assertSee($book->description);
91 $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
93 $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
94 $resp->assertRedirect($book->getUrl() . '-updated');
96 $resp = $this->get($book->getUrl() . '-updated');
97 $resp->assertSee($newName);
98 $resp->assertSee($newDesc);
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::all()->take(2);
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::all()->take(2);
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_show_view_has_copy_button()
267 $book = $this->entities->book();
268 $resp = $this->asEditor()->get($book->getUrl());
270 $this->withHtml($resp)->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
273 public function test_copy_view()
275 $book = $this->entities->book();
276 $resp = $this->asEditor()->get($book->getUrl('/copy'));
279 $resp->assertSee('Copy Book');
280 $this->withHtml($resp)->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
283 public function test_copy()
285 /** @var Book $book */
286 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
287 $resp = $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
289 /** @var Book $copy */
290 $copy = Book::query()->where('name', '=', 'My copy book')->first();
292 $resp->assertRedirect($copy->getUrl());
293 $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
296 public function test_copy_does_not_copy_non_visible_content()
298 /** @var Book $book */
299 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
301 // Hide child content
302 /** @var BookChild $page */
303 foreach ($book->getDirectChildren() as $child) {
304 $this->permissions->setEntityPermissions($child, [], []);
307 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
308 /** @var Book $copy */
309 $copy = Book::query()->where('name', '=', 'My copy book')->first();
311 $this->assertEquals(0, $copy->getDirectChildren()->count());
314 public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
316 /** @var Book $book */
317 $book = Book::query()->whereHas('chapters')->whereHas('directPages')->whereHas('chapters')->first();
318 $viewer = $this->users->viewer();
319 $this->permissions->grantUserRolePermissions($viewer, ['book-create-all']);
321 $this->actingAs($viewer)->post($book->getUrl('/copy'), ['name' => 'My copy book']);
322 /** @var Book $copy */
323 $copy = Book::query()->where('name', '=', 'My copy book')->first();
325 $this->assertEquals(0, $copy->pages()->count());
326 $this->assertEquals(0, $copy->chapters()->count());
329 public function test_copy_clones_cover_image_if_existing()
331 $book = $this->entities->book();
332 $bookRepo = $this->app->make(BookRepo::class);
333 $coverImageFile = $this->files->uploadedImage('cover.png');
334 $bookRepo->updateCoverImage($book, $coverImageFile);
336 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
337 /** @var Book $copy */
338 $copy = Book::query()->where('name', '=', 'My copy book')->first();
340 $this->assertNotNull($copy->cover);
341 $this->assertNotEquals($book->cover->id, $copy->cover->id);
344 public function test_copy_adds_book_to_shelves_if_edit_permissions_allows()
346 /** @var Bookshelf $shelfA */
347 /** @var Bookshelf $shelfB */
348 [$shelfA, $shelfB] = Bookshelf::query()->take(2)->get();
349 $book = $this->entities->book();
351 $shelfA->appendBook($book);
352 $shelfB->appendBook($book);
354 $viewer = $this->users->viewer();
355 $this->permissions->grantUserRolePermissions($viewer, ['book-update-all', 'book-create-all', 'bookshelf-update-all']);
356 $this->permissions->setEntityPermissions($shelfB);
359 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
360 /** @var Book $copy */
361 $copy = Book::query()->where('name', '=', 'My copy book')->first();
363 $this->assertTrue($copy->shelves()->where('id', '=', $shelfA->id)->exists());
364 $this->assertFalse($copy->shelves()->where('id', '=', $shelfB->id)->exists());