You are here

public function RuleExpressionTest::testDeletingExpressions in Rules 8.3

Tests that removing expressions by indices works.

File

tests/src/Unit/RuleExpressionTest.php, line 227

Class

RuleExpressionTest
@coversDefaultClass \Drupal\rules\Plugin\RulesExpression\RuleExpression @group Rules

Namespace

Drupal\Tests\rules\Unit

Code

public function testDeletingExpressions() {

  // Create a rule with 2 conditions and 2 actions.
  $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);

  // Delete the first action.
  $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()));

  // Delete the second condition.
  $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()));

  // Delete the remaining action.
  $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()));

  // Delete the remaining condition, rule should be empty now.
  $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()));
}