]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookTest.php
Moved more tests out of EntityTest
[bookstack] / tests / Entity / BookTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\Bookshelf;
7 use Tests\TestCase;
8
9 class BookTest extends TestCase
10 {
11     public function test_book_delete()
12     {
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();
17
18         $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
19         $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
20
21         $deleteReq = $this->delete($book->getUrl());
22         $deleteReq->assertRedirect(url('/books'));
23         $this->assertActivityExists('book_delete', $book);
24
25         $book->refresh();
26         $this->assertNotNull($book->deleted_at);
27
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);
33
34         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
35         $redirectReq->assertNotificationContains('Book Successfully Deleted');
36     }
37
38     public function test_cancel_on_create_page_leads_back_to_books_listing()
39     {
40         $resp = $this->asEditor()->get('/create-book');
41         $resp->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
42     }
43
44     public function test_cancel_on_edit_book_page_leads_back_to_book()
45     {
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');
50     }
51
52     public function test_next_previous_navigation_controls_show_within_book_content()
53     {
54         $book = Book::query()->first();
55         $chapter = $book->chapters->first();
56
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));
60
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));
65     }
66
67     public function test_recently_viewed_books_updates_as_expected()
68     {
69         $books = Book::all()->take(2);
70
71         $this->asAdmin()->get('/books')
72             ->assertElementNotContains('#recents', $books[0]->name)
73             ->assertElementNotContains('#recents', $books[1]->name);
74
75         $this->get($books[0]->getUrl());
76         $this->get($books[1]->getUrl());
77
78         $this->get('/books')
79             ->assertElementContains('#recents', $books[0]->name)
80             ->assertElementContains('#recents', $books[1]->name);
81     }
82
83     public function test_popular_books_updates_upon_visits()
84     {
85         $books = Book::all()->take(2);
86
87         $this->asAdmin()->get('/books')
88             ->assertElementNotContains('#popular', $books[0]->name)
89             ->assertElementNotContains('#popular', $books[1]->name);
90
91         $this->get($books[0]->getUrl());
92         $this->get($books[1]->getUrl());
93         $this->get($books[0]->getUrl());
94
95         $this->get('/books')
96             ->assertElementContains('#popular .book:nth-child(1)', $books[0]->name)
97             ->assertElementContains('#popular .book:nth-child(2)', $books[1]->name);
98     }
99
100     public function test_books_view_shows_view_toggle_option()
101     {
102         /** @var Book $book */
103         $editor = $this->getEditor();
104         setting()->putUser($editor, 'books_view_type', 'list');
105
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"]');
109
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'));
113
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"]');
117
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'));
121     }
122
123     public function test_slug_multi_byte_url_safe()
124     {
125         $book = $this->newBook([
126             'name' => 'информация',
127         ]);
128
129         $this->assertEquals('informatsiya', $book->slug);
130
131         $book = $this->newBook([
132             'name' => '¿Qué?',
133         ]);
134
135         $this->assertEquals('que', $book->slug);
136     }
137
138     public function test_slug_format()
139     {
140         $book = $this->newBook([
141             'name' => 'PartA / PartB / PartC',
142         ]);
143
144         $this->assertEquals('parta-partb-partc', $book->slug);
145     }
146 }