public function StyleSerializerTest::testSerializerResponses in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/rest/src/Tests/Views/StyleSerializerTest.php \Drupal\rest\Tests\Views\StyleSerializerTest::testSerializerResponses()
Checks the behavior of the Serializer callback paths and row plugins.
File
- core/
modules/ rest/ src/ Tests/ Views/ StyleSerializerTest.php, line 78 - Contains \Drupal\rest\Tests\Views\StyleSerializerTest.
Class
- StyleSerializerTest
- Tests the serializer style plugin.
Namespace
Drupal\rest\Tests\ViewsCode
public function testSerializerResponses() {
// Test the serialize callback.
$view = Views::getView('test_serializer_display_field');
$view
->initDisplay();
$this
->executeView($view);
$actual_json = $this
->drupalGetWithFormat('test/serialize/field', 'json');
$this
->assertResponse(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
->drupalGetHeaders();
$this
->assertEqual($headers['content-type'], 'application/json', 'The header Content-type is correct.');
$expected = array();
foreach ($view->result as $row) {
$expected_row = array();
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) drupal_render_root($output), 'The expected JSON preview output was found.');
// Test a 403 callback.
$this
->drupalGet('test/serialize/denied');
$this
->assertResponse(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 = array();
foreach ($view->result as $row) {
$entities[] = $row->_entity;
}
$expected = $serializer
->serialize($entities, 'json');
$actual_json = $this
->drupalGetWithFormat('test/serialize/entity', 'json');
$this
->assertResponse(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
->drupalGetWithFormat('test/serialize/entity', 'hal_json');
$this
->assertIdentical($actual_json, $expected, 'The expected HAL output was found.');
$this
->assertCacheTags($expected_cache_tags);
// Change the default format to xml.
$view
->setDisplay('rest_export_1');
$view
->getDisplay()
->setOption('style', array(
'type' => 'serializer',
'options' => array(
'uses_fields' => FALSE,
'formats' => array(
'xml' => 'xml',
),
),
));
$view
->save();
$expected = $serializer
->serialize($entities, 'xml');
$actual_xml = $this
->drupalGet('test/serialize/entity');
$this
->assertIdentical($actual_xml, $expected, 'The expected XML output was found.');
$this
->assertCacheContexts([
'languages:language_interface',
'theme',
'entity_test_view_grants',
'request_format',
]);
// Allow multiple formats.
$view
->setDisplay('rest_export_1');
$view
->getDisplay()
->setOption('style', array(
'type' => 'serializer',
'options' => array(
'uses_fields' => FALSE,
'formats' => array(
'xml' => 'xml',
'json' => 'json',
),
),
));
$view
->save();
$expected = $serializer
->serialize($entities, 'json');
$actual_json = $this
->drupalGetWithFormat('test/serialize/entity', 'json');
$this
->assertIdentical($actual_json, $expected, 'The expected JSON output was found.');
$expected = $serializer
->serialize($entities, 'xml');
$actual_xml = $this
->drupalGetWithFormat('test/serialize/entity', 'xml');
$this
->assertIdentical($actual_xml, $expected, 'The expected XML output was found.');
}