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)
69 // Ensure page shows other books
70 ->see($books[1]->name);
73 public function test_book_sort_item_returns_book_content()
76 $bookToSort = $books[0];
77 $firstPage = $bookToSort->pages[0];
78 $firstChapter = $bookToSort->chapters[0];
80 ->visit($bookToSort->getUrl() . '/sort-item')
81 // Ensure book details are returned
82 ->see($bookToSort->name)
83 ->see($firstPage->name)
84 ->see($firstChapter->name);
87 public function test_toggle_book_view()
89 $editor = $this->getEditor();
90 setting()->putUser($editor, 'books_view_type', 'grid');
92 $this->actingAs($editor)
94 ->pageHasElement('.featured-image-container')
95 ->submitForm('List View')
98 ->pageNotHasElement('.featured-image-container');
100 $this->actingAs($editor)
102 ->submitForm('Grid View')
103 ->seePageIs('/books')
104 ->pageHasElement('.featured-image-container');
108 public function pageCreation($chapter)
110 $page = factory(Page::class)->make([
111 'name' => 'My First Page'
115 // Navigate to page create form
116 ->visit($chapter->getUrl())
119 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
121 $this->seePageIs($draftPage->getUrl())
123 ->type($page->name, '#name')
124 ->type($page->html, '#html')
126 // Check redirect and page
127 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
130 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
134 public function chapterCreation(Book $book)
136 $chapter = factory(Chapter::class)->make([
137 'name' => 'My First Chapter'
141 // Navigate to chapter create page
142 ->visit($book->getUrl())
143 ->click('New Chapter')
144 ->seePageIs($book->getUrl() . '/create-chapter')
146 ->type($chapter->name, '#name')
147 ->type($chapter->description, '#description')
148 ->press('Save Chapter')
149 // Check redirect and landing page
150 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
151 ->see($chapter->name)->see($chapter->description);
153 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
157 public function bookCreation()
159 $book = factory(Book::class)->make([
160 'name' => 'My First Book'
164 // Choose to create a book
165 ->click('Create New Book')
166 ->seePageIs('/create-book')
167 // Fill out form & save
168 ->type($book->name, '#name')
169 ->type($book->description, '#description')
171 // Check it redirects correctly
172 ->seePageIs('/books/my-first-book')
173 ->see($book->name)->see($book->description);
175 // Ensure duplicate names are given different slugs
177 ->visit('/create-book')
178 ->type($book->name, '#name')
179 ->type($book->description, '#description')
180 ->press('Save Book');
182 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
183 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
185 $book = Book::where('slug', '=', 'my-first-book')->first();
189 public function test_entities_viewable_after_creator_deletion()
191 // Create required assets and revisions
192 $creator = $this->getEditor();
193 $updater = $this->getEditor();
194 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
195 $this->actingAs($creator);
196 app(UserRepo::class)->destroy($creator);
197 app(PageRepo::class)->savePageRevision($entities['page']);
199 $this->checkEntitiesViewable($entities);
202 public function test_entities_viewable_after_updater_deletion()
204 // Create required assets and revisions
205 $creator = $this->getEditor();
206 $updater = $this->getEditor();
207 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
208 $this->actingAs($updater);
209 app(UserRepo::class)->destroy($updater);
210 app(PageRepo::class)->savePageRevision($entities['page']);
212 $this->checkEntitiesViewable($entities);
215 private function checkEntitiesViewable($entities)
217 // Check pages and books are visible.
219 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
220 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
221 ->visit($entities['page']->getUrl())->seeStatusCode(200);
222 // Check revision listing shows no errors.
223 $this->visit($entities['page']->getUrl())
224 ->click('Revisions')->seeStatusCode(200);
227 public function test_recently_created_pages_view()
229 $user = $this->getEditor();
230 $content = $this->createEntityChainBelongingToUser($user);
232 $this->asAdmin()->visit('/pages/recently-created')
233 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
236 public function test_recently_updated_pages_view()
238 $user = $this->getEditor();
239 $content = $this->createEntityChainBelongingToUser($user);
241 $this->asAdmin()->visit('/pages/recently-updated')
242 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
245 public function test_old_page_slugs_redirect_to_new_pages()
247 $page = Page::first();
248 $pageUrl = $page->getUrl();
249 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
250 // Need to save twice since revisions are not generated in seeder.
251 $this->asAdmin()->visit($pageUrl)
252 ->clickInElement('#content', 'Edit')
253 ->type('super test', '#name')
254 ->press('Save Page');
256 $page = Page::first();
257 $pageUrl = $page->getUrl();
260 $this->visit($pageUrl)
261 ->clickInElement('#content', 'Edit')
262 ->type('super test page', '#name')
265 ->seePageIs($newPageUrl);
267 $this->visit($pageUrl)
268 ->seePageIs($newPageUrl);
271 public function test_recently_updated_pages_on_home()
273 $page = Page::orderBy('updated_at', 'asc')->first();
274 Page::where('id', '!=', $page->id)->update([
275 'updated_at' => Carbon::now()->subSecond(1)
277 $this->asAdmin()->visit('/')
278 ->dontSeeInElement('#recently-updated-pages', $page->name);
279 $this->visit($page->getUrl() . '/edit')
282 ->seeInElement('#recently-updated-pages', $page->name);
285 public function test_slug_multi_byte_lower_casing()
287 $entityRepo = app(EntityRepo::class);
288 $book = $entityRepo->createFromInput('book', [
292 $this->assertEquals('книга', $book->slug);
296 public function test_slug_format()
298 $entityRepo = app(EntityRepo::class);
299 $book = $entityRepo->createFromInput('book', [
300 'name' => 'PartA / PartB / PartC'
303 $this->assertEquals('parta-partb-partc', $book->slug);