View source
<?php
namespace Drupal\Tests\rules\Unit\Integration\Condition;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase;
class DataComparisonTest extends RulesIntegrationTestBase {
protected $condition;
protected function setUp() : void {
parent::setUp();
$this->condition = $this->conditionManager
->createInstance('rules_data_comparison');
}
public function testConditionEvaluationOperatorEquals() {
$this->condition
->setContextValue('data', TRUE)
->setContextValue('value', FALSE);
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('data', FALSE)
->setContextValue('value', FALSE);
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', 'Llama')
->setContextValue('operation', '==')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', 'Kitten')
->setContextValue('operation', '==')
->setContextValue('value', 'Llama');
$this
->assertFalse($this->condition
->evaluate());
$this->condition
->setContextValue('data', FALSE)
->setContextValue('operation', '==')
->setContextValue('value', FALSE);
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', TRUE)
->setContextValue('operation', '==')
->setContextValue('value', FALSE);
$this
->assertFalse($this->condition
->evaluate());
}
public function testConditionEvaluationOperatorContains() {
$this->condition
->setContextValue('data', 'Big Llama')
->setContextValue('operation', 'contains')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', 'Big Kitten')
->setContextValue('operation', 'contains')
->setContextValue('value', 'Big Kitten');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', [
'Llama',
'Kitten',
])
->setContextValue('operation', 'contains')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', [
'Kitten',
])
->setContextValue('operation', 'contains')
->setContextValue('value', [
'Llama',
]);
$this
->assertFalse($this->condition
->evaluate());
}
public function testConditionEvaluationOperatorIn() {
$this->condition
->setContextValue('data', 'Llama')
->setContextValue('operation', 'IN')
->setContextValue('value', [
'Llama',
'Kitten',
]);
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', [
'Llama',
])
->setContextValue('operation', 'IN')
->setContextValue('value', [
'Kitten',
]);
$this
->assertFalse($this->condition
->evaluate());
}
public function testConditionEvaluationOperatorLessThan() {
$this->condition
->setContextValue('data', 1)
->setContextValue('operation', '<')
->setContextValue('value', 2);
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', 2)
->setContextValue('operation', '<')
->setContextValue('value', 1);
$this
->assertFalse($this->condition
->evaluate());
}
public function testConditionEvaluationOperatorGreaterThan() {
$this->condition
->setContextValue('data', 2)
->setContextValue('operation', '>')
->setContextValue('value', 1);
$this
->assertTrue($this->condition
->evaluate());
$this->condition
->setContextValue('data', 1)
->setContextValue('operation', '>')
->setContextValue('value', 2);
$this
->assertFalse($this->condition
->evaluate());
}
public function testSummary() {
$this
->assertEquals('Data comparison', $this->condition
->summary());
}
public function testRefineContextDefinitions() {
$this->condition
->refineContextDefinitions([
'data' => DataDefinition::create('string'),
]);
$this
->assertEquals('string', $this->condition
->getContextDefinition('value')
->getDataType());
$this->condition
->setContextValue('operation', 'IN');
$this->condition
->refineContextDefinitions([
'data' => DataDefinition::create('string'),
]);
$this
->assertEquals('string', $this->condition
->getContextDefinition('value')
->getDataType());
$this
->assertTrue($this->condition
->getContextDefinition('value')
->isMultiple());
}
}