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 $resp->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
24 $resp = $this->get('/create-book');
25 $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_update()
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]);
61 $newName = $book->name . ' Updated';
62 $newDesc = $book->description . ' with more content';
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');
69 $resp = $this->put($book->getUrl(), ['name' => $newName, 'description' => $newDesc]);
70 $resp->assertRedirect($book->getUrl() . '-updated');
72 $resp = $this->get($book->getUrl() . '-updated');
73 $resp->assertSee($newName);
74 $resp->assertSee($newDesc);
77 public function test_delete()
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();
84 $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
85 $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
87 $deleteReq = $this->delete($book->getUrl());
88 $deleteReq->assertRedirect(url('/books'));
89 $this->assertActivityExists('book_delete', $book);
92 $this->assertNotNull($book->deleted_at);
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);
100 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
101 $redirectReq->assertNotificationContains('Book Successfully Deleted');
104 public function test_cancel_on_create_page_leads_back_to_books_listing()
106 $resp = $this->asEditor()->get('/create-book');
107 $resp->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
110 public function test_cancel_on_edit_book_page_leads_back_to_book()
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');
118 public function test_next_previous_navigation_controls_show_within_book_content()
120 $book = Book::query()->first();
121 $chapter = $book->chapters->first();
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));
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));
133 public function test_recently_viewed_books_updates_as_expected()
135 $books = Book::all()->take(2);
137 $this->asAdmin()->get('/books')
138 ->assertElementNotContains('#recents', $books[0]->name)
139 ->assertElementNotContains('#recents', $books[1]->name);
141 $this->get($books[0]->getUrl());
142 $this->get($books[1]->getUrl());
145 ->assertElementContains('#recents', $books[0]->name)
146 ->assertElementContains('#recents', $books[1]->name);
149 public function test_popular_books_updates_upon_visits()
151 $books = Book::all()->take(2);
153 $this->asAdmin()->get('/books')
154 ->assertElementNotContains('#popular', $books[0]->name)
155 ->assertElementNotContains('#popular', $books[1]->name);
157 $this->get($books[0]->getUrl());
158 $this->get($books[1]->getUrl());
159 $this->get($books[0]->getUrl());
162 ->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
163 ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
166 public function test_books_view_shows_view_toggle_option()
168 /** @var Book $book */
169 $editor = $this->getEditor();
170 setting()->putUser($editor, 'books_view_type', 'list');
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"]');
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'));
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"]');
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'));
189 public function test_slug_multi_byte_url_safe()
191 $book = $this->newBook([
192 'name' => 'информация',
195 $this->assertEquals('informaciya', $book->slug);
197 $book = $this->newBook([
201 $this->assertEquals('que', $book->slug);
204 public function test_slug_format()
206 $book = $this->newBook([
207 'name' => 'PartA / PartB / PartC',
210 $this->assertEquals('parta-partb-partc', $book->slug);
213 public function test_show_view_has_copy_button()
215 /** @var Book $book */
216 $book = Book::query()->first();
217 $resp = $this->asEditor()->get($book->getUrl());
219 $resp->assertElementContains("a[href=\"{$book->getUrl('/copy')}\"]", 'Copy');
222 public function test_copy_view()
224 /** @var Book $book */
225 $book = Book::query()->first();
226 $resp = $this->asEditor()->get($book->getUrl('/copy'));
229 $resp->assertSee('Copy Book');
230 $resp->assertElementExists("input[name=\"name\"][value=\"{$book->name}\"]");
233 public function test_copy()
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']);
239 /** @var Book $copy */
240 $copy = Book::query()->where('name', '=', 'My copy book')->first();
242 $resp->assertRedirect($copy->getUrl());
243 $this->assertEquals($book->getDirectChildren()->count(), $copy->getDirectChildren()->count());
246 public function test_copy_does_not_copy_non_visible_content()
248 /** @var Book $book */
249 $book = Book::query()->whereHas('chapters')->whereHas('pages')->first();
251 // Hide child content
252 /** @var BookChild $page */
253 foreach ($book->getDirectChildren() as $child) {
254 $child->restricted = true;
256 $this->regenEntityPermissions($child);
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();
263 $this->assertEquals(0, $copy->getDirectChildren()->count());
266 public function test_copy_does_not_copy_pages_or_chapters_if_user_cant_create()
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']);
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();
277 $this->assertEquals(0, $copy->pages()->count());
278 $this->assertEquals(0, $copy->chapters()->count());
281 public function test_copy_clones_cover_image_if_existing()
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);
289 $this->asEditor()->post($book->getUrl('/copy'), ['name' => 'My copy book']);
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);