3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Auth\UserRepo;
7 use BookStack\Entities\Repos\PageRepo;
10 class EntityTest extends BrowserKitTest
13 public function test_entity_creation()
16 $book = $this->bookCreation();
17 $chapter = $this->chapterCreation($book);
18 $page = $this->pageCreation($chapter);
21 $book = $this->bookUpdate($book);
24 $this->bookDelete($book);
27 public function bookDelete(Book $book)
30 ->visit($book->getUrl())
31 // Check link works correctly
33 ->seePageIs($book->getUrl() . '/delete')
34 // Ensure the book name is show to user
38 ->notSeeInDatabase('books', ['id' => $book->id]);
41 public function bookUpdate(Book $book)
43 $newName = $book->name . ' Updated';
46 ->visit($book->getUrl() . '/edit')
49 ->type($newName, '#name')
51 // Check page url and text
52 ->seePageIs($book->getUrl() . '-updated')
55 return Book::find($book->id);
58 public function test_book_sort_page_shows()
61 $bookToSort = $books[0];
63 ->visit($bookToSort->getUrl())
65 ->seePageIs($bookToSort->getUrl() . '/sort')
67 ->see($bookToSort->name);
70 public function test_book_sort_item_returns_book_content()
73 $bookToSort = $books[0];
74 $firstPage = $bookToSort->pages[0];
75 $firstChapter = $bookToSort->chapters[0];
77 ->visit($bookToSort->getUrl() . '/sort-item')
78 // Ensure book details are returned
79 ->see($bookToSort->name)
80 ->see($firstPage->name)
81 ->see($firstChapter->name);
84 public function test_toggle_book_view()
86 $editor = $this->getEditor();
87 setting()->putUser($editor, 'books_view_type', 'grid');
89 $this->actingAs($editor)
91 ->pageHasElement('.featured-image-container')
92 ->submitForm('List View')
95 ->pageNotHasElement('.featured-image-container');
97 $this->actingAs($editor)
99 ->submitForm('Grid View')
100 ->seePageIs('/books')
101 ->pageHasElement('.featured-image-container');
105 public function pageCreation($chapter)
107 $page = factory(Page::class)->make([
108 'name' => 'My First Page'
112 // Navigate to page create form
113 ->visit($chapter->getUrl())
116 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
118 $this->seePageIs($draftPage->getUrl())
120 ->type($page->name, '#name')
121 ->type($page->html, '#html')
123 // Check redirect and page
124 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
127 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
131 public function chapterCreation(Book $book)
133 $chapter = factory(Chapter::class)->make([
134 'name' => 'My First Chapter'
138 // Navigate to chapter create page
139 ->visit($book->getUrl())
140 ->click('New Chapter')
141 ->seePageIs($book->getUrl() . '/create-chapter')
143 ->type($chapter->name, '#name')
144 ->type($chapter->description, '#description')
145 ->press('Save Chapter')
146 // Check redirect and landing page
147 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
148 ->see($chapter->name)->see($chapter->description);
150 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
154 public function bookCreation()
156 $book = factory(Book::class)->make([
157 'name' => 'My First Book'
161 // Choose to create a book
162 ->click('Create New Book')
163 ->seePageIs('/create-book')
164 // Fill out form & save
165 ->type($book->name, '#name')
166 ->type($book->description, '#description')
168 // Check it redirects correctly
169 ->seePageIs('/books/my-first-book')
170 ->see($book->name)->see($book->description);
172 // Ensure duplicate names are given different slugs
174 ->visit('/create-book')
175 ->type($book->name, '#name')
176 ->type($book->description, '#description')
177 ->press('Save Book');
179 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
180 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
182 $book = Book::where('slug', '=', 'my-first-book')->first();
186 public function test_entities_viewable_after_creator_deletion()
188 // Create required assets and revisions
189 $creator = $this->getEditor();
190 $updater = $this->getEditor();
191 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
192 $this->actingAs($creator);
193 app(UserRepo::class)->destroy($creator);
194 app(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
196 $this->checkEntitiesViewable($entities);
199 public function test_entities_viewable_after_updater_deletion()
201 // Create required assets and revisions
202 $creator = $this->getEditor();
203 $updater = $this->getEditor();
204 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
205 $this->actingAs($updater);
206 app(UserRepo::class)->destroy($updater);
207 app(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
209 $this->checkEntitiesViewable($entities);
212 private function checkEntitiesViewable($entities)
214 // Check pages and books are visible.
216 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
217 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
218 ->visit($entities['page']->getUrl())->seeStatusCode(200);
219 // Check revision listing shows no errors.
220 $this->visit($entities['page']->getUrl())
221 ->click('Revisions')->seeStatusCode(200);
224 public function test_recently_updated_pages_view()
226 $user = $this->getEditor();
227 $content = $this->createEntityChainBelongingToUser($user);
229 $this->asAdmin()->visit('/pages/recently-updated')
230 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
233 public function test_old_page_slugs_redirect_to_new_pages()
235 $page = Page::first();
236 $pageUrl = $page->getUrl();
237 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
238 // Need to save twice since revisions are not generated in seeder.
239 $this->asAdmin()->visit($pageUrl)
240 ->clickInElement('#content', 'Edit')
241 ->type('super test', '#name')
242 ->press('Save Page');
244 $page = Page::first();
245 $pageUrl = $page->getUrl();
248 $this->visit($pageUrl)
249 ->clickInElement('#content', 'Edit')
250 ->type('super test page', '#name')
253 ->seePageIs($newPageUrl);
255 $this->visit($pageUrl)
256 ->seePageIs($newPageUrl);
259 public function test_recently_updated_pages_on_home()
261 $page = Page::orderBy('updated_at', 'asc')->first();
262 Page::where('id', '!=', $page->id)->update([
263 'updated_at' => Carbon::now()->subSecond(1)
265 $this->asAdmin()->visit('/')
266 ->dontSeeInElement('#recently-updated-pages', $page->name);
267 $this->visit($page->getUrl() . '/edit')
270 ->seeInElement('#recently-updated-pages', $page->name);
273 public function test_slug_multi_byte_lower_casing()
275 $book = $this->newBook([
279 $this->assertEquals('книга', $book->slug);
283 public function test_slug_format()
285 $book = $this->newBook([
286 'name' => 'PartA / PartB / PartC'
289 $this->assertEquals('parta-partb-partc', $book->slug);