3 class EntityTest extends BrowserKitTest
6 public function test_entity_creation()
10 $book = $this->bookCreation();
11 $chapter = $this->chapterCreation($book);
12 $page = $this->pageCreation($chapter);
15 $book = $this->bookUpdate($book);
18 $this->bookDelete($book);
21 public function bookDelete(\BookStack\Book $book)
24 ->visit($book->getUrl())
25 // Check link works correctly
27 ->seePageIs($book->getUrl() . '/delete')
28 // Ensure the book name is show to user
32 ->notSeeInDatabase('books', ['id' => $book->id]);
35 public function bookUpdate(\BookStack\Book $book)
37 $newName = $book->name . ' Updated';
40 ->visit($book->getUrl() . '/edit')
43 ->type($newName, '#name')
45 // Check page url and text
46 ->seePageIs($book->getUrl() . '-updated')
49 return \BookStack\Book::find($book->id);
52 public function test_book_sort_page_shows()
54 $books = \BookStack\Book::all();
55 $bookToSort = $books[0];
57 ->visit($bookToSort->getUrl())
59 ->seePageIs($bookToSort->getUrl() . '/sort')
61 ->see($bookToSort->name)
62 // Ensure page shows other books
63 ->see($books[1]->name);
66 public function test_book_sort_item_returns_book_content()
68 $books = \BookStack\Book::all();
69 $bookToSort = $books[0];
70 $firstPage = $bookToSort->pages[0];
71 $firstChapter = $bookToSort->chapters[0];
73 ->visit($bookToSort->getUrl() . '/sort-item')
74 // Ensure book details are returned
75 ->see($bookToSort->name)
76 ->see($firstPage->name)
77 ->see($firstChapter->name);
80 public function pageCreation($chapter)
82 $page = factory(\BookStack\Page::class)->make([
83 'name' => 'My First Page'
87 // Navigate to page create form
88 ->visit($chapter->getUrl())
91 $draftPage = \BookStack\Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
93 $this->seePageIs($draftPage->getUrl())
95 ->type($page->name, '#name')
96 ->type($page->html, '#html')
98 // Check redirect and page
99 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
102 $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
106 public function chapterCreation(\BookStack\Book $book)
108 $chapter = factory(\BookStack\Chapter::class)->make([
109 'name' => 'My First Chapter'
113 // Navigate to chapter create page
114 ->visit($book->getUrl())
115 ->click('New Chapter')
116 ->seePageIs($book->getUrl() . '/chapter/create')
118 ->type($chapter->name, '#name')
119 ->type($chapter->description, '#description')
120 ->press('Save Chapter')
121 // Check redirect and landing page
122 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
123 ->see($chapter->name)->see($chapter->description);
125 $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
129 public function bookCreation()
131 $book = factory(\BookStack\Book::class)->make([
132 'name' => 'My First Book'
136 // Choose to create a book
137 ->click('Create New Book')
138 ->seePageIs('/books/create')
139 // Fill out form & save
140 ->type($book->name, '#name')
141 ->type($book->description, '#description')
143 // Check it redirects correctly
144 ->seePageIs('/books/my-first-book')
145 ->see($book->name)->see($book->description);
147 // Ensure duplicate names are given different slugs
149 ->visit('/books/create')
150 ->type($book->name, '#name')
151 ->type($book->description, '#description')
152 ->press('Save Book');
154 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
155 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
157 $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
161 public function test_entities_viewable_after_creator_deletion()
163 // Create required assets and revisions
164 $creator = $this->getEditor();
165 $updater = $this->getEditor();
166 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
167 $this->actingAs($creator);
168 app('BookStack\Repos\UserRepo')->destroy($creator);
169 app('BookStack\Repos\EntityRepo')->savePageRevision($entities['page']);
171 $this->checkEntitiesViewable($entities);
174 public function test_entities_viewable_after_updater_deletion()
176 // Create required assets and revisions
177 $creator = $this->getEditor();
178 $updater = $this->getEditor();
179 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
180 $this->actingAs($updater);
181 app('BookStack\Repos\UserRepo')->destroy($updater);
182 app('BookStack\Repos\EntityRepo')->savePageRevision($entities['page']);
184 $this->checkEntitiesViewable($entities);
187 private function checkEntitiesViewable($entities)
189 // Check pages and books are visible.
191 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
192 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
193 ->visit($entities['page']->getUrl())->seeStatusCode(200);
194 // Check revision listing shows no errors.
195 $this->visit($entities['page']->getUrl())
196 ->click('Revisions')->seeStatusCode(200);
199 public function test_recently_created_pages_view()
201 $user = $this->getEditor();
202 $content = $this->createEntityChainBelongingToUser($user);
204 $this->asAdmin()->visit('/pages/recently-created')
205 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
208 public function test_recently_updated_pages_view()
210 $user = $this->getEditor();
211 $content = $this->createEntityChainBelongingToUser($user);
213 $this->asAdmin()->visit('/pages/recently-updated')
214 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
217 public function test_old_page_slugs_redirect_to_new_pages()
219 $page = \BookStack\Page::first();
220 $pageUrl = $page->getUrl();
221 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
222 // Need to save twice since revisions are not generated in seeder.
223 $this->asAdmin()->visit($pageUrl)
224 ->clickInElement('#content', 'Edit')
225 ->type('super test', '#name')
226 ->press('Save Page');
228 $page = \BookStack\Page::first();
229 $pageUrl = $page->getUrl();
232 $this->visit($pageUrl)
233 ->clickInElement('#content', 'Edit')
234 ->type('super test page', '#name')
237 ->seePageIs($newPageUrl);
239 $this->visit($pageUrl)
240 ->seePageIs($newPageUrl);
243 public function test_recently_updated_pages_on_home()
245 $page = \BookStack\Page::orderBy('updated_at', 'asc')->first();
246 $this->asAdmin()->visit('/')
247 ->dontSeeInElement('#recently-updated-pages', $page->name);
248 $this->visit($page->getUrl() . '/edit')
251 ->seeInElement('#recently-updated-pages', $page->name);
254 public function test_recently_created_pages_on_home()
256 $entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
257 $this->asAdmin()->visit('/')
258 ->seeInElement('#recently-created-pages', $entityChain['page']->name);