-<?php
+<?php namespace Tests;
+
+use BookStack\Book;
+use BookStack\Services\PermissionService;
+use BookStack\User;
class RestrictionsTest extends BrowserKitTest
{
+
+ /**
+ * @var User
+ */
protected $user;
+
+ /**
+ * @var User
+ */
protected $viewer;
- protected $restrictionService;
+
+ /**
+ * @var PermissionService
+ */
+ protected $permissionService;
public function setUp()
{
parent::setUp();
$this->user = $this->getEditor();
$this->viewer = $this->getViewer();
- $this->restrictionService = $this->app[\BookStack\Services\PermissionService::class];
- }
-
- protected function getViewer()
- {
- $role = \BookStack\Role::getRole('viewer');
- $viewer = $this->getNewBlankUser();
- $viewer->attachRole($role);;
- return $viewer;
+ $this->permissionService = $this->app[PermissionService::class];
}
/**
{
$entity->restricted = true;
$entity->permissions()->delete();
+
$role = $this->user->roles->first();
$viewerRole = $this->viewer->roles->first();
+
+ $permissions = [];
foreach ($actions as $action) {
- $entity->permissions()->create([
+ $permissions[] = [
'role_id' => $role->id,
'action' => strtolower($action)
- ]);
- $entity->permissions()->create([
+ ];
+ $permissions[] = [
'role_id' => $viewerRole->id,
'action' => strtolower($action)
- ]);
+ ];
}
+ $entity->permissions()->createMany($permissions);
+
$entity->save();
$entity->load('permissions');
- $this->restrictionService->buildJointPermissionsForEntity($entity);
+ $this->permissionService->buildJointPermissionsForEntity($entity);
$entity->load('jointPermissions');
}
public function test_book_view_restriction()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
public function test_book_create_restriction()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookUrl = $book->getUrl();
$this->actingAs($this->viewer)
public function test_book_update_restriction()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
public function test_book_delete_restriction()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
->type('test content', 'html')
->press('Save Page')
->seePageIs($chapter->book->getUrl() . '/page/test-page');
+
$this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page');
}
public function test_book_restriction_form()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$this->asAdmin()->visit($book->getUrl() . '/permissions')
->see('Book Permissions')
->check('restricted')
public function test_book_create_restriction_override()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookUrl = $book->getUrl();
$this->actingAs($this->viewer)
public function test_book_update_restriction_override()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
public function test_book_delete_restriction_override()
{
- $book = \BookStack\Book::first();
+ $book = Book::first();
$bookPage = $book->pages->first();
$bookChapter = $book->chapters->first();
->see('Delete Chapter');
}
+ public function test_page_visible_if_has_permissions_when_book_not_visible()
+ {
+ $book = Book::first();
+
+ $this->setEntityRestrictions($book, []);
+
+ $bookChapter = $book->chapters->first();
+ $bookPage = $bookChapter->pages->first();
+ $this->setEntityRestrictions($bookPage, ['view']);
+
+ $this->actingAs($this->viewer);
+ $this->get($bookPage->getUrl());
+ $this->assertResponseOk();
+ $this->see($bookPage->name);
+ $this->dontSee(substr($book->name, 0, 15));
+ $this->dontSee(substr($bookChapter->name, 0, 15));
+ }
+
}