3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\Repos\EntityRepo;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Entities\Repos\PageRepo;
11 class EntityTest extends BrowserKitTest
14 public function test_entity_creation()
17 $book = $this->bookCreation();
18 $chapter = $this->chapterCreation($book);
19 $page = $this->pageCreation($chapter);
22 $book = $this->bookUpdate($book);
25 $this->bookDelete($book);
28 public function bookDelete(Book $book)
31 ->visit($book->getUrl())
32 // Check link works correctly
34 ->seePageIs($book->getUrl() . '/delete')
35 // Ensure the book name is show to user
39 ->notSeeInDatabase('books', ['id' => $book->id]);
42 public function bookUpdate(Book $book)
44 $newName = $book->name . ' Updated';
47 ->visit($book->getUrl() . '/edit')
50 ->type($newName, '#name')
52 // Check page url and text
53 ->seePageIs($book->getUrl() . '-updated')
56 return Book::find($book->id);
59 public function test_book_sort_page_shows()
62 $bookToSort = $books[0];
64 ->visit($bookToSort->getUrl())
66 ->seePageIs($bookToSort->getUrl() . '/sort')
68 ->see($bookToSort->name);
71 public function test_book_sort_item_returns_book_content()
74 $bookToSort = $books[0];
75 $firstPage = $bookToSort->pages[0];
76 $firstChapter = $bookToSort->chapters[0];
78 ->visit($bookToSort->getUrl() . '/sort-item')
79 // Ensure book details are returned
80 ->see($bookToSort->name)
81 ->see($firstPage->name)
82 ->see($firstChapter->name);
85 public function test_toggle_book_view()
87 $editor = $this->getEditor();
88 setting()->putUser($editor, 'books_view_type', 'grid');
90 $this->actingAs($editor)
92 ->pageHasElement('.featured-image-container')
93 ->submitForm('List View')
96 ->pageNotHasElement('.featured-image-container');
98 $this->actingAs($editor)
100 ->submitForm('Grid View')
101 ->seePageIs('/books')
102 ->pageHasElement('.featured-image-container');
106 public function pageCreation($chapter)
108 $page = factory(Page::class)->make([
109 'name' => 'My First Page'
113 // Navigate to page create form
114 ->visit($chapter->getUrl())
117 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
119 $this->seePageIs($draftPage->getUrl())
121 ->type($page->name, '#name')
122 ->type($page->html, '#html')
124 // Check redirect and page
125 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
128 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
132 public function chapterCreation(Book $book)
134 $chapter = factory(Chapter::class)->make([
135 'name' => 'My First Chapter'
139 // Navigate to chapter create page
140 ->visit($book->getUrl())
141 ->click('New Chapter')
142 ->seePageIs($book->getUrl() . '/create-chapter')
144 ->type($chapter->name, '#name')
145 ->type($chapter->description, '#description')
146 ->press('Save Chapter')
147 // Check redirect and landing page
148 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
149 ->see($chapter->name)->see($chapter->description);
151 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
155 public function bookCreation()
157 $book = factory(Book::class)->make([
158 'name' => 'My First Book'
162 // Choose to create a book
163 ->click('Create New Book')
164 ->seePageIs('/create-book')
165 // Fill out form & save
166 ->type($book->name, '#name')
167 ->type($book->description, '#description')
169 // Check it redirects correctly
170 ->seePageIs('/books/my-first-book')
171 ->see($book->name)->see($book->description);
173 // Ensure duplicate names are given different slugs
175 ->visit('/create-book')
176 ->type($book->name, '#name')
177 ->type($book->description, '#description')
178 ->press('Save Book');
180 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
181 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
183 $book = Book::where('slug', '=', 'my-first-book')->first();
187 public function test_entities_viewable_after_creator_deletion()
189 // Create required assets and revisions
190 $creator = $this->getEditor();
191 $updater = $this->getEditor();
192 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
193 $this->actingAs($creator);
194 app(UserRepo::class)->destroy($creator);
195 app(PageRepo::class)->savePageRevision($entities['page']);
197 $this->checkEntitiesViewable($entities);
200 public function test_entities_viewable_after_updater_deletion()
202 // Create required assets and revisions
203 $creator = $this->getEditor();
204 $updater = $this->getEditor();
205 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
206 $this->actingAs($updater);
207 app(UserRepo::class)->destroy($updater);
208 app(PageRepo::class)->savePageRevision($entities['page']);
210 $this->checkEntitiesViewable($entities);
213 private function checkEntitiesViewable($entities)
215 // Check pages and books are visible.
217 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
218 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
219 ->visit($entities['page']->getUrl())->seeStatusCode(200);
220 // Check revision listing shows no errors.
221 $this->visit($entities['page']->getUrl())
222 ->click('Revisions')->seeStatusCode(200);
225 public function test_recently_updated_pages_view()
227 $user = $this->getEditor();
228 $content = $this->createEntityChainBelongingToUser($user);
230 $this->asAdmin()->visit('/pages/recently-updated')
231 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
234 public function test_old_page_slugs_redirect_to_new_pages()
236 $page = Page::first();
237 $pageUrl = $page->getUrl();
238 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
239 // Need to save twice since revisions are not generated in seeder.
240 $this->asAdmin()->visit($pageUrl)
241 ->clickInElement('#content', 'Edit')
242 ->type('super test', '#name')
243 ->press('Save Page');
245 $page = Page::first();
246 $pageUrl = $page->getUrl();
249 $this->visit($pageUrl)
250 ->clickInElement('#content', 'Edit')
251 ->type('super test page', '#name')
254 ->seePageIs($newPageUrl);
256 $this->visit($pageUrl)
257 ->seePageIs($newPageUrl);
260 public function test_recently_updated_pages_on_home()
262 $page = Page::orderBy('updated_at', 'asc')->first();
263 Page::where('id', '!=', $page->id)->update([
264 'updated_at' => Carbon::now()->subSecond(1)
266 $this->asAdmin()->visit('/')
267 ->dontSeeInElement('#recently-updated-pages', $page->name);
268 $this->visit($page->getUrl() . '/edit')
271 ->seeInElement('#recently-updated-pages', $page->name);
274 public function test_slug_multi_byte_lower_casing()
276 $entityRepo = app(EntityRepo::class);
277 $book = $entityRepo->createFromInput('book', [
281 $this->assertEquals('книга', $book->slug);
285 public function test_slug_format()
287 $entityRepo = app(EntityRepo::class);
288 $book = $entityRepo->createFromInput('book', [
289 'name' => 'PartA / PartB / PartC'
292 $this->assertEquals('parta-partb-partc', $book->slug);