Commit 402210ec authored by Jürgen Haas's avatar Jürgen Haas
Browse files

Issue #3271473 by jurgenhaas, mxh: StringComparisonBase doesn’t work consistently yet

parent 502b3715
Loading
Loading
Loading
Loading
+91 −11
Original line number Diff line number Diff line
@@ -38,9 +38,10 @@ class CompareScalarTest extends KernelTestBase {
   * Tests scalar value comparison.
   *
   * @dataProvider stringDataProvider
   * @dataProvider integerDataProvider
   * @dataProvider numericDataProvider
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testScalarValues($left, $right, $operator, $type, $case, $negate, $message): void {
  public function testScalarValues($left, $right, $operator, $type, $case, $negate, $message, $assertTrue = TRUE): void {
    // Configure default settings for condition.
    $config = [
      'left' => $left,
@@ -52,8 +53,13 @@ class CompareScalarTest extends KernelTestBase {
    ];
    /** @var \Drupal\eca_base\Plugin\ECA\Condition\ScalarComparison $condition */
    $condition = $this->conditionManager->createInstance('eca_scalar', $config);
    if ($assertTrue) {
      $this->assertTrue($condition->evaluate(), $message);
    }
    else {
      $this->assertFalse($condition->evaluate(), $message);
    }
  }

  /**
   * Provides multiple string test cases for the testScalarValues method.
@@ -178,27 +184,101 @@ class CompareScalarTest extends KernelTestBase {
   * Provides multiple integer test cases for the testScalarValues method.
   *
   * @return array
   *   The integer test cases.
   *   The numeric test cases.
   */
  public function integerDataProvider(): array {
  public function numericDataProvider(): array {
    return [
      [
        5,
        5,
        '5',
        '5',
        StringComparisonBase::COMPARE_EQUALS,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        '5 and 5 are equal.',
      ],
      [
        '5',
        '4',
        StringComparisonBase::COMPARE_GREATERTHAN,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        '5 is great than 4.',
      ],
      [
        '5.5',
        '5.4',
        StringComparisonBase::COMPARE_GREATERTHAN,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        '5.5 is great than 5.4.',
      ],
      [
        'a',
        '5',
        StringComparisonBase::COMPARE_EQUALS,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        'First value should be numeric.',
        FALSE,
      ],
      [
        '5',
        'a',
        StringComparisonBase::COMPARE_EQUALS,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        'Left and right are equal.',
        'First value should be numeric.',
        FALSE,
      ],
      [
        5,
        4,
        '15',
        '5',
        StringComparisonBase::COMPARE_GREATERTHAN,
        StringComparisonBase::COMPARE_TYPE_NUMERIC,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        'Left is great than right value.',
        '15 is greater than 5 for numeric comparison.',
      ],
      [
        '15',
        '5',
        StringComparisonBase::COMPARE_GREATERTHAN,
        StringComparisonBase::COMPARE_TYPE_VALUE,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        '15 is greater than 5 for value comparison.',
      ],
      [
        '15',
        '5',
        StringComparisonBase::COMPARE_LESSTHAN,
        StringComparisonBase::COMPARE_TYPE_LEXICAL,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        '15 is smaller than 5 for lexical comparison.',
      ],
      [
        'img15',
        'img5',
        StringComparisonBase::COMPARE_LESSTHAN,
        StringComparisonBase::COMPARE_TYPE_VALUE,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        'img15 is smaller than img5 for value comparison.',
      ],
      [
        'img15',
        'img5',
        StringComparisonBase::COMPARE_GREATERTHAN,
        StringComparisonBase::COMPARE_TYPE_NATURAL,
        Conditions::OPTION_NO,
        Conditions::OPTION_NO,
        'img15 is greater than img5 for natural comparison.',
      ],
    ];
  }
+66 −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_state" condition plugin.
 *
 * @group eca
 * @group eca_state
 */
class CompareStateValueTest extends KernelTestBase {

  protected static $modules = [
    'system',
    'eca',
    'eca_base',
  ];

  /**
   * @var \Drupal\eca\PluginManager\Condition|null
   */
  protected ?Condition $conditionManager;

  /**
   * @var \Drupal\eca\EcaState
   */
  protected $state;

  /**
   * {@inheritdoc}
   */
  public function setUp(): void {
    parent::setUp();
    $this->installConfig(static::$modules);
    $this->conditionManager = \Drupal::service('plugin.manager.eca.condition');
    $this->state = \Drupal::service('eca.state');
  }

  /**
   * Tests form field comparison.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testFormField(): void {
    $key = 'eca_state_key';
    $value = $this->randomString(32);
    $this->state->set($key, $value);
    $config = [
      'key' => $key,
      'value' => $value,
      'operator' => StringComparisonBase::COMPARE_EQUALS,
      'type' => StringComparisonBase::COMPARE_TYPE_VALUE,
      'case' => Conditions::OPTION_NO,
      'negate' => Conditions::OPTION_NO,
    ];
    /** @var \Drupal\eca_form\Plugin\ECA\Condition\FormFieldValue $condition */
    $condition = $this->conditionManager->createInstance('eca_state', $config);
    $this->assertTrue($condition->evaluate(), 'State value equals expected value.');
  }

}
+93 −66
Original line number Diff line number Diff line
@@ -11,7 +11,8 @@ use Drupal\node\NodeInterface;
use Drupal\user\Entity\User;

/**
 * Kernel tests for the "eca_entity_field_value" condition plugin.
 * Kernel tests for the "eca_entity_field_value" and
 * "eca_entity_original_field_value" condition plugins.
 *
 * @group eca
 * @group eca_content
@@ -28,8 +29,19 @@ class CompareFieldValueTest extends KernelTestBase {
    'eca_content',
  ];

  /**
   * @var \Drupal\eca\PluginManager\Condition|null
   */
  protected ?Condition $conditionManager;

  /**
   * @var \Drupal\node\NodeInterface|null
   */
  protected ?NodeInterface $node;

  /**
   * {@inheritdoc}
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  public function setUp(): void {
    parent::setUp();
@@ -38,108 +50,123 @@ class CompareFieldValueTest extends KernelTestBase {
    $this->installSchema('node', ['node_access']);
    $this->installConfig(static::$modules);
    User::create(['uid' => 1, 'name' => 'admin'])->save();
    $this->node = Node::create(['type' => 'article', 'uid' => 1, 'title' => 'First article']);
    $this->node->save();
    $this->conditionManager = \Drupal::service('plugin.manager.eca.condition');
  }

  /**
   * 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
   * Tests single string field comparison.
   *
   * @return void
   * @dataProvider fieldValueDataProvider
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   * @throws \Drupal\Component\Plugin\Exception\ContextException
   */
  private function evaluate(Condition $condition_manager, NodeInterface $node, array $defaults, array $test_values): void {
    $message = $test_values['message'];
    unset($test_values['message']);

  public function testNodeTitle(string $field_value, string $operator, string $message): void {
    $config = [
      'expected_value' => $field_value,
      'operator' => $operator,
      'field_name' => 'title',
      'type' => StringComparisonBase::COMPARE_TYPE_VALUE,
      'case' => Conditions::OPTION_NO,
      'negate' => Conditions::OPTION_NO,
    ];
    /** @var \Drupal\eca_content\Plugin\ECA\Condition\EntityFieldValue $condition */
    $condition = $condition_manager->createInstance('eca_entity_field_value', $test_values + $defaults);
    $condition->setContextValue('entity', $node);
    $condition = $this->conditionManager->createInstance('eca_entity_field_value', $config);
    $condition->setContextValue('entity', $this->node);
    $this->assertTrue($condition->evaluate(), $message);
  }

  /**
   * Tests single string field comparison.
   * @throws \Drupal\Core\Entity\EntityStorageException
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   * @throws \Drupal\Component\Plugin\Exception\ContextException
   */
  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 = [
  public function testNodeOriginalTitle(): void {
    $modifiedTitle = 'Modified title';
    $config = [
      'field_name' => 'title',
      'expected_value' => $modifiedTitle,
      'operator' => StringComparisonBase::COMPARE_EQUALS,
      'type' => StringComparisonBase::COMPARE_TYPE_VALUE,
      'case' => Conditions::OPTION_NO,
      'negate' => Conditions::OPTION_NO,
    ];
    // Configure test values.
    $tests = [
    $this->node
      ->setTitle($modifiedTitle)
      ->save();

    // Test modified node title.
    /** @var \Drupal\eca_content\Plugin\ECA\Condition\EntityFieldValue $condition */
    $condition = $this->conditionManager->createInstance('eca_entity_field_value', $config);
    $condition->setContextValue('entity', $this->node);
    $this->assertTrue($condition->evaluate(), 'Node title should be modified.');

    // Test original node title.
    $condition = $this->conditionManager->createInstance('eca_entity_original_field_value', $config);
    $condition->setContextValue('entity', $this->node);
    $this->assertFalse($condition->evaluate(), 'Original node title should not be modified.');
  }

  /**
   * Provides multiple string test cases for the testScalarValues method.
   *
   * @return array
   *   The string test cases.
   */
  public function fieldValueDataProvider(): array {
    return [
      [
        'expected_value' => 'First article',
        'operator' => StringComparisonBase::COMPARE_EQUALS,
        'message' => 'Title equals expected value.',
        'First article',
        StringComparisonBase::COMPARE_EQUALS,
        'Title equals expected value.',
      ],
      [
        'expected_value' => 'First',
        'operator' => StringComparisonBase::COMPARE_BEGINS_WITH,
        'message' => 'Title begins with expected value.',
        'First',
        StringComparisonBase::COMPARE_BEGINS_WITH,
        'Title begins with expected value.',
      ],
      [
        'expected_value' => 'article',
        'operator' => StringComparisonBase::COMPARE_ENDS_WITH,
        'message' => 'Title ends with expected value.',
        'article',
        StringComparisonBase::COMPARE_ENDS_WITH,
        'Title ends with expected value.',
      ],
      [
        'expected_value' => 't a',
        'operator' => StringComparisonBase::COMPARE_CONTAINS,
        'message' => 'Title contains expected value.',
        't a',
        StringComparisonBase::COMPARE_CONTAINS,
        'Title contains expected value.',
      ],
      [
        'expected_value' => 'An article',
        'operator' => StringComparisonBase::COMPARE_GREATERTHAN,
        'message' => 'Title is greater than expected value.',
        'An article',
        StringComparisonBase::COMPARE_GREATERTHAN,
        'Title is greater than expected value.',
      ],
      [
        'expected_value' => 'Second article',
        'operator' => StringComparisonBase::COMPARE_LESSTHAN,
        'message' => 'Title is less than expected value.',
        'Second article',
        StringComparisonBase::COMPARE_LESSTHAN,
        'Title is less than expected value.',
      ],
      [
        'expected_value' => 'First article',
        'operator' => StringComparisonBase::COMPARE_ATMOST,
        'message' => 'Title is at most the equal expected value.',
        'First article',
        StringComparisonBase::COMPARE_ATMOST,
        'Title is at most the equal expected value.',
      ],
      [
        'expected_value' => 'Second article',
        'operator' => StringComparisonBase::COMPARE_ATMOST,
        'message' => 'Title is at most expected value.',
        'Second article',
        StringComparisonBase::COMPARE_ATMOST,
        'Title is at most expected value.',
      ],
      [
        'expected_value' => 'First article',
        'operator' => StringComparisonBase::COMPARE_ATLEAST,
        'message' => 'Title is at least the equal expected value.',
        'First article',
        StringComparisonBase::COMPARE_ATLEAST,
        'Title is at least the equal expected value.',
      ],
      [
        'expected_value' => 'An article',
        'operator' => StringComparisonBase::COMPARE_ATLEAST,
        'message' => 'Title is at least expected value.',
        'An article',
        StringComparisonBase::COMPARE_ATLEAST,
        'Title is at least expected value.',
      ],
    ];

    // Test all the combinations.
    foreach ($tests as $test) {
      $this->evaluate($condition_manager, $node, $defaults, $test);
    }
  }

}
+64 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\eca_form\Kernel;

use Drupal\Core\Form\FormState;
use Drupal\eca\Plugin\ECA\Condition\StringComparisonBase;
use Drupal\eca\PluginManager\Condition;
use Drupal\eca\Service\Conditions;
use Drupal\eca_form\Event\FormBuild;
use Drupal\KernelTests\KernelTestBase;

/**
 * Kernel tests for the "eca_form_field_value" condition plugin.
 *
 * @group eca
 * @group eca_form
 */
class CompareFormValueTest extends KernelTestBase {

  protected static $modules = [
    'system',
    'eca',
    'eca_form',
  ];

  /**
   * @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 form field comparison.
   *
   * @throws \Drupal\Component\Plugin\Exception\PluginException
   */
  public function testFormField(): void {
    $config = [
      'field_name' => 'test_field',
      'field_value' => 'Test value',
      'operator' => StringComparisonBase::COMPARE_EQUALS,
      'type' => StringComparisonBase::COMPARE_TYPE_VALUE,
      'case' => Conditions::OPTION_NO,
      'negate' => Conditions::OPTION_NO,
    ];
    /** @var \Drupal\eca_form\Plugin\ECA\Condition\FormFieldValue $condition */
    $condition = $this->conditionManager->createInstance('eca_form_field_value', $config);
    $form_state = new FormState();
    $form_state->setValue('test_field', 'Test value');
    $form = [];
    $event = new FormBuild($form, $form_state, 'test_id');
    $condition->setEvent($event);
    $this->assertTrue($condition->evaluate(), 'Value of form field "test_field" equals expected value.');
  }

}
+9 −1
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\eca_misc\Plugin\Action;

use Drupal\Core\Access\AccessibleInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
@@ -25,7 +26,14 @@ class TokenLoadRouteParameter extends ConfigurableActionBase {
   * {@inheritdoc}
   */
  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
    $result = AccessResult::allowedIf($this->getRouteMatch()->getParameter($this->configuration['parameter_name']));
    $allowed = FALSE;
    if ($parameter = $this->getRouteMatch()->getParameter($this->configuration['parameter_name'])) {
      $allowed = TRUE;
      if ($parameter instanceof AccessibleInterface) {
        $allowed = $parameter->access('view', $account);
      }
    }
    $result = AccessResult::allowedIf($allowed);
    return $return_as_object ? $result : $result->isAllowed();
  }

Loading