]> BookStack Code Mirror - bookstack/blob - tests/EntityTest.php
07553e7dc26268825962ce8206add64218d3dee3
[bookstack] / tests / EntityTest.php
1 <?php
2
3 use Illuminate\Support\Facades\DB;
4
5 class EntityTest extends TestCase
6 {
7
8     public function testEntityCreation()
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 testBookSortPageShows()
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 testBookSortItemReturnsBookContent()
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             ->seePageIs($chapter->getUrl() . '/create-page')
93             // Fill out form
94             ->type($page->name, '#name')
95             ->type($page->html, '#html')
96             ->press('Save Page')
97             // Check redirect and page
98             ->seePageIs($chapter->book->getUrl() . '/page/my-first-page')
99             ->see($page->name);
100
101         $page = \BookStack\Page::where('slug', '=', 'my-first-page')->where('chapter_id', '=', $chapter->id)->first();
102         return $page;
103     }
104
105     public function chapterCreation(\BookStack\Book $book)
106     {
107         $chapter = factory(\BookStack\Chapter::class)->make([
108             'name' => 'My First Chapter'
109         ]);
110
111         $this->asAdmin()
112             // Navigate to chapter create page
113             ->visit($book->getUrl())
114             ->click('New Chapter')
115             ->seePageIs($book->getUrl() . '/chapter/create')
116             // Fill out form
117             ->type($chapter->name, '#name')
118             ->type($chapter->description, '#description')
119             ->press('Save Chapter')
120             // Check redirect and landing page
121             ->seePageIs($book->getUrl() . '/chapter/my-first-chapter')
122             ->see($chapter->name)->see($chapter->description);
123
124         $chapter = \BookStack\Chapter::where('slug', '=', 'my-first-chapter')->where('book_id', '=', $book->id)->first();
125         return $chapter;
126     }
127
128     public function bookCreation()
129     {
130         $book = factory(\BookStack\Book::class)->make([
131             'name' => 'My First Book'
132         ]);
133         $this->asAdmin()
134             ->visit('/books')
135             // Choose to create a book
136             ->click('Add new book')
137             ->seePageIs('/books/create')
138             // Fill out form & save
139             ->type($book->name, '#name')
140             ->type($book->description, '#description')
141             ->press('Save Book')
142             // Check it redirects correctly
143             ->seePageIs('/books/my-first-book')
144             ->see($book->name)->see($book->description);
145
146         // Ensure duplicate names are given different slugs
147         $this->asAdmin()
148             ->visit('/books/create')
149             ->type($book->name, '#name')
150             ->type($book->description, '#description')
151             ->press('Save Book')
152             ->seePageIs('/books/my-first-book-2');
153
154         $book = \BookStack\Book::where('slug', '=', 'my-first-book')->first();
155         return $book;
156     }
157
158     public function testPageSearch()
159     {
160         $book = \BookStack\Book::all()->first();
161         $page = $book->pages->first();
162
163         $this->asAdmin()
164             ->visit('/')
165             ->type($page->name, 'term')
166             ->press('header-search-box-button')
167             ->see('Search Results')
168             ->see($page->name)
169             ->click($page->name)
170             ->seePageIs($page->getUrl());
171     }
172
173
174     public function testEntitiesViewableAfterCreatorDeletion()
175     {
176         // Create required assets and revisions
177         $creator = $this->getNewUser();
178         $updater = $this->getNewUser();
179         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
180         $this->actingAs($creator);
181         app('BookStack\Repos\UserRepo')->destroy($creator);
182         app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
183
184         $this->checkEntitiesViewable($entities);
185     }
186
187     public function testEntitiesViewableAfterUpdaterDeletion()
188     {
189         // Create required assets and revisions
190         $creator = $this->getNewUser();
191         $updater = $this->getNewUser();
192         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
193         $this->actingAs($updater);
194         app('BookStack\Repos\UserRepo')->destroy($updater);
195         app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
196
197         $this->checkEntitiesViewable($entities);
198     }
199
200     private function checkEntitiesViewable($entities)
201     {
202         // Check pages and books are visible.
203         $this->asAdmin();
204         $this->visit($entities['book']->getUrl())->seeStatusCode(200)
205             ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
206             ->visit($entities['page']->getUrl())->seeStatusCode(200);
207         // Check revision listing shows no errors.
208         $this->visit($entities['page']->getUrl())
209             ->click('Revisions')->seeStatusCode(200);
210     }
211
212
213 }