You are here

public function PagererTest::testAdaptiveKeysQuerystring in Pagerer 8.2

Test proper functioning of multiple adaptive keys pagers.

File

tests/src/Functional/PagererTest.php, line 622

Class

PagererTest
Checks Pagerer functionality.

Namespace

Drupal\Tests\pagerer\Functional

Code

public function testAdaptiveKeysQuerystring() {

  // Add more entries in the log.
  for ($i = 0; $i < 2700; $i++) {
    $this->container
      ->get('logger.factory')
      ->get('pager_test')
      ->debug($this
      ->randomString());
  }

  // Setup the core overriden pager to Adaptive without breakers.
  $this
    ->drupalGet($this->pagererAdmin . '/preset/add');
  $this
    ->submitForm([
    'label' => 'ui_test',
    'id' => 'ui_test',
  ], 'Create');
  $this
    ->drupalGet($this->pagererAdmin);
  $this
    ->submitForm([
    'core_override_preset' => 'ui_test',
  ], 'Save configuration');
  $this
    ->drupalGet($this->pagererAdmin . '/preset/manage/ui_test');
  $this
    ->submitForm([
    'panes_container[left][style]' => 'none',
    'panes_container[center][style]' => 'adaptive',
    'panes_container[right][style]' => 'none',
  ], 'Save');
  $this
    ->drupalGet($this->pagererAdmin . '/preset/manage/ui_test');
  $this
    ->click('[id="edit-panes-container-center-actions-configure"]');
  $this
    ->submitForm([
    'breaker_display' => FALSE,
  ], 'Save');

  // Test data.
  $test_data = [
    [
      'pager_id' => 0,
      'pager_item' => 7,
      'expected_querystring_core' => '?page=75%2C0%2C%2C%2C0&page_ak=37.150',
      'expected_querystring_pg_0' => '?pg=75.0...0-ak-37_150',
      'expected_querystring_px_1' => '?px=76.1...1-ak-38_151',
    ],
    [
      'pager_id' => 0,
      'pager_item' => 5,
      'expected_querystring_core' => '?page=74%2C0%2C%2C%2C0&page_ak=37.150.75',
      'expected_querystring_pg_0' => '?pg=74.0...0-ak-37_150_75',
      'expected_querystring_px_1' => '?px=75.1...1-ak-38_151_76',
    ],
    [
      'pager_id' => 0,
      'pager_item' => 7,
      'expected_querystring_core' => '?page=75%2C0%2C%2C%2C0&page_ak=37.150.75',
      'expected_querystring_pg_0' => '?pg=75.0...0-ak-37_150_75',
      'expected_querystring_px_1' => '?px=76.1...1-ak-38_151_76',
    ],
    [
      'pager_id' => 0,
      'pager_item' => 7,
      'expected_querystring_core' => '?page=76%2C0%2C%2C%2C0&page_ak=37.150.75',
      'expected_querystring_pg_0' => '?pg=76.0...0-ak-37_150_75',
      'expected_querystring_px_1' => '?px=77.1...1-ak-38_151_76',
    ],
    [
      'pager_id' => 0,
      'pager_item' => 7,
      'expected_querystring_core' => '?page=77%2C0%2C%2C%2C0&page_ak=37.150.75',
      'expected_querystring_pg_0' => '?pg=77.0...0-ak-37_150_75',
      'expected_querystring_px_1' => '?px=78.1...1-ak-38_151_76',
    ],
    [
      'pager_id' => 0,
      'pager_item' => 9,
      'expected_querystring_core' => '?page=93%2C0%2C%2C%2C0&page_ak=79.112',
      'expected_querystring_pg_0' => '?pg=93.0...0-ak-79_112',
      'expected_querystring_px_1' => '?px=94.1...1-ak-80_113',
    ],
    [
      'pager_id' => 1,
      'pager_item' => 3,
      'expected_querystring_core' => '?page=93%2C2%2C%2C%2C0&page_ak=79.112',
      'expected_querystring_pg_0' => '?pg=93.2...0-ak-79_112',
      'expected_querystring_px_1' => '?px=94.3...1-ak-80_113',
    ],
    [
      'pager_id' => 4,
      'pager_item' => 8,
      'expected_querystring_core' => '?page=93%2C2%2C%2C%2C150&page_ak=79.112',
      'expected_querystring_pg_0' => '?pg=93.2...150-ak-79_112',
      'expected_querystring_px_1' => '?px=94.3...151-ak-80_113',
    ],
    [
      'pager_id' => 4,
      'pager_item' => 2,
      'expected_querystring_core' => '?page=93%2C2%2C%2C%2C75&page_ak=79.112%2C%2C%2C%2C0.113',
      'expected_querystring_pg_0' => '?pg=93.2...75-ak-79_112....0_113',
      'expected_querystring_px_1' => '?px=94.3...76-ak-80_113....1_114',
    ],
  ];

  // Loop through test sets.
  foreach ([
    'core',
    'pg_0',
    'px_1',
  ] as $set) {
    $match_querystring = 'expected_querystring_' . $set;
    switch ($set) {
      case 'core':
        break;
      case 'pg_0':

        // Override: 'pg' and 0-based page numbers.
        $this
          ->drupalGet($this->pagererAdmin . '/url_settings');
        $this
          ->submitForm([
          'core_override_querystring' => TRUE,
        ], 'Save configuration');
        break;
      case 'px_1':

        // Override: 'px' and 1-based page numbers.
        $this
          ->drupalGet($this->pagererAdmin . '/url_settings');
        $this
          ->submitForm([
          'index_base' => 1,
          'querystring_key' => 'px',
        ], 'Save configuration');
        break;
    }

    // First page.
    $this
      ->drupalGet('pager-test/multiple-pagers');

    // Loop through test pager clicks.
    foreach ($test_data as $id => $test) {
      $elements = $this
        ->xpath('//div[contains(@class, :pager)]//ul[contains(@class, :items)]/li[' . $test['pager_item'] . ']/a', [
        ':pager' => 'test-pager-' . $test['pager_id'],
        ':items' => 'pager__items',
      ]);
      $this
        ->assertStringContainsString($test[$match_querystring], $elements[0]
        ->getAttribute('href'), "Test {$id}");
      $elements[0]
        ->click();
      $this
        ->assertStringContainsString('pager-test/multiple-pagers' . $test[$match_querystring], $this
        ->getUrl(), "Test {$id}");
    }
  }
}