diff --git a/src/FacetManager/DefaultFacetManager.php b/src/FacetManager/DefaultFacetManager.php index 688f9ad..0718e96 100644 --- a/src/FacetManager/DefaultFacetManager.php +++ b/src/FacetManager/DefaultFacetManager.php @@ -320,4 +320,18 @@ class DefaultFacetManager { $facet_source_plugin->fillFacetsWithResults($this->facets); } + + /** + * Makes sure facets are processed and returns the updated facet entity. + * + * @param string $facet_id + * The id of the facet. + * @return \Drupal\facets\FacetInterface|NULL + * The updated facet if it exists, NULL otherwise. + */ + public function returnProcessedFacet($facet_id) { + $this->processFacets(); + return $this->facets[$facet_id]; + } + } diff --git a/src/Plugin/Condition/OtherFacet.php b/src/Plugin/Condition/OtherFacet.php index ebd4ee0..e91ccf8 100644 --- a/src/Plugin/Condition/OtherFacet.php +++ b/src/Plugin/Condition/OtherFacet.php @@ -14,6 +14,7 @@ use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountProxyInterface; +use Drupal\facets\FacetManager\DefaultFacetManager; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -48,6 +49,13 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn protected $currentUser; /** + * The facet manager service. + * + * @var \Drupal\facets\FacetManager\DefaultFacetManager + */ + protected $facetManager; + + /** * Creates a new instance of the condition. * * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage @@ -56,6 +64,8 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * The block plugin manager. * @param \Drupal\Core\Session\AccountProxyInterface $current_user * The currently logged in user. + * @param \Drupal\facets\FacetManager\DefaultFacetManager $facet_manager + * The default facet manager class. * @param array $configuration * The plugin configuration, i.e. an array with configuration values keyed * by configuration option name. The special key 'context' may be used to @@ -66,11 +76,12 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * @param mixed $plugin_definition * The plugin implementation definition. */ - public function __construct(EntityStorageInterface $entity_storage, BlockManager $block_manager, AccountProxyInterface $current_user, array $configuration, $plugin_id, $plugin_definition) { + public function __construct(EntityStorageInterface $entity_storage, BlockManager $block_manager, AccountProxyInterface $current_user, DefaultFacetManager $facet_manager,array $configuration, $plugin_id, $plugin_definition) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->facetStorage = $entity_storage; $this->blockManager = $block_manager; $this->currentUser = $current_user; + $this->facetManager = $facet_manager; } /** @@ -81,6 +92,7 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn $container->get('entity_type.manager')->getStorage('facets_facet'), $container->get('plugin.manager.block'), $container->get('current_user'), + $configuration->get('facets.manager'), $configuration, $plugin_id, $plugin_definition @@ -166,12 +178,16 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn $block_plugin_id = $block_plugin->getPluginId(); $facet_id = explode(PluginBase::DERIVATIVE_SEPARATOR, $block_plugin_id)[1]; - /** @var \Drupal\facets\FacetInterface $facet */ - $facet = $this->facetStorage->load($facet_id); + $facet = $this->facetManager->returnProcessedFacet($facet_id); foreach ($facet->getResults() as $result) { - if (($result->getRawValue() == $allowed_facet_value || $result->getDisplayValue() == $allowed_facet_value) && $result->isActive()) { - return TRUE; + if ($result->getRawValue() == $allowed_facet_value || $result->getDisplayValue() == $allowed_facet_value) { + if ($result->isActive() && !$this->isNegated()) { + return TRUE; + } + if (!$result->isActive() && $this->isNegated()) { + return TRUE; + } } } diff --git a/tests/src/Unit/Plugin/Condition/OtherFacetTest.php b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php index 92e02aa..7209032 100644 --- a/tests/src/Unit/Plugin/Condition/OtherFacetTest.php +++ b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php @@ -7,7 +7,9 @@ namespace Drupal\Tests\facets\Unit\Plugin\Condition; +use Drupal\facets\Entity\Facet; use Drupal\facets\Plugin\Condition\OtherFacet; +use Drupal\facets\Result\Result; use Drupal\Tests\UnitTestCase; /** @@ -28,7 +30,10 @@ class OtherFacetTest extends UnitTestCase { ->disableOriginalConstructor() ->getMock(); $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); - $sut = new OtherFacet($storage, $manager, $user, ['negate' => $negated], 'other_facet', ''); + $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager') + ->disableOriginalConstructor() + ->getMock(); + $sut = new OtherFacet($storage, $manager, $user, $facet_manager, ['negate' => $negated], 'other_facet', ''); $evaluation = $sut->evaluate(); if ($negated) { @@ -59,7 +64,10 @@ class OtherFacetTest extends UnitTestCase { ->method('createInstance') ->willReturn($block); $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); - $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager') + ->disableOriginalConstructor() + ->getMock(); + $sut = new OtherFacet($storage, $manager, $user, $facet_manager, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); $evaluation = $sut->evaluate(); if ($negated) { @@ -90,7 +98,10 @@ class OtherFacetTest extends UnitTestCase { ->method('createInstance') ->willReturn($block); $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); - $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager') + ->disableOriginalConstructor() + ->getMock(); + $sut = new OtherFacet($storage, $manager, $user, $facet_manager, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); $evaluation = $sut->evaluate(); if ($negated) { @@ -102,6 +113,57 @@ class OtherFacetTest extends UnitTestCase { } /** + * Active facet value. + * + * @dataProvider provideNegated + */ + public function testActiveFacetValue($negated) { + $facet = new Facet([], 'facets_facet'); + /** @var \Drupal\facets\Result\ResultInterface[] $results */ + $results = [ + new Result('llama', 'Llama', 1), + new Result('kitten', 'Kitten', 5), + new Result('puppy', 'Puppy', 3), + ]; + $results[0]->setActiveState(TRUE); + $facet->setResults($results); + + $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock') + ->disableOriginalConstructor() + ->getMock(); + $block->expects($this->exactly(0))->method('access'); + $block->expects($this->exactly(1)) + ->method('getPluginId') + ->willReturn('block:id'); + $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager') + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->exactly(1)) + ->method('createInstance') + ->willReturn($block); + $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $facet_manager = $this->getMockBuilder('\Drupal\facets\FacetManager\DefaultFacetManager') + ->disableOriginalConstructor() + ->getMock(); + $facet_manager->expects($this->exactly(1)) + ->method('returnProcessedFacet') + ->with('id') + ->willReturn($facet); + + $configuration = ['facets' => 'test', 'facet_value' => 'llama', 'negate' => $negated]; + $sut = new OtherFacet($storage, $manager, $user, $facet_manager, $configuration, 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertFalse($evaluation); + } + else { + $this->assertTrue($evaluation); + } + } + + /** * @return array */ public function provideNegated() {