X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/1ee3e779e4b9b0a92f701a72f21a72c83cb1ce68..refs/pull/2393/head:/tests/SharedTestHelpers.php diff --git a/tests/SharedTestHelpers.php b/tests/SharedTestHelpers.php index eb3f7b4bb..ffcc6f40c 100644 --- a/tests/SharedTestHelpers.php +++ b/tests/SharedTestHelpers.php @@ -1,16 +1,28 @@ users()->first(); - if (!empty($attributes)) $user->forceFill($attributes)->save(); + $user = Role::getRole('viewer')->users()->first(); + if (!empty($attributes)) { + $user->forceFill($attributes)->save(); + } return $user; } /** * Regenerate the permission for an entity. * @param Entity $entity - * @throws \Throwable + * @throws Throwable */ protected function regenEntityPermissions(Entity $entity) { - app(PermissionService::class)->buildJointPermissionsForEntity($entity); + $entity->rebuildPermissions(); $entity->load('jointPermissions'); } /** * Create and return a new bookshelf. * @param array $input - * @return \BookStack\Entities\Bookshelf + * @return Bookshelf */ public function newShelf($input = ['name' => 'test shelf', 'description' => 'My new test shelf']) { - return app(EntityRepo::class)->createFromInput('bookshelf', $input, false); + return app(BookshelfRepo::class)->create($input, []); } /** @@ -99,30 +111,30 @@ trait SharedTestHelpers * @return Book */ public function newBook($input = ['name' => 'test book', 'description' => 'My new test book']) { - return app(EntityRepo::class)->createFromInput('book', $input, false); + return app(BookRepo::class)->create($input); } /** * Create and return a new test chapter * @param array $input * @param Book $book - * @return \BookStack\Entities\Chapter + * @return Chapter */ public function newChapter($input = ['name' => 'test chapter', 'description' => 'My new test chapter'], Book $book) { - return app(EntityRepo::class)->createFromInput('chapter', $input, $book); + return app(ChapterRepo::class)->create($input, $book); } /** * Create and return a new test page * @param array $input * @return Page - * @throws \Throwable + * @throws Throwable */ public function newPage($input = ['name' => 'test page', 'html' => 'My new test page']) { $book = Book::first(); $pageRepo = app(PageRepo::class); - $draftPage = $pageRepo->getDraftPage($book); - return $pageRepo->publishPageDraft($draftPage, $input); + $draftPage = $pageRepo->getNewDraftPage($book); + return $pageRepo->publishDraft($draftPage, $input); } /** @@ -167,10 +179,10 @@ trait SharedTestHelpers /** * Give the given user some permissions. - * @param \BookStack\Auth\User $user + * @param User $user * @param array $permissions */ - protected function giveUserPermissions(\BookStack\Auth\User $user, $permissions = []) + protected function giveUserPermissions(User $user, $permissions = []) { $newRole = $this->createNewRole($permissions); $user->attachRole($newRole); @@ -198,7 +210,7 @@ trait SharedTestHelpers */ protected function mockHttpFetch($returnData, int $times = 1) { - $mockHttp = \Mockery::mock(HttpFetcher::class); + $mockHttp = Mockery::mock(HttpFetcher::class); $this->app[HttpFetcher::class] = $mockHttp; $mockHttp->shouldReceive('fetch') ->times($times) @@ -215,13 +227,11 @@ trait SharedTestHelpers protected function runWithEnv(string $name, $value, callable $callback) { Env::disablePutenv(); - $originalVal = $_ENV[$name] ?? null; + $originalVal = $_SERVER[$name] ?? null; if (is_null($value)) { - unset($_ENV[$name]); unset($_SERVER[$name]); } else { - $_ENV[$name] = $value; $_SERVER[$name] = $value; } @@ -230,10 +240,8 @@ trait SharedTestHelpers if (is_null($originalVal)) { unset($_SERVER[$name]); - unset($_ENV[$name]); } else { $_SERVER[$name] = $originalVal; - $_ENV[$name] = $originalVal; } } @@ -259,4 +267,48 @@ trait SharedTestHelpers self::assertThat($passed, self::isTrue(), "Failed asserting that given map:\n\n{$toCheckStr}\n\nincludes:\n\n{$toIncludeStr}"); } + /** + * Assert a permission error has occurred. + */ + protected function assertPermissionError($response) + { + PHPUnit::assertTrue($this->isPermissionError($response->baseResponse ?? $response->response), "Failed asserting the response contains a permission error."); + } + + /** + * Assert a permission error has occurred. + */ + protected function assertNotPermissionError($response) + { + PHPUnit::assertFalse($this->isPermissionError($response->baseResponse ?? $response->response), "Failed asserting the response does not contain a permission error."); + } + + /** + * Check if the given response is a permission error. + */ + private function isPermissionError($response): bool + { + return $response->status() === 302 + && $response->headers->get('Location') === url('/') + && strpos(session()->pull('error', ''), 'You do not have permission to access') === 0; + } + + /** + * Set a test handler as the logging interface for the application. + * Allows capture of logs for checking against during tests. + */ + protected function withTestLogger(): TestHandler + { + $monolog = new Logger('testing'); + $testHandler = new TestHandler(); + $monolog->pushHandler($testHandler); + + Log::extend('testing', function() use ($monolog) { + return $monolog; + }); + Log::setDefaultDriver('testing'); + + return $testHandler; + } + } \ No newline at end of file