View source
<?php
namespace Drupal\Tests\views_ajax_get\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
class ExposedFilterViewsAjaxGetTest extends WebDriverTestBase {
public static $modules = [
'views_ajax_get_cache_test',
];
protected $defaultTheme = 'stark';
public function testExposedFiltering() {
$this
->createContentType([
'type' => 'page',
]);
$this
->createNode([
'title' => 'Page One',
]);
$this
->createNode([
'title' => 'Page Two',
]);
$config = $this
->config('system.performance');
$config
->set('cache.page.max_age', 300);
$config
->save();
$this
->drupalGet('views-ajax-get-cache-test');
$this
->addAjaxCompleteHandler();
$session = $this
->getSession();
$this
->submitForm([
'title' => 'Page One',
], t('Filter'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$drupal_settings = $this
->getDrupalSettings();
$this
->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);
$this
->assertEquals('MISS', $drupal_settings['viewsAjaxGetCacheHeader']);
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
$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']);
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page Two', $html);
$this
->assertStringNotContainsString('Page One', $html);
$this
->submitForm([
'title' => 'Page One',
], t('Filter'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$drupal_settings = $this
->getDrupalSettings();
$this
->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
$this
->submitForm([
'title' => 'Page Two',
], t('Filter'));
$this
->assertSession()
->assertWaitOnAjaxRequest();
$drupal_settings = $this
->getDrupalSettings();
$this
->assertEquals('GET', $drupal_settings['viewsAjaxGetMethod']);
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page Two', $html);
$this
->assertStringNotContainsString('Page One', $html);
}
protected function addAjaxCompleteHandler() {
$javascript = <<<JS
(function(\$, drupalSettings) {
\$(document).on('ajaxComplete', function(event, xhr, settings) {
drupalSettings.viewsAjaxGetMethod = settings.type;
drupalSettings.viewsAjaxGetCacheHeader = xhr.getResponseHeader('X-Drupal-Cache');
});
}(jQuery, drupalSettings));
JS;
$this
->getSession()
->executeScript($javascript);
}
}