class ConditionExpressionTest in Rules 8.3
@coversDefaultClass \Drupal\rules\Plugin\RulesExpression\ConditionExpression @group Rules
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\rules\Unit\ConditionExpressionTest
Expanded class hierarchy of ConditionExpressionTest
File
- tests/
src/ Unit/ ConditionExpressionTest.php, line 21
Namespace
Drupal\Tests\rules\UnitView source
class ConditionExpressionTest extends UnitTestCase {
/**
* The mocked condition manager.
*
* @var \Drupal\rules\Core\ConditionManager|\Prophecy\Prophecy\ProphecyInterface
*/
protected $conditionManager;
/**
* The mocked data processor manager.
*
* @var \Drupal\rules\Context\DataProcessorManager|\Prophecy\Prophecy\ProphecyInterface
*/
protected $processorManager;
/**
* The condition object being tested.
*
* @var \Drupal\rules\Plugin\RulesExpression\ConditionExpression
*/
protected $conditionExpression;
/**
* A condition plugin that always evaluates to TRUE.
*
* @var \Drupal\rules\Core\RulesConditionInterface|\Prophecy\Prophecy\ProphecyInterface
*/
protected $trueCondition;
/**
* The mocked expression manager object.
*
* @var \Drupal\rules\src\Logger\|\Prophecy\Prophecy\ProphecyInterface
*/
protected $rulesDebugLogger;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Create a test condition plugin that always evaluates to TRUE.
$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());
}
/**
* Tests that context values get data processed with processor mappings.
*/
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);
// Mock some original old value that will be replaced by the data processor.
$this->trueCondition
->getContextValue('test')
->willReturn('old_value')
->shouldBeCalled();
// 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);
$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);
// Build some mocked execution state.
$state = $this
->prophesize(ExecutionStateInterface::class);
$prophecy = $state
->getVariable('test');
/** @var \Prophecy\Prophecy\MethodProphecy $prophecy */
$prophecy
->willReturn('old_value');
$this
->assertTrue($condition
->executeWithState($state
->reveal()));
}
/**
* 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);
$this->conditionManager
->getDefinition('test_condition')
->willReturn([
'label' => 'Test Condition',
])
->shouldBeCalledTimes(1);
// Create a condition which is negated.
$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());
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ConditionExpressionTest:: |
protected | property | The condition object being tested. | |
ConditionExpressionTest:: |
protected | property | The mocked condition manager. | |
ConditionExpressionTest:: |
protected | property | The mocked data processor manager. | |
ConditionExpressionTest:: |
protected | property | The mocked expression manager object. | |
ConditionExpressionTest:: |
protected | property | A condition plugin that always evaluates to TRUE. | |
ConditionExpressionTest:: |
protected | function |
Overrides UnitTestCase:: |
|
ConditionExpressionTest:: |
public | function | Tests that context values get data processed with processor mappings. | |
ConditionExpressionTest:: |
public | function | Tests that negating a condition works. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |