You are here

public function RestExportTest::testBuildResponse in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Kernel/Views/RestExportTest.php \Drupal\Tests\rest\Kernel\Views\RestExportTest::testBuildResponse()
  2. 9 core/modules/rest/tests/src/Kernel/Views/RestExportTest.php \Drupal\Tests\rest\Kernel\Views\RestExportTest::testBuildResponse()

@covers ::buildResponse

File

core/modules/rest/tests/src/Kernel/Views/RestExportTest.php, line 47

Class

RestExportTest
Tests the REST export view display plugin.

Namespace

Drupal\Tests\rest\Kernel\Views

Code

public function testBuildResponse() {

  /** @var \Drupal\views\Entity\View $view */
  $view = View::load('test_serializer_display_entity');
  $display =& $view
    ->getDisplay('rest_export_1');
  $display['display_options']['defaults']['style'] = FALSE;
  $display['display_options']['style']['type'] = 'serializer';
  $display['display_options']['style']['options']['formats'] = [
    'json',
    'xml',
  ];
  $view
    ->save();

  // No custom header should be set yet.
  $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []);
  $this
    ->assertEmpty($response->headers
    ->get('Custom-Header'));

  // Clear render cache.

  /** @var \Drupal\Core\Cache\MemoryBackend $render_cache */
  $render_cache = $this->container
    ->get('cache_factory')
    ->get('render');
  $render_cache
    ->deleteAll();

  // A custom header should now be added.
  // @see rest_test_views_views_post_execute()
  $header = $this
    ->randomString();
  $this->container
    ->get('state')
    ->set('rest_test_views_set_header', $header);
  $response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []);
  $this
    ->assertEquals($header, $response->headers
    ->get('Custom-Header'));
}