You are here

public function ExposedFilterViewsAjaxGetTest::testExposedFiltering in Views Ajax Get 8

Same name and namespace in other branches
  1. 2.0.x tests/src/FunctionalJavascript/ExposedFilterViewsAjaxGetTest.php \Drupal\Tests\views_ajax_get\FunctionalJavascript\ExposedFilterViewsAjaxGetTest::testExposedFiltering()

Tests if exposed filtering via AJAX works for the "Content" View.

File

tests/src/FunctionalJavascript/ExposedFilterViewsAjaxGetTest.php, line 22

Class

ExposedFilterViewsAjaxGetTest
Tests basic AJAX functionality of Views exposed forms using GET requests.

Namespace

Drupal\Tests\views_ajax_get\FunctionalJavascript

Code

public function testExposedFiltering() {

  // Create a Content type and two test nodes.
  $this
    ->createContentType([
    'type' => 'page',
  ]);
  $this
    ->createNode([
    'title' => 'Page One',
  ]);
  $this
    ->createNode([
    'title' => 'Page Two',
  ]);

  // Enable page cache.
  $config = $this
    ->config('system.performance');
  $config
    ->set('cache.page.max_age', 300);
  $config
    ->save();

  // Visit the test view page to test caching.
  $this
    ->drupalGet('views-ajax-get-cache-test');
  $this
    ->addAjaxCompleteHandler();
  $session = $this
    ->getSession();

  // Search for "Page One".
  $this
    ->submitForm([
    'title' => 'Page One',
  ], t('Filter'));
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Check that the AJAX request was a GET and cache missed first time.
  $drupal_settings = $this
    ->getDrupalSettings();
  $this
    ->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);
  $this
    ->assertEquals('MISS', $drupal_settings['viewsAjaxGetCacheHeader']);

  // Verify that only the "Page One" Node is present.
  $html = $session
    ->getPage()
    ->getHtml();
  $this
    ->assertContains('Page One', $html);
  $this
    ->assertNotContains('Page Two', $html);

  // Search for "Page Two".
  $this
    ->submitForm([
    'title' => 'Page Two',
  ], t('Filter'));
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $drupal_settings = $this
    ->getDrupalSettings();
  $this
    ->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);
  $this
    ->assertEquals('MISS', $drupal_settings['viewsAjaxGetCacheHeader']);

  // Verify that only the "Page Two" Node is present.
  $html = $session
    ->getPage()
    ->getHtml();
  $this
    ->assertContains('Page Two', $html);
  $this
    ->assertNotContains('Page One', $html);

  // Search for "Page One".
  $this
    ->submitForm([
    'title' => 'Page One',
  ], t('Filter'));
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // AJAX request should be a get and a hit now.
  $drupal_settings = $this
    ->getDrupalSettings();
  $this
    ->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);

  // @todo Check for X-Drupal-Cache header is HIT. Right now it's coming
  // up in the XHR response header as MISS, event though it seems to be HIT
  // server side.
  // Verify that only the "Page One" Node is present.
  $html = $session
    ->getPage()
    ->getHtml();
  $this
    ->assertContains('Page One', $html);
  $this
    ->assertNotContains('Page Two', $html);

  // Search for "Page Two".
  $this
    ->submitForm([
    'title' => 'Page Two',
  ], t('Filter'));
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $drupal_settings = $this
    ->getDrupalSettings();
  $this
    ->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);

  // @todo Check for X-Drupal-Cache header is HIT. Right now it's coming
  // up in the XHR response header as MISS, event though it seems to be HIT
  // server side.
  // Verify that only the "Page Two" Node is present.
  $html = $session
    ->getPage()
    ->getHtml();
  $this
    ->assertContains('Page Two', $html);
  $this
    ->assertNotContains('Page One', $html);
}