Loading modules/base/src/Plugin/ECA/Condition/EcaState.php +2 −2 Original line number Diff line number Diff line Loading @@ -18,14 +18,14 @@ class EcaState extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { protected function getLeftValue(): string { return $this->state->get($this->configuration['key'], ''); } /** * {@inheritdoc} */ protected function getSecondValue(): string { protected function getRightValue(): string { return $this->configuration['value']; } Loading modules/base/src/Plugin/ECA/Condition/ScalarComparison.php +8 −8 Original line number Diff line number Diff line Loading @@ -18,15 +18,15 @@ class ScalarComparison extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { return $this->configuration['right'] ?? ''; protected function getLeftValue(): string { return $this->configuration['left'] ?? ''; } /** * {@inheritdoc} */ protected function getSecondValue(): string { return $this->configuration['left'] ?? ''; protected function getRightValue(): string { return $this->configuration['right'] ?? ''; } /** Loading @@ -43,16 +43,16 @@ class ScalarComparison extends StringComparisonBase { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $form['right'] = [ $form['left'] = [ '#type' => 'textarea', '#title' => $this->t('First value'), '#default_value' => $this->getSecondValue(), '#default_value' => $this->getLeftValue(), '#weight' => -10, ]; $form['left'] = [ $form['right'] = [ '#type' => 'textarea', '#title' => $this->t('Second value'), '#default_value' => $this->getFirstValue(), '#default_value' => $this->getRightValue(), '#weight' => -8, ]; return parent::buildConfigurationForm($form, $form_state); Loading modules/base/tests/src/Kernel/CompareScalarTest.php 0 → 100644 +206 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\eca_base\Kernel; use Drupal\eca\Plugin\ECA\Condition\StringComparisonBase; use Drupal\eca\PluginManager\Condition; use Drupal\eca\Service\Conditions; use Drupal\KernelTests\KernelTestBase; /** * Kernel tests for the "eca_scalar" condition plugin. * * @group eca * @group eca_base */ class CompareScalarTest extends KernelTestBase { protected static $modules = [ 'eca', 'eca_base', ]; /** * @var \Drupal\eca\PluginManager\Condition|null */ protected ?Condition $conditionManager; /** * {@inheritdoc} */ public function setUp(): void { parent::setUp(); $this->installConfig(static::$modules); $this->conditionManager = \Drupal::service('plugin.manager.eca.condition'); } /** * Tests scalar value comparison. * * @dataProvider stringDataProvider * @dataProvider integerDataProvider */ public function testScalarValues($left, $right, $operator, $type, $case, $negate, $message): void { // Configure default settings for condition. $config = [ 'left' => $left, 'right' => $right, 'operator' => $operator, 'type' => $type, 'case' => $case, 'negate' => $negate, ]; /** @var \Drupal\eca_base\Plugin\ECA\Condition\ScalarComparison $condition */ $condition = $this->conditionManager->createInstance('eca_scalar', $config); $this->assertTrue($condition->evaluate(), $message); } /** * Provides multiple string test cases for the testScalarValues method. * * @return array * The string test cases. */ public function stringDataProvider(): array { return [ [ 'my test string', 'my test string', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left equals right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_YES, Conditions::OPTION_NO, 'Left equals (case sensitiv) right value.', ], [ 'my test string', 'My Test String', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_YES, Conditions::OPTION_YES, 'Left does not equal (case sensitiv) right value.', ], [ 'my test string', 'my test', StringComparisonBase::COMPARE_BEGINS_WITH, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left begins with right value.', ], [ 'my test string', 'test string', StringComparisonBase::COMPARE_ENDS_WITH, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left ends with right value.', ], [ 'my test string', 'test', StringComparisonBase::COMPARE_CONTAINS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left contains right value.', ], [ 'my test string', 'a test string', StringComparisonBase::COMPARE_GREATERTHAN, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is greater than right value.', ], [ 'my test string', 'your test string', StringComparisonBase::COMPARE_LESSTHAN, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is less than right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_ATMOST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at most the equal right value.', ], [ 'my test string', 'your test string', StringComparisonBase::COMPARE_ATMOST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at most right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_ATLEAST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at least the equal right value.', ], [ 'my test string', 'a test string', StringComparisonBase::COMPARE_ATLEAST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at least right value.', ], ]; } /** * Provides multiple integer test cases for the testScalarValues method. * * @return array * The integer test cases. */ public function integerDataProvider(): array { return [ [ 5, 5, StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_NUMERIC, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left and right are equal.', ], [ 5, 4, StringComparisonBase::COMPARE_GREATERTHAN, StringComparisonBase::COMPARE_TYPE_NUMERIC, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is great than right value.', ], ]; } } modules/content/src/Plugin/ECA/Condition/EntityFieldValue.php +9 −9 Original line number Diff line number Diff line Loading @@ -69,16 +69,9 @@ class EntityFieldValue extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { return $this->getExpectedValue(); } /** * {@inheritdoc} */ protected function getSecondValue(): string { protected function getLeftValue(): string { $target_value = $this->targetValue ?? $this->getTargetValue(); if (NULL === $target_value && $this->getFirstValue() === '') { if (NULL === $target_value && $this->getRightValue() === '') { // Since the StringComparisonBase always compares string values, we want // to make sure, that the evaluation will return FALSE for the very rare // situation, that an empty string is expected to be contained. Loading @@ -87,6 +80,13 @@ class EntityFieldValue extends StringComparisonBase { return $target_value ?? ''; } /** * {@inheritdoc} */ protected function getRightValue(): string { return $this->getExpectedValue(); } /** * Get the configured and tokenized field name. * Loading modules/content/tests/src/Kernel/CompareFieldValueTest.php 0 → 100644 +145 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\eca_content\Kernel; use Drupal\eca\Plugin\ECA\Condition\StringComparisonBase; use Drupal\eca\PluginManager\Condition; use Drupal\eca\Service\Conditions; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Drupal\user\Entity\User; /** * Kernel tests for the "eca_entity_field_value" condition plugin. * * @group eca * @group eca_content */ class CompareFieldValueTest extends KernelTestBase { protected static $modules = [ 'system', 'user', 'field', 'text', 'node', 'eca', 'eca_content', ]; /** * {@inheritdoc} */ public function setUp(): void { parent::setUp(); $this->installEntitySchema('user'); $this->installEntitySchema('node'); $this->installSchema('node', ['node_access']); $this->installConfig(static::$modules); User::create(['uid' => 1, 'name' => 'admin'])->save(); } /** * Evaluates the given test values with a fresh condition plugin. * * @param \Drupal\eca\PluginManager\Condition $condition_manager * @param \Drupal\node\NodeInterface $node * @param array $defaults * @param array $test_values * * @return void */ private function evaluate(Condition $condition_manager, NodeInterface $node, array $defaults, array $test_values): void { $message = $test_values['message']; unset($test_values['message']); /** @var \Drupal\eca_content\Plugin\ECA\Condition\EntityFieldValue $condition */ $condition = $condition_manager->createInstance('eca_entity_field_value', $test_values + $defaults); $condition->setContextValue('entity', $node); $this->assertTrue($condition->evaluate(), $message); } /** * Tests single string field comparison. */ public function testNodeTitle(): void { /** @var \Drupal\eca\PluginManager\Condition $condition_manager */ $condition_manager = \Drupal::service('plugin.manager.eca.condition'); /** @var \Drupal\node\NodeInterface $node */ $node = Node::create([ 'type' => 'article', 'uid' => 1, 'title' => 'First article', ]); $node->save(); // Configure default settings for condition. $defaults = [ 'field_name' => 'title', 'type' => StringComparisonBase::COMPARE_TYPE_VALUE, 'case' => Conditions::OPTION_NO, 'negate' => Conditions::OPTION_NO, ]; // Configure test values. $tests = [ [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_EQUALS, 'message' => 'Title equals expected value.', ], [ 'expected_value' => 'First', 'operator' => StringComparisonBase::COMPARE_BEGINS_WITH, 'message' => 'Title begins with expected value.', ], [ 'expected_value' => 'article', 'operator' => StringComparisonBase::COMPARE_ENDS_WITH, 'message' => 'Title ends with expected value.', ], [ 'expected_value' => 't a', 'operator' => StringComparisonBase::COMPARE_CONTAINS, 'message' => 'Title contains expected value.', ], [ 'expected_value' => 'An article', 'operator' => StringComparisonBase::COMPARE_GREATERTHAN, 'message' => 'Title is greater than expected value.', ], [ 'expected_value' => 'Second article', 'operator' => StringComparisonBase::COMPARE_LESSTHAN, 'message' => 'Title is less than expected value.', ], [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_ATMOST, 'message' => 'Title is at most the equal expected value.', ], [ 'expected_value' => 'Second article', 'operator' => StringComparisonBase::COMPARE_ATMOST, 'message' => 'Title is at most expected value.', ], [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_ATLEAST, 'message' => 'Title is at least the equal expected value.', ], [ 'expected_value' => 'An article', 'operator' => StringComparisonBase::COMPARE_ATLEAST, 'message' => 'Title is at least expected value.', ], ]; // Test all the combinations. foreach ($tests as $test) { $this->evaluate($condition_manager, $node, $defaults, $test); } } } Loading
modules/base/src/Plugin/ECA/Condition/EcaState.php +2 −2 Original line number Diff line number Diff line Loading @@ -18,14 +18,14 @@ class EcaState extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { protected function getLeftValue(): string { return $this->state->get($this->configuration['key'], ''); } /** * {@inheritdoc} */ protected function getSecondValue(): string { protected function getRightValue(): string { return $this->configuration['value']; } Loading
modules/base/src/Plugin/ECA/Condition/ScalarComparison.php +8 −8 Original line number Diff line number Diff line Loading @@ -18,15 +18,15 @@ class ScalarComparison extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { return $this->configuration['right'] ?? ''; protected function getLeftValue(): string { return $this->configuration['left'] ?? ''; } /** * {@inheritdoc} */ protected function getSecondValue(): string { return $this->configuration['left'] ?? ''; protected function getRightValue(): string { return $this->configuration['right'] ?? ''; } /** Loading @@ -43,16 +43,16 @@ class ScalarComparison extends StringComparisonBase { * {@inheritdoc} */ public function buildConfigurationForm(array $form, FormStateInterface $form_state): array { $form['right'] = [ $form['left'] = [ '#type' => 'textarea', '#title' => $this->t('First value'), '#default_value' => $this->getSecondValue(), '#default_value' => $this->getLeftValue(), '#weight' => -10, ]; $form['left'] = [ $form['right'] = [ '#type' => 'textarea', '#title' => $this->t('Second value'), '#default_value' => $this->getFirstValue(), '#default_value' => $this->getRightValue(), '#weight' => -8, ]; return parent::buildConfigurationForm($form, $form_state); Loading
modules/base/tests/src/Kernel/CompareScalarTest.php 0 → 100644 +206 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\eca_base\Kernel; use Drupal\eca\Plugin\ECA\Condition\StringComparisonBase; use Drupal\eca\PluginManager\Condition; use Drupal\eca\Service\Conditions; use Drupal\KernelTests\KernelTestBase; /** * Kernel tests for the "eca_scalar" condition plugin. * * @group eca * @group eca_base */ class CompareScalarTest extends KernelTestBase { protected static $modules = [ 'eca', 'eca_base', ]; /** * @var \Drupal\eca\PluginManager\Condition|null */ protected ?Condition $conditionManager; /** * {@inheritdoc} */ public function setUp(): void { parent::setUp(); $this->installConfig(static::$modules); $this->conditionManager = \Drupal::service('plugin.manager.eca.condition'); } /** * Tests scalar value comparison. * * @dataProvider stringDataProvider * @dataProvider integerDataProvider */ public function testScalarValues($left, $right, $operator, $type, $case, $negate, $message): void { // Configure default settings for condition. $config = [ 'left' => $left, 'right' => $right, 'operator' => $operator, 'type' => $type, 'case' => $case, 'negate' => $negate, ]; /** @var \Drupal\eca_base\Plugin\ECA\Condition\ScalarComparison $condition */ $condition = $this->conditionManager->createInstance('eca_scalar', $config); $this->assertTrue($condition->evaluate(), $message); } /** * Provides multiple string test cases for the testScalarValues method. * * @return array * The string test cases. */ public function stringDataProvider(): array { return [ [ 'my test string', 'my test string', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left equals right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_YES, Conditions::OPTION_NO, 'Left equals (case sensitiv) right value.', ], [ 'my test string', 'My Test String', StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_YES, Conditions::OPTION_YES, 'Left does not equal (case sensitiv) right value.', ], [ 'my test string', 'my test', StringComparisonBase::COMPARE_BEGINS_WITH, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left begins with right value.', ], [ 'my test string', 'test string', StringComparisonBase::COMPARE_ENDS_WITH, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left ends with right value.', ], [ 'my test string', 'test', StringComparisonBase::COMPARE_CONTAINS, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left contains right value.', ], [ 'my test string', 'a test string', StringComparisonBase::COMPARE_GREATERTHAN, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is greater than right value.', ], [ 'my test string', 'your test string', StringComparisonBase::COMPARE_LESSTHAN, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is less than right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_ATMOST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at most the equal right value.', ], [ 'my test string', 'your test string', StringComparisonBase::COMPARE_ATMOST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at most right value.', ], [ 'my test string', 'my test string', StringComparisonBase::COMPARE_ATLEAST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at least the equal right value.', ], [ 'my test string', 'a test string', StringComparisonBase::COMPARE_ATLEAST, StringComparisonBase::COMPARE_TYPE_VALUE, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is at least right value.', ], ]; } /** * Provides multiple integer test cases for the testScalarValues method. * * @return array * The integer test cases. */ public function integerDataProvider(): array { return [ [ 5, 5, StringComparisonBase::COMPARE_EQUALS, StringComparisonBase::COMPARE_TYPE_NUMERIC, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left and right are equal.', ], [ 5, 4, StringComparisonBase::COMPARE_GREATERTHAN, StringComparisonBase::COMPARE_TYPE_NUMERIC, Conditions::OPTION_NO, Conditions::OPTION_NO, 'Left is great than right value.', ], ]; } }
modules/content/src/Plugin/ECA/Condition/EntityFieldValue.php +9 −9 Original line number Diff line number Diff line Loading @@ -69,16 +69,9 @@ class EntityFieldValue extends StringComparisonBase { /** * {@inheritdoc} */ protected function getFirstValue(): string { return $this->getExpectedValue(); } /** * {@inheritdoc} */ protected function getSecondValue(): string { protected function getLeftValue(): string { $target_value = $this->targetValue ?? $this->getTargetValue(); if (NULL === $target_value && $this->getFirstValue() === '') { if (NULL === $target_value && $this->getRightValue() === '') { // Since the StringComparisonBase always compares string values, we want // to make sure, that the evaluation will return FALSE for the very rare // situation, that an empty string is expected to be contained. Loading @@ -87,6 +80,13 @@ class EntityFieldValue extends StringComparisonBase { return $target_value ?? ''; } /** * {@inheritdoc} */ protected function getRightValue(): string { return $this->getExpectedValue(); } /** * Get the configured and tokenized field name. * Loading
modules/content/tests/src/Kernel/CompareFieldValueTest.php 0 → 100644 +145 −0 Original line number Diff line number Diff line <?php namespace Drupal\Tests\eca_content\Kernel; use Drupal\eca\Plugin\ECA\Condition\StringComparisonBase; use Drupal\eca\PluginManager\Condition; use Drupal\eca\Service\Conditions; use Drupal\KernelTests\KernelTestBase; use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; use Drupal\user\Entity\User; /** * Kernel tests for the "eca_entity_field_value" condition plugin. * * @group eca * @group eca_content */ class CompareFieldValueTest extends KernelTestBase { protected static $modules = [ 'system', 'user', 'field', 'text', 'node', 'eca', 'eca_content', ]; /** * {@inheritdoc} */ public function setUp(): void { parent::setUp(); $this->installEntitySchema('user'); $this->installEntitySchema('node'); $this->installSchema('node', ['node_access']); $this->installConfig(static::$modules); User::create(['uid' => 1, 'name' => 'admin'])->save(); } /** * Evaluates the given test values with a fresh condition plugin. * * @param \Drupal\eca\PluginManager\Condition $condition_manager * @param \Drupal\node\NodeInterface $node * @param array $defaults * @param array $test_values * * @return void */ private function evaluate(Condition $condition_manager, NodeInterface $node, array $defaults, array $test_values): void { $message = $test_values['message']; unset($test_values['message']); /** @var \Drupal\eca_content\Plugin\ECA\Condition\EntityFieldValue $condition */ $condition = $condition_manager->createInstance('eca_entity_field_value', $test_values + $defaults); $condition->setContextValue('entity', $node); $this->assertTrue($condition->evaluate(), $message); } /** * Tests single string field comparison. */ public function testNodeTitle(): void { /** @var \Drupal\eca\PluginManager\Condition $condition_manager */ $condition_manager = \Drupal::service('plugin.manager.eca.condition'); /** @var \Drupal\node\NodeInterface $node */ $node = Node::create([ 'type' => 'article', 'uid' => 1, 'title' => 'First article', ]); $node->save(); // Configure default settings for condition. $defaults = [ 'field_name' => 'title', 'type' => StringComparisonBase::COMPARE_TYPE_VALUE, 'case' => Conditions::OPTION_NO, 'negate' => Conditions::OPTION_NO, ]; // Configure test values. $tests = [ [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_EQUALS, 'message' => 'Title equals expected value.', ], [ 'expected_value' => 'First', 'operator' => StringComparisonBase::COMPARE_BEGINS_WITH, 'message' => 'Title begins with expected value.', ], [ 'expected_value' => 'article', 'operator' => StringComparisonBase::COMPARE_ENDS_WITH, 'message' => 'Title ends with expected value.', ], [ 'expected_value' => 't a', 'operator' => StringComparisonBase::COMPARE_CONTAINS, 'message' => 'Title contains expected value.', ], [ 'expected_value' => 'An article', 'operator' => StringComparisonBase::COMPARE_GREATERTHAN, 'message' => 'Title is greater than expected value.', ], [ 'expected_value' => 'Second article', 'operator' => StringComparisonBase::COMPARE_LESSTHAN, 'message' => 'Title is less than expected value.', ], [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_ATMOST, 'message' => 'Title is at most the equal expected value.', ], [ 'expected_value' => 'Second article', 'operator' => StringComparisonBase::COMPARE_ATMOST, 'message' => 'Title is at most expected value.', ], [ 'expected_value' => 'First article', 'operator' => StringComparisonBase::COMPARE_ATLEAST, 'message' => 'Title is at least the equal expected value.', ], [ 'expected_value' => 'An article', 'operator' => StringComparisonBase::COMPARE_ATLEAST, 'message' => 'Title is at least expected value.', ], ]; // Test all the combinations. foreach ($tests as $test) { $this->evaluate($condition_manager, $node, $defaults, $test); } } }