You are here

public function SolrFieldNamesTest::testSolrFieldNames in Search API Solr 8.3

Same name and namespace in other branches
  1. 8 tests/src/Kernel/SolrFieldNamesTest.php \Drupal\Tests\search_api_solr\Kernel\SolrFieldNamesTest::testSolrFieldNames()
  2. 8.2 tests/src/Kernel/SolrFieldNamesTest.php \Drupal\Tests\search_api_solr\Kernel\SolrFieldNamesTest::testSolrFieldNames()
  3. 4.x tests/src/Kernel/SolrFieldNamesTest.php \Drupal\Tests\search_api_solr\Kernel\SolrFieldNamesTest::testSolrFieldNames()

@covers ::getSolrFieldNames

File

tests/src/Kernel/SolrFieldNamesTest.php, line 41

Class

SolrFieldNamesTest
Tests index and search capabilities using the Solr search backend.

Namespace

Drupal\Tests\search_api_solr\Kernel

Code

public function testSolrFieldNames() {

  // Multi-value link field.
  $field = FieldStorageConfig::create([
    'field_name' => 'field_links',
    'entity_type' => 'user',
    'type' => 'link',
    'cardinality' => FieldStorageConfigInterface::CARDINALITY_UNLIMITED,
  ]);
  $field
    ->save();
  FieldConfig::create([
    'field_storage' => $field,
    'bundle' => 'user',
  ])
    ->save();

  // Single-value text field.
  $field = FieldStorageConfig::create([
    'field_name' => 'field_bio',
    'entity_type' => 'user',
    'type' => 'string_long',
    'cardinality' => 1,
  ]);
  $field
    ->save();
  FieldConfig::create([
    'field_storage' => $field,
    'bundle' => 'user',
  ])
    ->save();
  $index = Index::create([
    'id' => 'index',
    'datasource_settings' => [
      'entity:node' => [
        'plugin_id' => 'entity:node',
        'settings' => [],
      ],
    ],
    'field_settings' => [
      'title' => [
        'label' => 'Link title',
        'type' => 'string',
        'datasource_id' => 'entity:node',
        'property_path' => 'uid:entity:field_links:title',
      ],
      'bio' => [
        'label' => 'Bio field',
        'type' => 'string',
        'datasource_id' => 'entity:node',
        'property_path' => 'uid:entity:field_bio:value',
      ],
    ],
  ]);
  $backend = SearchApiSolrBackend::create($this->container, [], 'test', []);
  $fields = $backend
    ->getSolrFieldNames($index);
  $this
    ->assertSame($fields['title'], 'sm_title');
  $this
    ->assertSame($fields['bio'], 'ss_bio');
  $fields = $index
    ->getFields();
  $cardinality = $this
    ->invokeMethod($backend, 'getPropertyPathCardinality', [
    $fields['title']
      ->getPropertyPath(),
    $fields['title']
      ->getDatasource()
      ->getPropertyDefinitions(),
  ]);
  $this
    ->assertEquals(FieldStorageConfigInterface::CARDINALITY_UNLIMITED, $cardinality);
  $cardinality = $this
    ->invokeMethod($backend, 'getPropertyPathCardinality', [
    $fields['bio']
      ->getPropertyPath(),
    $fields['bio']
      ->getDatasource()
      ->getPropertyDefinitions(),
  ]);
  $this
    ->assertEquals(1, $cardinality);

  // Test Typed Data.
  $index = Index::create([
    'id' => 'typed_data_index',
    'datasource_settings' => [
      'search_api_solr_test_widget' => [
        'plugin_id' => 'search_api_solr_test_widget',
        'settings' => [],
      ],
    ],
    'field_settings' => [
      'widget_types' => [
        'label' => 'Widget Types',
        'type' => 'string',
        'datasource_id' => 'search_api_solr_test_widget',
        'property_path' => 'widget_types',
      ],
    ],
  ]);
  $backend = SearchApiSolrBackend::create($this->container, [], 'test', []);
  $fields = $backend
    ->getSolrFieldNames($index);
  $this
    ->assertSame('sm_widget_types', $fields['widget_types']);
}