View source
<?php
namespace Drupal\Tests\views\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
class ExposedFilterAJAXTest extends WebDriverTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
protected static $modules = [
'node',
'views',
'views_test_modal',
];
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
\Drupal::configFactory()
->getEditable('views.view.content')
->set('display.default.display_options.use_ajax', TRUE)
->save();
$this
->createContentType([
'type' => 'page',
]);
$this
->createNode([
'title' => 'Page One',
]);
$this
->createNode([
'title' => 'Page Two',
]);
$user = $this
->drupalCreateUser([
'administer site configuration',
'access content',
'access content overview',
'edit any page content',
]);
$this
->drupalLogin($user);
}
public function testExposedFiltering() {
$this
->drupalGet('admin/content');
$session = $this
->getSession();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringContainsString('Page Two', $html);
$this
->submitForm([
'title' => 'Page One',
], 'Filter');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
$this
->submitForm([
'title' => 'Page Two',
], 'Filter');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page Two', $html);
$this
->assertStringNotContainsString('Page One', $html);
$this
->submitForm([
'action' => 'node_make_sticky_action',
'node_bulk_form[0]' => TRUE,
], 'Apply to selected items');
$this
->assertSession()
->pageTextContains('Make content sticky was applied to 1 item.');
$this
->submitForm([], 'Reset');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->pageTextContains('Page One');
$this
->assertSession()
->pageTextContains('Page Two');
$this
->assertFalse($session
->getPage()
->hasButton('Reset'));
}
public function testExposedFiltersInModal() {
$this
->drupalGet('views-test-modal/modal');
$assert = $this
->assertSession();
$assert
->elementExists('named', [
'link',
'Administer content',
])
->click();
$dialog = $assert
->waitForElementVisible('css', '.views-test-modal');
$session = $this
->getSession();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringContainsString('Page Two', $html);
$session
->getPage()
->fillField('title', 'Page One');
$assert
->elementExists('css', '.ui-dialog-buttonpane')
->pressButton('Filter');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
$assert
->buttonExists('Close', $dialog)
->press();
$assert
->elementExists('named', [
'link',
'Administer content',
])
->click();
$assert
->waitForElementVisible('css', '.views-test-modal');
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringContainsString('Page Two', $html);
$session
->getPage()
->fillField('title', 'Page One');
$assert
->elementExists('css', '.ui-dialog-buttonpane')
->pressButton('Filter');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
}
public function testExposedFilteringWithButtonElement() {
\Drupal::service('theme_installer')
->install([
'views_test_theme',
]);
$this
->config('system.theme')
->set('default', 'views_test_theme')
->save();
$this
->drupalGet('admin/content');
$session = $this
->getSession();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringContainsString('Page Two', $html);
$button_tag = $session
->getPage()
->findButton('edit-submit-content')
->getTagName();
$this
->assertEquals('button', $button_tag);
$drupal_settings = $this
->getDrupalSettings();
$ajax_views_before = $drupal_settings['views']['ajaxViews'];
$this
->submitForm([
'title' => 'Page One',
], 'Filter');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$html = $session
->getPage()
->getHtml();
$this
->assertStringContainsString('Page One', $html);
$this
->assertStringNotContainsString('Page Two', $html);
$drupal_settings = $this
->getDrupalSettings();
$ajax_views_after = $drupal_settings['views']['ajaxViews'];
$this
->assertSame($ajax_views_before, $ajax_views_after);
}
}