X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/da1a66abd384911085d595e4ef06d36430a38a27..refs/pull/5280/head:/tests/TestCase.php diff --git a/tests/TestCase.php b/tests/TestCase.php index abee1d3b3..b63de3076 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -3,12 +3,10 @@ namespace Tests; use BookStack\Entities\Models\Entity; +use BookStack\Http\HttpClientHistory; +use BookStack\Http\HttpRequestService; use BookStack\Settings\SettingService; -use BookStack\Uploads\HttpFetcher; -use GuzzleHttp\Client; -use GuzzleHttp\Handler\MockHandler; -use GuzzleHttp\HandlerStack; -use GuzzleHttp\Middleware; +use BookStack\Users\Models\User; use Illuminate\Contracts\Console\Kernel; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Foundation\Testing\TestCase as BaseTestCase; @@ -17,10 +15,8 @@ use Illuminate\Support\Env; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Testing\Assert as PHPUnit; -use Mockery; use Monolog\Handler\TestHandler; use Monolog\Logger; -use Psr\Http\Client\ClientInterface; use Ssddanbrown\AssertHtml\TestsHtml; use Tests\Helpers\EntityProvider; use Tests\Helpers\FileProvider; @@ -109,33 +105,11 @@ abstract class TestCase extends BaseTestCase } /** - * Mock the HttpFetcher service and return the given data on fetch. + * Mock the http client used in BookStack http calls. */ - protected function mockHttpFetch($returnData, int $times = 1) + protected function mockHttpClient(array $responses = []): HttpClientHistory { - $mockHttp = Mockery::mock(HttpFetcher::class); - $this->app[HttpFetcher::class] = $mockHttp; - $mockHttp->shouldReceive('fetch') - ->times($times) - ->andReturn($returnData); - } - - /** - * Mock the http client used in BookStack. - * Returns a reference to the container which holds all history of http transactions. - * - * @link https://docs.guzzlephp.org/en/stable/testing.html#history-middleware - */ - protected function &mockHttpClient(array $responses = []): array - { - $container = []; - $history = Middleware::history($container); - $mock = new MockHandler($responses); - $handlerStack = new HandlerStack($mock); - $handlerStack->push($history); - $this->app[ClientInterface::class] = new Client(['handler' => $handlerStack]); - - return $container; + return $this->app->make(HttpRequestService::class)->mockClient($responses); } /** @@ -213,18 +187,14 @@ abstract class TestCase extends BaseTestCase */ private function isPermissionError($response): bool { + if ($response->status() === 403 && $response instanceof JsonResponse) { + $errMessage = $response->getData(true)['error']['message'] ?? ''; + return $errMessage === 'You do not have permission to perform the requested action.'; + } + return $response->status() === 302 - && ( - ( - $response->headers->get('Location') === url('/') - && strpos(session()->pull('error', ''), 'You do not have permission to access') === 0 - ) - || - ( - $response instanceof JsonResponse && - $response->json(['error' => 'You do not have permission to perform the requested action.']) - ) - ); + && $response->headers->get('Location') === url('/') + && str_starts_with(session()->pull('error', ''), 'You do not have permission to access'); } /** @@ -278,8 +248,8 @@ abstract class TestCase extends BaseTestCase $detailsToCheck = ['type' => $type]; if ($entity) { - $detailsToCheck['entity_type'] = $entity->getMorphClass(); - $detailsToCheck['entity_id'] = $entity->id; + $detailsToCheck['loggable_type'] = $entity->getMorphClass(); + $detailsToCheck['loggable_id'] = $entity->id; } if ($detail) {