You are here

protected function ElasticsearchTest::regressionTests2 in Elasticsearch Connector 8

Same name and namespace in other branches
  1. 8.2 src/Tests/ElasticsearchTest.php \Drupal\elasticsearch_connector\Tests\ElasticsearchTest::regressionTests2()

File

src/Tests/ElasticsearchTest.php, line 466
Contains \Drupal\elasticsearch_connector\Tests\ElasticsearchTest.

Class

ElasticsearchTest
Tests index and search capabilities using the elasticsearch backend.

Namespace

Drupal\elasticsearch_connector\Tests

Code

protected function regressionTests2() {

  // Create a "keywords" field on the test entity type.
  FieldStorageConfig::create(array(
    'field_name' => 'prices',
    'entity_type' => 'entity_test',
    'type' => 'decimal',
    'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
  ))
    ->save();
  FieldConfig::create(array(
    'field_name' => 'prices',
    'entity_type' => 'entity_test',
    'bundle' => 'item',
    'label' => 'Prices',
  ))
    ->save();

  // Regression test for #1916474.

  /** @var \Drupal\search_api\Index\IndexInterface $index */
  $index = Index::load($this->indexId);
  $index
    ->getFields(FALSE)[$this
    ->getFieldId('prices')]
    ->setType('decimal')
    ->setIndexed(TRUE, TRUE);
  $success = $index
    ->save();
  $this
    ->assertTrue($success, 'The index field settings were successfully changed.');

  // Reset the static cache so the new values will be available.
  \Drupal::entityManager()
    ->getStorage('search_api_server')
    ->resetCache(array(
    $this->serverId,
  ));
  \Drupal::entityManager()
    ->getStorage('search_api_index')
    ->resetCache(array(
    $this->serverId,
  ));
  entity_create('entity_test', array(
    'id' => 6,
    'prices' => array(
      '3.5',
      '3.25',
      '3.75',
      '3.5',
    ),
    'type' => 'item',
  ))
    ->save();
  $this
    ->indexItems($this->indexId);
  $query = $this
    ->buildSearch(NULL, array(
    'prices,3.25',
  ));
  sleep(1);
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 1, 'Filter on decimal field returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    6,
  )), 'Filter on decimal field returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $query = $this
    ->buildSearch(NULL, array(
    'prices,3.50',
  ));
  sleep(1);
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 1, 'Filter on decimal field returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    6,
  )), 'Filter on decimal field returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
}