You are here

protected function SearchApiBulkFormTest::createIndexedContent in Search API 8

Creates and indexes test content.

The index is composed of three datasources, including a non-entity one, in order to test the bulk form on a view aggregating different entity types and even non-entity rows:

  • entity:entity_test: Datasource for 'entity_test' entity.
  • entity:entity_test_string_id Datasource for 'entity_test_string_id' entity.
  • search_api_test: Non-entity datasource.

For each datasource we create and index two entries.

Throws

\Drupal\Core\Entity\EntityStorageException Thrown if the bundle does not exist or was needed but not specified.

\Drupal\Core\TypedData\Exception\MissingDataException If the complex data structure is unset and no property can be created.

1 call to SearchApiBulkFormTest::createIndexedContent()
SearchApiBulkFormTest::setUp in tests/src/Functional/SearchApiBulkFormTest.php

File

tests/src/Functional/SearchApiBulkFormTest.php, line 130

Class

SearchApiBulkFormTest
Tests the Search API bulk form Views field plugin.

Namespace

Drupal\Tests\search_api\Functional

Code

protected function createIndexedContent() {
  $foo_data_definition = FooDataDefinition::create()
    ->setMainPropertyName('foo')
    ->setLabel('Foo');

  // Create 2 items for each datasource.
  $search_api_test_values = [];
  for ($i = 1; $i <= 2; $i++) {

    // Entity: entity_type.
    $entity = EntityTest::create([
      'name' => $this
        ->randomString(),
    ]);
    $entity
      ->save();

    // Entity: entity_test_string_id.
    $entity = EntityTestStringId::create([
      'id' => "{$i}",
      'name' => $this
        ->randomString(),
    ]);
    $entity
      ->save();

    // Non-entity data.

    /** @var \Drupal\Core\TypedData\Plugin\DataType\Map $foo */
    $foo = \Drupal::getContainer()
      ->get('typed_data_manager')
      ->createInstance('map', [
      'data_definition' => $foo_data_definition,
      'name' => NULL,
      'parent' => NULL,
    ]);
    $foo
      ->set('foo', $this
      ->randomMachineName());
    $search_api_test_values["{$i}:en"] = $foo;
  }
  $state = \Drupal::state();
  $state
    ->set('search_api_test.datasource.return.loadMultiple', $search_api_test_values);
  $state
    ->set('search_api_test.datasource.return.getItemLanguage', 'en');
  $state
    ->set('search_api_test.datasource.return.getPropertyDefinitions', [
    'foo' => $foo_data_definition,
  ]);
  $this->index
    ->trackItemsInserted('search_api_test', array_keys($search_api_test_values));
  $this->index
    ->indexItems();
  $query_helper = \Drupal::getContainer()
    ->get('search_api.query_helper');
  $query = $query_helper
    ->createQuery($this->index);
  $results = $query
    ->execute()
    ->getResultItems();

  // Check that content has been indexed.
  $this
    ->assertCount(6, $results);
  $this
    ->assertArrayHasKey('entity:entity_test/1:en', $results);
  $this
    ->assertArrayHasKey('entity:entity_test/2:en', $results);
  $this
    ->assertArrayHasKey('entity:entity_test_string_id/1:und', $results);
  $this
    ->assertArrayHasKey('entity:entity_test_string_id/2:und', $results);
  $this
    ->assertArrayHasKey('search_api_test/1:en', $results);
  $this
    ->assertArrayHasKey('search_api_test/2:en', $results);
}