]> BookStack Code Mirror - bookstack/blob - tests/Entity/EntityTest.php
Merge branch 'auth' of git://github.com/benrubson/BookStack into benrubson-auth
[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 Illuminate\Support\Facades\DB;
11 use Tests\BrowserKitTest;
12
13 class EntityTest extends BrowserKitTest
14 {
15
16     public function test_entity_creation()
17     {
18         // Test Creation
19         $book = $this->bookCreation();
20         $chapter = $this->chapterCreation($book);
21         $page = $this->pageCreation($chapter);
22
23         // Test Updating
24         $book = $this->bookUpdate($book);
25
26         // Test Deletion
27         $this->bookDelete($book);
28     }
29
30     public function bookDelete(Book $book)
31     {
32         $this->asAdmin()
33             ->visit($book->getUrl())
34             // Check link works correctly
35             ->click('Delete')
36             ->seePageIs($book->getUrl() . '/delete')
37             // Ensure the book name is show to user
38             ->see($book->name)
39             ->press('Confirm')
40             ->seePageIs('/books')
41             ->notSeeInDatabase('books', ['id' => $book->id]);
42     }
43
44     public function bookUpdate(Book $book)
45     {
46         $newName = $book->name . ' Updated';
47         $this->asAdmin()
48             // Go to edit screen
49             ->visit($book->getUrl() . '/edit')
50             ->see($book->name)
51             // Submit new name
52             ->type($newName, '#name')
53             ->press('Save Book')
54             // Check page url and text
55             ->seePageIs($book->getUrl() . '-updated')
56             ->see($newName);
57
58         return Book::find($book->id);
59     }
60
61     public function test_book_sort_page_shows()
62     {
63         $books =  Book::all();
64         $bookToSort = $books[0];
65         $this->asAdmin()
66             ->visit($bookToSort->getUrl())
67             ->click('Sort')
68             ->seePageIs($bookToSort->getUrl() . '/sort')
69             ->seeStatusCode(200)
70             ->see($bookToSort->name);
71     }
72
73     public function test_book_sort_item_returns_book_content()
74     {
75         $books =  Book::all();
76         $bookToSort = $books[0];
77         $firstPage = $bookToSort->pages[0];
78         $firstChapter = $bookToSort->chapters[0];
79         $this->asAdmin()
80             ->visit($bookToSort->getUrl() . '/sort-item')
81             // Ensure book details are returned
82             ->see($bookToSort->name)
83             ->see($firstPage->name)
84             ->see($firstChapter->name);
85     }
86
87     public function test_toggle_book_view()
88     {
89         $editor = $this->getEditor();
90         setting()->putUser($editor, 'books_view_type', 'grid');
91
92         $this->actingAs($editor)
93             ->visit('/books')
94             ->pageHasElement('.featured-image-container')
95             ->submitForm('List View')
96             // Check redirection.
97             ->seePageIs('/books')
98             ->pageNotHasElement('.featured-image-container');
99
100         $this->actingAs($editor)
101             ->visit('/books')
102             ->submitForm('Grid View')
103             ->seePageIs('/books')
104             ->pageHasElement('.featured-image-container');
105
106     }
107
108     public function pageCreation($chapter)
109     {
110         $page = factory(Page::class)->make([
111             'name' => 'My First Page'
112         ]);
113
114         $this->asAdmin()
115             // Navigate to page create form
116             ->visit($chapter->getUrl())
117             ->click('New Page');
118
119         $draftPage = Page::where('draft', '=', true)->orderBy('created_at', 'desc')->first();
120
121         $this->seePageIs($draftPage->getUrl())
122             // Fill out form
123             ->type($page->name, '#name')
124             ->type($page->html, '#html')
125             ->press('Save Page')
126             // Check redirect and page
127             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
128             ->see($page->name);
129
130         $page = Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
131         return $page;
132     }
133
134     public function chapterCreation(Book $book)
135     {
136         $chapter = factory(Chapter::class)->make([
137             'name' => 'My First Chapter'
138         ]);
139
140         $this->asAdmin()
141             // Navigate to chapter create page
142             ->visit($book->getUrl())
143             ->click('New Chapter')
144             ->seePageIs($book->getUrl() . '/create-chapter')
145             // Fill out form
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);
152
153         $chapter = Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
154         return $chapter;
155     }
156
157     public function bookCreation()
158     {
159         $book = factory(Book::class)->make([
160             'name' => 'My First Book'
161         ]);
162         $this->asAdmin()
163             ->visit('/books')
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')
170             ->press('Save Book')
171             // Check it redirects correctly
172             ->seePageIs('/books/my-first-book')
173             ->see($book->name)->see($book->description);
174
175         // Ensure duplicate names are given different slugs
176         $this->asAdmin()
177             ->visit('/create-book')
178             ->type($book->name, '#name')
179             ->type($book->description, '#description')
180             ->press('Save Book');
181
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");
184
185         $book = Book::where('slug', '=', 'my-first-book')->first();
186         return $book;
187     }
188
189     public function test_entities_viewable_after_creator_deletion()
190     {
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>>']);
198
199         $this->checkEntitiesViewable($entities);
200     }
201
202     public function test_entities_viewable_after_updater_deletion()
203     {
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>']);
211
212         $this->checkEntitiesViewable($entities);
213     }
214
215     private function checkEntitiesViewable($entities)
216     {
217         // Check pages and books are visible.
218         $this->asAdmin();
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);
225     }
226
227     public function test_recently_updated_pages_view()
228     {
229         $user = $this->getEditor();
230         $content = $this->createEntityChainBelongingToUser($user);
231
232         $this->asAdmin()->visit('/pages/recently-updated')
233             ->seeInNthElement('.entity-list .page', 0, $content['page']->name);
234     }
235
236     public function test_old_page_slugs_redirect_to_new_pages()
237     {
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');
246
247         $page = Page::first();
248         $pageUrl = $page->getUrl();
249
250         // Second Save
251         $this->visit($pageUrl)
252             ->clickInElement('#content', 'Edit')
253             ->type('super test page', '#name')
254             ->press('Save Page')
255             // Check redirect
256             ->seePageIs($newPageUrl);
257
258         $this->visit($pageUrl)
259             ->seePageIs($newPageUrl);
260     }
261
262     public function test_recently_updated_pages_on_home()
263     {
264         $page = Page::orderBy('updated_at', 'asc')->first();
265         Page::where('id', '!=', $page->id)->update([
266             'updated_at' => Carbon::now()->subSecond(1)
267         ]);
268         $this->asAdmin()->visit('/')
269             ->dontSeeInElement('#recently-updated-pages', $page->name);
270         $this->visit($page->getUrl() . '/edit')
271             ->press('Save Page')
272             ->visit('/')
273             ->seeInElement('#recently-updated-pages', $page->name);
274     }
275
276     public function test_slug_multi_byte_url_safe()
277     {
278         $book = $this->newBook([
279             'name' => 'информация'
280         ]);
281
282         $this->assertEquals('informatsiya', $book->slug);
283
284         $book = $this->newBook([
285             'name' => '¿Qué?'
286         ]);
287
288         $this->assertEquals('que', $book->slug);
289     }
290
291     public function test_slug_format()
292     {
293         $book = $this->newBook([
294             'name' => 'PartA / PartB / PartC'
295         ]);
296
297         $this->assertEquals('parta-partb-partc', $book->slug);
298     }
299
300     public function test_shelf_cancel_creation_returns_to_correct_place()
301     {
302         $shelf = Bookshelf::first();
303
304         // Cancel button from shelf goes back to shelf
305         $this->asEditor()->visit($shelf->getUrl('/create-book'))
306             ->see('Cancel')
307             ->click('Cancel')
308             ->seePageIs($shelf->getUrl());
309
310         // Cancel button from books goes back to books
311         $this->asEditor()->visit('/create-book')
312             ->see('Cancel')
313             ->click('Cancel')
314             ->seePageIs('/books');
315
316         // Cancel button from book edit goes back to book
317         $book = Book::first();
318
319         $this->asEditor()->visit($book->getUrl('/edit'))
320             ->see('Cancel')
321             ->click('Cancel')
322             ->seePageIs($book->getUrl());
323     }
324
325     public function test_page_within_chapter_deletion_returns_to_chapter()
326     {
327         $chapter = Chapter::query()->first();
328         $page = $chapter->pages()->first();
329
330         $this->asEditor()->visit($page->getUrl('/delete'))
331             ->submitForm('Confirm')
332             ->seePageIs($chapter->getUrl());
333     }
334
335     public function test_page_delete_removes_entity_from_its_activity()
336     {
337         $page = Page::query()->first();
338
339         $this->asEditor()->put($page->getUrl(), [
340             'name' => 'My updated page',
341             'html' => '<p>updated content</p>',
342         ]);
343         $page->refresh();
344
345         $this->seeInDatabase('activities', [
346             'entity_id' => $page->id,
347             'entity_type' => $page->getMorphClass(),
348         ]);
349
350         $resp = $this->delete($page->getUrl());
351         $resp->assertResponseStatus(302);
352
353         $this->dontSeeInDatabase('activities', [
354             'entity_id' => $page->id,
355             'entity_type' => $page->getMorphClass(),
356         ]);
357
358         $this->seeInDatabase('activities', [
359             'extra' => 'My updated page',
360             'entity_id' => 0,
361             'entity_type' => '',
362         ]);
363     }
364
365 }