3 class RestrictionsTest extends TestCase
7 public function setUp()
10 $this->user = $this->getNewUser();
14 * Manually set some restrictions on an entity.
15 * @param \BookStack\Entity $entity
18 protected function setEntityRestrictions(\BookStack\Entity $entity, $actions)
20 $entity->restricted = true;
21 $entity->restrictions()->delete();
22 $role = $this->user->roles->first();
23 foreach ($actions as $action) {
24 $entity->restrictions()->create([
25 'role_id' => $role->id,
26 'action' => strtolower($action)
30 $entity->load('restrictions');
33 public function test_book_view_restriction()
35 $book = \BookStack\Book::first();
36 $bookPage = $book->pages->first();
37 $bookChapter = $book->chapters->first();
39 $bookUrl = $book->getUrl();
40 $this->actingAs($this->user)
42 ->seePageIs($bookUrl);
44 $this->setEntityRestrictions($book, []);
46 $this->forceVisit($bookUrl)
47 ->see('Book not found');
48 $this->forceVisit($bookPage->getUrl())
49 ->see('Book not found');
50 $this->forceVisit($bookChapter->getUrl())
51 ->see('Book not found');
53 $this->setEntityRestrictions($book, ['view']);
55 $this->visit($bookUrl)
57 $this->visit($bookPage->getUrl())
58 ->see($bookPage->name);
59 $this->visit($bookChapter->getUrl())
60 ->see($bookChapter->name);
63 public function test_book_create_restriction()
65 $book = \BookStack\Book::first();
67 $bookUrl = $book->getUrl();
68 $this->actingAs($this->user)
70 ->seeInElement('.action-buttons', 'New Page')
71 ->seeInElement('.action-buttons', 'New Chapter');
73 $this->setEntityRestrictions($book, ['view', 'delete', 'update']);
75 $this->forceVisit($bookUrl . '/chapter/create')
76 ->see('You do not have permission')->seePageIs('/');
77 $this->forceVisit($bookUrl . '/page/create')
78 ->see('You do not have permission')->seePageIs('/');
79 $this->visit($bookUrl)->dontSeeInElement('.action-buttons', 'New Page')
80 ->dontSeeInElement('.action-buttons', 'New Chapter');
82 $this->setEntityRestrictions($book, ['view', 'create']);
84 $this->visit($bookUrl . '/chapter/create')
85 ->type('test chapter', 'name')
86 ->type('test description for chapter', 'description')
87 ->press('Save Chapter')
88 ->seePageIs($bookUrl . '/chapter/test-chapter');
89 $this->visit($bookUrl . '/page/create')
90 ->type('test page', 'name')
91 ->type('test content', 'html')
93 ->seePageIs($bookUrl . '/page/test-page');
94 $this->visit($bookUrl)->seeInElement('.action-buttons', 'New Page')
95 ->seeInElement('.action-buttons', 'New Chapter');
98 public function test_book_update_restriction()
100 $book = \BookStack\Book::first();
101 $bookPage = $book->pages->first();
102 $bookChapter = $book->chapters->first();
104 $bookUrl = $book->getUrl();
105 $this->actingAs($this->user)
106 ->visit($bookUrl . '/edit')
109 $this->setEntityRestrictions($book, ['view', 'delete']);
111 $this->forceVisit($bookUrl . '/edit')
112 ->see('You do not have permission')->seePageIs('/');
113 $this->forceVisit($bookPage->getUrl() . '/edit')
114 ->see('You do not have permission')->seePageIs('/');
115 $this->forceVisit($bookChapter->getUrl() . '/edit')
116 ->see('You do not have permission')->seePageIs('/');
118 $this->setEntityRestrictions($book, ['view', 'update']);
120 $this->visit($bookUrl . '/edit')
121 ->seePageIs($bookUrl . '/edit');
122 $this->visit($bookPage->getUrl() . '/edit')
123 ->seePageIs($bookPage->getUrl() . '/edit');
124 $this->visit($bookChapter->getUrl() . '/edit')
125 ->see('Edit Chapter');
128 public function test_book_delete_restriction()
130 $book = \BookStack\Book::first();
131 $bookPage = $book->pages->first();
132 $bookChapter = $book->chapters->first();
134 $bookUrl = $book->getUrl();
135 $this->actingAs($this->user)
136 ->visit($bookUrl . '/delete')
137 ->see('Delete Book');
139 $this->setEntityRestrictions($book, ['view', 'update']);
141 $this->forceVisit($bookUrl . '/delete')
142 ->see('You do not have permission')->seePageIs('/');
143 $this->forceVisit($bookPage->getUrl() . '/delete')
144 ->see('You do not have permission')->seePageIs('/');
145 $this->forceVisit($bookChapter->getUrl() . '/delete')
146 ->see('You do not have permission')->seePageIs('/');
148 $this->setEntityRestrictions($book, ['view', 'delete']);
150 $this->visit($bookUrl . '/delete')
151 ->seePageIs($bookUrl . '/delete')->see('Delete Book');
152 $this->visit($bookPage->getUrl() . '/delete')
153 ->seePageIs($bookPage->getUrl() . '/delete')->see('Delete Page');
154 $this->visit($bookChapter->getUrl() . '/delete')
155 ->see('Delete Chapter');
158 public function test_chapter_view_restriction()
160 $chapter = \BookStack\Chapter::first();
161 $chapterPage = $chapter->pages->first();
163 $chapterUrl = $chapter->getUrl();
164 $this->actingAs($this->user)
166 ->seePageIs($chapterUrl);
168 $this->setEntityRestrictions($chapter, []);
170 $this->forceVisit($chapterUrl)
171 ->see('Chapter not found');
172 $this->forceVisit($chapterPage->getUrl())
173 ->see('Page not found');
175 $this->setEntityRestrictions($chapter, ['view']);
177 $this->visit($chapterUrl)
178 ->see($chapter->name);
179 $this->visit($chapterPage->getUrl())
180 ->see($chapterPage->name);
183 public function test_chapter_create_restriction()
185 $chapter = \BookStack\Chapter::first();
187 $chapterUrl = $chapter->getUrl();
188 $this->actingAs($this->user)
190 ->seeInElement('.action-buttons', 'New Page');
192 $this->setEntityRestrictions($chapter, ['view', 'delete', 'update']);
194 $this->forceVisit($chapterUrl . '/create-page')
195 ->see('You do not have permission')->seePageIs('/');
196 $this->visit($chapterUrl)->dontSeeInElement('.action-buttons', 'New Page');
198 $this->setEntityRestrictions($chapter, ['view', 'create']);
201 $this->visit($chapterUrl . '/create-page')
202 ->type('test page', 'name')
203 ->type('test content', 'html')
205 ->seePageIs($chapter->book->getUrl() . '/page/test-page');
206 $this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page');
209 public function test_chapter_update_restriction()
211 $chapter = \BookStack\Chapter::first();
212 $chapterPage = $chapter->pages->first();
214 $chapterUrl = $chapter->getUrl();
215 $this->actingAs($this->user)
216 ->visit($chapterUrl . '/edit')
217 ->see('Edit Chapter');
219 $this->setEntityRestrictions($chapter, ['view', 'delete']);
221 $this->forceVisit($chapterUrl . '/edit')
222 ->see('You do not have permission')->seePageIs('/');
223 $this->forceVisit($chapterPage->getUrl() . '/edit')
224 ->see('You do not have permission')->seePageIs('/');
226 $this->setEntityRestrictions($chapter, ['view', 'update']);
228 $this->visit($chapterUrl . '/edit')
229 ->seePageIs($chapterUrl . '/edit')->see('Edit Chapter');
230 $this->visit($chapterPage->getUrl() . '/edit')
231 ->seePageIs($chapterPage->getUrl() . '/edit');
234 public function test_chapter_delete_restriction()
236 $chapter = \BookStack\Chapter::first();
237 $chapterPage = $chapter->pages->first();
239 $chapterUrl = $chapter->getUrl();
240 $this->actingAs($this->user)
241 ->visit($chapterUrl . '/delete')
242 ->see('Delete Chapter');
244 $this->setEntityRestrictions($chapter, ['view', 'update']);
246 $this->forceVisit($chapterUrl . '/delete')
247 ->see('You do not have permission')->seePageIs('/');
248 $this->forceVisit($chapterPage->getUrl() . '/delete')
249 ->see('You do not have permission')->seePageIs('/');
251 $this->setEntityRestrictions($chapter, ['view', 'delete']);
253 $this->visit($chapterUrl . '/delete')
254 ->seePageIs($chapterUrl . '/delete')->see('Delete Chapter');
255 $this->visit($chapterPage->getUrl() . '/delete')
256 ->seePageIs($chapterPage->getUrl() . '/delete')->see('Delete Page');
259 public function test_page_view_restriction()
261 $page = \BookStack\Page::first();
263 $pageUrl = $page->getUrl();
264 $this->actingAs($this->user)
266 ->seePageIs($pageUrl);
268 $this->setEntityRestrictions($page, ['update', 'delete']);
270 $this->forceVisit($pageUrl)
271 ->see('Page not found');
273 $this->setEntityRestrictions($page, ['view']);
275 $this->visit($pageUrl)
279 public function test_page_update_restriction()
281 $page = \BookStack\Chapter::first();
283 $pageUrl = $page->getUrl();
284 $this->actingAs($this->user)
285 ->visit($pageUrl . '/edit')
286 ->seeInField('name', $page->name);
288 $this->setEntityRestrictions($page, ['view', 'delete']);
290 $this->forceVisit($pageUrl . '/edit')
291 ->see('You do not have permission')->seePageIs('/');
293 $this->setEntityRestrictions($page, ['view', 'update']);
295 $this->visit($pageUrl . '/edit')
296 ->seePageIs($pageUrl . '/edit')->seeInField('name', $page->name);
299 public function test_page_delete_restriction()
301 $page = \BookStack\Page::first();
303 $pageUrl = $page->getUrl();
304 $this->actingAs($this->user)
305 ->visit($pageUrl . '/delete')
306 ->see('Delete Page');
308 $this->setEntityRestrictions($page, ['view', 'update']);
310 $this->forceVisit($pageUrl . '/delete')
311 ->see('You do not have permission')->seePageIs('/');
313 $this->setEntityRestrictions($page, ['view', 'delete']);
315 $this->visit($pageUrl . '/delete')
316 ->seePageIs($pageUrl . '/delete')->see('Delete Page');
319 public function test_book_restriction_form()
321 $book = \BookStack\Book::first();
322 $this->asAdmin()->visit($book->getUrl() . '/restrict')
323 ->see('Book Restrictions')
324 ->check('restricted')
325 ->check('restrictions[2][view]')
326 ->press('Save Restrictions')
327 ->seeInDatabase('books', ['id' => $book->id, 'restricted' => true])
328 ->seeInDatabase('restrictions', [
329 'restrictable_id' => $book->id,
330 'restrictable_type' => 'BookStack\Book',
336 public function test_chapter_restriction_form()
338 $chapter = \BookStack\Chapter::first();
339 $this->asAdmin()->visit($chapter->getUrl() . '/restrict')
340 ->see('Chapter Restrictions')
341 ->check('restricted')
342 ->check('restrictions[2][update]')
343 ->press('Save Restrictions')
344 ->seeInDatabase('chapters', ['id' => $chapter->id, 'restricted' => true])
345 ->seeInDatabase('restrictions', [
346 'restrictable_id' => $chapter->id,
347 'restrictable_type' => 'BookStack\Chapter',
353 public function test_page_restriction_form()
355 $page = \BookStack\Page::first();
356 $this->asAdmin()->visit($page->getUrl() . '/restrict')
357 ->see('Page Restrictions')
358 ->check('restricted')
359 ->check('restrictions[2][delete]')
360 ->press('Save Restrictions')
361 ->seeInDatabase('pages', ['id' => $page->id, 'restricted' => true])
362 ->seeInDatabase('restrictions', [
363 'restrictable_id' => $page->id,
364 'restrictable_type' => 'BookStack\Page',
370 public function test_restricted_pages_not_visible_in_book_navigation_on_pages()
372 $chapter = \BookStack\Chapter::first();
373 $page = $chapter->pages->first();
374 $page2 = $chapter->pages[2];
376 $this->setEntityRestrictions($page, []);
378 $this->actingAs($this->user)
379 ->visit($page2->getUrl())
380 ->dontSeeInElement('.sidebar-page-list', $page->name);
383 public function test_restricted_pages_not_visible_in_book_navigation_on_chapters()
385 $chapter = \BookStack\Chapter::first();
386 $page = $chapter->pages->first();
388 $this->setEntityRestrictions($page, []);
390 $this->actingAs($this->user)
391 ->visit($chapter->getUrl())
392 ->dontSeeInElement('.sidebar-page-list', $page->name);
395 public function test_restricted_pages_not_visible_on_chapter_pages()
397 $chapter = \BookStack\Chapter::first();
398 $page = $chapter->pages->first();
400 $this->setEntityRestrictions($page, []);
402 $this->actingAs($this->user)
403 ->visit($chapter->getUrl())
404 ->dontSee($page->name);