You are here

public function StyleSerializerTest::testLivePreview in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/rest/src/Tests/Views/StyleSerializerTest.php \Drupal\rest\Tests\Views\StyleSerializerTest::testLivePreview()

Tests the live preview output for json output.

File

core/modules/rest/src/Tests/Views/StyleSerializerTest.php, line 464
Contains \Drupal\rest\Tests\Views\StyleSerializerTest.

Class

StyleSerializerTest
Tests the serializer style plugin.

Namespace

Drupal\rest\Tests\Views

Code

public function testLivePreview() {

  // We set up a request so it looks like an request in the live preview.
  $request = new Request();
  $request
    ->setFormat('drupal_ajax', 'application/vnd.drupal-ajax');
  $request->headers
    ->set('Accept', 'application/vnd.drupal-ajax');

  /** @var \Symfony\Component\HttpFoundation\RequestStack $request_stack */
  $request_stack = \Drupal::service('request_stack');
  $request_stack
    ->push($request);
  $view = Views::getView('test_serializer_display_entity');
  $view
    ->setDisplay('rest_export_1');
  $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');
  $view->live_preview = TRUE;
  $build = $view
    ->preview();
  $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.');
}