]> BookStack Code Mirror - bookstack/blob - tests/EntityTest.php
Updated user interfaces for LDAP and added email from LDAP
[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     public function testInvalidPageSearch()
174     {
175         $this->asAdmin()
176             ->visit('/')
177             ->type('<p>test</p>', 'term')
178             ->press('header-search-box-button')
179             ->see('Search Results')
180             ->seeStatusCode(200);
181     }
182
183     public function testEmptySearchRedirectsBack()
184     {
185         $this->asAdmin()
186             ->visit('/')
187             ->visit('/search/all')
188             ->seePageIs('/');
189     }
190
191     public function testBookSearch()
192     {
193         $book = \BookStack\Book::all()->first();
194         $page = $book->pages->last();
195         $chapter = $book->chapters->last();
196
197         $this->asAdmin()
198             ->visit('/search/book/' . $book->id . '?term=' . urlencode($page->name))
199             ->see($page->name)
200
201             ->visit('/search/book/' . $book->id  . '?term=' . urlencode($chapter->name))
202             ->see($chapter->name);
203     }
204
205     public function testEmptyBookSearchRedirectsBack()
206     {
207         $book = \BookStack\Book::all()->first();
208         $this->asAdmin()
209             ->visit('/books')
210             ->visit('/search/book/' . $book->id . '?term=')
211             ->seePageIs('/books');
212     }
213
214
215     public function testEntitiesViewableAfterCreatorDeletion()
216     {
217         // Create required assets and revisions
218         $creator = $this->getNewUser();
219         $updater = $this->getNewUser();
220         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
221         $this->actingAs($creator);
222         app('BookStack\Repos\UserRepo')->destroy($creator);
223         app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
224
225         $this->checkEntitiesViewable($entities);
226     }
227
228     public function testEntitiesViewableAfterUpdaterDeletion()
229     {
230         // Create required assets and revisions
231         $creator = $this->getNewUser();
232         $updater = $this->getNewUser();
233         $entities = $this->createEntityChainBelongingToUser($creator, $updater);
234         $this->actingAs($updater);
235         app('BookStack\Repos\UserRepo')->destroy($updater);
236         app('BookStack\Repos\PageRepo')->saveRevision($entities['page']);
237
238         $this->checkEntitiesViewable($entities);
239     }
240
241     private function checkEntitiesViewable($entities)
242     {
243         // Check pages and books are visible.
244         $this->asAdmin();
245         $this->visit($entities['book']->getUrl())->seeStatusCode(200)
246             ->visit($entities['chapter']->getUrl())->seeStatusCode(200)
247             ->visit($entities['page']->getUrl())->seeStatusCode(200);
248         // Check revision listing shows no errors.
249         $this->visit($entities['page']->getUrl())
250             ->click('Revisions')->seeStatusCode(200);
251     }
252
253
254 }