X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/a81a56706e8be77586631f3619ad84df36c8d84e..refs/pull/945/head:/tests/Permissions/RestrictionsTest.php diff --git a/tests/Permissions/RestrictionsTest.php b/tests/Permissions/RestrictionsTest.php index 0aa1389a6..2bbb1a5fa 100644 --- a/tests/Permissions/RestrictionsTest.php +++ b/tests/Permissions/RestrictionsTest.php @@ -1,57 +1,42 @@ -user = $this->getNewUser(); + $this->user = $this->getEditor(); $this->viewer = $this->getViewer(); - $this->restrictionService = $this->app[\BookStack\Services\RestrictionService::class]; } - protected function getViewer() + protected function setEntityRestrictions(Entity $entity, $actions = [], $roles = []) { - $role = \BookStack\Role::getRole('viewer'); - $viewer = $this->getNewBlankUser(); - $viewer->attachRole($role);; - return $viewer; - } - - /** - * Manually set some restrictions on an entity. - * @param \BookStack\Entity $entity - * @param $actions - */ - protected function setEntityRestrictions(\BookStack\Entity $entity, $actions) - { - $entity->restricted = true; - $entity->restrictions()->delete(); - $role = $this->user->roles->first(); - $viewerRole = $this->viewer->roles->first(); - foreach ($actions as $action) { - $entity->restrictions()->create([ - 'role_id' => $role->id, - 'action' => strtolower($action) - ]); - $entity->restrictions()->create([ - 'role_id' => $viewerRole->id, - 'action' => strtolower($action) - ]); - } - $entity->save(); - $entity->load('restrictions'); - $this->restrictionService->buildEntityPermissionsForEntity($entity); - $entity->load('permissions'); + $roles = [ + $this->user->roles->first(), + $this->viewer->roles->first(), + ]; + parent::setEntityRestrictions($entity, $actions, $roles); } public function test_book_view_restriction() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookPage = $book->pages->first(); $bookChapter = $book->chapters->first(); @@ -65,9 +50,9 @@ class RestrictionsTest extends TestCase $this->forceVisit($bookUrl) ->see('Book not found'); $this->forceVisit($bookPage->getUrl()) - ->see('Book not found'); + ->see('Page not found'); $this->forceVisit($bookChapter->getUrl()) - ->see('Book not found'); + ->see('Chapter not found'); $this->setEntityRestrictions($book, ['view']); @@ -81,7 +66,7 @@ class RestrictionsTest extends TestCase public function test_book_create_restriction() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookUrl = $book->getUrl(); $this->actingAs($this->viewer) @@ -95,21 +80,21 @@ class RestrictionsTest extends TestCase $this->setEntityRestrictions($book, ['view', 'delete', 'update']); - $this->forceVisit($bookUrl . '/chapter/create') + $this->forceVisit($bookUrl . '/create-chapter') ->see('You do not have permission')->seePageIs('/'); - $this->forceVisit($bookUrl . '/page/create') + $this->forceVisit($bookUrl . '/create-page') ->see('You do not have permission')->seePageIs('/'); $this->visit($bookUrl)->dontSeeInElement('.action-buttons', 'New Page') ->dontSeeInElement('.action-buttons', 'New Chapter'); $this->setEntityRestrictions($book, ['view', 'create']); - $this->visit($bookUrl . '/chapter/create') + $this->visit($bookUrl . '/create-chapter') ->type('test chapter', 'name') ->type('test description for chapter', 'description') ->press('Save Chapter') ->seePageIs($bookUrl . '/chapter/test-chapter'); - $this->visit($bookUrl . '/page/create') + $this->visit($bookUrl . '/create-page') ->type('test page', 'name') ->type('test content', 'html') ->press('Save Page') @@ -120,7 +105,7 @@ class RestrictionsTest extends TestCase public function test_book_update_restriction() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookPage = $book->pages->first(); $bookChapter = $book->chapters->first(); @@ -150,7 +135,7 @@ class RestrictionsTest extends TestCase public function test_book_delete_restriction() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookPage = $book->pages->first(); $bookChapter = $book->chapters->first(); @@ -226,6 +211,7 @@ class RestrictionsTest extends TestCase ->type('test content', 'html') ->press('Save Page') ->seePageIs($chapter->book->getUrl() . '/page/test-page'); + $this->visit($chapterUrl)->seeInElement('.action-buttons', 'New Page'); } @@ -341,14 +327,14 @@ class RestrictionsTest extends TestCase public function test_book_restriction_form() { - $book = \BookStack\Book::first(); + $book = Book::first(); $this->asAdmin()->visit($book->getUrl() . '/permissions') ->see('Book Permissions') ->check('restricted') ->check('restrictions[2][view]') ->press('Save Permissions') ->seeInDatabase('books', ['id' => $book->id, 'restricted' => true]) - ->seeInDatabase('restrictions', [ + ->seeInDatabase('entity_permissions', [ 'restrictable_id' => $book->id, 'restrictable_type' => 'BookStack\Book', 'role_id' => '2', @@ -365,7 +351,7 @@ class RestrictionsTest extends TestCase ->check('restrictions[2][update]') ->press('Save Permissions') ->seeInDatabase('chapters', ['id' => $chapter->id, 'restricted' => true]) - ->seeInDatabase('restrictions', [ + ->seeInDatabase('entity_permissions', [ 'restrictable_id' => $chapter->id, 'restrictable_type' => 'BookStack\Chapter', 'role_id' => '2', @@ -382,7 +368,7 @@ class RestrictionsTest extends TestCase ->check('restrictions[2][delete]') ->press('Save Permissions') ->seeInDatabase('pages', ['id' => $page->id, 'restricted' => true]) - ->seeInDatabase('restrictions', [ + ->seeInDatabase('entity_permissions', [ 'restrictable_id' => $page->id, 'restrictable_type' => 'BookStack\Page', 'role_id' => '2', @@ -429,7 +415,7 @@ class RestrictionsTest extends TestCase public function test_book_create_restriction_override() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookUrl = $book->getUrl(); $this->actingAs($this->viewer) @@ -439,21 +425,21 @@ class RestrictionsTest extends TestCase $this->setEntityRestrictions($book, ['view', 'delete', 'update']); - $this->forceVisit($bookUrl . '/chapter/create') + $this->forceVisit($bookUrl . '/create-chapter') ->see('You do not have permission')->seePageIs('/'); - $this->forceVisit($bookUrl . '/page/create') + $this->forceVisit($bookUrl . '/create-page') ->see('You do not have permission')->seePageIs('/'); $this->visit($bookUrl)->dontSeeInElement('.action-buttons', 'New Page') ->dontSeeInElement('.action-buttons', 'New Chapter'); $this->setEntityRestrictions($book, ['view', 'create']); - $this->visit($bookUrl . '/chapter/create') + $this->visit($bookUrl . '/create-chapter') ->type('test chapter', 'name') ->type('test description for chapter', 'description') ->press('Save Chapter') ->seePageIs($bookUrl . '/chapter/test-chapter'); - $this->visit($bookUrl . '/page/create') + $this->visit($bookUrl . '/create-page') ->type('test page', 'name') ->type('test content', 'html') ->press('Save Page') @@ -464,7 +450,7 @@ class RestrictionsTest extends TestCase public function test_book_update_restriction_override() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookPage = $book->pages->first(); $bookChapter = $book->chapters->first(); @@ -494,7 +480,7 @@ class RestrictionsTest extends TestCase public function test_book_delete_restriction_override() { - $book = \BookStack\Book::first(); + $book = Book::first(); $bookPage = $book->pages->first(); $bookChapter = $book->chapters->first(); @@ -522,4 +508,110 @@ class RestrictionsTest extends TestCase ->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)); + } + + public function test_book_sort_view_permission() + { + $firstBook = Book::first(); + $secondBook = Book::find(2); + $thirdBook = Book::find(3); + + $this->setEntityRestrictions($firstBook, ['view', 'update']); + $this->setEntityRestrictions($secondBook, ['view']); + $this->setEntityRestrictions($thirdBook, ['view', 'update']); + + // Test sort page visibility + $this->actingAs($this->user)->visit($secondBook->getUrl() . '/sort') + ->see('You do not have permission') + ->seePageIs('/'); + + // Check sort page on first book + $this->actingAs($this->user)->visit($firstBook->getUrl() . '/sort') + ->see($thirdBook->name) + ->dontSee($secondBook->name); + } + + public function test_book_sort_permission() { + $firstBook = Book::first(); + $secondBook = Book::find(2); + + $this->setEntityRestrictions($firstBook, ['view', 'update']); + $this->setEntityRestrictions($secondBook, ['view']); + + $firstBookChapter = $this->app[EntityRepo::class]->createFromInput('chapter', + ['name' => 'first book chapter'], $firstBook); + $secondBookChapter = $this->app[EntityRepo::class]->createFromInput('chapter', + ['name' => 'second book chapter'], $secondBook); + + // Create request data + $reqData = [ + [ + 'id' => $firstBookChapter->id, + 'sort' => 0, + 'parentChapter' => false, + 'type' => 'chapter', + 'book' => $secondBook->id + ] + ]; + + // Move chapter from first book to a second book + $this->actingAs($this->user)->put($firstBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]) + ->followRedirects() + ->see('You do not have permission') + ->seePageIs('/'); + + $reqData = [ + [ + 'id' => $secondBookChapter->id, + 'sort' => 0, + 'parentChapter' => false, + 'type' => 'chapter', + 'book' => $firstBook->id + ] + ]; + + // Move chapter from second book to first book + $this->actingAs($this->user)->put($firstBook->getUrl() . '/sort', ['sort-tree' => json_encode($reqData)]) + ->followRedirects() + ->see('You do not have permission') + ->seePageIs('/'); + } + + public function test_can_create_page_if_chapter_has_permissions_when_book_not_visible() + { + $book = Book::first(); + $this->setEntityRestrictions($book, []); + $bookChapter = $book->chapters->first(); + $this->setEntityRestrictions($bookChapter, ['view']); + + $this->actingAs($this->user)->visit($bookChapter->getUrl()) + ->dontSee('New Page'); + + $this->setEntityRestrictions($bookChapter, ['view', 'create']); + + $this->actingAs($this->user)->visit($bookChapter->getUrl()) + ->click('New Page') + ->seeStatusCode(200) + ->type('test page', 'name') + ->type('test content', 'html') + ->press('Save Page') + ->seePageIs($book->getUrl('/page/test-page')) + ->seeStatusCode(200); + } }