diff --git a/src/Plugin/Condition/OtherFacet.php b/src/Plugin/Condition/OtherFacet.php index 378c919..ebd4ee0 100644 --- a/src/Plugin/Condition/OtherFacet.php +++ b/src/Plugin/Condition/OtherFacet.php @@ -13,6 +13,7 @@ use Drupal\Core\Condition\ConditionPluginBase; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; +use Drupal\Core\Session\AccountProxyInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -40,12 +41,21 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn protected $blockManager; /** + * The user that's currently logged in. + * + * @var \Drupal\Core\Session\AccountProxyInterface + */ + protected $currentUser; + + /** * Creates a new instance of the condition. * * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage * The entity storage. * @param \Drupal\Core\Block\BlockManager $block_manager * The block plugin manager. + * @param \Drupal\Core\Session\AccountProxyInterface $current_user + * The currently logged in user. * @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 @@ -56,10 +66,11 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn * @param mixed $plugin_definition * The plugin implementation definition. */ - public function __construct(EntityStorageInterface $entity_storage, BlockManager $block_manager, array $configuration, $plugin_id, $plugin_definition) { + public function __construct(EntityStorageInterface $entity_storage, BlockManager $block_manager, AccountProxyInterface $current_user, 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; } /** @@ -69,6 +80,7 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn return new static( $container->get('entity_type.manager')->getStorage('facets_facet'), $container->get('plugin.manager.block'), + $container->get('current_user'), $configuration, $plugin_id, $plugin_definition @@ -132,6 +144,9 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn if (empty($allowed_facets) && !$this->isNegated()) { return TRUE; } + if (empty($allowed_facets) && $this->isNegated()) { + return FALSE; + } /** @var \Drupal\facets\Plugin\Block\FacetBlock $block_plugin */ $block_plugin = $this->blockManager->createInstance($allowed_facets); @@ -139,7 +154,13 @@ class OtherFacet extends ConditionPluginBase implements ContainerFactoryPluginIn // Allowed facet value is not set, so we only have to check if the block is // shown here. if (empty($allowed_facet_value)) { - return $block_plugin->access(\Drupal::currentUser()); + $access_result = $block_plugin->access($this->currentUser); + if ($this->isNegated()) { + // A negated configuration should return the inverse of the normal + // result. + return !($access_result); + } + return $access_result; } $block_plugin_id = $block_plugin->getPluginId(); diff --git a/tests/src/Unit/Plugin/Condition/OtherFacetTest.php b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php new file mode 100644 index 0000000..92e02aa --- /dev/null +++ b/tests/src/Unit/Plugin/Condition/OtherFacetTest.php @@ -0,0 +1,113 @@ +getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $manager = $this->getMockBuilder('\Drupal\Core\Block\BlockManager') + ->disableOriginalConstructor() + ->getMock(); + $user = $this->getMock('\Drupal\Core\Session\AccountProxyInterface'); + $sut = new OtherFacet($storage, $manager, $user, ['negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertFalse($evaluation); + } + else { + $this->assertTrue($evaluation); + } + } + + /** + * Displayed facet. + * + * @dataProvider provideNegated + */ + public function testDisplayedFacet($negated) { + $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock') + ->disableOriginalConstructor() + ->getMock(); + $block->expects($this->exactly(1)) + ->method('access') + ->willReturn(TRUE); + $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'); + $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertFalse($evaluation); + } + else { + $this->assertTrue($evaluation); + } + } + + /** + * Hidden facet. + * + * @dataProvider provideNegated + */ + public function testHiddenFacet($negated) { + $block = $this->getMockBuilder('\Drupal\facets\Plugin\Block\FacetBlock') + ->disableOriginalConstructor() + ->getMock(); + $block->expects($this->exactly(1)) + ->method('access') + ->willReturn(FALSE); + $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'); + $sut = new OtherFacet($storage, $manager, $user, ['facets' => 'test', 'negate' => $negated], 'other_facet', ''); + + $evaluation = $sut->evaluate(); + if ($negated) { + $this->assertTrue($evaluation); + } + else { + $this->assertFalse($evaluation); + } + } + + /** + * @return array + */ + public function provideNegated() { + return [ + 'normal' => [FALSE], + 'negated' => [TRUE], + ]; + } +}