6 use BookStack\Repos\EntityRepo;
7 use BookStack\Repos\UserRepo;
9 class EntityTest extends BrowserKitTest
12 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 pageCreation($chapter)
88 $page = factory(Page::class)->make([
89 'name' => 'My First Page'
93 // Navigate to page create form
94 ->visit($chapter->getUrl())
97 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
99 $this->seePageIs($draftPage->getUrl())
101 ->type($page->name, '#name')
102 ->type($page->html, '#html')
104 // Check redirect and page
105 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
108 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
112 public function chapterCreation(Book $book)
114 $chapter = factory(Chapter::class)->make([
115 'name' => 'My First Chapter'
119 // Navigate to chapter create page
120 ->visit($book->getUrl())
121 ->click('New Chapter')
122 ->seePageIs($book->getUrl() . '/chapter/create')
124 ->type($chapter->name, '#name')
125 ->type($chapter->description, '#description')
126 ->press('Save Chapter')
127 // Check redirect and landing page
128 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
129 ->see($chapter->name)->see($chapter->description);
131 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
135 public function bookCreation()
137 $book = factory(Book::class)->make([
138 'name' => 'My First Book'
142 // Choose to create a book
143 ->click('Create New Book')
144 ->seePageIs('/books/create')
145 // Fill out form & save
146 ->type($book->name, '#name')
147 ->type($book->description, '#description')
149 // Check it redirects correctly
150 ->seePageIs('/books/my-first-book')
151 ->see($book->name)->see($book->description);
153 // Ensure duplicate names are given different slugs
155 ->visit('/books/create')
156 ->type($book->name, '#name')
157 ->type($book->description, '#description')
158 ->press('Save Book');
160 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
161 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
163 $book = Book::where('slug', '=', 'my-first-book')->first();
167 public function test_entities_viewable_after_creator_deletion()
169 // Create required assets and revisions
170 $creator = $this->getEditor();
171 $updater = $this->getEditor();
172 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
173 $this->actingAs($creator);
174 app(UserRepo::class)->destroy($creator);
175 app(EntityRepo::class)->savePageRevision($entities['page']);
177 $this->checkEntitiesViewable($entities);
180 public function test_entities_viewable_after_updater_deletion()
182 // Create required assets and revisions
183 $creator = $this->getEditor();
184 $updater = $this->getEditor();
185 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
186 $this->actingAs($updater);
187 app(UserRepo::class)->destroy($updater);
188 app(EntityRepo::class)->savePageRevision($entities['page']);
190 $this->checkEntitiesViewable($entities);
193 private function checkEntitiesViewable($entities)
195 // Check pages and books are visible.
197 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
198 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
199 ->visit($entities['page']->getUrl())->seeStatusCode(200);
200 // Check revision listing shows no errors.
201 $this->visit($entities['page']->getUrl())
202 ->click('Revisions')->seeStatusCode(200);
205 public function test_recently_created_pages_view()
207 $user = $this->getEditor();
208 $content = $this->createEntityChainBelongingToUser($user);
210 $this->asAdmin()->visit('/pages/recently-created')
211 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
214 public function test_recently_updated_pages_view()
216 $user = $this->getEditor();
217 $content = $this->createEntityChainBelongingToUser($user);
219 $this->asAdmin()->visit('/pages/recently-updated')
220 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
223 public function test_old_page_slugs_redirect_to_new_pages()
225 $page = Page::first();
226 $pageUrl = $page->getUrl();
227 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
228 // Need to save twice since revisions are not generated in seeder.
229 $this->asAdmin()->visit($pageUrl)
230 ->clickInElement('#content', 'Edit')
231 ->type('super test', '#name')
232 ->press('Save Page');
234 $page = Page::first();
235 $pageUrl = $page->getUrl();
238 $this->visit($pageUrl)
239 ->clickInElement('#content', 'Edit')
240 ->type('super test page', '#name')
243 ->seePageIs($newPageUrl);
245 $this->visit($pageUrl)
246 ->seePageIs($newPageUrl);
249 public function test_recently_updated_pages_on_home()
251 $page = Page::orderBy('updated_at', 'asc')->first();
252 $this->asAdmin()->visit('/')
253 ->dontSeeInElement('#recently-updated-pages', $page->name);
254 $this->visit($page->getUrl() . '/edit')
257 ->seeInElement('#recently-updated-pages', $page->name);