You are here

public function TestItemsTrait::createItems in Search API 8

Creates a certain number of test items.

Parameters

\Drupal\search_api\IndexInterface $index: The index that should be used for the items.

int $count: The number of items to create.

array[] $fields: The fields to create on the items, with keys being combined property paths and values being arrays with properties to set on the field.

\Drupal\Core\TypedData\ComplexDataInterface|null $object: (optional) The object to set on each item as the "original object".

array|null $datasource_ids: (optional) An array of datasource IDs to use for the items, in that order (starting again from the front if necessary).

Return value

\Drupal\search_api\Item\ItemInterface[] An array containing the requested test items.

20 calls to TestItemsTrait::createItems()
AddURLTest::testAddFieldValues in tests/src/Unit/Processor/AddURLTest.php
Tests whether the "URI" field is correctly filled by the processor.
FieldsProcessorPluginBaseTest::getTestItem in tests/src/Unit/Processor/FieldsProcessorPluginBaseTest.php
Returns an array with one test item suitable for this test case.
FieldsProcessorPluginBaseTest::testProcessFieldRemoveValue in tests/src/Unit/Processor/FieldsProcessorPluginBaseTest.php
Tests whether removing values in processFieldValue() works correctly.
FieldsProcessorPluginBaseTest::testProcessFieldsTokenized in tests/src/Unit/Processor/FieldsProcessorPluginBaseTest.php
Tests whether the processField() method operates correctly.
HighlightTest::testExcerptExcludeFields in tests/src/Unit/Processor/HighlightTest.php
Tests excerpts with some fields excluded.

... See full list

File

tests/src/Unit/Processor/TestItemsTrait.php, line 86

Class

TestItemsTrait
Provides common methods for test cases that need to create search items.

Namespace

Drupal\Tests\search_api\Unit\Processor

Code

public function createItems(IndexInterface $index, $count, array $fields, ComplexDataInterface $object = NULL, array $datasource_ids = [
  'entity:node',
]) {
  $datasource_count = count($datasource_ids);
  $items = [];
  for ($i = 0; $i < $count; ++$i) {
    $datasource_id = $datasource_ids[$i % $datasource_count];
    $this->itemIds[$i] = $item_id = Utility::createCombinedId($datasource_id, $i + 1 . ':en');
    $item = new Item($index, $item_id);
    if (isset($object)) {
      $item
        ->setOriginalObject($object);
    }
    foreach ($fields as $combined_property_path => $field_info) {
      list($field_info['datasource_id'], $field_info['property_path']) = Utility::splitCombinedId($combined_property_path);

      // Only add fields of the right datasource.
      if (!in_array($field_info['datasource_id'], [
        NULL,
        $datasource_id,
      ], TRUE)) {
        continue;
      }
      $fields_helper = \Drupal::getContainer()
        ->get('search_api.fields_helper');
      $field_id = $fields_helper
        ->getNewFieldId($index, $field_info['property_path']);
      $field = $fields_helper
        ->createField($index, $field_id, $field_info);
      $item
        ->setField($field_id, $field);
    }
    $item
      ->setFieldsExtracted(TRUE);
    $items[$item_id] = $item;
  }
  return $items;
}