You are here

protected function ElasticsearchTest::searchSuccess1 in Elasticsearch Connector 8

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

Tests whether some test searches have the correct results.

File

src/Tests/ElasticsearchTest.php, line 110
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 searchSuccess1() {
  $prepareSearch = $this
    ->buildSearch('test')
    ->range(1, 2)
    ->sort($this
    ->getFieldId('id'), 'ASC');
  sleep(1);
  $results = $prepareSearch
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 4, 'Search for »test« returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    2,
    3,
  )), 'Search for »test« returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $ids = $this
    ->getItemIds(array(
    2,
  ));
  $id = reset($ids);
  $this
    ->assertEqual(key($results
    ->getResultItems()), $id);
  $this
    ->assertEqual($results
    ->getResultItems()[$id]
    ->getId(), $id);
  $this
    ->assertEqual($results
    ->getResultItems()[$id]
    ->getDatasourceId(), 'entity:entity_test');
  $prepareSearch = $this
    ->buildSearch('test foo')
    ->sort($this
    ->getFieldId('id'), 'ASC');
  sleep(1);
  $results = $prepareSearch
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 3, 'Search for »test foo« returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    4,
  )), 'Search for »test foo« returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $prepareSearch = $this
    ->buildSearch('foo', array(
    'type,item',
  ))
    ->sort($this
    ->getFieldId('id'), 'ASC');
  sleep(1);
  $results = $prepareSearch
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 2, 'Search for »foo« returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
  )), 'Search for »foo« returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $keys = array(
    '#conjunction' => 'AND',
    'test',
    array(
      '#conjunction' => 'OR',
      'baz',
      'foobar',
    ),
    array(
      '#conjunction' => 'OR',
      '#negation' => TRUE,
      'bar',
      'fooblob',
    ),
  );
  $prepareSearch = $this
    ->buildSearch($keys);
  sleep(1);
  $results = $prepareSearch
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 1, 'Complex search 1 returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    4,
  )), 'Complex search 1 returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
}