コード例 #1
0
 /**
  * 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());
 }
コード例 #2
0
 /**
  * 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());
 }
コード例 #3
0
 /**
  * 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());
 }
コード例 #4
0
 /**
  * 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());
 }
コード例 #5
0
 /**
  * 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());
 }
コード例 #6
0
 /**
  * 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());
 }
コード例 #7
0
 /**
  * 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());
 }
コード例 #8
0
 /**
  * Tests that negating a condition works.
  */
 public function testNegation()
 {
     $this->trueCondition->getContextDefinitions()->willReturn([]);
     $this->trueCondition->refineContextDefinitions([])->shouldBeCalledTimes(1);
     $this->trueCondition->getProvidedContextDefinitions()->willReturn([])->shouldBeCalledTimes(1);
     $this->conditionManager->createInstance('test_condition', ['negate' => TRUE])->willReturn($this->trueCondition->reveal())->shouldBeCalledTimes(1);
     // Create a condition which is negated.
     $condition_expression = new RulesCondition(['condition_id' => 'test_condition', 'negate' => TRUE], '', [], $this->conditionManager->reveal(), $this->processorManager->reveal());
     $this->assertFalse($condition_expression->execute());
 }
コード例 #9
0
 /**
  * Tests that context values get data processed with processor mappings.
  */
 public function testDataProcessor()
 {
     $condition = new RulesCondition(['condition_id' => 'test_condition'] + ContextConfig::create()->process('test', 'foo', [])->toArray(), '', [], $this->conditionManager->reveal(), $this->processorManager->reveal());
     // Build some mocked context and definitions for our mock condition.
     $context = $this->prophesize(ContextInterface::class);
     $condition->setContext('test', $context->reveal());
     $this->trueCondition->getContextDefinitions()->willReturn(['test' => $this->prophesize(ContextDefinitionInterface::class)->reveal()])->shouldBeCalledTimes(2);
     $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')->shouldBeCalledTimes(1);
     // 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);
     $this->conditionManager->createInstance('test_condition', ['negate' => FALSE])->willReturn($this->trueCondition->reveal())->shouldBeCalledTimes(1);
     $this->conditionManager->createInstance('test_condition')->willReturn($this->trueCondition->reveal())->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);
     $this->assertTrue($condition->execute());
 }
コード例 #10
0
 /**
  * @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());
 }
コード例 #11
0
 /**
  * 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();
 }
コード例 #12
0
 /**
  * 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());
 }
コード例 #13
0
 /**
  * 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());
 }
コード例 #14
0
 /**
  * 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());
 }
コード例 #15
0
 /**
  * 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());
 }
コード例 #16
0
 /**
  * Tests the summary.
  *
  * @covers ::summary
  */
 public function testSummary()
 {
     $this->assertEquals('Data comparison', $this->condition->summary());
 }
コード例 #17
0
 /**
  * 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());
 }
コード例 #18
0
 /**
  * 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());
 }