1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Bookshelf;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Page;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Entities\Repos\PageRepo;
10 use Tests\BrowserKitTest;
12 class EntityTest extends BrowserKitTest
15 public function test_entity_creation()
18 $book = $this->bookCreation();
19 $chapter = $this->chapterCreation($book);
20 $page = $this->pageCreation($chapter);
23 $book = $this->bookUpdate($book);
26 $this->bookDelete($book);
29 public function bookDelete(Book $book)
32 ->visit($book->getUrl())
33 // Check link works correctly
35 ->seePageIs($book->getUrl() . '/delete')
36 // Ensure the book name is show to user
40 ->notSeeInDatabase('books', ['id' => $book->id]);
43 public function bookUpdate(Book $book)
45 $newName = $book->name . ' Updated';
48 ->visit($book->getUrl() . '/edit')
51 ->type($newName, '#name')
53 // Check page url and text
54 ->seePageIs($book->getUrl() . '-updated')
57 return Book::find($book->id);
60 public function test_book_sort_page_shows()
63 $bookToSort = $books[0];
65 ->visit($bookToSort->getUrl())
67 ->seePageIs($bookToSort->getUrl() . '/sort')
69 ->see($bookToSort->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(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
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(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
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_updated_pages_view()
228 $user = $this->getEditor();
229 $content = $this->createEntityChainBelongingToUser($user);
231 $this->asAdmin()->visit('/pages/recently-updated')
232 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
235 public function test_old_page_slugs_redirect_to_new_pages()
237 $page = Page::first();
238 $pageUrl = $page->getUrl();
239 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
240 // Need to save twice since revisions are not generated in seeder.
241 $this->asAdmin()->visit($pageUrl)
242 ->clickInElement('#content', 'Edit')
243 ->type('super test', '#name')
244 ->press('Save Page');
246 $page = Page::first();
247 $pageUrl = $page->getUrl();
250 $this->visit($pageUrl)
251 ->clickInElement('#content', 'Edit')
252 ->type('super test page', '#name')
255 ->seePageIs($newPageUrl);
257 $this->visit($pageUrl)
258 ->seePageIs($newPageUrl);
261 public function test_recently_updated_pages_on_home()
263 $page = Page::orderBy('updated_at', 'asc')->first();
264 Page::where('id', '!=', $page->id)->update([
265 'updated_at' => Carbon::now()->subSecond(1)
267 $this->asAdmin()->visit('/')
268 ->dontSeeInElement('#recently-updated-pages', $page->name);
269 $this->visit($page->getUrl() . '/edit')
272 ->seeInElement('#recently-updated-pages', $page->name);
275 public function test_slug_multi_byte_lower_casing()
277 $book = $this->newBook([
281 $this->assertEquals('книга', $book->slug);
285 public function test_slug_format()
287 $book = $this->newBook([
288 'name' => 'PartA / PartB / PartC'
291 $this->assertEquals('parta-partb-partc', $book->slug);
294 public function test_shelf_cancel_creation_returns_to_correct_place()
296 $shelf = Bookshelf::first();
298 // Cancel button from shelf goes back to shelf
299 $this->asEditor()->visit($shelf->getUrl('/create-book'))
302 ->seePageIs($shelf->getUrl());
304 // Cancel button from books goes back to books
305 $this->asEditor()->visit('/create-book')
308 ->seePageIs('/books');
310 // Cancel button from book edit goes back to book
311 $book = Book::first();
313 $this->asEditor()->visit($book->getUrl('/edit'))
316 ->seePageIs($book->getUrl());
319 public function test_page_within_chapter_deletion_returns_to_chapter()
321 $chapter = Chapter::query()->first();
322 $page = $chapter->pages()->first();
324 $this->asEditor()->visit($page->getUrl('/delete'))
325 ->submitForm('Confirm')
326 ->seePageIs($chapter->getUrl());