You are here

public function CToolsViewsBasicViewBlockTest::testItemsPerPage in Chaos Tool Suite (ctools) 8.3

Test ctools_views "items_per_page" configuration.

File

modules/ctools_views/tests/src/Functional/CToolsViewsBasicViewBlockTest.php, line 58

Class

CToolsViewsBasicViewBlockTest
Tests ctools_views block display plugin overrides settings from a basic View.

Namespace

Drupal\Tests\ctools_views\Functional

Code

public function testItemsPerPage() {
  $default_theme = $this
    ->config('system.theme')
    ->get('default');

  // Get the "Configure block" form for our Views block.
  $this
    ->drupalGet('admin/structure/block/add/views_block:ctools_views_test_view-block_pager/' . $default_theme);
  $this
    ->assertNotEmpty($this
    ->xpath('//input[@type="number" and @name="settings[override][items_per_page]"]'), 'items_per_page setting is a number field');

  // Add block to sidebar_first region with default settings.
  $edit = [];
  $edit['region'] = 'sidebar_first';
  $edit['settings[override][items_per_page]'] = 0;
  $this
    ->drupalPostForm('admin/structure/block/add/views_block:ctools_views_test_view-block_pager/' . $default_theme, $edit, $this
    ->t('Save block'));

  // Assert items per page default settings.
  $this
    ->drupalGet('<front>');
  $result = $this
    ->xpath('//div[contains(@class, "region-sidebar-first")]/div[contains(@class, "block-views")]/h2');
  $this
    ->assertEquals('CTools Views Pager Block', $result[0]
    ->getText());
  $this
    ->assertSession()
    ->pageTextContains('Showing 3 records on page 1');
  $this
    ->assertEquals(3, count($this
    ->xpath('//div[contains(@class, "view-display-id-block_pager")]//table/tbody/tr')));

  // Override items per page settings.
  $edit = [];
  $edit['region'] = 'sidebar_first';
  $edit['settings[override][items_per_page]'] = 2;
  $this
    ->drupalPostForm('admin/structure/block/manage/views_block__ctools_views_test_view_block_pager', $edit, $this
    ->t('Save block'));
  $block = $this->storage
    ->load('views_block__ctools_views_test_view_block_pager');
  $config = $block
    ->getPlugin()
    ->getConfiguration();
  $this
    ->assertEquals(2, $config['items_per_page'], "'Items per page' is properly saved.");

  // Assert items per page overridden settings.
  $this
    ->drupalGet('<front>');
  $result = $this
    ->xpath('//div[contains(@class, "region-sidebar-first")]/div[contains(@class, "block-views")]/h2');
  $this
    ->assertEquals('CTools Views Pager Block', $result[0]
    ->getText());
  $this
    ->assertSession()
    ->pageTextContains('Showing 2 records on page 1');
  $this
    ->assertEquals(2, count($this
    ->xpath('//div[contains(@class, "view-display-id-block_pager")]//table/tbody/tr')));
  $elements = $this
    ->xpath('//div[contains(@class, "view-display-id-block_pager")]//table//tr//td[contains(@class, "views-field-id")]');
  $results = array_map(function ($element) {
    return $element
      ->getText();
  }, $elements);
  $this
    ->assertEquals([
    1,
    2,
  ], $results);
}