RestExportTest.php in Drupal 10
File
core/modules/rest/tests/src/Kernel/Views/RestExportTest.php
View source
<?php
namespace Drupal\Tests\rest\Kernel\Views;
use Drupal\rest\Plugin\views\display\RestExport;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Entity\View;
use Drupal\views\Tests\ViewTestData;
class RestExportTest extends ViewsKernelTestBase {
public static $testViews = [
'test_serializer_display_entity',
];
protected static $modules = [
'rest_test_views',
'serialization',
'rest',
'entity_test',
];
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
ViewTestData::createTestViews(static::class, [
'rest_test_views',
]);
$this
->installEntitySchema('entity_test');
}
public function testBuildResponse() {
$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();
$response = RestExport::buildResponse('test_serializer_display_entity', 'rest_export_1', []);
$this
->assertEmpty($response->headers
->get('Custom-Header'));
$render_cache = $this->container
->get('cache_factory')
->get('render');
$render_cache
->deleteAll();
$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'));
}
}