You are here

public function FieldsProcessorPluginBaseTest::testProcessConditionValueArrayHandling in Search API 8

Tests whether overriding processConditionValue() works correctly.

File

tests/src/Unit/Processor/FieldsProcessorPluginBaseTest.php, line 605

Class

FieldsProcessorPluginBaseTest
Tests the base class for fields-based processors.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testProcessConditionValueArrayHandling() {
  $override = function (&$value) {
    $length = strlen($value);
    if ($length == 2) {
      $value = '';
    }
    elseif ($length == 3) {
      $value .= '*';
    }
  };
  $this->processor
    ->setMethodOverride('process', $override);
  $query = \Drupal::getContainer()
    ->get('search_api.query_helper')
    ->createQuery($this->index);
  $query
    ->addCondition('text_field', [
    'a',
    'b',
  ], 'NOT IN');
  $query
    ->addCondition('text_field', [
    'a',
    'bo',
  ], 'IN');
  $query
    ->addCondition('text_field', [
    'ab',
    'bo',
  ], 'NOT IN');
  $query
    ->addCondition('text_field', [
    'a',
    'bo',
  ], 'BETWEEN');
  $query
    ->addCondition('text_field', [
    'ab',
    'bo',
  ], 'NOT BETWEEN');
  $query
    ->addCondition('text_field', [
    'a',
    'bar',
  ], 'IN');
  $query
    ->addCondition('text_field', [
    'abo',
    'baz',
  ], 'BETWEEN');
  $this->processor
    ->preprocessSearchQuery($query);
  $expected = [
    new Condition('text_field', [
      'a',
      'b',
    ], 'NOT IN'),
    new Condition('text_field', [
      'a',
    ], 'IN'),
    new Condition('text_field', [
      'a',
      'bo',
    ], 'BETWEEN'),
    new Condition('text_field', [
      'ab',
      'bo',
    ], 'NOT BETWEEN'),
    new Condition('text_field', [
      'a',
      'bar*',
    ], 'IN'),
    new Condition('text_field', [
      'abo*',
      'baz*',
    ], 'BETWEEN'),
  ];
  $this
    ->assertEquals($expected, array_merge($query
    ->getConditionGroup()
    ->getConditions()), 'Conditions were preprocessed correctly.');
}