You are here

protected function ProcessorTestBase::generateItem in Search API 8

Generates a single test item.

Parameters

array $values: An associative array with the following keys:

  • datasource: The datasource plugin ID.
  • item: The item object to be indexed.
  • item_id: The datasource-specific raw item ID.
  • *: Any other keys will be treated as property paths, and their values as a single value for a field with that property path.

Return value

\Drupal\search_api\Item\Item|\Drupal\search_api\Item\ItemInterface The generated test item.

2 calls to ProcessorTestBase::generateItem()
ProcessorTestBase::generateItems in tests/src/Kernel/Processor/ProcessorTestBase.php
Generates some test items.
RenderedItemTest::testSearchExcerptField in tests/src/Kernel/Processor/RenderedItemTest.php
Tests that the "Search excerpt" field in entity displays works correctly.

File

tests/src/Kernel/Processor/ProcessorTestBase.php, line 174

Class

ProcessorTestBase
Provides a base class for Drupal unit tests for processors.

Namespace

Drupal\Tests\search_api\Kernel\Processor

Code

protected function generateItem(array $values) {
  $id = Utility::createCombinedId($values['datasource'], $values['item_id']);
  $item = \Drupal::getContainer()
    ->get('search_api.fields_helper')
    ->createItemFromObject($this->index, $values['item'], $id);
  foreach ([
    NULL,
    $values['datasource'],
  ] as $datasource_id) {
    foreach ($this->index
      ->getFieldsByDatasource($datasource_id) as $key => $field) {

      /** @var \Drupal\search_api\Item\FieldInterface $field */
      $field = clone $field;
      if (isset($values[$field
        ->getPropertyPath()])) {
        $field
          ->addValue($values[$field
          ->getPropertyPath()]);
      }
      $item
        ->setField($key, $field);
    }
  }
  return $item;
}