1 <?php namespace Tests\Entity;
3 use BookStack\Entities\Bookshelf;
4 use BookStack\Entities\Book;
5 use BookStack\Entities\Chapter;
6 use BookStack\Entities\Page;
7 use BookStack\Auth\UserRepo;
8 use BookStack\Entities\Repos\PageRepo;
10 use Illuminate\Support\Facades\DB;
11 use Tests\BrowserKitTest;
13 class EntityTest extends BrowserKitTest
16 public function test_entity_creation()
19 $book = $this->bookCreation();
20 $chapter = $this->chapterCreation($book);
21 $page = $this->pageCreation($chapter);
24 $book = $this->bookUpdate($book);
27 $this->bookDelete($book);
30 public function bookDelete(Book $book)
33 ->visit($book->getUrl())
34 // Check link works correctly
36 ->seePageIs($book->getUrl() . '/delete')
37 // Ensure the book name is show to user
41 ->notSeeInDatabase('books', ['id' => $book->id]);
44 public function bookUpdate(Book $book)
46 $newName = $book->name . ' Updated';
49 ->visit($book->getUrl() . '/edit')
52 ->type($newName, '#name')
54 // Check page url and text
55 ->seePageIs($book->getUrl() . '-updated')
58 return Book::find($book->id);
61 public function test_book_sort_page_shows()
64 $bookToSort = $books[0];
66 ->visit($bookToSort->getUrl())
68 ->seePageIs($bookToSort->getUrl() . '/sort')
70 ->see($bookToSort->name);
73 public function test_book_sort_item_returns_book_content()
76 $bookToSort = $books[0];
77 $firstPage = $bookToSort->pages[0];
78 $firstChapter = $bookToSort->chapters[0];
80 ->visit($bookToSort->getUrl() . '/sort-item')
81 // Ensure book details are returned
82 ->see($bookToSort->name)
83 ->see($firstPage->name)
84 ->see($firstChapter->name);
87 public function test_toggle_book_view()
89 $editor = $this->getEditor();
90 setting()->putUser($editor, 'books_view_type', 'grid');
92 $this->actingAs($editor)
94 ->pageHasElement('.featured-image-container')
95 ->submitForm('List View')
98 ->pageNotHasElement('.featured-image-container');
100 $this->actingAs($editor)
102 ->submitForm('Grid View')
103 ->seePageIs('/books')
104 ->pageHasElement('.featured-image-container');
108 public function pageCreation($chapter)
110 $page = factory(Page::class)->make([
111 'name' => 'My First Page'
115 // Navigate to page create form
116 ->visit($chapter->getUrl())
119 $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
121 $this->seePageIs($draftPage->getUrl())
123 ->type($page->name, '#name')
124 ->type($page->html, '#html')
126 // Check redirect and page
127 ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
130 $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
134 public function chapterCreation(Book $book)
136 $chapter = factory(Chapter::class)->make([
137 'name' => 'My First Chapter'
141 // Navigate to chapter create page
142 ->visit($book->getUrl())
143 ->click('New Chapter')
144 ->seePageIs($book->getUrl() . '/create-chapter')
146 ->type($chapter->name, '#name')
147 ->type($chapter->description, '#description')
148 ->press('Save Chapter')
149 // Check redirect and landing page
150 ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
151 ->see($chapter->name)->see($chapter->description);
153 $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
157 public function bookCreation()
159 $book = factory(Book::class)->make([
160 'name' => 'My First Book'
164 // Choose to create a book
165 ->click('Create New Book')
166 ->seePageIs('/create-book')
167 // Fill out form & save
168 ->type($book->name, '#name')
169 ->type($book->description, '#description')
171 // Check it redirects correctly
172 ->seePageIs('/books/my-first-book')
173 ->see($book->name)->see($book->description);
175 // Ensure duplicate names are given different slugs
177 ->visit('/create-book')
178 ->type($book->name, '#name')
179 ->type($book->description, '#description')
180 ->press('Save Book');
182 $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
183 $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
185 $book = Book::where('slug', '=', 'my-first-book')->first();
189 public function test_entities_viewable_after_creator_deletion()
191 // Create required assets and revisions
192 $creator = $this->getEditor();
193 $updater = $this->getEditor();
194 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
195 $this->actingAs($creator);
196 app(UserRepo::class)->destroy($creator);
197 app(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
199 $this->checkEntitiesViewable($entities);
202 public function test_entities_viewable_after_updater_deletion()
204 // Create required assets and revisions
205 $creator = $this->getEditor();
206 $updater = $this->getEditor();
207 $entities = $this->createEntityChainBelongingToUser($creator, $updater);
208 $this->actingAs($updater);
209 app(UserRepo::class)->destroy($updater);
210 app(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
212 $this->checkEntitiesViewable($entities);
215 private function checkEntitiesViewable($entities)
217 // Check pages and books are visible.
219 $this->visit($entities['book']->getUrl())->seeStatusCode(200)
220 ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
221 ->visit($entities['page']->getUrl())->seeStatusCode(200);
222 // Check revision listing shows no errors.
223 $this->visit($entities['page']->getUrl())
224 ->click('Revisions')->seeStatusCode(200);
227 public function test_recently_updated_pages_view()
229 $user = $this->getEditor();
230 $content = $this->createEntityChainBelongingToUser($user);
232 $this->asAdmin()->visit('/pages/recently-updated')
233 ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
236 public function test_old_page_slugs_redirect_to_new_pages()
238 $page = Page::first();
239 $pageUrl = $page->getUrl();
240 $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
241 // Need to save twice since revisions are not generated in seeder.
242 $this->asAdmin()->visit($pageUrl)
243 ->clickInElement('#content', 'Edit')
244 ->type('super test', '#name')
245 ->press('Save Page');
247 $page = Page::first();
248 $pageUrl = $page->getUrl();
251 $this->visit($pageUrl)
252 ->clickInElement('#content', 'Edit')
253 ->type('super test page', '#name')
256 ->seePageIs($newPageUrl);
258 $this->visit($pageUrl)
259 ->seePageIs($newPageUrl);
262 public function test_recently_updated_pages_on_home()
264 $page = Page::orderBy('updated_at', 'asc')->first();
265 Page::where('id', '!=', $page->id)->update([
266 'updated_at' => Carbon::now()->subSecond(1)
268 $this->asAdmin()->visit('/')
269 ->dontSeeInElement('#recently-updated-pages', $page->name);
270 $this->visit($page->getUrl() . '/edit')
273 ->seeInElement('#recently-updated-pages', $page->name);
276 public function test_slug_multi_byte_url_safe()
278 $book = $this->newBook([
279 'name' => 'информация'
282 $this->assertEquals('informatsiya', $book->slug);
284 $book = $this->newBook([
288 $this->assertEquals('que', $book->slug);
291 public function test_slug_format()
293 $book = $this->newBook([
294 'name' => 'PartA / PartB / PartC'
297 $this->assertEquals('parta-partb-partc', $book->slug);
300 public function test_shelf_cancel_creation_returns_to_correct_place()
302 $shelf = Bookshelf::first();
304 // Cancel button from shelf goes back to shelf
305 $this->asEditor()->visit($shelf->getUrl('/create-book'))
308 ->seePageIs($shelf->getUrl());
310 // Cancel button from books goes back to books
311 $this->asEditor()->visit('/create-book')
314 ->seePageIs('/books');
316 // Cancel button from book edit goes back to book
317 $book = Book::first();
319 $this->asEditor()->visit($book->getUrl('/edit'))
322 ->seePageIs($book->getUrl());
325 public function test_page_within_chapter_deletion_returns_to_chapter()
327 $chapter = Chapter::query()->first();
328 $page = $chapter->pages()->first();
330 $this->asEditor()->visit($page->getUrl('/delete'))
331 ->submitForm('Confirm')
332 ->seePageIs($chapter->getUrl());
335 public function test_page_delete_removes_entity_from_its_activity()
337 $page = Page::query()->first();
339 $this->asEditor()->put($page->getUrl(), [
340 'name' => 'My updated page',
341 'html' => '<p>updated content</p>',
345 $this->seeInDatabase('activities', [
346 'entity_id' => $page->id,
347 'entity_type' => $page->getMorphClass(),
350 $resp = $this->delete($page->getUrl());
351 $resp->assertResponseStatus(302);
353 $this->dontSeeInDatabase('activities', [
354 'entity_id' => $page->id,
355 'entity_type' => $page->getMorphClass(),
358 $this->seeInDatabase('activities', [
359 'extra' => 'My updated page',