function HttpExceptionNormalizerTest::testNormalize

Tests normalize.

@legacy-covers ::normalize

File

core/modules/jsonapi/tests/src/Unit/Normalizer/HttpExceptionNormalizerTest.php, line 37

Class

HttpExceptionNormalizerTest
Tests Drupal\jsonapi\Normalizer\HttpExceptionNormalizer.

Namespace

Drupal\Tests\jsonapi\Unit\Normalizer

Code

public function testNormalize() : void {
  $request_stack = $this->prophesize(RequestStack::class);
  $request_stack->getCurrentRequest()
    ->willReturn(Request::create('http://localhost/'));
  $container = $this->prophesize(ContainerInterface::class);
  $container->get('request_stack')
    ->willReturn($request_stack->reveal());
  $config = $this->prophesize(ImmutableConfig::class);
  $config->get('error_level')
    ->willReturn(ERROR_REPORTING_DISPLAY_VERBOSE);
  $config_factory = $this->prophesize(ConfigFactory::class);
  $config_factory->get('system.logging')
    ->willReturn($config->reveal());
  $container->get('config.factory')
    ->willReturn($config_factory->reveal());
  $cache_contexts_manager = $this->prophesize(CacheContextsManager::class);
  $cache_contexts_manager->assertValidTokens([
    'user.permissions',
  ])
    ->willReturn(TRUE);
  $container->get('cache_contexts_manager')
    ->willReturn($cache_contexts_manager->reveal());
  \Drupal::setContainer($container->reveal());
  $cacheability = new TestCacheableDependency([], [], Cache::PERMANENT);
  $exception = new CacheableAccessDeniedHttpException($cacheability, 'lorem', NULL, 13);
  $current_user = $this->prophesize(AccountInterface::class);
  $current_user->hasPermission('access site reports')
    ->willReturn(TRUE);
  $normalizer = new HttpExceptionNormalizer($current_user->reveal());
  $normalized = $normalizer->normalize($exception, 'api_json');
  $this->assertInstanceOf(HttpExceptionNormalizerValue::class, $normalized);
  $this->assertEquals(0, $normalized->getCacheMaxAge());
  $normalized = $normalized->getNormalization();
  $error = $normalized[0];
  $this->assertNotEmpty($error['meta']);
  $this->assertNotEmpty($error['source']);
  $this->assertSame('13', $error['code']);
  $this->assertSame('403', $error['status']);
  $this->assertEquals('Forbidden', $error['title']);
  $this->assertEquals('lorem', $error['detail']);
  $this->assertArrayHasKey('trace', $error['meta']);
  $this->assertNotEmpty($error['meta']['trace']);
  $current_user = $this->prophesize(AccountInterface::class);
  $current_user->hasPermission('access site reports')
    ->willReturn(FALSE);
  $normalizer = new HttpExceptionNormalizer($current_user->reveal());
  $normalized = $normalizer->normalize($exception, 'api_json');
  $this->assertInstanceOf(HttpExceptionNormalizerValue::class, $normalized);
  $this->assertEquals(Cache::PERMANENT, $normalized->getCacheMaxAge());
  $normalized = $normalized->getNormalization();
  $error = $normalized[0];
  $this->assertArrayNotHasKey('meta', $error);
  $this->assertArrayNotHasKey('source', $error);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.