UserHasEntityFieldAccessTest.php in Rules 8.3
File
tests/src/Unit/Integration/Condition/UserHasEntityFieldAccessTest.php
View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Condition;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Tests\rules\Unit\Integration\RulesEntityIntegrationTestBase;
use Drupal\user\UserInterface;
class UserHasEntityFieldAccessTest extends RulesEntityIntegrationTestBase {
protected $condition;
protected function setUp() : void {
parent::setUp();
$this
->enableModule('user');
$this->condition = $this->conditionManager
->createInstance('rules_entity_field_access');
}
public function testConditionEvaluation() {
$account = $this
->prophesizeEntity(UserInterface::class);
$entity = $this
->prophesizeEntity(ContentEntityInterface::class);
$items = $this
->prophesize(FieldItemListInterface::class);
$entity
->getEntityTypeId()
->willReturn('user');
$entity
->hasField('potato-field')
->willReturn(TRUE)
->shouldBeCalledTimes(3);
$definition = $this
->prophesize(FieldDefinitionInterface::class);
$entity
->getFieldDefinition('potato-field')
->willReturn($definition
->reveal())
->shouldBeCalledTimes(2);
$entity
->get('potato-field')
->willReturn($items
->reveal())
->shouldBeCalledTimes(2);
$this->condition
->setContextValue('entity', $entity
->reveal())
->setContextValue('field', 'potato-field')
->setContextValue('user', $account
->reveal());
$this->entityAccess
->access($entity
->reveal(), 'view', $account
->reveal())
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$this->entityAccess
->access($entity
->reveal(), 'edit', $account
->reveal())
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$this->entityAccess
->access($entity
->reveal(), 'delete', $account
->reveal())
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$this->entityAccess
->fieldAccess('view', $definition
->reveal(), $account
->reveal(), $items
->reveal())
->willReturn(TRUE)
->shouldBeCalledTimes(1);
$this->entityAccess
->fieldAccess('edit', $definition
->reveal(), $account
->reveal(), $items
->reveal())
->willReturn(FALSE)
->shouldBeCalledTimes(1);
$this->condition
->setContextValue('operation', 'view');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('operation', 'edit');
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('operation', 'delete');
$this
->assertFalse($this->condition
->evaluate());
}
}