class DataComparisonTest

@coversDefaultClass \Drupal\rules\Plugin\Condition\DataComparison @group RulesCondition

Hierarchy

Expanded class hierarchy of DataComparisonTest

File

tests/src/Unit/Integration/Condition/DataComparisonTest.php, line 12

Namespace

Drupal\Tests\rules\Unit\Integration\Condition
View 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

Title Sort descending Deprecated Modifiers Object type Summary Overriden Title Overrides
DataComparisonTest::$condition protected property The condition to be tested.
DataComparisonTest::setUp protected function Overrides RulesIntegrationTestBase::setUp
DataComparisonTest::testConditionEvaluationOperatorContains public function Tests evaluating the condition with the &quot;contains&quot; operation.
DataComparisonTest::testConditionEvaluationOperatorEquals public function Tests evaluating the condition with the &quot;equals&quot; operator.
DataComparisonTest::testConditionEvaluationOperatorGreaterThan public function Tests evaluating the condition with the &quot;is greater than&quot; operation.
DataComparisonTest::testConditionEvaluationOperatorIn public function Tests evaluating the condition with the &quot;IN&quot; operation.
DataComparisonTest::testConditionEvaluationOperatorLessThan public function Tests evaluating the condition with the &quot;is less than&quot; operation.
DataComparisonTest::testRefineContextDefinitions public function @covers ::refineContextDefinitions
DataComparisonTest::testSummary public function Tests the summary.
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
RulesIntegrationTestBase::$actionManager protected property
RulesIntegrationTestBase::$cacheBackend protected property
RulesIntegrationTestBase::$classResolver protected property The class resolver mock for the typed data manager.
RulesIntegrationTestBase::$conditionManager protected property
RulesIntegrationTestBase::$container protected property The Drupal service container.
RulesIntegrationTestBase::$dataFetcher protected property The data fetcher service.
RulesIntegrationTestBase::$dataFilterManager protected property The data filter manager.
RulesIntegrationTestBase::$enabledModules protected property Array object keyed with module names and TRUE as value.
RulesIntegrationTestBase::$entityFieldManager protected property
RulesIntegrationTestBase::$entityTypeBundleInfo protected property
RulesIntegrationTestBase::$entityTypeManager protected property
RulesIntegrationTestBase::$fieldTypeCategoryManager protected property The field type category info plugin manager.
RulesIntegrationTestBase::$logger protected property A mocked Rules logger.channel.rules_debug service. 6
RulesIntegrationTestBase::$messenger protected property The messenger service.
RulesIntegrationTestBase::$moduleHandler protected property
RulesIntegrationTestBase::$namespaces protected property All setup&#039;ed namespaces.
RulesIntegrationTestBase::$placeholderResolver protected property The placeholder resolver service.
RulesIntegrationTestBase::$rulesDataProcessorManager protected property
RulesIntegrationTestBase::$rulesExpressionManager protected property
RulesIntegrationTestBase::$typedDataManager protected property
RulesIntegrationTestBase::constructModulePath protected function Determines the path to a module&#039;s class files.
RulesIntegrationTestBase::enableModule protected function Fakes the enabling of a module and adds its namespace for plugin loading.
RulesIntegrationTestBase::getTypedData protected function Returns a typed data object.
RulesIntegrationTestBase::prophesizeEntity protected function Helper method to mock irrelevant cache methods on entities.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals Deprecated protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUpBeforeClass public static function