You are here

protected function ElasticsearchTest::regressionTests in Elasticsearch Connector 8

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

File

src/Tests/ElasticsearchTest.php, line 294
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 regressionTests() {

  // Regression tests for #2007872.
  $prepareSearch = $this
    ->buildSearch('test')
    ->sort($this
    ->getFieldId('id'), 'ASC')
    ->sort($this
    ->getFieldId('type'), 'ASC');
  $results = $prepareSearch
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 4, 'Sorting on field with NULLs returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    3,
    4,
  )), 'Sorting on field with NULLs returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $query = $this
    ->buildSearch();
  $filter = $query
    ->createFilter('OR');
  $filter
    ->condition($this
    ->getFieldId('id'), 3);
  $filter
    ->condition($this
    ->getFieldId('type'), 'article');
  $query
    ->filter($filter);
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 3, 'OR filter on field with NULLs returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    3,
    4,
    5,
  )), 'OR filter on field with NULLs returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);

  // Regression tests for #1863672.
  $query = $this
    ->buildSearch();
  $filter = $query
    ->createFilter('OR');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'orange');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'apple');
  $query
    ->filter($filter);
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 4, 'OR filter on multi-valued field returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    4,
    5,
  )), 'OR filter on multi-valued field returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $query = $this
    ->buildSearch();
  $filter = $query
    ->createFilter('OR');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'orange');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'strawberry');
  $query
    ->filter($filter);
  $filter = $query
    ->createFilter('OR');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'apple');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'grape');
  $query
    ->filter($filter);
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 3, 'Multiple OR filters on multi-valued field returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    2,
    4,
    5,
  )), 'Multiple OR filters on multi-valued field returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $query = $this
    ->buildSearch();
  $filter1 = $query
    ->createFilter('OR');
  $filter = $query
    ->createFilter('AND');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'orange');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'apple');
  $filter1
    ->filter($filter);
  $filter = $query
    ->createFilter('AND');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'strawberry');
  $filter
    ->condition($this
    ->getFieldId('keywords'), 'grape');
  $filter1
    ->filter($filter);
  $query
    ->filter($filter1);
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 3, 'Complex nested filters on multi-valued field returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    2,
    4,
    5,
  )), 'Complex nested filters on multi-valued field returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);

  // Regression tests for #2111753.
  $keys = array(
    '#conjunction' => 'OR',
    'foo',
    'test',
  );
  $query = $this
    ->buildSearch($keys, array(), array(
    $this
      ->getFieldId('name'),
  ));
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 3, 'OR keywords returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    4,
  )), 'OR keywords returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $query = $this
    ->buildSearch($keys, array(), array(
    $this
      ->getFieldId('name'),
    $this
      ->getFieldId('body'),
  ));
  $query
    ->range(0, 0);
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 5, 'Multi-field OR keywords returned correct number of results.');

  //$this->assertFalse($results->getResultItems(), 'Multi-field OR keywords returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $keys = array(
    '#conjunction' => 'OR',
    'foo',
    'test',
    array(
      '#conjunction' => 'AND',
      'bar',
      'baz',
    ),
  );
  $query = $this
    ->buildSearch($keys, array(), array(
    $this
      ->getFieldId('name'),
  ));
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 4, 'Nested OR keywords returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    4,
    5,
  )), 'Nested OR keywords returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $keys = array(
    '#conjunction' => 'OR',
    array(
      '#conjunction' => 'AND',
      'foo',
      'test',
    ),
    array(
      '#conjunction' => 'AND',
      'bar',
      'baz',
    ),
  );
  $query = $this
    ->buildSearch($keys, array(), array(
    $this
      ->getFieldId('name'),
    $this
      ->getFieldId('body'),
  ));
  $query
    ->sort($this
    ->getFieldId('id'), 'ASC');
  $results = $query
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 4, 'Nested multi-field OR keywords returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    1,
    2,
    4,
    5,
  )), 'Nested multi-field OR keywords returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);

  // Regression tests for #2127001.
  $keys = array(
    '#conjunction' => 'AND',
    '#negation' => TRUE,
    'foo',
    'bar',
  );
  $results = $this
    ->buildSearch($keys)
    ->sort('search_api_id', 'ASC')
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 2, 'Negated AND fulltext search returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    3,
    4,
  )), 'Negated AND fulltext search returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $keys = array(
    '#conjunction' => 'OR',
    '#negation' => TRUE,
    'foo',
    'baz',
  );
  $results = $this
    ->buildSearch($keys)
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 1, 'Negated OR fulltext search returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    3,
  )), 'Negated OR fulltext search returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
  $keys = array(
    '#conjunction' => 'AND',
    'test',
    array(
      '#conjunction' => 'AND',
      '#negation' => TRUE,
      'foo',
      'bar',
    ),
  );
  $results = $this
    ->buildSearch($keys)
    ->sort('search_api_id', 'ASC')
    ->execute();
  $this
    ->assertEqual($results
    ->getResultCount(), 2, 'Nested NOT AND fulltext search returned correct number of results.');
  $this
    ->assertEqual(array_keys($results
    ->getResultItems()), $this
    ->getItemIds(array(
    3,
    4,
  )), 'Nested NOT AND fulltext search returned correct result.');
  $this
    ->assertIgnored($results);
  $this
    ->assertWarnings($results);
}