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