class DataComparisonTest in Rules 8.3
@coversDefaultClass \Drupal\rules\Plugin\Condition\DataComparison @group RulesCondition
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase
- class \Drupal\Tests\rules\Unit\Integration\Condition\DataComparisonTest
- class \Drupal\Tests\rules\Unit\Integration\RulesIntegrationTestBase
Expanded class hierarchy of DataComparisonTest
File
- tests/
src/ Unit/ Integration/ Condition/ DataComparisonTest.php, line 12
Namespace
Drupal\Tests\rules\Unit\Integration\ConditionView source
class DataComparisonTest extends RulesIntegrationTestBase {
/**
* The condition to be tested.
*
* @var \Drupal\rules\Core\RulesConditionInterface
*/
protected $condition;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->condition = $this->conditionManager
->createInstance('rules_data_comparison');
}
/**
* Tests evaluating the condition with the "equals" operator.
*
* @covers ::evaluate
*/
public function testConditionEvaluationOperatorEquals() {
// Test that when a boolean data does not equal a boolean value
// and the operator is not set - should fallback to '=='.
$this->condition
->setContextValue('data', TRUE)
->setContextValue('value', FALSE);
$this
->assertFalse($this->condition
->evaluate());
// Test that when both data and value are false booleans
// and the operator is not set - should fallback to '=='.
$this->condition
->setContextValue('data', FALSE)
->setContextValue('value', FALSE);
$this
->assertTrue($this->condition
->evaluate());
// Test that when the data string equals the value string and the operator
// is '==', TRUE is returned.
$this->condition
->setContextValue('data', 'Llama')
->setContextValue('operation', '==')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
// Test that when the data string does not equal the value string and the
// operation is '==', FALSE is returned.
$this->condition
->setContextValue('data', 'Kitten')
->setContextValue('operation', '==')
->setContextValue('value', 'Llama');
$this
->assertFalse($this->condition
->evaluate());
// Test that when both data and value are false booleans and the operation
// is '==', TRUE is returned.
$this->condition
->setContextValue('data', FALSE)
->setContextValue('operation', '==')
->setContextValue('value', FALSE);
$this
->assertTrue($this->condition
->evaluate());
// Test that when a boolean data does not equal a boolean value
// and the operation is '==', FALSE is returned.
$this->condition
->setContextValue('data', TRUE)
->setContextValue('operation', '==')
->setContextValue('value', FALSE);
$this
->assertFalse($this->condition
->evaluate());
}
/**
* Tests evaluating the condition with the "contains" operation.
*
* @covers ::evaluate
*/
public function testConditionEvaluationOperatorContains() {
// Test that when the data string contains the value string, and the
// operation is 'CONTAINS', TRUE is returned.
$this->condition
->setContextValue('data', 'Big Llama')
->setContextValue('operation', 'contains')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
// Test that when the data string does not contain the value string, and
// the operation is 'contains', TRUE is returned.
$this->condition
->setContextValue('data', 'Big Kitten')
->setContextValue('operation', 'contains')
->setContextValue('value', 'Big Kitten');
$this
->assertTrue($this->condition
->evaluate());
// Test that when a data array contains the value string, and the operation
// is 'CONTAINS', TRUE is returned.
$this->condition
->setContextValue('data', [
'Llama',
'Kitten',
])
->setContextValue('operation', 'contains')
->setContextValue('value', 'Llama');
$this
->assertTrue($this->condition
->evaluate());
// Test that when a data array does not contain the value array, and the
// operation is 'CONTAINS', TRUE is returned.
$this->condition
->setContextValue('data', [
'Kitten',
])
->setContextValue('operation', 'contains')
->setContextValue('value', [
'Llama',
]);
$this
->assertFalse($this->condition
->evaluate());
}
/**
* Tests evaluating the condition with the "IN" operation.
*
* @covers ::evaluate
*/
public function testConditionEvaluationOperatorIn() {
// Test that when the data string is 'IN' the value array, TRUE is returned.
$this->condition
->setContextValue('data', 'Llama')
->setContextValue('operation', 'IN')
->setContextValue('value', [
'Llama',
'Kitten',
]);
$this
->assertTrue($this->condition
->evaluate());
// Test that when the data array is not in the value array, and the
// operation is 'IN', FALSE is returned.
$this->condition
->setContextValue('data', [
'Llama',
])
->setContextValue('operation', 'IN')
->setContextValue('value', [
'Kitten',
]);
$this
->assertFalse($this->condition
->evaluate());
}
/**
* Tests evaluating the condition with the "is less than" operation.
*
* @covers ::evaluate
*/
public function testConditionEvaluationOperatorLessThan() {
// Test that when data is less than value and operation is '<',
// TRUE is returned.
$this->condition
->setContextValue('data', 1)
->setContextValue('operation', '<')
->setContextValue('value', 2);
$this
->assertTrue($this->condition
->evaluate());
// Test that when data is greater than value and operation is '<',
// FALSE is returned.
$this->condition
->setContextValue('data', 2)
->setContextValue('operation', '<')
->setContextValue('value', 1);
$this
->assertFalse($this->condition
->evaluate());
}
/**
* Tests evaluating the condition with the "is greater than" operation.
*
* @covers ::evaluate
*/
public function testConditionEvaluationOperatorGreaterThan() {
// Test that when data is greater than value and operation is '>',
// TRUE is returned.
$this->condition
->setContextValue('data', 2)
->setContextValue('operation', '>')
->setContextValue('value', 1);
$this
->assertTrue($this->condition
->evaluate());
// Test that when data is less than value and operation is '>',
// FALSE is returned.
$this->condition
->setContextValue('data', 1)
->setContextValue('operation', '>')
->setContextValue('value', 2);
$this
->assertFalse($this->condition
->evaluate());
}
/**
* Tests the summary.
*
* @covers ::summary
*/
public function testSummary() {
$this
->assertEquals('Data comparison', $this->condition
->summary());
}
/**
* @covers ::refineContextDefinitions
*/
public function testRefineContextDefinitions() {
// When a string is selected for comparison, the value must be string also.
$this->condition
->refineContextDefinitions([
'data' => DataDefinition::create('string'),
]);
$this
->assertEquals('string', $this->condition
->getContextDefinition('value')
->getDataType());
// IN operation requires a list of strings as value.
$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());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DataComparisonTest:: |
protected | property | The condition to be tested. | |
DataComparisonTest:: |
protected | function |
Overrides RulesIntegrationTestBase:: |
|
DataComparisonTest:: |
public | function | Tests evaluating the condition with the "contains" operation. | |
DataComparisonTest:: |
public | function | Tests evaluating the condition with the "equals" operator. | |
DataComparisonTest:: |
public | function | Tests evaluating the condition with the "is greater than" operation. | |
DataComparisonTest:: |
public | function | Tests evaluating the condition with the "IN" operation. | |
DataComparisonTest:: |
public | function | Tests evaluating the condition with the "is less than" operation. | |
DataComparisonTest:: |
public | function | @covers ::refineContextDefinitions | |
DataComparisonTest:: |
public | function | Tests the summary. | |
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. | |
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | The class resolver mock for the typed data manager. | |
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | The Drupal service container. | |
RulesIntegrationTestBase:: |
protected | property | The data fetcher service. | |
RulesIntegrationTestBase:: |
protected | property | The data filter manager. | |
RulesIntegrationTestBase:: |
protected | property | Array object keyed with module names and TRUE as value. | |
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | A mocked Rules logger.channel.rules_debug service. | 6 |
RulesIntegrationTestBase:: |
protected | property | The messenger service. | |
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | All setup'ed namespaces. | |
RulesIntegrationTestBase:: |
protected | property | The placeholder resolver service. | |
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | property | ||
RulesIntegrationTestBase:: |
protected | function | Determines the path to a module's class files. | |
RulesIntegrationTestBase:: |
protected | function | Fakes the enabling of a module and adds its namespace for plugin loading. | |
RulesIntegrationTestBase:: |
protected | function | Returns a typed data object. | |
RulesIntegrationTestBase:: |
protected | function | Helper method to mock irrelevant cache methods on entities. | |
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. |