View source
<?php
namespace Drupal\Tests\views\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\views\Tests\ViewTestData;
class PaginationAJAXTest extends WebDriverTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
protected static $modules = [
'node',
'views',
'views_test_config',
];
protected $defaultTheme = 'stark';
public static $testViews = [
'test_content_ajax',
];
protected $user;
protected function setUp() : void {
parent::setUp();
ViewTestData::createTestViews(self::class, [
'views_test_config',
]);
$this
->createContentType([
'type' => 'page',
]);
for ($i = 1; $i <= 11; $i++) {
$this
->createNode([
'title' => 'Node ' . $i . ' content',
'changed' => $i * 1000,
]);
}
$user = $this
->drupalCreateUser([
'administer site configuration',
'access content',
'access content overview',
]);
$this
->drupalLogin($user);
}
public function testBasicPagination() {
$this
->drupalGet('test-content-ajax');
$session_assert = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$settings = $this
->getDrupalSettings();
$expected_view_path = '/test-content-ajax';
$this
->assertEquals($expected_view_path, current($settings['views']['ajaxViews'])['view_path']);
$page
->selectFieldOption('edit-items-per-page', 5);
$page
->pressButton('Filter');
$session_assert
->assertWaitOnAjaxRequest();
$page
->clickLink('Updated');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(5, $rows);
$this
->assertStringContainsString('Node 1 content', $rows[0]
->getHtml());
$this
->clickLink('Go to page 2');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(5, $rows);
$this
->assertStringContainsString('Node 6 content', $rows[0]
->getHtml());
$link = $page
->findLink('Go to page 3');
$this
->assertEquals('?status=All&type=All&langcode=All&items_per_page=5&order=changed&sort=asc&title=&page=2', $link
->getAttribute('href'));
$this
->assertNoDuplicateAssetsOnPage();
$this
->clickLink('Go to page 3');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(1, $rows);
$this
->assertStringContainsString('Node 11 content', $rows[0]
->getHtml());
$this
->clickLink('Go to first page');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(5, $rows);
$this
->assertStringContainsString('Node 1 content', $rows[0]
->getHtml());
$this
->clickLink('Go to next page');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(5, $rows);
$this
->assertStringContainsString('Node 6 content', $rows[0]
->getHtml());
$this
->clickLink('Go to last page');
$session_assert
->assertWaitOnAjaxRequest();
$rows = $page
->findAll('css', 'tbody tr');
$this
->assertCount(1, $rows);
$this
->assertStringContainsString('Node 11 content', $rows[0]
->getHtml());
$settings = $this
->getDrupalSettings();
$this
->assertEquals($expected_view_path, current($settings['views']['ajaxViews'])['view_path']);
}
protected function assertNoDuplicateAssetsOnPage() : void {
$scripts = $this
->getSession()
->getPage()
->findAll('xpath', '//script');
$script_src = [];
foreach ($scripts as $script) {
$this
->assertNotContains($script
->getAttribute('src'), $script_src);
$script_src[] = $script
->getAttribute('src');
}
}
}