You are here

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

Test ctools_views "offset" configuration.

File

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

Class

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

Namespace

Drupal\Tests\ctools_views\Functional

Code

public function testOffset() {
  $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][pager_offset]"]'), '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 pager offset default settings.
  $this
    ->drupalGet('<front>');
  $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,
    3,
  ], $results);

  // Override pager offset settings.
  $edit = [];
  $edit['region'] = 'sidebar_first';
  $edit['settings[override][items_per_page]'] = 0;
  $edit['settings[override][pager_offset]'] = 1;
  $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(1, $config['pager_offset'], "'Pager offset' is properly saved.");

  // Assert pager offset overridden settings.
  $this
    ->drupalGet('<front>');
  $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([
    2,
    3,
    4,
  ], $results);
}