You are here

public function CustomDataTypesTest::testCustomDataTypes in Search API 8

Tests custom data types integration.

File

tests/src/Kernel/System/CustomDataTypesTest.php, line 117

Class

CustomDataTypesTest
Tests custom data types integration.

Namespace

Drupal\Tests\search_api\Kernel\System

Code

public function testCustomDataTypes() {
  $original_value = $this->entities[1]
    ->get('name')
    ->getValue()[0]['value'];
  $original_type = $this->index
    ->getField('name')
    ->getType();
  $item = $this->index
    ->loadItem('entity:entity_test_mulrev_changed/1:en');
  $item = \Drupal::getContainer()
    ->get('search_api.fields_helper')
    ->createItemFromObject($this->index, $item, 'entity:entity_test_mulrev_changed/1:en');
  $name_field = $item
    ->getField('name');
  $processed_value = $name_field
    ->getValues()[0];
  $processed_type = $name_field
    ->getType();
  $label = $name_field
    ->getLabel();
  $this
    ->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
  $this
    ->assertEquals($original_type, $processed_type, 'The processed type matches the original type.');
  $this
    ->assertEquals('Name', $label, 'The label is correctly set.');

  // Reset the fields on the item and change to the supported data type.
  $item
    ->setFieldsExtracted(FALSE);
  $item
    ->setFields([]);
  $field = $this->index
    ->getField('name')
    ->setType('search_api_test')
    ->setLabel("Test");
  $this->index
    ->addField($field);
  $name_field = $item
    ->getField('name');
  $processed_value = $name_field
    ->getValues()[0];
  $processed_type = $name_field
    ->getType();
  $this
    ->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
  $this
    ->assertEquals('search_api_test', $processed_type, 'The processed type matches the new type.');
  $this
    ->assertEquals('Test', $name_field
    ->getLabel(), 'The label is correctly set.');

  // Reset the fields on the item and change to the non-supported data type.
  $item
    ->setFieldsExtracted(FALSE);
  $item
    ->setFields([]);
  $field = $this->index
    ->getField('name')
    ->setType('search_api_test_unsupported');
  $this->index
    ->addField($field);
  $name_field = $item
    ->getField('name');
  $processed_value = $name_field
    ->getValues()[0];
  $processed_type = $name_field
    ->getType();
  $this
    ->assertEquals($original_value, $processed_value, 'The processed value matches the original value');
  $this
    ->assertEquals('string', $processed_type, 'The processed type matches the fallback type.');

  // Reset the fields on the item and change to the data altering data type.
  $item
    ->setFieldsExtracted(FALSE);
  $item
    ->setFields([]);
  $field = $this->index
    ->getField('name')
    ->setType('search_api_test_altering');
  $this->index
    ->addField($field);
  $name_field = $item
    ->getField('name');
  $processed_value = $name_field
    ->getValues()[0];
  $processed_type = $name_field
    ->getType();
  $this
    ->assertEquals(strlen($original_value), $processed_value, 'The processed value matches the altered original value');
  $this
    ->assertEquals('search_api_test_altering', $processed_type, 'The processed type matches the defined type.');
}