]> BookStack Code Mirror - bookstack/blob - tests/Entity/EntityTest.php
Merge branch 'feature/#1598' of git://github.com/cw1998/BookStack into cw1998-feature...
[bookstack] / tests / Entity / EntityTest.php
1 <?php namespace Tests\Entity;
2
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;
9 use Carbon\Carbon;
10 use Tests\BrowserKitTest;
11
12 class EntityTest extends BrowserKitTest
13 {
14
15     public function test_entity_creation()
16     {
17         // Test Creation
18         $book = $this->bookCreation();
19         $chapter = $this->chapterCreation($book);
20         $page = $this->pageCreation($chapter);
21
22         // Test Updating
23         $book = $this->bookUpdate($book);
24
25         // Test Deletion
26         $this->bookDelete($book);
27     }
28
29     public function bookDelete(Book $book)
30     {
31         $this->asAdmin()
32             ->visit($book->getUrl())
33             // Check link works correctly
34             ->click('Delete')
35             ->seePageIs($book->getUrl() . '/delete')
36             // Ensure the book name is show to user
37             ->see($book->name)
38             ->press('Confirm')
39             ->seePageIs('/books')
40             ->notSeeInDatabase('books', ['id' => $book->id]);
41     }
42
43     public function bookUpdate(Book $book)
44     {
45         $newName = $book->name . ' Updated';
46         $this->asAdmin()
47             // Go to edit screen
48             ->visit($book->getUrl() . '/edit')
49             ->see($book->name)
50             // Submit new name
51             ->type($newName, '#name')
52             ->press('Save Book')
53             // Check page url and text
54             ->seePageIs($book->getUrl() . '-updated')
55             ->see($newName);
56
57         return Book::find($book->id);
58     }
59
60     public function test_book_sort_page_shows()
61     {
62         $books =  Book::all();
63         $bookToSort = $books[0];
64         $this->asAdmin()
65             ->visit($bookToSort->getUrl())
66             ->click('Sort')
67             ->seePageIs($bookToSort->getUrl() . '/sort')
68             ->seeStatusCode(200)
69             ->see($bookToSort->name);
70     }
71
72     public function test_book_sort_item_returns_book_content()
73     {
74         $books =  Book::all();
75         $bookToSort = $books[0];
76         $firstPage = $bookToSort->pages[0];
77         $firstChapter = $bookToSort->chapters[0];
78         $this->asAdmin()
79             ->visit($bookToSort->getUrl() . '/sort-item')
80             // Ensure book details are returned
81             ->see($bookToSort->name)
82             ->see($firstPage->name)
83             ->see($firstChapter->name);
84     }
85
86     public function test_toggle_book_view()
87     {
88         $editor = $this->getEditor();
89         setting()->putUser($editor, 'books_view_type', 'grid');
90
91         $this->actingAs($editor)
92             ->visit('/books')
93             ->pageHasElement('.featured-image-container')
94             ->submitForm('List View')
95             // Check redirection.
96             ->seePageIs('/books')
97             ->pageNotHasElement('.featured-image-container');
98
99         $this->actingAs($editor)
100             ->visit('/books')
101             ->submitForm('Grid View')
102             ->seePageIs('/books')
103             ->pageHasElement('.featured-image-container');
104
105     }
106
107     public function pageCreation($chapter)
108     {
109         $page = factory(Page::class)->make([
110             'name' => 'My First Page'
111         ]);
112
113         $this->asAdmin()
114             // Navigate to page create form
115             ->visit($chapter->getUrl())
116             ->click('New Page');
117
118         $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
119
120         $this->seePageIs($draftPage->getUrl())
121             // Fill out form
122             ->type($page->name, '#name')
123             ->type($page->html, '#html')
124             ->press('Save Page')
125             // Check redirect and page
126             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
127             ->see($page->name);
128
129         $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
130         return $page;
131     }
132
133     public function chapterCreation(Book $book)
134     {
135         $chapter = factory(Chapter::class)->make([
136             'name' => 'My First Chapter'
137         ]);
138
139         $this->asAdmin()
140             // Navigate to chapter create page
141             ->visit($book->getUrl())
142             ->click('New Chapter')
143             ->seePageIs($book->getUrl() . '/create-chapter')
144             // Fill out form
145             ->type($chapter->name, '#name')
146             ->type($chapter->description, '#description')
147             ->press('Save Chapter')
148             // Check redirect and landing page
149             ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
150             ->see($chapter->name)->see($chapter->description);
151
152         $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
153         return $chapter;
154     }
155
156     public function bookCreation()
157     {
158         $book = factory(Book::class)->make([
159             'name' => 'My First Book'
160         ]);
161         $this->asAdmin()
162             ->visit('/books')
163             // Choose to create a book
164             ->click('Create New Book')
165             ->seePageIs('/create-book')
166             // Fill out form & save
167             ->type($book->name, '#name')
168             ->type($book->description, '#description')
169             ->press('Save Book')
170             // Check it redirects correctly
171             ->seePageIs('/books/my-first-book')
172             ->see($book->name)->see($book->description);
173
174         // Ensure duplicate names are given different slugs
175         $this->asAdmin()
176             ->visit('/create-book')
177             ->type($book->name, '#name')
178             ->type($book->description, '#description')
179             ->press('Save Book');
180
181         $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
182         $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
183
184         $book = Book::where('slug', '=', 'my-first-book')->first();
185         return $book;
186     }
187
188     public function test_entities_viewable_after_creator_deletion()
189     {
190         // Create required assets and revisions
191         $creator = $this->getEditor();
192         $updater = $this->getEditor();
193         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
194         $this->actingAs($creator);
195         app(UserRepo::class)->destroy($creator);
196         app(PageRepo::class)->update($entities['page'], ['html' => '<p>hello!</p>>']);
197
198         $this->checkEntitiesViewable($entities);
199     }
200
201     public function test_entities_viewable_after_updater_deletion()
202     {
203         // Create required assets and revisions
204         $creator = $this->getEditor();
205         $updater = $this->getEditor();
206         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
207         $this->actingAs($updater);
208         app(UserRepo::class)->destroy($updater);
209         app(PageRepo::class)->update($entities['page'], ['html' => '<p>Hello there!</p>']);
210
211         $this->checkEntitiesViewable($entities);
212     }
213
214     private function checkEntitiesViewable($entities)
215     {
216         // Check pages and books are visible.
217         $this->asAdmin();
218         $this->visit($entities['book']->getUrl())->seeStatusCode(200)
219             ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
220             ->visit($entities['page']->getUrl())->seeStatusCode(200);
221         // Check revision listing shows no errors.
222         $this->visit($entities['page']->getUrl())
223             ->click('Revisions')->seeStatusCode(200);
224     }
225
226     public function test_recently_updated_pages_view()
227     {
228         $user = $this->getEditor();
229         $content = $this->createEntityChainBelongingToUser($user);
230
231         $this->asAdmin()->visit('/pages/recently-updated')
232             ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
233     }
234
235     public function test_old_page_slugs_redirect_to_new_pages()
236     {
237         $page = Page::first();
238         $pageUrl = $page->getUrl();
239         $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
240         // Need to save twice since revisions are not generated in seeder.
241         $this->asAdmin()->visit($pageUrl)
242             ->clickInElement('#content', 'Edit')
243             ->type('super test', '#name')
244             ->press('Save Page');
245
246         $page = Page::first();
247         $pageUrl = $page->getUrl();
248
249         // Second Save
250         $this->visit($pageUrl)
251             ->clickInElement('#content', 'Edit')
252             ->type('super test page', '#name')
253             ->press('Save Page')
254             // Check redirect
255             ->seePageIs($newPageUrl);
256
257         $this->visit($pageUrl)
258             ->seePageIs($newPageUrl);
259     }
260
261     public function test_recently_updated_pages_on_home()
262     {
263         $page = Page::orderBy('updated_at', 'asc')->first();
264         Page::where('id', '!=', $page->id)->update([
265             'updated_at' => Carbon::now()->subSecond(1)
266         ]);
267         $this->asAdmin()->visit('/')
268             ->dontSeeInElement('#recently-updated-pages', $page->name);
269         $this->visit($page->getUrl() . '/edit')
270             ->press('Save Page')
271             ->visit('/')
272             ->seeInElement('#recently-updated-pages', $page->name);
273     }
274
275     public function test_slug_multi_byte_lower_casing()
276     {
277         $book = $this->newBook([
278             'name' => 'КНИГА'
279         ]);
280
281         $this->assertEquals('книга', $book->slug);
282     }
283
284
285     public function test_slug_format()
286     {
287         $book = $this->newBook([
288             'name' => 'PartA / PartB / PartC'
289         ]);
290
291         $this->assertEquals('parta-partb-partc', $book->slug);
292     }
293
294     public function test_shelf_cancel_creation_returns_to_correct_place()
295     {
296         $shelf = Bookshelf::first();
297
298         // Cancel button from shelf goes back to shelf
299         $this->asEditor()->visit($shelf->getUrl('/create-book'))
300             ->see('Cancel')
301             ->click('Cancel')
302             ->seePageIs($shelf->getUrl());
303
304         // Cancel button from books goes back to books
305         $this->asEditor()->visit('/create-book')
306             ->see('Cancel')
307             ->click('Cancel')
308             ->seePageIs('/books');
309
310         // Cancel button from book edit goes back to book
311         $book = Book::first();
312
313         $this->asEditor()->visit($book->getUrl('/edit'))
314             ->see('Cancel')
315             ->click('Cancel')
316             ->seePageIs($book->getUrl());
317     }
318
319     public function test_page_within_chapter_deletion_returns_to_chapter()
320     {
321         $chapter = Chapter::query()->first();
322         $page = $chapter->pages()->first();
323
324         $this->asEditor()->visit($page->getUrl('/delete'))
325             ->submitForm('Confirm')
326             ->seePageIs($chapter->getUrl());
327     }
328
329 }