From 8245758cb980bcb5846c0e86b4e33792ec3bd664 Mon Sep 17 00:00:00 2001
From: Matthew Grasmick <matthew.grasmick@acquia.com>
Date: Wed, 2 Dec 2015 16:00:43 -0500
Subject: [PATCH] Issue #2568413 by pwolanin, grasmash, dawehner, xjm: Pass
 views config options to Rest Export serializer

---
 .../rest/src/Plugin/views/style/Serializer.php     |  2 +-
 .../src/Unit/Plugin/views/style/SerializerTest.php | 81 ++++++++++++++++++++++
 2 files changed, 82 insertions(+), 1 deletion(-)
 create mode 100644 core/modules/rest/tests/src/Unit/Plugin/views/style/SerializerTest.php

diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php
index d9e7274..00286e6 100644
--- a/core/modules/rest/src/Plugin/views/style/Serializer.php
+++ b/core/modules/rest/src/Plugin/views/style/Serializer.php
@@ -135,7 +135,7 @@ public function render() {
     else {
       $content_type = !empty($this->options['formats']) ? reset($this->options['formats']) : 'json';
     }
-    return $this->serializer->serialize($rows, $content_type);
+    return $this->serializer->serialize($rows, $content_type, array('views_style_plugin' => $this));
   }
 
   /**
diff --git a/core/modules/rest/tests/src/Unit/Plugin/views/style/SerializerTest.php b/core/modules/rest/tests/src/Unit/Plugin/views/style/SerializerTest.php
new file mode 100644
index 0000000..d93f649
--- /dev/null
+++ b/core/modules/rest/tests/src/Unit/Plugin/views/style/SerializerTest.php
@@ -0,0 +1,81 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\rest\Unit\Plugin\views\style\SerializerTest.
+ */
+
+namespace Drupal\Tests\rest\Unit\Plugin\views\style;
+
+use Drupal\rest\Plugin\views\display\RestExport;
+use Drupal\rest\Plugin\views\style\Serializer;
+use Drupal\Tests\UnitTestCase;
+use Drupal\views\ViewExecutable;
+use Symfony\Component\Serializer\SerializerInterface;
+
+/**
+ * @coversDefaultClass \Drupal\rest\Plugin\views\style\Serializer
+ * @group rest
+ */
+class SerializerTest extends UnitTestCase {
+
+  /**
+   * The View instance.
+   *
+   * @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $view;
+
+  /**
+   * The RestExport display handler.
+   *
+   * @var \Drupal\rest\Plugin\views\display\RestExport|\PHPUnit_Framework_MockObject_MockObject
+   */
+  protected $displayHandler;
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+
+    $this->view = $this->getMockBuilder(ViewExecutable::class)
+      ->disableOriginalConstructor()
+      ->getMock();
+
+    // Make the view result empty so we don't have to mock the row plugin render
+    // call.
+    $this->view->result = [];
+
+    $this->displayHandler = $this->getMockBuilder(RestExport::class)
+      ->disableOriginalConstructor()
+      ->getMock();
+
+    $this->displayHandler->expects($this->any())
+      ->method('getContentType')
+      ->willReturn('json');
+  }
+
+  /**
+   * Tests that the symfony serializer receives style plugin from the render() method.
+   *
+   * @covers ::render
+   */
+  public function testSerializerReceivesOptions() {
+
+    $mock_serializer = $this->prophesize(SerializerInterface::class);
+
+    // This is the main expectation of the test. We want to make sure the
+    // serializer options are passed to the SerializerInterface object.
+    $mock_serializer->serialize([], 'json', ['views_style_plugin' => $mock_serializer])
+      ->willReturn('')
+      ->shouldBeCalled();
+
+    $view_serializer_style = new Serializer([], 'dummy_serializer', [], $mock_serializer->reveal(), ['json', 'xml']);
+    $view_serializer_style->options = ['formats' => ['xml', 'json']];
+    $view_serializer_style->view = $this->view;
+    $view_serializer_style->displayHandler = $this->displayHandler;
+    $view_serializer_style->render();
+  }
+}
-- 
2.2.1

