You are here

public function FieldsProcessorPluginBaseTest::testProcessFieldsTokenized in Search API 8

Tests whether the processField() method operates correctly.

File

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

Class

FieldsProcessorPluginBaseTest
Tests the base class for fields-based processors.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function testProcessFieldsTokenized() {
  $override = function (&$value, $type) {
    switch ($type) {
      case 'integer':
        ++$value;
        return;
      case 'string':
        $value = "++{$value}";
        return;
    }
    if (strpos($value, ' ')) {
      $value = TestFieldsProcessorPlugin::createTokenizedText($value, 4)
        ->getTokens();
    }
    elseif ($value == 'bar') {
      $value = TestFieldsProcessorPlugin::createTokenizedText('*bar', 2)
        ->getTokens();
    }
    elseif ($value == 'baz') {
      $value = '';
    }
    else {
      $value = "*{$value}";
    }
  };
  $this->processor
    ->setMethodOverride('processFieldValue', $override);
  $value = TestFieldsProcessorPlugin::createTokenizedText('foobar baz', 3);
  $tokens = $value
    ->getTokens();
  $tokens[] = new TextToken('foo bar', 2);
  $value
    ->setTokens($tokens);
  $fields = [
    'field1' => [
      'type' => 'text',
      'values' => [
        TestFieldsProcessorPlugin::createTokenizedText('foo bar baz', 3),
        $value,
        new TextValue('foo'),
        new TextValue('foo bar'),
        new TextValue('bar'),
        new TextValue('baz'),
      ],
    ],
    'field2' => [
      'type' => 'integer',
      'values' => [
        1,
        3,
      ],
    ],
    'field3' => [
      'type' => 'string',
      'values' => [
        'foo',
        'foo bar baz',
      ],
    ],
  ];
  $items = $this
    ->createItems($this->index, 1, $fields);
  $this->processor
    ->setConfiguration([
    'fields' => [
      'field1',
      'field2',
      'field3',
    ],
  ]);
  $this->processor
    ->preprocessIndexItems($items);
  $fields = $items[$this->itemIds[0]]
    ->getFields();

  /** @var \Drupal\search_api\Plugin\search_api\data_type\value\TextValueInterface[] $values */
  $values = $fields['field1']
    ->getValues();
  $summary = [];
  foreach ($values as $i => $value) {
    $summary[$i]['text'] = $value
      ->toText();
    $tokens = $value
      ->getTokens();
    if ($tokens !== NULL) {
      $summary[$i]['tokens'] = [];
      foreach ($tokens as $token) {
        $summary[$i]['tokens'][] = [
          'text' => $token
            ->getText(),
          'boost' => $token
            ->getBoost(),
        ];
      }
    }
  }
  $expected = [
    [
      'text' => '*foo *bar',
      'tokens' => [
        [
          'text' => '*foo',
          'boost' => 3,
        ],
        [
          'text' => '*bar',
          'boost' => 6,
        ],
      ],
    ],
    [
      'text' => '*foobar foo bar',
      'tokens' => [
        [
          'text' => '*foobar',
          'boost' => 3,
        ],
        [
          'text' => 'foo',
          'boost' => 8,
        ],
        [
          'text' => 'bar',
          'boost' => 8,
        ],
      ],
    ],
    [
      'text' => '*foo',
    ],
    [
      'text' => 'foo bar',
      'tokens' => [
        [
          'text' => 'foo',
          'boost' => 4,
        ],
        [
          'text' => 'bar',
          'boost' => 4,
        ],
      ],
    ],
    [
      'text' => '*bar',
      'tokens' => [
        [
          'text' => '*bar',
          'boost' => 2,
        ],
      ],
    ],
  ];
  $this
    ->assertEquals($expected, $summary);
  $expected = [
    2,
    4,
  ];
  $this
    ->assertEquals('integer', $fields['field2']
    ->getType());
  $this
    ->assertEquals($expected, $fields['field2']
    ->getValues());
  $expected = [
    '++foo',
    '++foo bar baz',
  ];
  $this
    ->assertEquals('string', $fields['field3']
    ->getType());
  $this
    ->assertEquals($expected, $fields['field3']
    ->getValues());
}