3 use BookStack\Entities\Book;
4 use BookStack\Entities\Chapter;
5 use BookStack\Entities\Page;
6 use BookStack\Entities\EntityRepo;
7 use BookStack\Auth\UserRepo;
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)
68 // Ensure page shows other books
69 ->see($books[1]->name);
72 public function test_book_sort_item_returns_book_content()
75 $bookToSort = $books[0];
76 $firstPage = $bookToSort->pages[0];
77 $firstChapter = $bookToSort->chapters[0];
79 ->visit($bookToSort->getUrl() . '/sort-item')
80 // Ensure book details are returned
81 ->see($bookToSort->name)
82 ->see($firstPage->name)
83 ->see($firstChapter->name);
86 public function test_toggle_book_view()
88 $editor = $this->getEditor();
89 setting()->putUser($editor, 'books_view_type', 'grid');
91 $this->actingAs($editor)
93 ->pageHasElement('.featured-image-container')
94 ->submitForm('List View')
97 ->pageNotHasElement('.featured-image-container');
99 $this->actingAs($editor)
101 ->submitForm('Grid View')
102 ->seePageIs('/books')
103 ->pageHasElement('.featured-image-container');
107 public function pageCreation($chapter)
109 $page = factory(Page::class)->make([
110 'name' => 'My First Page'
114 // Navigate to page create form
115 ->visit($chapter->getUrl())
118 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
120 $this->seePageIs($draftPage->getUrl())
122 ->type($page->name, '#name')
123 ->type($page->html, '#html')
125 // Check redirect and page
126 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
129 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
133 public function chapterCreation(Book $book)
135 $chapter = factory(Chapter::class)->make([
136 'name' => 'My First Chapter'
140 // Navigate to chapter create page
141 ->visit($book->getUrl())
142 ->click('New Chapter')
143 ->seePageIs($book->getUrl() . '/create-chapter')
145 ->type($chapter->name, '#name')
146 ->type($chapter->description, '#description')
147 ->press('Save Chapter')
148 // Check redirect and landing page
149 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
150 ->see($chapter->name)->see($chapter->description);
152 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
156 public function bookCreation()
158 $book = factory(Book::class)->make([
159 'name' => 'My First Book'
163 // Choose to create a book
164 ->click('Create New Book')
165 ->seePageIs('/create-book')
166 // Fill out form & save
167 ->type($book->name, '#name')
168 ->type($book->description, '#description')
170 // Check it redirects correctly
171 ->seePageIs('/books/my-first-book')
172 ->see($book->name)->see($book->description);
174 // Ensure duplicate names are given different slugs
176 ->visit('/create-book')
177 ->type($book->name, '#name')
178 ->type($book->description, '#description')
179 ->press('Save Book');
181 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
182 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
184 $book = Book::where('slug', '=', 'my-first-book')->first();
188 public function test_entities_viewable_after_creator_deletion()
190 // Create required assets and revisions
191 $creator = $this->getEditor();
192 $updater = $this->getEditor();
193 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
194 $this->actingAs($creator);
195 app(UserRepo::class)->destroy($creator);
196 app(EntityRepo::class)->savePageRevision($entities['page']);
198 $this->checkEntitiesViewable($entities);
201 public function test_entities_viewable_after_updater_deletion()
203 // Create required assets and revisions
204 $creator = $this->getEditor();
205 $updater = $this->getEditor();
206 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
207 $this->actingAs($updater);
208 app(UserRepo::class)->destroy($updater);
209 app(EntityRepo::class)->savePageRevision($entities['page']);
211 $this->checkEntitiesViewable($entities);
214 private function checkEntitiesViewable($entities)
216 // Check pages and books are visible.
218 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
219 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
220 ->visit($entities['page']->getUrl())->seeStatusCode(200);
221 // Check revision listing shows no errors.
222 $this->visit($entities['page']->getUrl())
223 ->click('Revisions')->seeStatusCode(200);
226 public function test_recently_created_pages_view()
228 $user = $this->getEditor();
229 $content = $this->createEntityChainBelongingToUser($user);
231 $this->asAdmin()->visit('/pages/recently-created')
232 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
235 public function test_recently_updated_pages_view()
237 $user = $this->getEditor();
238 $content = $this->createEntityChainBelongingToUser($user);
240 $this->asAdmin()->visit('/pages/recently-updated')
241 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
244 public function test_old_page_slugs_redirect_to_new_pages()
246 $page = Page::first();
247 $pageUrl = $page->getUrl();
248 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
249 // Need to save twice since revisions are not generated in seeder.
250 $this->asAdmin()->visit($pageUrl)
251 ->clickInElement('#content', 'Edit')
252 ->type('super test', '#name')
253 ->press('Save Page');
255 $page = Page::first();
256 $pageUrl = $page->getUrl();
259 $this->visit($pageUrl)
260 ->clickInElement('#content', 'Edit')
261 ->type('super test page', '#name')
264 ->seePageIs($newPageUrl);
266 $this->visit($pageUrl)
267 ->seePageIs($newPageUrl);
270 public function test_recently_updated_pages_on_home()
272 $page = Page::orderBy('updated_at', 'asc')->first();
273 Page::where('id', '!=', $page->id)->update([
274 'updated_at' => Carbon::now()->subSecond(1)
276 $this->asAdmin()->visit('/')
277 ->dontSeeInElement('#recently-updated-pages', $page->name);
278 $this->visit($page->getUrl() . '/edit')
281 ->seeInElement('#recently-updated-pages', $page->name);
284 public function test_slug_multi_byte_lower_casing()
286 $entityRepo = app(EntityRepo::class);
287 $book = $entityRepo->createFromInput('book', [
291 $this->assertEquals('книга', $book->slug);
295 public function test_slug_format()
297 $entityRepo = app(EntityRepo::class);
298 $book = $entityRepo->createFromInput('book', [
299 'name' => 'PartA / PartB / PartC'
302 $this->assertEquals('parta-partb-partc', $book->slug);