diff --git a/src/Entity/Page.php b/src/Entity/Page.php index 47e74dc..3e46732 100644 --- a/src/Entity/Page.php +++ b/src/Entity/Page.php @@ -316,6 +316,7 @@ public function __sleep() { $vars = parent::__sleep(); // Ensure any plugin collections are stored correctly before serializing. + // @todo Let https://www.drupal.org/node/2650588 handle this instead. foreach ($this->getPluginCollections() as $plugin_config_key => $plugin_collection) { $this->set($plugin_config_key, $plugin_collection->getConfiguration()); } diff --git a/src/Entity/PageVariant.php b/src/Entity/PageVariant.php index bf5d9ee..a092d52 100644 --- a/src/Entity/PageVariant.php +++ b/src/Entity/PageVariant.php @@ -247,7 +247,7 @@ public function getPage() { if (!$this->page) { throw new \UnexpectedValueException('The page variant has no associated page'); } - return Page::load($this->page); + return $this->getPageStorage()->load($this->page); } /** @@ -255,8 +255,9 @@ public function getPage() { */ public function getContexts() { if (!$this->contexts) { - $contexts = $this->getContextMapper()->getContextValues($this->getStaticContexts()); - $this->contexts = array_merge($this->getPage()->getContexts(), $contexts); + $static_contexts = $this->getContextMapper()->getContextValues($this->getStaticContexts()); + $page_contexts = $this->getPage()->getContexts(); + $this->contexts = array_merge($static_contexts, $page_contexts); } return $this->contexts; } @@ -410,4 +411,23 @@ protected function getContextMapper() { return \Drupal::service('page_manager.context_mapper'); } + /** + * Wraps the page entity storage. + * + * @return \Drupal\Core\Entity\EntityStorageInterface + */ + protected function getPageStorage() { + return \Drupal::entityTypeManager()->getStorage('page'); + } + + /** + * {@inheritdoc} + */ + public function __sleep() { + $vars = parent::__sleep(); + // Gathered contexts objects should not be serialized. + unset($vars[array_search('contexts', $vars)]); + return $vars; + } + } diff --git a/tests/src/Unit/PageVariantTest.php b/tests/src/Unit/PageVariantTest.php new file mode 100644 index 0000000..f7279d2 --- /dev/null +++ b/tests/src/Unit/PageVariantTest.php @@ -0,0 +1,95 @@ +pageVariant = new PageVariant(['id' => 'the_page_variant', 'page' => 'the_page'], 'page_variant'); + $this->page = $this->prophesize(PageInterface::class); + + $entity_storage = $this->prophesize(EntityStorageInterface::class); + $entity_storage->load('the_page')->willReturn($this->page->reveal()); + + $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class); + $entity_type_manager->getStorage('page')->willReturn($entity_storage); + + $this->contextMapper = $this->prophesize(ContextMapperInterface::class); + + $container = new ContainerBuilder(); + $container->set('entity_type.manager', $entity_type_manager->reveal()); + $container->set('page_manager.context_mapper', $this->contextMapper->reveal()); + \Drupal::setContainer($container); + } + + /** + * @covers ::getContexts + * @dataProvider providerTestGetContexts + */ + public function testGetContexts($static_contexts, $page_contexts, $expected) { + $this->contextMapper->getContextValues([])->willReturn($static_contexts); + $this->page->getContexts()->willReturn($page_contexts); + + $contexts = $this->pageVariant->getContexts(); + $this->assertSame($expected, $contexts); + } + + public function providerTestGetContexts() { + $data = []; + $data['empty'] = [ + [], + [], + [], + ]; + $data['additive'] = [ + ['static' => 'static'], + ['page' => 'page'], + ['static' => 'static', 'page' => 'page'], + ]; + $data['conflicting'] = [ + ['foo' => 'static'], + ['foo' => 'page'], + ['foo' => 'page'], + ]; + return $data; + } + +}