3 use Illuminate\Support\Facades\DB;
5 class EntityTest extends TestCase
8 public function test_entity_creation()
12 $book = $this->bookCreation();
13 $chapter = $this->chapterCreation($book);
14 $page = $this->pageCreation($chapter);
17 $book = $this->bookUpdate($book);
20 $this->bookDelete($book);
23 public function bookDelete(\BookStack\Book $book)
26 ->visit($book->getUrl())
27 // Check link works correctly
29 ->seePageIs($book->getUrl() . '/delete')
30 // Ensure the book name is show to user
34 ->notSeeInDatabase('books', ['id' => $book->id]);
37 public function bookUpdate(\BookStack\Book $book)
39 $newName = $book->name . ' Updated';
42 ->visit($book->getUrl() . '/edit')
45 ->type($newName, '#name')
47 // Check page url and text
48 ->seePageIs($book->getUrl() . '-updated')
51 return \BookStack\Book::find($book->id);
54 public function test_book_sort_page_shows()
56 $books = \BookStack\Book::all();
57 $bookToSort = $books[0];
59 ->visit($bookToSort->getUrl())
61 ->seePageIs($bookToSort->getUrl() . '/sort')
63 ->see($bookToSort->name)
64 // Ensure page shows other books
65 ->see($books[1]->name);
68 public function test_book_sort_item_returns_book_content()
70 $books = \BookStack\Book::all();
71 $bookToSort = $books[0];
72 $firstPage = $bookToSort->pages[0];
73 $firstChapter = $bookToSort->chapters[0];
75 ->visit($bookToSort->getUrl() . '/sort-item')
76 // Ensure book details are returned
77 ->see($bookToSort->name)
78 ->see($firstPage->name)
79 ->see($firstChapter->name);
82 public function pageCreation($chapter)
84 $page = factory(\BookStack\Page::class)->make([
85 'name' => 'My First Page'
89 // Navigate to page create form
90 ->visit($chapter->getUrl())
93 $draftPage = \BookStack\Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
95 $this->seePageIs($draftPage->getUrl())
97 ->type($page->name, '#name')
98 ->type($page->html, '#html')
100 // Check redirect and page
101 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
104 $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
108 public function chapterCreation(\BookStack\Book $book)
110 $chapter = factory(\BookStack\Chapter::class)->make([
111 'name' => 'My First Chapter'
115 // Navigate to chapter create page
116 ->visit($book->getUrl())
117 ->click('New Chapter')
118 ->seePageIs($book->getUrl() . '/chapter/create')
120 ->type($chapter->name, '#name')
121 ->type($chapter->description, '#description')
122 ->press('Save Chapter')
123 // Check redirect and landing page
124 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
125 ->see($chapter->name)->see($chapter->description);
127 $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
131 public function bookCreation()
133 $book = factory(\BookStack\Book::class)->make([
134 'name' => 'My First Book'
138 // Choose to create a book
139 ->click('Add new book')
140 ->seePageIs('/books/create')
141 // Fill out form & save
142 ->type($book->name, '#name')
143 ->type($book->description, '#description')
145 // Check it redirects correctly
146 ->seePageIs('/books/my-first-book')
147 ->see($book->name)->see($book->description);
149 // Ensure duplicate names are given different slugs
151 ->visit('/books/create')
152 ->type($book->name, '#name')
153 ->type($book->description, '#description')
154 ->press('Save Book');
156 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
157 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
159 $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
163 public function test_entities_viewable_after_creator_deletion()
165 // Create required assets and revisions
166 $creator = $this->getEditor();
167 $updater = $this->getEditor();
168 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
169 $this->actingAs($creator);
170 app('BookStack\Repos\UserRepo')->destroy($creator);
171 app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
173 $this->checkEntitiesViewable($entities);
176 public function test_entities_viewable_after_updater_deletion()
178 // Create required assets and revisions
179 $creator = $this->getEditor();
180 $updater = $this->getEditor();
181 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
182 $this->actingAs($updater);
183 app('BookStack\Repos\UserRepo')->destroy($updater);
184 app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
186 $this->checkEntitiesViewable($entities);
189 private function checkEntitiesViewable($entities)
191 // Check pages and books are visible.
193 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
194 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
195 ->visit($entities['page']->getUrl())->seeStatusCode(200);
196 // Check revision listing shows no errors.
197 $this->visit($entities['page']->getUrl())
198 ->click('Revisions')->seeStatusCode(200);
201 public function test_recently_created_pages_view()
203 $user = $this->getEditor();
204 $content = $this->createEntityChainBelongingToUser($user);
206 $this->asAdmin()->visit('/pages/recently-created')
207 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
210 public function test_recently_updated_pages_view()
212 $user = $this->getEditor();
213 $content = $this->createEntityChainBelongingToUser($user);
215 $this->asAdmin()->visit('/pages/recently-updated')
216 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
219 public function test_old_page_slugs_redirect_to_new_pages()
221 $page = \BookStack\Page::first();
222 $pageUrl = $page->getUrl();
223 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
224 // Need to save twice since revisions are not generated in seeder.
225 $this->asAdmin()->visit($pageUrl)
226 ->clickInElement('#content', 'Edit')
227 ->type('super test', '#name')
228 ->press('Save Page');
230 $page = \BookStack\Page::first();
231 $pageUrl = $page->getUrl();
234 $this->visit($pageUrl)
235 ->clickInElement('#content', 'Edit')
236 ->type('super test page', '#name')
239 ->seePageIs($newPageUrl)
241 ->seePageIs($newPageUrl);
244 public function test_recently_updated_pages_on_home()
246 $page = \BookStack\Page::orderBy('updated_at', 'asc')->first();
247 $this->asAdmin()->visit('/')
248 ->dontSeeInElement('#recently-updated-pages', $page->name);
249 $this->visit($page->getUrl() . '/edit')
252 ->seeInElement('#recently-updated-pages', $page->name);
255 public function test_recently_created_pages_on_home()
257 $entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
258 $this->asAdmin()->visit('/')
259 ->seeInElement('#recently-created-pages', $entityChain['page']->name);