6 use BookStack\Repos\EntityRepo;
7 use BookStack\Repos\UserRepo;
9 class EntityTest extends BrowserKitTest
12 public function test_entity_creation()
15 $book = $this->bookCreation();
16 $chapter = $this->chapterCreation($book);
17 $page = $this->pageCreation($chapter);
20 $book = $this->bookUpdate($book);
23 $this->bookDelete($book);
26 public function bookDelete(Book $book)
29 ->visit($book->getUrl())
30 // Check link works correctly
32 ->seePageIs($book->getUrl() . '/delete')
33 // Ensure the book name is show to user
37 ->notSeeInDatabase('books', ['id' => $book->id]);
40 public function bookUpdate(Book $book)
42 $newName = $book->name . ' Updated';
45 ->visit($book->getUrl() . '/edit')
48 ->type($newName, '#name')
50 // Check page url and text
51 ->seePageIs($book->getUrl() . '-updated')
54 return Book::find($book->id);
57 public function test_book_sort_page_shows()
60 $bookToSort = $books[0];
62 ->visit($bookToSort->getUrl())
64 ->seePageIs($bookToSort->getUrl() . '/sort')
66 ->see($bookToSort->name)
67 // Ensure page shows other books
68 ->see($books[1]->name);
71 public function test_book_sort_item_returns_book_content()
74 $bookToSort = $books[0];
75 $firstPage = $bookToSort->pages[0];
76 $firstChapter = $bookToSort->chapters[0];
78 ->visit($bookToSort->getUrl() . '/sort-item')
79 // Ensure book details are returned
80 ->see($bookToSort->name)
81 ->see($firstPage->name)
82 ->see($firstChapter->name);
85 public function test_toggle_book_view()
87 $editor = $this->getEditor();
88 setting()->putUser($editor, 'books_view_type', 'grid');
90 $this->actingAs($editor)
92 ->pageHasElement('.featured-image-container')
93 ->submitForm('List View')
96 ->pageNotHasElement('.featured-image-container');
98 $this->actingAs($editor)
100 ->submitForm('Grid View')
101 ->seePageIs('/books')
102 ->pageHasElement('.featured-image-container');
106 public function pageCreation($chapter)
108 $page = factory(Page::class)->make([
109 'name' => 'My First Page'
113 // Navigate to page create form
114 ->visit($chapter->getUrl())
117 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
119 $this->seePageIs($draftPage->getUrl())
121 ->type($page->name, '#name')
122 ->type($page->html, '#html')
124 // Check redirect and page
125 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
128 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
132 public function chapterCreation(Book $book)
134 $chapter = factory(Chapter::class)->make([
135 'name' => 'My First Chapter'
139 // Navigate to chapter create page
140 ->visit($book->getUrl())
141 ->click('New Chapter')
142 ->seePageIs($book->getUrl() . '/chapter/create')
144 ->type($chapter->name, '#name')
145 ->type($chapter->description, '#description')
146 ->press('Save Chapter')
147 // Check redirect and landing page
148 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
149 ->see($chapter->name)->see($chapter->description);
151 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
155 public function bookCreation()
157 $book = factory(Book::class)->make([
158 'name' => 'My First Book'
162 // Choose to create a book
163 ->click('Create New Book')
164 ->seePageIs('/books/create')
165 // Fill out form & save
166 ->type($book->name, '#name')
167 ->type($book->description, '#description')
169 // Check it redirects correctly
170 ->seePageIs('/books/my-first-book')
171 ->see($book->name)->see($book->description);
173 // Ensure duplicate names are given different slugs
175 ->visit('/books/create')
176 ->type($book->name, '#name')
177 ->type($book->description, '#description')
178 ->press('Save Book');
180 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
181 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
183 $book = Book::where('slug', '=', 'my-first-book')->first();
187 public function test_entities_viewable_after_creator_deletion()
189 // Create required assets and revisions
190 $creator = $this->getEditor();
191 $updater = $this->getEditor();
192 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
193 $this->actingAs($creator);
194 app(UserRepo::class)->destroy($creator);
195 app(EntityRepo::class)->savePageRevision($entities['page']);
197 $this->checkEntitiesViewable($entities);
200 public function test_entities_viewable_after_updater_deletion()
202 // Create required assets and revisions
203 $creator = $this->getEditor();
204 $updater = $this->getEditor();
205 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
206 $this->actingAs($updater);
207 app(UserRepo::class)->destroy($updater);
208 app(EntityRepo::class)->savePageRevision($entities['page']);
210 $this->checkEntitiesViewable($entities);
213 private function checkEntitiesViewable($entities)
215 // Check pages and books are visible.
217 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
218 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
219 ->visit($entities['page']->getUrl())->seeStatusCode(200);
220 // Check revision listing shows no errors.
221 $this->visit($entities['page']->getUrl())
222 ->click('Revisions')->seeStatusCode(200);
225 public function test_recently_created_pages_view()
227 $user = $this->getEditor();
228 $content = $this->createEntityChainBelongingToUser($user);
230 $this->asAdmin()->visit('/pages/recently-created')
231 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
234 public function test_recently_updated_pages_view()
236 $user = $this->getEditor();
237 $content = $this->createEntityChainBelongingToUser($user);
239 $this->asAdmin()->visit('/pages/recently-updated')
240 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
243 public function test_old_page_slugs_redirect_to_new_pages()
245 $page = Page::first();
246 $pageUrl = $page->getUrl();
247 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
248 // Need to save twice since revisions are not generated in seeder.
249 $this->asAdmin()->visit($pageUrl)
250 ->clickInElement('#content', 'Edit')
251 ->type('super test', '#name')
252 ->press('Save Page');
254 $page = Page::first();
255 $pageUrl = $page->getUrl();
258 $this->visit($pageUrl)
259 ->clickInElement('#content', 'Edit')
260 ->type('super test page', '#name')
263 ->seePageIs($newPageUrl);
265 $this->visit($pageUrl)
266 ->seePageIs($newPageUrl);
269 public function test_recently_updated_pages_on_home()
271 $page = Page::orderBy('updated_at', 'asc')->first();
272 $this->asAdmin()->visit('/')
273 ->dontSeeInElement('#recently-updated-pages', $page->name);
274 $this->visit($page->getUrl() . '/edit')
277 ->seeInElement('#recently-updated-pages', $page->name);
280 public function test_slug_multi_byte_lower_casing()
282 $entityRepo = app(EntityRepo::class);
283 $book = $entityRepo->createFromInput('book', [
287 $this->assertEquals('книга', $book->slug);
291 public function test_slug_format()
293 $entityRepo = app(EntityRepo::class);
294 $book = $entityRepo->createFromInput('book', [
295 'name' => 'PartA / PartB / PartC'
298 $this->assertEquals('parta-partb-partc', $book->slug);