You are here

public function UrlTest::testDrupalGetQueryParameters in Drupal 8

Tests UrlHelper::filterQueryParameters().

File

core/modules/system/tests/src/Functional/Common/UrlTest.php, line 222

Class

UrlTest
Confirm that \Drupal\Core\Url, \Drupal\Component\Utility\UrlHelper::filterQueryParameters(), \Drupal\Component\Utility\UrlHelper::buildQuery(), and \Drupal\Core\Utility\LinkGeneratorInterface::generate() work correctly with various input.

Namespace

Drupal\Tests\system\Functional\Common

Code

public function testDrupalGetQueryParameters() {
  $original = [
    'a' => 1,
    'b' => [
      'd' => 4,
      'e' => [
        'f' => 5,
      ],
    ],
    'c' => 3,
  ];

  // First-level exclusion.
  $result = $original;
  unset($result['b']);
  $this
    ->assertEqual(UrlHelper::filterQueryParameters($original, [
    'b',
  ]), $result, "'b' was removed.");

  // Second-level exclusion.
  $result = $original;
  unset($result['b']['d']);
  $this
    ->assertEqual(UrlHelper::filterQueryParameters($original, [
    'b[d]',
  ]), $result, "'b[d]' was removed.");

  // Third-level exclusion.
  $result = $original;
  unset($result['b']['e']['f']);
  $this
    ->assertEqual(UrlHelper::filterQueryParameters($original, [
    'b[e][f]',
  ]), $result, "'b[e][f]' was removed.");

  // Multiple exclusions.
  $result = $original;
  unset($result['a'], $result['b']['e'], $result['c']);
  $this
    ->assertEqual(UrlHelper::filterQueryParameters($original, [
    'a',
    'b[e]',
    'c',
  ]), $result, "'a', 'b[e]', 'c' were removed.");
}