View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Engine\ConditionExpressionInterface;
use Drupal\rules\Engine\ExpressionManagerInterface;
use Drupal\rules\Plugin\RulesExpression\ActionSetExpression;
use Drupal\rules\Plugin\RulesExpression\RuleExpression;
use Drupal\rules\Plugin\RulesExpression\ActionExpression;
use Drupal\rules\Plugin\RulesExpression\AndExpression;
use Drupal\rules\Plugin\RulesExpression\OrExpression;
use Prophecy\Argument;
class RuleExpressionTest extends RulesUnitTestBase {
protected $expressionManager;
protected $rule;
protected $conditions;
protected $actions;
protected function setUp() : void {
parent::setUp();
$this->expressionManager = $this
->prophesize(ExpressionManagerInterface::class);
$this->conditions = new AndExpression([], 'rules_and', [
'label' => 'Condition set (AND)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->expressionManager
->createInstance('rules_and', [])
->willReturn($this->conditions);
$this->actions = new ActionSetExpression([], 'rules_action_set', [], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->expressionManager
->createInstance('rules_action_set', [])
->willReturn($this->actions);
$this->rule = new RuleExpression([], 'rules_rule', [
'label' => 'Rule',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
public function testContainersOnConstruct() {
$this
->assertSame($this->conditions, $this->rule
->getConditions());
$this
->assertSame($this->actions, $this->rule
->getActions());
}
public function testSetConditionsGetConditions() {
$or = new OrExpression([], 'rules_or', [
'label' => 'Condition set (OR)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->rule
->setConditions($or);
$this
->assertSame($or, $this->rule
->getConditions());
$and = new AndExpression([], 'rules_and', [
'label' => 'Condition set (AND)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->rule
->setConditions($and);
$this
->assertSame($and, $this->rule
->getConditions());
}
public function testSetActionsGetActions() {
$action_set = new ActionSetExpression([], '', [], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->rule
->setActions($action_set);
$this
->assertSame($action_set, $this->rule
->getActions());
}
public function testActionExecution() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($this->testActionExpression
->reveal())
->execute();
}
public function testConditionFails() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldNotBeCalled();
$this->rule
->addExpressionObject($this->falseConditionExpression
->reveal())
->addExpressionObject($this->testActionExpression
->reveal())
->execute();
}
public function testTwoConditionsTrue() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$this->trueConditionExpression
->getWeight()
->willReturn(0);
$second_condition = $this
->prophesize(ConditionExpressionInterface::class);
$second_condition
->getUuid()
->willReturn('true_uuid2');
$second_condition
->getWeight()
->willReturn(0);
$second_condition
->executeWithState(Argument::type(ExecutionStateInterface::class))
->willReturn(TRUE);
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($second_condition
->reveal())
->addExpressionObject($this->testActionExpression
->reveal())
->execute();
}
public function testTwoConditionsFalse() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldNotBeCalled();
$this->testActionExpression
->getWeight()
->willReturn(0);
$this->trueConditionExpression
->getWeight()
->willReturn(0);
$this->falseConditionExpression
->getWeight()
->willReturn(0);
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($this->falseConditionExpression
->reveal())
->addExpressionObject($this->testActionExpression
->reveal())
->execute();
}
public function testNestedRules() {
$this->testActionExpression
->executeWithState(Argument::type(ExecutionStateInterface::class))
->shouldBeCalledTimes(1);
$nested = new RuleExpression([], 'rules_rule', [
'label' => 'Rule',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal());
$nested
->setConditions(new AndExpression([], 'rules_and', [
'label' => 'Condition set (AND)',
], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal()));
$nested
->setActions(new ActionSetExpression([], 'rules_action_set', [], $this->expressionManager
->reveal(), $this->rulesDebugLogger
->reveal()));
$nested
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($this->testActionExpression
->reveal());
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal())
->addExpressionObject($nested)
->execute();
}
public function testLookupExpression() {
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal());
$uuid = $this->trueConditionExpression
->reveal()
->getUuid();
$this
->assertSame($this->trueConditionExpression
->reveal(), $this->rule
->getExpression($uuid));
$this->rule
->addExpressionObject($this->testActionExpression
->reveal());
$uuid = $this->testActionExpression
->reveal()
->getUuid();
$this
->assertSame($this->testActionExpression
->reveal(), $this->rule
->getExpression($uuid));
$this
->assertFalse($this->rule
->getExpression('invalid UUID'));
}
public function testDeletingExpressions() {
$this->rule
->addExpressionObject($this->trueConditionExpression
->reveal());
$this->rule
->addExpressionObject($this->falseConditionExpression
->reveal());
$this->rule
->addExpressionObject($this->testActionExpression
->reveal());
$second_action = $this
->prophesize(ActionExpression::class);
$second_action
->getUuid()
->willReturn('action_uuid2');
$this->rule
->addExpressionObject($second_action
->reveal());
$this->trueConditionExpression
->getWeight()
->willReturn(0);
$this->falseConditionExpression
->getWeight()
->willReturn(0);
$this->testActionExpression
->getWeight()
->willReturn(0);
$second_action
->getWeight()
->willReturn(0);
$uuid = $this->testActionExpression
->reveal()
->getUuid();
$this->rule
->deleteExpression($uuid);
$this
->assertEquals(2, count($this->rule
->getConditions()
->getIterator()));
$this
->assertEquals(1, count($this->rule
->getActions()
->getIterator()));
$uuid = $this->falseConditionExpression
->reveal()
->getUuid();
$this->rule
->deleteExpression($uuid);
$this
->assertEquals(1, count($this->rule
->getConditions()
->getIterator()));
$this
->assertEquals(1, count($this->rule
->getActions()
->getIterator()));
$uuid = $second_action
->reveal()
->getUuid();
$this->rule
->deleteExpression($uuid);
$this
->assertEquals(1, count($this->rule
->getConditions()
->getIterator()));
$this
->assertEquals(0, count($this->rule
->getActions()
->getIterator()));
$uuid = $this->trueConditionExpression
->reveal()
->getUuid();
$this->rule
->deleteExpression($uuid);
$this
->assertEquals(0, count($this->rule
->getConditions()
->getIterator()));
$this
->assertEquals(0, count($this->rule
->getActions()
->getIterator()));
}
}