public function StyleSerializerTest::testSerializerResponses in Drupal 8
Same name and namespace in other branches
- 9 core/modules/rest/tests/src/Functional/Views/StyleSerializerTest.php \Drupal\Tests\rest\Functional\Views\StyleSerializerTest::testSerializerResponses()
Checks the behavior of the Serializer callback paths and row plugins.
File
- core/
modules/ rest/ tests/ src/ Functional/ Views/ StyleSerializerTest.php, line 130
Class
- StyleSerializerTest
- Tests the serializer style plugin.
Namespace
Drupal\Tests\rest\Functional\ViewsCode
public function testSerializerResponses() {
// Test the serialize callback.
$view = Views::getView('test_serializer_display_field');
$view
->initDisplay();
$this
->executeView($view);
$actual_json = $this
->drupalGet('test/serialize/field', [
'query' => [
'_format' => 'json',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertCacheTags($view
->getCacheTags());
$this
->assertCacheContexts([
'languages:language_interface',
'theme',
'request_format',
]);
// @todo Due to https://www.drupal.org/node/2352009 we can't yet test the
// propagation of cache max-age.
// Test the http Content-type.
$headers = $this
->getSession()
->getResponseHeaders();
$this
->assertSame([
'application/json',
], $headers['Content-Type']);
$expected = [];
foreach ($view->result as $row) {
$expected_row = [];
foreach ($view->field as $id => $field) {
$expected_row[$id] = $field
->render($row);
}
$expected[] = $expected_row;
}
$this
->assertIdentical($actual_json, json_encode($expected), 'The expected JSON output was found.');
// Test that the rendered output and the preview output are the same.
$view
->destroy();
$view
->setDisplay('rest_export_1');
// Mock the request content type by setting it on the display handler.
$view->display_handler
->setContentType('json');
$output = $view
->preview();
$this
->assertIdentical($actual_json, (string) $this->renderer
->renderRoot($output), 'The expected JSON preview output was found.');
// Test a 403 callback.
$this
->drupalGet('test/serialize/denied', [
'query' => [
'_format' => 'json',
],
]);
$this
->assertSession()
->statusCodeEquals(403);
// Test the entity rows.
$view = Views::getView('test_serializer_display_entity');
$view
->initDisplay();
$this
->executeView($view);
// Get the serializer service.
$serializer = $this->container
->get('serializer');
$entities = [];
foreach ($view->result as $row) {
$entities[] = $row->_entity;
}
$expected = $serializer
->serialize($entities, 'json');
$actual_json = $this
->drupalGet('test/serialize/entity', [
'query' => [
'_format' => 'json',
],
]);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertIdentical($actual_json, $expected, 'The expected JSON output was found.');
$expected_cache_tags = $view
->getCacheTags();
$expected_cache_tags[] = 'entity_test_list';
/** @var \Drupal\Core\Entity\EntityInterface $entity */
foreach ($entities as $entity) {
$expected_cache_tags = Cache::mergeTags($expected_cache_tags, $entity
->getCacheTags());
}
$this
->assertCacheTags($expected_cache_tags);
$this
->assertCacheContexts([
'languages:language_interface',
'theme',
'entity_test_view_grants',
'request_format',
]);
$expected = $serializer
->serialize($entities, 'hal_json');
$actual_json = $this
->drupalGet('test/serialize/entity', [
'query' => [
'_format' => 'hal_json',
],
]);
$this
->assertIdentical($actual_json, $expected, 'The expected HAL output was found.');
$this
->assertCacheTags($expected_cache_tags);
// Change the format to xml.
$view
->setDisplay('rest_export_1');
$view
->getDisplay()
->setOption('style', [
'type' => 'serializer',
'options' => [
'uses_fields' => FALSE,
'formats' => [
'xml' => 'xml',
],
],
]);
$view
->save();
$expected = $serializer
->serialize($entities, 'xml');
$actual_xml = $this
->drupalGet('test/serialize/entity', [
'query' => [
'_format' => 'xml',
],
]);
$this
->assertSame(trim($expected), $actual_xml);
$this
->assertCacheContexts([
'languages:language_interface',
'theme',
'entity_test_view_grants',
'request_format',
]);
// Allow multiple formats.
$view
->setDisplay('rest_export_1');
$view
->getDisplay()
->setOption('style', [
'type' => 'serializer',
'options' => [
'uses_fields' => FALSE,
'formats' => [
'xml' => 'xml',
'json' => 'json',
],
],
]);
$view
->save();
$expected = $serializer
->serialize($entities, 'json');
$actual_json = $this
->drupalGet('test/serialize/entity', [
'query' => [
'_format' => 'json',
],
]);
$this
->assertIdentical($actual_json, $expected, 'The expected JSON output was found.');
$expected = $serializer
->serialize($entities, 'xml');
$actual_xml = $this
->drupalGet('test/serialize/entity', [
'query' => [
'_format' => 'xml',
],
]);
$this
->assertSame(trim($expected), $actual_xml);
}