You are here

protected function ViewsTest::regressionTest2869121 in Search API 8

Tests setting the "Fulltext search" filter to "Required".

This previously caused problems with form validation and caching.

See also

https://www.drupal.org/node/2869121

https://www.drupal.org/node/2873246

https://www.drupal.org/node/2871030

1 call to ViewsTest::regressionTest2869121()
ViewsTest::regressionTests in tests/src/Functional/ViewsTest.php
Contains regression tests for previous, fixed bugs.

File

tests/src/Functional/ViewsTest.php, line 396

Class

ViewsTest
Tests the Views integration of the Search API.

Namespace

Drupal\Tests\search_api\Functional

Code

protected function regressionTest2869121() {

  // Make sure setting the fulltext filter to "Required" works as expected.
  $view = View::load('search_api_test_view');
  $displays = $view
    ->get('display');
  $displays['default']['display_options']['filters']['search_api_fulltext']['expose']['required'] = TRUE;
  $displays['default']['display_options']['cache']['type'] = 'search_api_time';
  $view
    ->set('display', $displays);
  $view
    ->save();
  $this
    ->checkResults([], [], 'Search without required fulltext keywords');
  $this
    ->assertSession()
    ->responseNotContains('Error message');
  $this
    ->checkResults([
    'search_api_fulltext' => 'foo test',
  ], [
    1,
    2,
    4,
  ], 'Search for multiple words');
  $this
    ->assertSession()
    ->responseNotContains('Error message');
  $this
    ->checkResults([
    'search_api_fulltext' => 'fo',
  ], [], 'Search for short word');
  $this
    ->assertSession()
    ->pageTextContains('You must include at least one positive keyword with 3 characters or more');

  // Make sure this also works with the exposed form in a block, and doesn't
  // throw fatal errors on all pages with the block.
  $view = View::load('search_api_test_view');
  $displays = $view
    ->get('display');
  $displays['page_1']['display_options']['exposed_block'] = TRUE;
  $view
    ->set('display', $displays);
  $view
    ->save();
  Block::create([
    'id' => 'search_api_test_view',
    'theme' => $this->defaultTheme,
    'weight' => -20,
    'plugin' => 'views_exposed_filter_block:search_api_test_view-page_1',
    'region' => 'content',
  ])
    ->save();
  $this
    ->drupalGet('');

  // We submit the form three times, to make extra sure all Views caches are
  // triggered.
  for ($i = 0; $i < 3; ++$i) {

    // Flush the page-level caches to make sure the Views cache plugin is
    // used (so we could reproduce the bug if it's there).
    \Drupal::getContainer()
      ->get('cache.page')
      ->deleteAll();
    \Drupal::getContainer()
      ->get('cache.dynamic_page_cache')
      ->deleteAll();
    $this
      ->submitForm([], 'Search');
    $this
      ->assertSession()
      ->addressMatches('#^/search-api-test#');
    $this
      ->assertSession()
      ->responseNotContains('Error message');
    $this
      ->assertSession()
      ->pageTextNotContains('search results');

    // Make sure the Views cache was used, none of the two page caches.
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Drupal-Cache', 'MISS');
    $this
      ->assertSession()
      ->responseHeaderEquals('X-Drupal-Dynamic-Cache', 'MISS');
  }
}