/** * Tests evaluating the condition. * * @covers ::evaluate */ 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); // Test with 'view', 'edit' and 'delete'. Both 'view' and 'edit' will have // general entity access, but the 'potato-field' should deny access for the // 'edit' operation. Hence, 'edit' and 'delete' should return FALSE. $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()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $entity = $this->prophesizeEntity(EntityInterface::class); $entity->isNew()->willReturn(TRUE)->shouldBeCalledTimes(1); // Add the test node to our context as the evaluated entity. $this->condition->setContextValue('entity', $entity->reveal()); $this->assertTrue($this->condition->evaluate()); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); // Create a test condition plugin that always evaluates to TRUE. $this->trueCondition = $this->prophesize(RulesConditionInterface::class); $this->trueCondition->execute()->willReturn(TRUE); $this->trueCondition->evaluate()->willReturn(TRUE); $this->conditionManager = $this->prophesize(ConditionManager::class); $this->processorManager = $this->prophesize(DataProcessorManager::class); $this->conditionExpression = new RulesCondition(['condition_id' => 'test_condition'], '', [], $this->conditionManager->reveal(), $this->processorManager->reveal()); }
/** * Tests evaluating the condition for path without an alias. * * @covers ::evaluate */ public function testConditionEvaluationPathWithoutAlias() { $this->aliasManager->getAliasByPath('path-without-alias', NULL)->willReturn('path-without-alias')->shouldBeCalledTimes(1); $this->aliasManager->getAliasByPath('path-without-alias', 'en')->willReturn('path-without-alias')->shouldBeCalledTimes(1); // First, only set the path context. $this->condition->setContextValue('path', 'path-without-alias'); // Test without language context set. $this->assertFalse($this->condition->evaluate()); // Test with language context set. $this->condition->setContextValue('language', $this->englishLanguage->reveal()); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition for an alias that can not be resolved. * * @covers ::evaluate */ public function testConditionEvaluationAliasWithoutPath() { $this->aliasManager->getPathByAlias('alias-for-path-that-does-not-exist', NULL)->willReturn('alias-for-path-that-does-not-exist')->shouldBeCalledTimes(1); $this->aliasManager->getPathByAlias('alias-for-path-that-does-not-exist', 'en')->willReturn('alias-for-path-that-does-not-exist')->shouldBeCalledTimes(1); // First, only set the path context. $this->condition->setContextValue('alias', 'alias-for-path-that-does-not-exist'); // Test without language context set. $this->assertFalse($this->condition->evaluate()); // Test with language context set. $this->condition->setContextValue('language', $this->englishLanguage->reveal()); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $entity = $this->prophesizeEntity(ContentEntityInterface::class); $entity->hasField('existing-field')->willReturn(TRUE)->shouldBeCalledTimes(1); $entity->hasField('non-existing-field')->willReturn(FALSE)->shouldBeCalledTimes(1); $this->condition->setContextValue('entity', $entity->reveal()); // Test with an existing field. $this->condition->setContextValue('field', 'existing-field'); $this->assertTrue($this->condition->evaluate()); // Test with a non-existing field. $this->condition->setContextValue('field', 'non-existing-field'); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $sticky_node = $this->prophesizeEntity(NodeInterface::class); $sticky_node->isSticky()->willReturn(TRUE)->shouldBeCalledTimes(1); // Set the node context value. $this->condition->setContextValue('node', $sticky_node->reveal()); $this->assertTrue($this->condition->evaluate()); $unsticky_node = $this->prophesizeEntity(NodeInterface::class); $unsticky_node->isSticky()->willReturn(FALSE)->shouldBeCalledTimes(1); // Set the node context value. $this->condition->setContextValue('node', $unsticky_node->reveal()); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $entity = $this->prophesizeEntity(EntityInterface::class); $entity->getEntityTypeId()->willReturn('node')->shouldBeCalledTimes(2); // Add the test node to our context as the evaluated entity, along with an // explicit entity type string. // First, test with a value that should evaluate TRUE. $this->condition->setContextValue('entity', $entity->reveal())->setContextValue('type', 'node'); $this->assertTrue($this->condition->evaluate()); // Then test with values that should evaluate FALSE. $this->condition->setContextValue('type', 'taxonomy_term'); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $blocked_user = $this->prophesizeEntity(UserInterface::class); $blocked_user->isBlocked()->willReturn(TRUE)->shouldbeCalledTimes(1); // Set the user context value. $this->condition->setContextValue('user', $blocked_user->reveal()); $this->assertTrue($this->condition->evaluate()); $active_user = $this->prophesizeEntity(UserInterface::class); $active_user->isBlocked()->willReturn(FALSE)->shouldbeCalledTimes(1); // Set the user context value. $this->condition->setContextValue('user', $active_user->reveal()); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { $node = $this->prophesizeEntity(NodeInterface::class); $node->getType()->willReturn('page'); // Set the node context value. $this->condition->setContextValue('node', $node->reveal()); // Test evaluation with a list that contains the actual node type. $this->condition->setContextValue('types', ['page', 'article']); $this->assertTrue($this->condition->evaluate()); // Test with a list that does not contain the actual node type. $this->condition->setContextValue('types', ['apple', 'banana']); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition with the "is greater than" operation. * * @covers ::evaluate */ public function testConditionEvaluationOperatorGreaterThan() { // Test that when data is greater than value and operation is '>', // TRUE is returned. $this->condition->setContextValue('data', 2)->setContextValue('operation', '>')->setContextValue('value', 1); $this->assertTrue($this->condition->evaluate()); // Test that when data is less than value and operation is '>', // FALSE is returned. $this->condition->setContextValue('data', 1)->setContextValue('operation', '>')->setContextValue('value', 2); $this->assertFalse($this->condition->evaluate()); }
/** * Test the behavior with unsupported operations. * * @covers ::execute * * @expectedException \InvalidArgumentException */ public function testInvalidOperationException() { // Set-up a mock object with roles 'authenticated' and 'editor', but not // 'administrator'. $account = $this->prophesizeEntity(UserInterface::class); $account->getRoles()->willReturn(['authenticated', 'editor']); $this->condition->setContextValue('user', $account->reveal()); $authenticated = $this->prophesize(RoleInterface::class); $authenticated->id()->willReturn('authenticated'); // Now test INVALID as operation. An exception must be thrown. $this->condition->setContextValue('roles', [$authenticated->reveal()]); $this->condition->setContextValue('operation', 'INVALID'); $this->condition->evaluate(); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { // Set-up a mock object with roles 'authenticated' and 'editor', but not // 'administrator'. $account = $this->prophesizeEntity(UserInterface::class); $account->getRoles()->willReturn(['authenticated', 'editor'])->shouldBeCalledTimes(7); $this->condition->setContextValue('user', $account->reveal()); $authenticated = $this->prophesize(RoleInterface::class); $authenticated->id()->willReturn('authenticated'); $editor = $this->prophesize(RoleInterface::class); $editor->id()->willReturn('authenticated'); $administrator = $this->prophesize(RoleInterface::class); $administrator->id()->willReturn('administrator'); // First test the default AND condition with both roles the user has. $this->condition->setContextValue('roles', [$authenticated->reveal(), $editor->reveal()]); $this->assertTrue($this->condition->evaluate()); // User doesn't have the administrator role, this should fail. $this->condition->setContextValue('roles', [$authenticated->reveal(), $administrator->reveal()]); $this->assertFalse($this->condition->evaluate()); // Only one role, should succeed. $this->condition->setContextValue('roles', [$authenticated->reveal()]); $this->assertTrue($this->condition->evaluate()); // A role the user doesn't have. $this->condition->setContextValue('roles', [$administrator->reveal()]); $this->assertFalse($this->condition->evaluate()); // Only one role, the user has with OR condition, should succeed. $this->condition->setContextValue('roles', [$authenticated->reveal()]); $this->condition->setContextValue('operation', 'OR'); $this->assertTrue($this->condition->evaluate()); // User doesn't have the administrator role, but has the authenticated, // should succeed. $this->condition->setContextValue('roles', [$authenticated->reveal(), $administrator->reveal()]); $this->condition->setContextValue('operation', 'OR'); $this->assertTrue($this->condition->evaluate()); // User doesn't have the administrator role. This should fail. $this->condition->setContextValue('roles', [$administrator->reveal()]); $this->condition->setContextValue('operation', 'OR'); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { // Test array of string values $list = ['One', 'Two', 'Three']; // Test that the list doesn't contain 'Zero'. $this->condition->setContextValue('list', $list)->setContextValue('item', 'Zero'); $this->assertFalse($this->condition->evaluate()); // Test that the list contains 'One'. $this->condition->setContextValue('list', $list)->setContextValue('item', 'One'); $this->assertTrue($this->condition->evaluate()); // Test that the list contains 'Two'. $this->condition->setContextValue('list', $list)->setContextValue('item', 'Two'); $this->assertTrue($this->condition->evaluate()); // Test that the list contains 'Three'. $this->condition->setContextValue('list', $list)->setContextValue('item', 'Three'); $this->assertTrue($this->condition->evaluate()); // Test that the list doesn't contain 'Four'. $this->condition->setContextValue('list', $list)->setContextValue('item', 'Four'); $this->assertFalse($this->condition->evaluate()); // Create array of mock entities $entity_zero = $this->prophesizeEntity(EntityInterface::class); $entity_zero->id()->willReturn('entity_zero_id'); $entity_one = $this->prophesizeEntity(EntityInterface::class); $entity_one->id()->willReturn('entity_one_id'); $entity_two = $this->prophesizeEntity(EntityInterface::class); $entity_two->id()->willReturn('entity_two_id'); $entity_three = $this->prophesizeEntity(EntityInterface::class); $entity_three->id()->willReturn('entity_three_id'); $entity_four = $this->prophesizeEntity(EntityInterface::class); $entity_four->id()->willReturn('entity_four_id'); // Test array of entities $entity_list = [$entity_one->reveal(), $entity_two->reveal(), $entity_three->reveal()]; // Test that the list of entities doesn't contain entity 'entity_zero'. $this->condition->setContextValue('list', $entity_list)->setContextValue('item', $entity_zero->reveal()); $this->assertFalse($this->condition->evaluate()); // Test that the list of entities contains entity 'entity_one'. $this->condition->setContextValue('list', $entity_list)->setContextValue('item', $entity_one->reveal()); $this->assertTrue($this->condition->evaluate()); // Test that the list of entities contains entity 'entity_two'. $this->condition->setContextValue('list', $entity_list)->setContextValue('item', $entity_two->reveal()); $this->assertTrue($this->condition->evaluate()); // Test that the list of entities contains entity 'entity_three'. $this->condition->setContextValue('list', $entity_list)->setContextValue('item', $entity_three->reveal()); $this->assertTrue($this->condition->evaluate()); // Test that the list of entities doesn't contain entity 'entity_four'. $this->condition->setContextValue('list', $entity_list)->setContextValue('item', $entity_four->reveal()); $this->assertFalse($this->condition->evaluate()); }
/** * Tests evaluating the condition. * * @covers ::evaluate */ public function testConditionEvaluation() { // Test a ComplexDataInterface object. $entity_adapter_empty = $this->prophesize(ComplexDataInterface::class); $entity_adapter_empty->isEmpty()->willReturn(TRUE)->shouldBeCalledTimes(1); $this->condition->getContext('data')->setContextData($entity_adapter_empty->reveal()); $this->assertTrue($this->condition->evaluate()); $entity_adapter_full = $this->prophesize(ComplexDataInterface::class); $entity_adapter_full->isEmpty()->willReturn(FALSE)->shouldBeCalledTimes(1); $this->condition->getContext('data')->setContextData($entity_adapter_full->reveal()); $this->assertFalse($this->condition->evaluate()); // These should all return FALSE. // A non-empty array. $this->condition->getContext('data')->setContextData($this->getTypedData('list', [1, 2, 3])); $this->assertFalse($this->condition->evaluate()); // An array containing an empty list. $this->condition->getContext('data')->setContextData($this->getTypedData('list', [[]])); $this->assertFalse($this->condition->evaluate()); // An array with a zero-value element. $this->condition->getContext('data')->setContextData($this->getTypedData('list', [0])); $this->assertFalse($this->condition->evaluate()); // A scalar value. $this->condition->getContext('data')->setContextData($this->getTypedData('integer', 1)); $this->assertFalse($this->condition->evaluate()); $this->condition->getContext('data')->setContextData($this->getTypedData('string', 'short string')); $this->assertFalse($this->condition->evaluate()); // These should all return TRUE. // An empty array. $this->condition->getContext('data')->setContextData($this->getTypedData('list', [])); $this->assertTrue($this->condition->evaluate()); // The false/zero/NULL values. $this->condition->getContext('data')->setContextData($this->getTypedData('boolean', FALSE)); $this->assertTrue($this->condition->evaluate()); $this->condition->getContext('data')->setContextData($this->getTypedData('integer', 0)); $this->assertTrue($this->condition->evaluate()); $this->condition->getContext('data')->setContextData($this->getTypedData('string', NULL)); $this->assertTrue($this->condition->evaluate()); // An empty string. $this->condition->getContext('data')->setContextData($this->getTypedData('string', '')); $this->assertTrue($this->condition->evaluate()); }