diff --git a/src/Entity/PageVariant.php b/src/Entity/PageVariant.php index a092d52..c1198c8 100644 --- a/src/Entity/PageVariant.php +++ b/src/Entity/PageVariant.php @@ -125,9 +125,9 @@ class PageVariant extends ConfigEntityBase implements PageVariantInterface { /** * An array of collected contexts. * - * @var \Drupal\Component\Plugin\Context\ContextInterface[] + * @var \Drupal\Component\Plugin\Context\ContextInterface[]|null */ - protected $contexts = []; + protected $contexts = NULL; /** * Static context references. @@ -254,7 +254,7 @@ public function getPage() { * {@inheritdoc} */ public function getContexts() { - if (!$this->contexts) { + if (is_null($this->contexts)) { $static_contexts = $this->getContextMapper()->getContextValues($this->getStaticContexts()); $page_contexts = $this->getPage()->getContexts(); $this->contexts = array_merge($static_contexts, $page_contexts); @@ -263,6 +263,16 @@ public function getContexts() { } /** + * Resets the collected contexts. + * + * @return $this + */ + protected function resetCollectedContexts() { + $this->contexts = NULL; + return $this; + } + + /** * {@inheritdoc} */ public function getWeight() { @@ -347,6 +357,7 @@ public function getStaticContext($name) { */ public function setStaticContext($name, $configuration) { $this->static_context[$name] = $configuration; + $this->resetCollectedContexts(); return $this; } @@ -355,6 +366,7 @@ public function setStaticContext($name, $configuration) { */ public function removeStaticContext($name) { unset($this->static_context[$name]); + $this->resetCollectedContexts(); return $this; } diff --git a/tests/src/Unit/PageVariantTest.php b/tests/src/Unit/PageVariantTest.php index f7279d2..cfe8ead 100644 --- a/tests/src/Unit/PageVariantTest.php +++ b/tests/src/Unit/PageVariantTest.php @@ -65,11 +65,13 @@ protected function setUp() { * @dataProvider providerTestGetContexts */ public function testGetContexts($static_contexts, $page_contexts, $expected) { - $this->contextMapper->getContextValues([])->willReturn($static_contexts); - $this->page->getContexts()->willReturn($page_contexts); + $this->contextMapper->getContextValues([])->willReturn($static_contexts)->shouldBeCalledTimes(1); + $this->page->getContexts()->willReturn($page_contexts)->shouldBeCalledTimes(1); $contexts = $this->pageVariant->getContexts(); $this->assertSame($expected, $contexts); + $contexts = $this->pageVariant->getContexts(); + $this->assertSame($expected, $contexts); } public function providerTestGetContexts() { @@ -92,4 +94,20 @@ public function providerTestGetContexts() { return $data; } + /** + * @covers ::getContexts + * @covers ::removeStaticContext + */ + public function testGetContextsAfterReset() { + $this->contextMapper->getContextValues([])->willReturn([])->shouldBeCalledTimes(2); + $this->page->getContexts()->willReturn([])->shouldBeCalledTimes(2); + + $expected = []; + $contexts = $this->pageVariant->getContexts(); + $this->assertSame($expected, $contexts); + $this->pageVariant->removeStaticContext('anything'); + $contexts = $this->pageVariant->getContexts(); + $this->assertSame($expected, $contexts); + } + }