diff --git a/core/modules/views/src/ViewExecutable.php b/core/modules/views/src/ViewExecutable.php index 6d5e55c..da8ef32 100644 --- a/core/modules/views/src/ViewExecutable.php +++ b/core/modules/views/src/ViewExecutable.php @@ -1536,9 +1536,7 @@ public function attachDisplays() { $this->is_attachment = TRUE; // Find out which other displays attach to the current one. foreach ($this->display_handler->getAttachedDisplays() as $id) { - // Create a clone for the attachments to manipulate. 'static' refers to the current class name. - $cloned_view = new static($this->storage, $this->user); - $cloned_view->setRequest($this->getRequest()); + $cloned_view = Views::executableFactory()->get($this->storage); $this->displayHandlers->get($id)->attachTo($cloned_view, $this->current_display, $this->element); } $this->is_attachment = FALSE; diff --git a/core/modules/views/tests/src/Unit/ViewExecutableUnitTest.php b/core/modules/views/tests/src/Unit/ViewExecutableUnitTest.php index 78e92dc..89b7d0a 100644 --- a/core/modules/views/tests/src/Unit/ViewExecutableUnitTest.php +++ b/core/modules/views/tests/src/Unit/ViewExecutableUnitTest.php @@ -40,6 +40,13 @@ class ViewExecutableUnitTest extends UnitTestCase { protected $displayCollection; /** + * The mocked view executable. + * + * @var \Drupal\views\ViewExecutableFactory|\PHPUnit_Framework_MockObject_MockObject + */ + protected $viewExecutableFactory; + + /** * {@inheritdoc} */ protected function setUp() { @@ -54,9 +61,14 @@ protected function setUp() { ->disableOriginalConstructor() ->getMock(); + $this->viewExecutableFactory = $this->getMockBuilder('Drupal\views\ViewExecutableFactory') + ->disableOriginalConstructor() + ->getMock(); + $translation = $this->getStringTranslationStub(); $container = new ContainerBuilder(); $container->set('string_translation', $translation); + $container->set('views.executable', $this->viewExecutableFactory); \Drupal::setContainer($container); } @@ -64,8 +76,8 @@ protected function setUp() { * Tests the buildThemeFunctions() method. */ public function testBuildThemeFunctions() { - /** @var \Drupal\views\ViewExecutable $view */ - /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */ + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ list($view, $display) = $this->setupBaseViewAndDisplay(); unset($view->display_handler); @@ -122,8 +134,8 @@ public function testGenerateHandlerId() { * @covers ::addHandler() */ public function testAddHandler() { - /** @var \Drupal\views\ViewExecutable $view */ - /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */ + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ list($view, $display) = $this->setupBaseViewAndDisplay(); $views_data = []; @@ -163,8 +175,8 @@ public function testAddHandler() { * @covers ::addHandler() */ public function testAddHandlerWithEntityField() { - /** @var \Drupal\views\ViewExecutable $view */ - /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase $display */ + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ list($view, $display) = $this->setupBaseViewAndDisplay(); $views_data = []; @@ -203,6 +215,55 @@ public function testAddHandlerWithEntityField() { } /** + * Tests attachDisplays(). + * + * @covers ::attachDisplays() + */ + public function testAttachDisplays() { + /** @var \Drupal\views\ViewExecutable|\PHPUnit_Framework_MockObject_MockObject $view */ + /** @var \Drupal\views\Plugin\views\display\DisplayPluginBase|\PHPUnit_Framework_MockObject_MockObject $display */ + list($view, $display) = $this->setupBaseViewAndDisplay(); + + $display->expects($this->atLeastOnce()) + ->method('acceptAttachments') + ->willReturn(TRUE); + $display->expects($this->atLeastOnce()) + ->method('getAttachedDisplays') + ->willReturn(['page_1']); + + $cloned_view = $this->getMockBuilder('Drupal\views\ViewExecutable') + ->disableOriginalConstructor() + ->getMock(); + $this->viewExecutableFactory->expects($this->atLeastOnce()) + ->method('get') + ->willReturn($cloned_view); + + $page_display = $this->getMockBuilder('Drupal\views\Plugin\views\display\DisplayPluginBase') + ->disableOriginalConstructor() + ->getMock(); + + $display_collection = $this->getMockBuilder('Drupal\views\DisplayPluginCollection') + ->disableOriginalConstructor() + ->getMock(); + + $display_collection->expects($this->atLeastOnce()) + ->method('get') + ->with('page_1') + ->willReturn($page_display); + $view->displayHandlers = $display_collection; + + // Setup the expectations. + + $page_display->expects($this->once()) + ->method('attachTo') + ->with($cloned_view, 'default', $view->element); + + $view->attachDisplays(); + + + } + + /** * Setups a view executable and default display. * * @return array @@ -239,6 +300,8 @@ protected function setupBaseViewAndDisplay() { ->method('has') ->with('default') ->willReturn(TRUE); + return array($view, $display); } + }