View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Engine\ConditionExpressionContainer;
use Drupal\rules\Engine\ExpressionManagerInterface;
class RulesConditionContainerTest extends RulesUnitTestBase {
protected function getMockConditionContainer(array $methods = [], $class = 'RulesConditionContainerMock') {
return $this
->getMockForAbstractClass(ConditionExpressionContainer::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], $class, TRUE, TRUE, TRUE, $methods);
}
public function testAddExpressionObject() {
$container = $this
->getMockConditionContainer();
$container
->addExpressionObject($this->trueConditionExpression
->reveal());
$property = new \ReflectionProperty($container, 'conditions');
$property
->setAccessible(TRUE);
$this
->assertEquals([
$this->trueConditionExpression
->reveal(),
], array_values($property
->getValue($container)));
}
public function testNegate() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [], '', FALSE);
$this
->assertFalse($container
->isNegated());
$this
->assertTrue($container
->execute());
$container
->negate(TRUE);
$this
->assertTrue($container
->isNegated());
$this
->assertFalse($container
->execute());
}
public function testExecute() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [], '', FALSE);
$this
->assertTrue($container
->execute());
}
public function testLookupExpression() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$container
->addExpressionObject($this->trueConditionExpression
->reveal());
$uuid = $this->trueConditionExpression
->reveal()
->getUuid();
$this
->assertSame($this->trueConditionExpression
->reveal(), $container
->getExpression($uuid));
$this
->assertFalse($container
->getExpression('invalid UUID'));
}
public function testLookupNestedExpression() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$container
->addExpressionObject($this->trueConditionExpression
->reveal());
$nested_container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$nested_container
->addExpressionObject($this->falseConditionExpression
->reveal());
$container
->addExpressionObject($nested_container);
$uuid = $this->falseConditionExpression
->reveal()
->getUuid();
$this
->assertSame($this->falseConditionExpression
->reveal(), $container
->getExpression($uuid));
}
public function testDeletingCondition() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$container
->addExpressionObject($this->trueConditionExpression
->reveal());
$container
->addExpressionObject($this->falseConditionExpression
->reveal());
$uuid = $this->trueConditionExpression
->reveal()
->getUuid();
$this
->assertTrue($container
->deleteExpression($uuid));
foreach ($container as $condition) {
$this
->assertSame($this->falseConditionExpression
->reveal(), $condition);
}
$this
->assertFalse($container
->deleteExpression('invalid UUID'));
}
public function testDeletingNestedCondition() {
$container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$container
->addExpressionObject($this->trueConditionExpression
->reveal());
$nested_container = $this
->getMockForAbstractClass(RulesConditionContainerTestStub::class, [
[],
'test_id',
[],
$this
->prophesize(ExpressionManagerInterface::class)
->reveal(),
$this
->prophesize(LoggerChannelInterface::class)
->reveal(),
], '', TRUE);
$nested_container
->addExpressionObject($this->falseConditionExpression
->reveal());
$container
->addExpressionObject($nested_container);
$uuid = $this->falseConditionExpression
->reveal()
->getUuid();
$this
->assertTrue($container
->deleteExpression($uuid));
$this
->assertEquals(0, count($nested_container
->getIterator()));
}
}
abstract class RulesConditionContainerTestStub extends ConditionExpressionContainer {
public function evaluate(ExecutionStateInterface $state) {
return TRUE;
}
}