]> BookStack Code Mirror - bookstack/blob - tests/Entity/EntityTest.php
Added issue template
[bookstack] / tests / Entity / EntityTest.php
1 <?php
2
3 use Illuminate\Support\Facades\DB;
4
5 class EntityTest extends TestCase
6 {
7
8     public function test_entity_creation()
9     {
10
11         // Test Creation
12         $book = $this->bookCreation();
13         $chapter = $this->chapterCreation($book);
14         $page = $this->pageCreation($chapter);
15
16         // Test Updating
17         $book = $this->bookUpdate($book);
18
19         // Test Deletion
20         $this->bookDelete($book);
21     }
22
23     public function bookDelete(\BookStack\Book $book)
24     {
25         $this->asAdmin()
26             ->visit($book->getUrl())
27             // Check link works correctly
28             ->click('Delete')
29             ->seePageIs($book->getUrl() . '/delete')
30             // Ensure the book name is show to user
31             ->see($book->name)
32             ->press('Confirm')
33             ->seePageIs('/books')
34             ->notSeeInDatabase('books', ['id' => $book->id]);
35     }
36
37     public function bookUpdate(\BookStack\Book $book)
38     {
39         $newName = $book->name . ' Updated';
40         $this->asAdmin()
41             // Go to edit screen
42             ->visit($book->getUrl() . '/edit')
43             ->see($book->name)
44             // Submit new name
45             ->type($newName, '#name')
46             ->press('Save Book')
47             // Check page url and text
48             ->seePageIs($book->getUrl() . '-updated')
49             ->see($newName);
50
51         return \BookStack\Book::find($book->id);
52     }
53
54     public function test_book_sort_page_shows()
55     {
56         $books =  \BookStack\Book::all();
57         $bookToSort = $books[0];
58         $this->asAdmin()
59             ->visit($bookToSort->getUrl())
60             ->click('Sort')
61             ->seePageIs($bookToSort->getUrl() . '/sort')
62             ->seeStatusCode(200)
63             ->see($bookToSort->name)
64             // Ensure page shows other books
65             ->see($books[1]->name);
66     }
67
68     public function test_book_sort_item_returns_book_content()
69     {
70         $books =  \BookStack\Book::all();
71         $bookToSort = $books[0];
72         $firstPage = $bookToSort->pages[0];
73         $firstChapter = $bookToSort->chapters[0];
74         $this->asAdmin()
75             ->visit($bookToSort->getUrl() . '/sort-item')
76             // Ensure book details are returned
77             ->see($bookToSort->name)
78             ->see($firstPage->name)
79             ->see($firstChapter->name);
80     }
81
82     public function pageCreation($chapter)
83     {
84         $page = factory(\BookStack\Page::class)->make([
85             'name' => 'My First Page'
86         ]);
87
88         $this->asAdmin()
89             // Navigate to page create form
90             ->visit($chapter->getUrl())
91             ->click('New Page');
92
93         $draftPage = \BookStack\Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
94
95         $this->seePageIs($draftPage->getUrl())
96             // Fill out form
97             ->type($page->name, '#name')
98             ->type($page->html, '#html')
99             ->press('Save Page')
100             // Check redirect and page
101             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
102             ->see($page->name);
103
104         $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
105         return $page;
106     }
107
108     public function chapterCreation(\BookStack\Book $book)
109     {
110         $chapter = factory(\BookStack\Chapter::class)->make([
111             'name' => 'My First Chapter'
112         ]);
113
114         $this->asAdmin()
115             // Navigate to chapter create page
116             ->visit($book->getUrl())
117             ->click('New Chapter')
118             ->seePageIs($book->getUrl() . '/chapter/create')
119             // Fill out form
120             ->type($chapter->name, '#name')
121             ->type($chapter->description, '#description')
122             ->press('Save Chapter')
123             // Check redirect and landing page
124             ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
125             ->see($chapter->name)->see($chapter->description);
126
127         $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
128         return $chapter;
129     }
130
131     public function bookCreation()
132     {
133         $book = factory(\BookStack\Book::class)->make([
134             'name' => 'My First Book'
135         ]);
136         $this->asAdmin()
137             ->visit('/books')
138             // Choose to create a book
139             ->click('Add new book')
140             ->seePageIs('/books/create')
141             // Fill out form & save
142             ->type($book->name, '#name')
143             ->type($book->description, '#description')
144             ->press('Save Book')
145             // Check it redirects correctly
146             ->seePageIs('/books/my-first-book')
147             ->see($book->name)->see($book->description);
148
149         // Ensure duplicate names are given different slugs
150         $this->asAdmin()
151             ->visit('/books/create')
152             ->type($book->name, '#name')
153             ->type($book->description, '#description')
154             ->press('Save Book')
155             ->seePageIs('/books/my-first-book-2');
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\PageRepo')->saveRevision($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\PageRepo')->saveRevision($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::all()->first();
220         $pageUrl = $page->getUrl();
221         $newPageUrl = '/books/' . $page->book->slug . '/page/super-test-page';
222         $this->asAdmin()->visit($pageUrl)
223             ->clickInElement('#content', 'Edit')
224             ->type('super test page', '#name')
225             ->press('Save Page')
226             ->seePageIs($newPageUrl)
227             ->visit($pageUrl)
228             ->seePageIs($newPageUrl);
229     }
230
231     public function test_recently_updated_pages_on_home()
232     {
233         $page = \BookStack\Page::orderBy('updated_at', 'asc')->first();
234         $this->asAdmin()->visit('/')
235             ->dontSeeInElement('#recently-updated-pages', $page->name);
236         $this->visit($page->getUrl() . '/edit')
237             ->press('Save Page')
238             ->visit('/')
239             ->seeInElement('#recently-updated-pages', $page->name);
240     }
241
242     public function test_recently_created_pages_on_home()
243     {
244         $entityChain = $this->createEntityChainBelongingToUser($this->getEditor());
245         $this->asAdmin()->visit('/')
246             ->seeInElement('#recently-created-pages', $entityChain['page']->name);
247     }
248
249 }