]> BookStack Code Mirror - bookstack/blob - tests/Entity/BookTest.php
Testing: Extracted copy tests to their own class
[bookstack] / tests / Entity / BookTest.php
1 <?php
2
3 namespace Tests\Entity;
4
5 use BookStack\Entities\Models\Book;
6 use BookStack\Entities\Models\BookChild;
7 use BookStack\Entities\Models\Bookshelf;
8 use BookStack\Entities\Repos\BookRepo;
9 use Tests\TestCase;
10
11 class BookTest extends TestCase
12 {
13     public function test_create()
14     {
15         $book = Book::factory()->make([
16             'name' => 'My First Book',
17         ]);
18
19         $resp = $this->asEditor()->get('/books');
20         $this->withHtml($resp)->assertElementContains('a[href="' . url('/create-book') . '"]', 'Create New Book');
21
22         $resp = $this->get('/create-book');
23         $this->withHtml($resp)->assertElementContains('form[action="' . url('/books') . '"][method="POST"]', 'Save Book');
24
25         $resp = $this->post('/books', $book->only('name', 'description_html'));
26         $resp->assertRedirect('/books/my-first-book');
27
28         $resp = $this->get('/books/my-first-book');
29         $resp->assertSee($book->name);
30         $resp->assertSee($book->descriptionInfo()->getPlain());
31     }
32
33     public function test_create_uses_different_slugs_when_name_reused()
34     {
35         $book = Book::factory()->make([
36             'name' => 'My First Book',
37         ]);
38
39         $this->asEditor()->post('/books', $book->only('name', 'description_html'));
40         $this->asEditor()->post('/books', $book->only('name', 'description_html'));
41
42         $books = Book::query()->where('name', '=', $book->name)
43             ->orderBy('id', 'desc')
44             ->take(2)
45             ->get();
46
47         $this->assertMatchesRegularExpression('/my-first-book-[0-9a-zA-Z]{3}/', $books[0]->slug);
48         $this->assertEquals('my-first-book', $books[1]->slug);
49     }
50
51     public function test_create_sets_tags()
52     {
53         // Cheeky initial update to refresh slug
54         $this->asEditor()->post('books', [
55             'name'             => 'My book with tags',
56             'description_html' => '<p>A book with tags</p>',
57             'tags'             => [
58                 [
59                     'name'  => 'Category',
60                     'value' => 'Donkey Content',
61                 ],
62                 [
63                     'name'  => 'Level',
64                     'value' => '5',
65                 ],
66             ],
67         ]);
68
69         /** @var Book $book */
70         $book = Book::query()->where('name', '=', 'My book with tags')->firstOrFail();
71         $tags = $book->tags()->get();
72
73         $this->assertEquals(2, $tags->count());
74         $this->assertEquals('Donkey Content', $tags[0]->value);
75         $this->assertEquals('Level', $tags[1]->name);
76     }
77
78     public function test_update()
79     {
80         $book = $this->entities->book();
81         // Cheeky initial update to refresh slug
82         $this->asEditor()->put($book->getUrl(), ['name' => $book->name . '5', 'description_html' => $book->description_html]);
83         $book->refresh();
84
85         $newName = $book->name . ' Updated';
86         $newDesc = $book->description_html . '<p>with more content</p>';
87
88         $resp = $this->get($book->getUrl('/edit'));
89         $resp->assertSee($book->name);
90         $resp->assertSee($book->description_html);
91         $this->withHtml($resp)->assertElementContains('form[action="' . $book->getUrl() . '"]', 'Save Book');
92
93         $resp = $this->put($book->getUrl(), ['name' => $newName, 'description_html' => $newDesc]);
94         $resp->assertRedirect($book->getUrl() . '-updated');
95
96         $resp = $this->get($book->getUrl() . '-updated');
97         $resp->assertSee($newName);
98         $resp->assertSee($newDesc, false);
99     }
100
101     public function test_update_sets_tags()
102     {
103         $book = $this->entities->book();
104
105         $this->assertEquals(0, $book->tags()->count());
106
107         // Cheeky initial update to refresh slug
108         $this->asEditor()->put($book->getUrl(), [
109             'name' => $book->name,
110             'tags' => [
111                 [
112                     'name'  => 'Category',
113                     'value' => 'Dolphin Content',
114                 ],
115                 [
116                     'name'  => 'Level',
117                     'value' => '5',
118                 ],
119             ],
120         ]);
121
122         $book->refresh();
123         $tags = $book->tags()->get();
124
125         $this->assertEquals(2, $tags->count());
126         $this->assertEquals('Dolphin Content', $tags[0]->value);
127         $this->assertEquals('Level', $tags[1]->name);
128     }
129
130     public function test_delete()
131     {
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();
136
137         $deleteViewReq = $this->asEditor()->get($book->getUrl('/delete'));
138         $deleteViewReq->assertSeeText('Are you sure you want to delete this book?');
139
140         $deleteReq = $this->delete($book->getUrl());
141         $deleteReq->assertRedirect(url('/books'));
142         $this->assertActivityExists('book_delete', $book);
143
144         $book->refresh();
145         $this->assertNotNull($book->deleted_at);
146
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);
152
153         $redirectReq = $this->get($deleteReq->baseResponse->headers->get('location'));
154         $this->assertNotificationContains($redirectReq, 'Book Successfully Deleted');
155     }
156
157     public function test_cancel_on_create_page_leads_back_to_books_listing()
158     {
159         $resp = $this->asEditor()->get('/create-book');
160         $this->withHtml($resp)->assertElementContains('form a[href="' . url('/books') . '"]', 'Cancel');
161     }
162
163     public function test_cancel_on_edit_book_page_leads_back_to_book()
164     {
165         $book = $this->entities->book();
166         $resp = $this->asEditor()->get($book->getUrl('/edit'));
167         $this->withHtml($resp)->assertElementContains('form a[href="' . $book->getUrl() . '"]', 'Cancel');
168     }
169
170     public function test_next_previous_navigation_controls_show_within_book_content()
171     {
172         $book = $this->entities->book();
173         $chapter = $book->chapters->first();
174
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));
178
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));
183     }
184
185     public function test_recently_viewed_books_updates_as_expected()
186     {
187         $books = Book::take(2)->get();
188
189         $resp = $this->asAdmin()->get('/books');
190         $this->withHtml($resp)->assertElementNotContains('#recents', $books[0]->name)
191             ->assertElementNotContains('#recents', $books[1]->name);
192
193         $this->get($books[0]->getUrl());
194         $this->get($books[1]->getUrl());
195
196         $resp = $this->get('/books');
197         $this->withHtml($resp)->assertElementContains('#recents', $books[0]->name)
198             ->assertElementContains('#recents', $books[1]->name);
199     }
200
201     public function test_popular_books_updates_upon_visits()
202     {
203         $books = Book::take(2)->get();
204
205         $resp = $this->asAdmin()->get('/books');
206         $this->withHtml($resp)->assertElementNotContains('#popular', $books[0]->name)
207             ->assertElementNotContains('#popular', $books[1]->name);
208
209         $this->get($books[0]->getUrl());
210         $this->get($books[1]->getUrl());
211         $this->get($books[0]->getUrl());
212
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);
216     }
217
218     public function test_books_view_shows_view_toggle_option()
219     {
220         /** @var Book $book */
221         $editor = $this->users->editor();
222         setting()->putUser($editor, 'books_view_type', 'list');
223
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"]');
227
228         $resp = $this->patch("/preferences/change-view/books", ['view' => 'grid']);
229         $resp->assertRedirect();
230         $this->assertEquals('grid', setting()->getUser($editor, 'books_view_type'));
231
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"]');
235
236         $resp = $this->patch("/preferences/change-view/books", ['view_type' => 'list']);
237         $resp->assertRedirect();
238         $this->assertEquals('list', setting()->getUser($editor, 'books_view_type'));
239     }
240
241     public function test_description_limited_to_specific_html()
242     {
243         $book = $this->entities->book();
244
245         $input = '<h1>Test</h1><p id="abc" href="beans">Content<a href="#cat" target="_blank" data-a="b">a</a><section>Hello</section></p>';
246         $expected = '<p>Content<a href="#cat" target="_blank">a</a></p>';
247
248         $this->asEditor()->put($book->getUrl(), [
249             'name' => $book->name,
250             'description_html' => $input
251         ]);
252
253         $book->refresh();
254         $this->assertEquals($expected, $book->description_html);
255     }
256
257     public function test_show_view_displays_description_if_no_description_html_set()
258     {
259         $book = $this->entities->book();
260         $book->description_html = '';
261         $book->description = "My great\ndescription\n\nwith newlines";
262         $book->save();
263
264         $resp = $this->asEditor()->get($book->getUrl());
265         $resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false);
266     }
267 }