View source
<?php
namespace Drupal\Tests\rules\Unit;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Plugin\Context\ContextDefinitionInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\rules\Context\DataProcessorInterface;
use Drupal\rules\Context\ContextConfig;
use Drupal\rules\Context\DataProcessorManager;
use Drupal\rules\Context\ExecutionStateInterface;
use Drupal\rules\Core\ConditionManager;
use Drupal\rules\Core\RulesConditionInterface;
use Drupal\rules\Plugin\RulesExpression\ConditionExpression;
use Prophecy\Argument;
class ConditionExpressionTest extends UnitTestCase {
protected $conditionManager;
protected $processorManager;
protected $conditionExpression;
protected $trueCondition;
protected $rulesDebugLogger;
protected function setUp() : void {
parent::setUp();
$this->trueCondition = $this
->prophesize(RulesConditionInterface::class);
$this->trueCondition
->execute()
->willReturn(TRUE);
$this->trueCondition
->evaluate()
->willReturn(TRUE);
$this->conditionManager = $this
->prophesize(ConditionManager::class);
$this->processorManager = $this
->prophesize(DataProcessorManager::class);
$this->rulesDebugLogger = $this
->prophesize(LoggerChannelInterface::class);
$this->conditionExpression = new ConditionExpression([
'condition_id' => 'test_condition',
], '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
}
public function testDataProcessor() {
$this->conditionManager
->createInstance('test_condition', [
'negate' => FALSE,
])
->willReturn($this->trueCondition
->reveal())
->shouldBeCalledTimes(1);
$this->conditionManager
->getDefinition('test_condition')
->willReturn([
'label' => 'Test Condition',
])
->shouldBeCalledTimes(1);
$condition = new ConditionExpression([
'condition_id' => 'test_condition',
] + ContextConfig::create()
->process('test', 'foo', [])
->toArray(), '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
$this->trueCondition
->getContextDefinitions()
->willReturn([
'test' => $this
->prophesize(ContextDefinitionInterface::class)
->reveal(),
])
->shouldBeCalledTimes(1);
$this->trueCondition
->getContextDefinition('test')
->willReturn($this
->prophesize(ContextDefinitionInterface::class)
->reveal())
->shouldBeCalledTimes(1);
$this->trueCondition
->getProvidedContextDefinitions()
->willReturn([])
->shouldBeCalledTimes(1);
$this->trueCondition
->getContextValue('test')
->willReturn('old_value')
->shouldBeCalled();
$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);
$state = $this
->prophesize(ExecutionStateInterface::class);
$prophecy = $state
->getVariable('test');
$prophecy
->willReturn('old_value');
$this
->assertTrue($condition
->executeWithState($state
->reveal()));
}
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);
$this->conditionManager
->getDefinition('test_condition')
->willReturn([
'label' => 'Test Condition',
])
->shouldBeCalledTimes(1);
$condition_expression = new ConditionExpression([
'condition_id' => 'test_condition',
'negate' => TRUE,
], '', [
'label' => 'Test Condition',
], $this->conditionManager
->reveal(), $this->processorManager
->reveal(), $this->rulesDebugLogger
->reveal());
$condition_expression
->setStringTranslation($this
->getStringTranslationStub());
$this
->assertFalse($condition_expression
->execute());
}
}