diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 4c004fc..cf34965 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -177,43 +177,6 @@ public function testSerializerResponses() { $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.'); } /** @@ -292,8 +255,7 @@ public function testRestRenderCaching() { $this->assertFalse($render_cache->get($original)); - // Request the page, once in XML and once in JSON to ensure that the caching - // varies by it. + // Request the page in JSON. $result1 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); $this->assertHeader('content-type', 'application/json'); @@ -301,16 +263,6 @@ public function testRestRenderCaching() { $this->assertCacheTags($cache_tags); $this->assertTrue($render_cache->get($original)); - $result_xml = $this->drupalGetWithFormat('test/serialize/entity', 'xml'); - $this->addRequestWithFormat('xml'); - $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); - $this->assertCacheContexts($cache_contexts); - $this->assertCacheTags($cache_tags); - $this->assertTrue($render_cache->get($original)); - - // Ensure that the XML output is different from the JSON one. - $this->assertNotEqual($result1, $result_xml); - // Ensure that the cached page works. $result2 = $this->drupalGetJSON('test/serialize/entity'); $this->addRequestWithFormat('json'); @@ -338,80 +290,6 @@ public function testRestRenderCaching() { } /** - * Tests the response format configuration. - */ - public function testResponseFormatConfiguration() { - $this->drupalLogin($this->adminUser); - - $style_options = 'admin/structure/views/nojs/display/test_serializer_display_field/rest_export_1/style_options'; - - // Select only 'xml' as an accepted format. - $this->drupalPostForm($style_options, array('style_options[formats][xml]' => 'xml'), t('Apply')); - $this->drupalPostForm(NULL, array(), t('Save')); - - // Should return a 406. - $this->drupalGetWithFormat('test/serialize/field', 'json'); - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(406, 'A 406 response was returned when JSON was requested.'); - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'xml'); - $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); - $this->assertResponse(200, 'A 200 response was returned when XML was requested.'); - - // Add 'json' as an accepted format, so we have multiple. - $this->drupalPostForm($style_options, array('style_options[formats][json]' => 'json'), t('Apply')); - $this->drupalPostForm(NULL, array(), t('Save')); - - // Should return a 200. - // @todo This should be fixed when we have better content negotiation. - $this->drupalGet('test/serialize/field'); - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when any format was requested.'); - - // Should return a 200. Emulates a sample Firefox header. - $this->drupalGet('test/serialize/field', array(), array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')); - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when a browser accept header was requested.'); - - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'json'); - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when JSON was requested.'); - $headers = $this->drupalGetHeaders(); - $this->assertEqual($headers['content-type'], 'application/json', 'The header Content-type is correct.'); - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'xml'); - $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); - $this->assertResponse(200, 'A 200 response was returned when XML was requested'); - $headers = $this->drupalGetHeaders(); - $this->assertTrue(strpos($headers['content-type'], 'text/xml') !== FALSE, 'The header Content-type is correct.'); - // Should return a 406. - $this->drupalGetWithFormat('test/serialize/field', 'html'); - // We want to show the first format by default, see - // \Drupal\rest\Plugin\views\style\Serializer::render. - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when HTML was requested.'); - - // Now configure now format, so all of them should be allowed. - $this->drupalPostForm($style_options, array('style_options[formats][json]' => '0', 'style_options[formats][xml]' => '0'), t('Apply')); - - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'json'); - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when JSON was requested.'); - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'xml'); - $this->assertHeader('content-type', 'text/xml; charset=UTF-8'); - $this->assertResponse(200, 'A 200 response was returned when XML was requested'); - // Should return a 200. - $this->drupalGetWithFormat('test/serialize/field', 'html'); - // We want to show the first format by default, see - // \Drupal\rest\Plugin\views\style\Serializer::render. - $this->assertHeader('content-type', 'application/json'); - $this->assertResponse(200, 'A 200 response was returned when HTML was requested.'); - } - - /** * Test the field ID alias functionality of the DataFieldRow plugin. */ public function testUIFieldAlias() { @@ -575,25 +453,6 @@ public function testLivePreview() { $rendered_json = $build['#plain_text']; $this->assertTrue(!isset($build['#markup']) && $rendered_json == $expected, 'Ensure the previewed json is escaped.'); $view->destroy(); - - $expected = $serializer->serialize($entities, 'xml'); - - // Change the request format to xml. - $view->setDisplay('rest_export_1'); - $view->getDisplay()->setOption('style', array( - 'type' => 'serializer', - 'options' => array( - 'uses_fields' => FALSE, - 'formats' => array( - 'xml' => 'xml', - ), - ), - )); - - $this->executeView($view); - $build = $view->preview(); - $rendered_xml = $build['#plain_text']; - $this->assertEqual($rendered_xml, $expected, 'Ensure we preview xml when we change the request format.'); } /** diff --git a/core/modules/serialization/src/Encoder/XmlEncoder.php b/core/modules/serialization/src/Encoder/XmlEncoder.php new file mode 100644 index 0000000..45a1b84 --- /dev/null +++ b/core/modules/serialization/src/Encoder/XmlEncoder.php @@ -0,0 +1,84 @@ +baseEncoder)) { + $this->baseEncoder = new BaseXmlEncoder(); + } + + return $this->baseEncoder; + } + + /** + * Sets the base encoder instance. + * + * @param \Symfony\Component\Serializer\Encoder\XmlEncoder $encoder + */ + public function setBaseEncoder($encoder) { + $this->baseEncoder = $encoder; + } + + /** + * {@inheritdoc} + */ + public function encode($data, $format, array $context = array()){ + return $this->getBaseEncoder()->encode($data, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsEncoding($format) { + return in_array($format, static::$format); + } + + /** + * {@inheritdoc} + */ + public function decode($data, $format, array $context = array()){ + return $this->getBaseEncoder()->decode($data, $format, $context); + } + + /** + * {@inheritdoc} + */ + public function supportsDecoding($format) { + return in_array($format, static::$format); + } + +}