3 use Illuminate\Support\Facades\DB;
5 class EntityTest extends TestCase
8 public function testEntityCreation()
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 testBookSortPageShows()
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 testBookSortItemReturnsBookContent()
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())
92 ->seePageIs($chapter->getUrl() . '/create-page')
94 ->type($page->name, '#name')
95 ->type($page->html, '#html')
97 // Check redirect and page
98 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
101 $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
105 public function chapterCreation(\BookStack\Book $book)
107 $chapter = factory(\BookStack\Chapter::class)->make([
108 'name' => 'My First Chapter'
112 // Navigate to chapter create page
113 ->visit($book->getUrl())
114 ->click('New Chapter')
115 ->seePageIs($book->getUrl() . '/chapter/create')
117 ->type($chapter->name, '#name')
118 ->type($chapter->description, '#description')
119 ->press('Save Chapter')
120 // Check redirect and landing page
121 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
122 ->see($chapter->name)->see($chapter->description);
124 $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
128 public function bookCreation()
130 $book = factory(\BookStack\Book::class)->make([
131 'name' => 'My First Book'
135 // Choose to create a book
136 ->click('Add new book')
137 ->seePageIs('/books/create')
138 // Fill out form & save
139 ->type($book->name, '#name')
140 ->type($book->description, '#description')
142 // Check it redirects correctly
143 ->seePageIs('/books/my-first-book')
144 ->see($book->name)->see($book->description);
146 // Ensure duplicate names are given different slugs
148 ->visit('/books/create')
149 ->type($book->name, '#name')
150 ->type($book->description, '#description')
152 ->seePageIs('/books/my-first-book-2');
154 $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
158 public function testPageSearch()
160 $book = \BookStack\Book::all()->first();
161 $page = $book->pages->first();
165 ->type($page->name, 'term')
166 ->press('header-search-box-button')
167 ->see('Search Results')
170 ->seePageIs($page->getUrl());
173 public function testInvalidPageSearch()
177 ->type('<p>test</p>', 'term')
178 ->press('header-search-box-button')
179 ->see('Search Results')
180 ->seeStatusCode(200);
183 public function testEmptySearchRedirectsBack()
187 ->visit('/search/all')
191 public function testBookSearch()
193 $book = \BookStack\Book::all()->first();
194 $page = $book->pages->last();
195 $chapter = $book->chapters->last();
198 ->visit('/search/book/' . $book->id . '?term=' . urlencode($page->name))
201 ->visit('/search/book/' . $book->id . '?term=' . urlencode($chapter->name))
202 ->see($chapter->name);
205 public function testEmptyBookSearchRedirectsBack()
207 $book = \BookStack\Book::all()->first();
210 ->visit('/search/book/' . $book->id . '?term=')
211 ->seePageIs('/books');
215 public function testEntitiesViewableAfterCreatorDeletion()
217 // Create required assets and revisions
218 $creator = $this->getNewUser();
219 $updater = $this->getNewUser();
220 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
221 $this->actingAs($creator);
222 app('BookStack\Repos\UserRepo')->destroy($creator);
223 app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
225 $this->checkEntitiesViewable($entities);
228 public function testEntitiesViewableAfterUpdaterDeletion()
230 // Create required assets and revisions
231 $creator = $this->getNewUser();
232 $updater = $this->getNewUser();
233 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
234 $this->actingAs($updater);
235 app('BookStack\Repos\UserRepo')->destroy($updater);
236 app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
238 $this->checkEntitiesViewable($entities);
241 private function checkEntitiesViewable($entities)
243 // Check pages and books are visible.
245 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
246 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
247 ->visit($entities['page']->getUrl())->seeStatusCode(200);
248 // Check revision listing shows no errors.
249 $this->visit($entities['page']->getUrl())
250 ->click('Revisions')->seeStatusCode(200);