/** * 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()); }
/** * 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()); }
/** * @covers ::refineContextDefinitions */ public function testRefineContextDefinitions() { // When a string is selected for comparison, the value must be string also. $this->condition->refineContextDefinitions(['data' => DataDefinition::create('string')]); $this->assertEquals('string', $this->condition->getContextDefinition('value')->getDataType()); // IN operation requires a list of strings as value. $this->condition->setContextValue('operation', 'IN'); $this->condition->refineContextDefinitions(['data' => DataDefinition::create('string')]); $this->assertEquals('string', $this->condition->getContextDefinition('value')->getDataType()); $this->assertTrue($this->condition->getContextDefinition('value')->isMultiple()); }
/** * Tests evaluating the condition with the "is greater than" operator. * * @covers ::evaluate */ public function testConditionEvaluationOperatorGreaterThan() { // Test that when data is greater than value and operator is '>', // TRUE is returned. $this->condition->setContextValue('data', 2)->setContextValue('operator', '>')->setContextValue('value', 1); $this->assertTrue($this->condition->evaluate()); // Test that when data is less than value and operator is '>', // FALSE is returned. $this->condition->setContextValue('data', 1)->setContextValue('operator', '>')->setContextValue('value', 2); $this->assertFalse($this->condition->evaluate()); }
/** * Tests that context values get data processed with processor mappings. */ public function testDataProcessor() { $this->conditionManager->createInstance('test_condition', ['negate' => FALSE])->willReturn($this->trueCondition->reveal())->shouldBeCalledTimes(1); $condition = new RulesCondition(['condition_id' => 'test_condition'] + ContextConfig::create()->process('test', 'foo', [])->toArray(), '', [], $this->conditionManager->reveal(), $this->processorManager->reveal()); $this->trueCondition->getContextDefinitions()->willReturn(['test' => $this->prophesize(ContextDefinitionInterface::class)->reveal()])->shouldBeCalledTimes(1); $this->trueCondition->getProvidedContextDefinitions()->willReturn([])->shouldBeCalledTimes(1); // Mock some original old value that will be replaced by the data processor. $this->trueCondition->getContextValue('test')->willReturn('old_value')->shouldBeCalled(); // The outcome of the data processor needs to get set on the condition. $this->trueCondition->setContextValue('test', 'new_value')->shouldBeCalledTimes(1); $this->trueCondition->refineContextDefinitions([])->shouldBeCalledTimes(1); $data_processor = $this->prophesize(DataProcessorInterface::class); $data_processor->process('old_value', Argument::any())->willReturn('new_value')->shouldBeCalledTimes(1); $this->processorManager->createInstance('foo', [])->willReturn($data_processor->reveal())->shouldBeCalledTimes(1); // Build some mocked execution state. $state = $this->prophesize(ExecutionStateInterface::class); $prophecy = $state->getVariable('test'); /** @var \Prophecy\Prophecy\MethodProphecy $prophecy */ $prophecy->willReturn('old_value'); $this->assertTrue($condition->executeWithState($state->reveal())); }
/** * 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() { // Test that the list count is greater than 2. $condition = $this->condition->setContextValue('list', [1, 2, 3, 4])->setContextValue('operator', '>')->setContextValue('value', '2'); $this->assertTrue($condition->evaluate()); // Test that the list count is less than 4. $condition = $this->condition->setContextValue('list', [1, 2, 3])->setContextValue('operator', '<')->setContextValue('value', '4'); $this->assertTrue($condition->evaluate()); // Test that the list count is equal to 3. $condition = $this->condition->setContextValue('list', [1, 2, 3])->setContextValue('operator', '==')->setContextValue('value', '3'); $this->assertTrue($condition->evaluate()); // Test that the list count is equal to 0. $condition = $this->condition->setContextValue('list', [])->setContextValue('operator', '==')->setContextValue('value', '0'); $this->assertTrue($condition->evaluate()); // Test that the list count is not less than 2. $condition = $this->condition->setContextValue('list', [1, 2])->setContextValue('operator', '<')->setContextValue('value', '2'); $this->assertFalse($condition->evaluate()); // Test that list count is not greater than 5. $condition = $this->condition->setContextValue('list', [1, 2, 3])->setContextValue('operator', '>')->setContextValue('value', '5'); $this->assertFalse($condition->evaluate()); // Test that the list count is not equal to 0. $condition = $this->condition->setContextValue('list', [1, 2, 3])->setContextValue('operator', '==')->setContextValue('value', '0'); $this->assertFalse($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() { // 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()); }