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 pageCreation($chapter)
87 $page = factory(Page::class)->make([
88 'name' => 'My First Page'
92 // Navigate to page create form
93 ->visit($chapter->getUrl())
96 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
98 $this->seePageIs($draftPage->getUrl())
100 ->type($page->name, '#name')
101 ->type($page->html, '#html')
103 // Check redirect and page
104 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
107 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
111 public function chapterCreation(Book $book)
113 $chapter = factory(Chapter::class)->make([
114 'name' => 'My First Chapter'
118 // Navigate to chapter create page
119 ->visit($book->getUrl())
120 ->click('New Chapter')
121 ->seePageIs($book->getUrl() . '/chapter/create')
123 ->type($chapter->name, '#name')
124 ->type($chapter->description, '#description')
125 ->press('Save Chapter')
126 // Check redirect and landing page
127 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
128 ->see($chapter->name)->see($chapter->description);
130 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
134 public function bookCreation()
136 $book = factory(Book::class)->make([
137 'name' => 'My First Book'
141 // Choose to create a book
142 ->click('Create New Book')
143 ->seePageIs('/books/create')
144 // Fill out form & save
145 ->type($book->name, '#name')
146 ->type($book->description, '#description')
148 // Check it redirects correctly
149 ->seePageIs('/books/my-first-book')
150 ->see($book->name)->see($book->description);
152 // Ensure duplicate names are given different slugs
154 ->visit('/books/create')
155 ->type($book->name, '#name')
156 ->type($book->description, '#description')
157 ->press('Save Book');
159 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
160 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
162 $book = Book::where('slug', '=', 'my-first-book')->first();
166 public function test_entities_viewable_after_creator_deletion()
168 // Create required assets and revisions
169 $creator = $this->getEditor();
170 $updater = $this->getEditor();
171 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
172 $this->actingAs($creator);
173 app(UserRepo::class)->destroy($creator);
174 app(EntityRepo::class)->savePageRevision($entities['page']);
176 $this->checkEntitiesViewable($entities);
179 public function test_entities_viewable_after_updater_deletion()
181 // Create required assets and revisions
182 $creator = $this->getEditor();
183 $updater = $this->getEditor();
184 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
185 $this->actingAs($updater);
186 app(UserRepo::class)->destroy($updater);
187 app(EntityRepo::class)->savePageRevision($entities['page']);
189 $this->checkEntitiesViewable($entities);
192 private function checkEntitiesViewable($entities)
194 // Check pages and books are visible.
196 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
197 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
198 ->visit($entities['page']->getUrl())->seeStatusCode(200);
199 // Check revision listing shows no errors.
200 $this->visit($entities['page']->getUrl())
201 ->click('Revisions')->seeStatusCode(200);
204 public function test_recently_created_pages_view()
206 $user = $this->getEditor();
207 $content = $this->createEntityChainBelongingToUser($user);
209 $this->asAdmin()->visit('/pages/recently-created')
210 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
213 public function test_recently_updated_pages_view()
215 $user = $this->getEditor();
216 $content = $this->createEntityChainBelongingToUser($user);
218 $this->asAdmin()->visit('/pages/recently-updated')
219 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
222 public function test_old_page_slugs_redirect_to_new_pages()
224 $page = Page::first();
225 $pageUrl = $page->getUrl();
226 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
227 // Need to save twice since revisions are not generated in seeder.
228 $this->asAdmin()->visit($pageUrl)
229 ->clickInElement('#content', 'Edit')
230 ->type('super test', '#name')
231 ->press('Save Page');
233 $page = Page::first();
234 $pageUrl = $page->getUrl();
237 $this->visit($pageUrl)
238 ->clickInElement('#content', 'Edit')
239 ->type('super test page', '#name')
242 ->seePageIs($newPageUrl);
244 $this->visit($pageUrl)
245 ->seePageIs($newPageUrl);
248 public function test_recently_updated_pages_on_home()
250 $page = Page::orderBy('updated_at', 'asc')->first();
251 $this->asAdmin()->visit('/')
252 ->dontSeeInElement('#recently-updated-pages', $page->name);
253 $this->visit($page->getUrl() . '/edit')
256 ->seeInElement('#recently-updated-pages', $page->name);
259 public function test_slug_multi_byte_lower_casing()
261 $entityRepo = app(EntityRepo::class);
262 $book = $entityRepo->createFromInput('book', [
266 $this->assertEquals('книга', $book->slug);
270 public function test_slug_format()
272 $entityRepo = app(EntityRepo::class);
273 $book = $entityRepo->createFromInput('book', [
274 'name' => 'PartA / PartB / PartC'
277 $this->assertEquals('parta-partb-partc', $book->slug);