]> BookStack Code Mirror - bookstack/blob - tests/Entity/EntityTest.php
Merge branch 'master' of https://github.com/BookStackApp/BookStack
[bookstack] / tests / Entity / EntityTest.php
1 <?php
2
3 class EntityTest extends BrowserKitTest
4 {
5
6     public function test_entity_creation()
7     {
8
9         // Test Creation
10         $book = $this->bookCreation();
11         $chapter = $this->chapterCreation($book);
12         $page = $this->pageCreation($chapter);
13
14         // Test Updating
15         $book = $this->bookUpdate($book);
16
17         // Test Deletion
18         $this->bookDelete($book);
19     }
20
21     public function bookDelete(\BookStack\Book $book)
22     {
23         $this->asAdmin()
24             ->visit($book->getUrl())
25             // Check link works correctly
26             ->click('Delete')
27             ->seePageIs($book->getUrl() . '/delete')
28             // Ensure the book name is show to user
29             ->see($book->name)
30             ->press('Confirm')
31             ->seePageIs('/books')
32             ->notSeeInDatabase('books', ['id' => $book->id]);
33     }
34
35     public function bookUpdate(\BookStack\Book $book)
36     {
37         $newName = $book->name . ' Updated';
38         $this->asAdmin()
39             // Go to edit screen
40             ->visit($book->getUrl() . '/edit')
41             ->see($book->name)
42             // Submit new name
43             ->type($newName, '#name')
44             ->press('Save Book')
45             // Check page url and text
46             ->seePageIs($book->getUrl() . '-updated')
47             ->see($newName);
48
49         return \BookStack\Book::find($book->id);
50     }
51
52     public function test_book_sort_page_shows()
53     {
54         $books =  \BookStack\Book::all();
55         $bookToSort = $books[0];
56         $this->asAdmin()
57             ->visit($bookToSort->getUrl())
58             ->click('Sort')
59             ->seePageIs($bookToSort->getUrl() . '/sort')
60             ->seeStatusCode(200)
61             ->see($bookToSort->name)
62             // Ensure page shows other books
63             ->see($books[1]->name);
64     }
65
66     public function test_book_sort_item_returns_book_content()
67     {
68         $books =  \BookStack\Book::all();
69         $bookToSort = $books[0];
70         $firstPage = $bookToSort->pages[0];
71         $firstChapter = $bookToSort->chapters[0];
72         $this->asAdmin()
73             ->visit($bookToSort->getUrl() . '/sort-item')
74             // Ensure book details are returned
75             ->see($bookToSort->name)
76             ->see($firstPage->name)
77             ->see($firstChapter->name);
78     }
79
80     public function pageCreation($chapter)
81     {
82         $page = factory(\BookStack\Page::class)->make([
83             'name' => 'My First Page'
84         ]);
85
86         $this->asAdmin()
87             // Navigate to page create form
88             ->visit($chapter->getUrl())
89             ->click('New Page');
90
91         $draftPage = \BookStack\Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
92
93         $this->seePageIs($draftPage->getUrl())
94             // Fill out form
95             ->type($page->name, '#name')
96             ->type($page->html, '#html')
97             ->press('Save Page')
98             // Check redirect and page
99             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
100             ->see($page->name);
101
102         $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
103         return $page;
104     }
105
106     public function chapterCreation(\BookStack\Book $book)
107     {
108         $chapter = factory(\BookStack\Chapter::class)->make([
109             'name' => 'My First Chapter'
110         ]);
111
112         $this->asAdmin()
113             // Navigate to chapter create page
114             ->visit($book->getUrl())
115             ->click('New Chapter')
116             ->seePageIs($book->getUrl() . '/chapter/create')
117             // Fill out form
118             ->type($chapter->name, '#name')
119             ->type($chapter->description, '#description')
120             ->press('Save Chapter')
121             // Check redirect and landing page
122             ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
123             ->see($chapter->name)->see($chapter->description);
124
125         $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
126         return $chapter;
127     }
128
129     public function bookCreation()
130     {
131         $book = factory(\BookStack\Book::class)->make([
132             'name' => 'My First Book'
133         ]);
134         $this->asAdmin()
135             ->visit('/books')
136             // Choose to create a book
137             ->click('Create New Book')
138             ->seePageIs('/books/create')
139             // Fill out form & save
140             ->type($book->name, '#name')
141             ->type($book->description, '#description')
142             ->press('Save Book')
143             // Check it redirects correctly
144             ->seePageIs('/books/my-first-book')
145             ->see($book->name)->see($book->description);
146
147         // Ensure duplicate names are given different slugs
148         $this->asAdmin()
149             ->visit('/books/create')
150             ->type($book->name, '#name')
151             ->type($book->description, '#description')
152             ->press('Save Book');
153         
154         $expectedPattern = '/\/books\/my-first-book-[0-9a-zA-Z]{3}/';
155         $this->assertRegExp($expectedPattern, $this->currentUri, "Did not land on expected page [$expectedPattern].\n");
156
157         $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
158         return $book;
159     }
160
161     public function test_entities_viewable_after_creator_deletion()
162     {
163         // Create required assets and revisions
164         $creator = $this->getEditor();
165         $updater = $this->getEditor();
166         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
167         $this->actingAs($creator);
168         app('BookStack\Repos\UserRepo')->destroy($creator);
169         app('BookStack\Repos\EntityRepo')->savePageRevision($entities['page']);
170
171         $this->checkEntitiesViewable($entities);
172     }
173
174     public function test_entities_viewable_after_updater_deletion()
175     {
176         // Create required assets and revisions
177         $creator = $this->getEditor();
178         $updater = $this->getEditor();
179         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
180         $this->actingAs($updater);
181         app('BookStack\Repos\UserRepo')->destroy($updater);
182         app('BookStack\Repos\EntityRepo')->savePageRevision($entities['page']);
183
184         $this->checkEntitiesViewable($entities);
185     }
186
187     private function checkEntitiesViewable($entities)
188     {
189         // Check pages and books are visible.
190         $this->asAdmin();
191         $this->visit($entities['book']->getUrl())->seeStatusCode(200)
192             ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
193             ->visit($entities['page']->getUrl())->seeStatusCode(200);
194         // Check revision listing shows no errors.
195         $this->visit($entities['page']->getUrl())
196             ->click('Revisions')->seeStatusCode(200);
197     }
198
199     public function test_recently_created_pages_view()
200     {
201         $user = $this->getEditor();
202         $content = $this->createEntityChainBelongingToUser($user);
203
204         $this->asAdmin()->visit('/pages/recently-created')
205             ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
206     }
207
208     public function test_recently_updated_pages_view()
209     {
210         $user = $this->getEditor();
211         $content = $this->createEntityChainBelongingToUser($user);
212
213         $this->asAdmin()->visit('/pages/recently-updated')
214             ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
215     }
216
217     public function test_old_page_slugs_redirect_to_new_pages()
218     {
219         $page = \BookStack\Page::first();
220         $pageUrl = $page->getUrl();
221         $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
222         // Need to save twice since revisions are not generated in seeder.
223         $this->asAdmin()->visit($pageUrl)
224             ->clickInElement('#content', 'Edit')
225             ->type('super test', '#name')
226             ->press('Save Page');
227
228         $page = \BookStack\Page::first();
229         $pageUrl = $page->getUrl();
230
231         // Second Save
232         $this->visit($pageUrl)
233             ->clickInElement('#content', 'Edit')
234             ->type('super test page', '#name')
235             ->press('Save Page')
236             // Check redirect
237             ->seePageIs($newPageUrl);
238
239         $this->visit($pageUrl)
240             ->seePageIs($newPageUrl);
241     }
242
243     public function test_recently_updated_pages_on_home()
244     {
245         $page = \BookStack\Page::orderBy('updated_at', 'asc')->first();
246         $this->asAdmin()->visit('/')
247             ->dontSeeInElement('#recently-updated-pages', $page->name);
248         $this->visit($page->getUrl() . '/edit')
249             ->press('Save Page')
250             ->visit('/')
251             ->seeInElement('#recently-updated-pages', $page->name);
252     }
253
254     public function test_recently_created_pages_on_home()
255     {
256         $entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
257         $this->asAdmin()->visit('/')
258             ->seeInElement('#recently-created-pages', $entityChain['page']->name);
259     }
260
261 }