3 namespace Tests\Entity;
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
9 class BookTest extends TestCase
11 public function test_book_delete()
13 $book = Book::query()->whereHas('pages')->whereHas('chapters')->first();
14 $this->assertNull($book->deleted_at);
15 $pageCount = $book->pages()->count();
16 $chapterCount = $book->chapters()->count();
18 $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
19 $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
21 $deleteReq = $this->delete($book->getUrl());
22 $deleteReq->assertRedirect(url('/books'));
23 $this->assertActivityExists('book_delete', $book);
26 $this->assertNotNull($book->deleted_at);
28 $this->assertTrue($book->pages()->count() === 0);
29 $this->assertTrue($book->chapters()->count() === 0);
30 $this->assertTrue($book->pages()->withTrashed()->count() === $pageCount);
31 $this->assertTrue($book->chapters()->withTrashed()->count() === $chapterCount);
32 $this->assertTrue($book->deletions()->count() === 1);
34 $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
35 $redirectReq->assertNotificationContains('Book Successfully Deleted');
38 public function test_cancel_on_create_page_leads_back_to_books_listing()
40 $resp = $this->asEditor()->get('/create-book');
41 $resp->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
44 public function test_cancel_on_edit_book_page_leads_back_to_book()
46 /** @var Book $book */
47 $book = Book::query()->first();
48 $resp = $this->asEditor()->get($book->getUrl('/edit'));
49 $resp->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
52 public function test_next_previous_navigation_controls_show_within_book_content()
54 $book = Book::query()->first();
55 $chapter = $book->chapters->first();
57 $resp = $this->asEditor()->get($chapter->getUrl());
58 $resp->assertElementContains('#sibling-navigation', 'Next');
59 $resp->assertElementContains('#sibling-navigation', substr($chapter->pages[0]->name, 0, 20));
61 $resp = $this->get($chapter->pages[0]->getUrl());
62 $resp->assertElementContains('#sibling-navigation', substr($chapter->pages[1]->name, 0, 20));
63 $resp->assertElementContains('#sibling-navigation', 'Previous');
64 $resp->assertElementContains('#sibling-navigation', substr($chapter->name, 0, 20));
67 public function test_recently_viewed_books_updates_as_expected()
69 $books = Book::all()->take(2);
71 $this->asAdmin()->get('/books')
72 ->assertElementNotContains('#recents', $books[0]->name)
73 ->assertElementNotContains('#recents', $books[1]->name);
75 $this->get($books[0]->getUrl());
76 $this->get($books[1]->getUrl());
79 ->assertElementContains('#recents', $books[0]->name)
80 ->assertElementContains('#recents', $books[1]->name);
83 public function test_popular_books_updates_upon_visits()
85 $books = Book::all()->take(2);
87 $this->asAdmin()->get('/books')
88 ->assertElementNotContains('#popular', $books[0]->name)
89 ->assertElementNotContains('#popular', $books[1]->name);
91 $this->get($books[0]->getUrl());
92 $this->get($books[1]->getUrl());
93 $this->get($books[0]->getUrl());
96 ->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
97 ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
100 public function test_books_view_shows_view_toggle_option()
102 /** @var Book $book */
103 $editor = $this->getEditor();
104 setting()->putUser($editor, 'books_view_type', 'list');
106 $resp = $this->actingAs($editor)->get('/books');
107 $resp->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'Grid View');
108 $resp->assertElementExists('input[name="view_type"][value="grid"]');
110 $resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'grid']);
111 $resp->assertRedirect();
112 $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
114 $resp = $this->actingAs($editor)->get('/books');
115 $resp->assertElementContains('form[action$="/settings/users/' . $editor->id . '/switch-books-view"]', 'List View');
116 $resp->assertElementExists('input[name="view_type"][value="list"]');
118 $resp = $this->patch("/settings/users/{$editor->id}/switch-books-view", ['view_type' => 'list']);
119 $resp->assertRedirect();
120 $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
123 public function test_slug_multi_byte_url_safe()
125 $book = $this->newBook([
126 'name' => 'информация',
129 $this->assertEquals('informatsiya', $book->slug);
131 $book = $this->newBook([
135 $this->assertEquals('que', $book->slug);
138 public function test_slug_format()
140 $book = $this->newBook([
141 'name' => 'PartA / PartB / PartC',
144 $this->assertEquals('parta-partb-partc', $book->slug);