You are here

public function DisplayTest::testReadMoreCustomURL in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testReadMoreCustomURL()

Tests the readmore with custom URL.

File

core/modules/views/tests/src/Functional/Plugin/DisplayTest.php, line 204

Class

DisplayTest
Tests the basic display plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testReadMoreCustomURL() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $view = Views::getView('test_display_more');
  $view
    ->setDisplay('default');
  $view->display_handler
    ->setOption('use_more', 1);
  $view->display_handler
    ->setOption('use_more_always', 1);
  $view->display_handler
    ->setOption('link_display', 'custom_url');

  // Test more link without leading slash.
  $view->display_handler
    ->setOption('link_url', 'node');
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node', $output, 'The read more link with href "/node" was found.');

  // Test more link with leading slash.
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', '/node');
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node', $output, 'The read more link with href "/node" was found.');

  // Test more link with absolute url.
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', 'http://drupal.org');
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('http://drupal.org', $output, 'The read more link with href "http://drupal.org" was found.');

  // Test more link with query parameters in the url.
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', 'node?page=1&foo=bar');
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node?page=1&foo=bar', $output, 'The read more link with href "/node?page=1&foo=bar" was found.');

  // Test more link with fragment in the url.
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', 'node#target');
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node#target', $output, 'The read more link with href "/node#target" was found.');

  // Test more link with arguments.
  $view = Views::getView('test_simple_argument');
  $view
    ->setDisplay('default');
  $view->display_handler
    ->setOption('use_more', 1);
  $view->display_handler
    ->setOption('use_more_always', 1);
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', 'node?date={{ raw_arguments.age }}&foo=bar');
  $view
    ->setArguments([
    22,
  ]);
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node?date=22&foo=bar', $output, 'The read more link with href "/node?date=22&foo=bar" was found.');

  // Test more link with 1 dimension array query parameters with arguments.
  $view = Views::getView('test_simple_argument');
  $view
    ->setDisplay('default');
  $view->display_handler
    ->setOption('use_more', 1);
  $view->display_handler
    ->setOption('use_more_always', 1);
  $view->display_handler
    ->setOption('link_display', 'custom_url');
  $view->display_handler
    ->setOption('link_url', '/node?f[0]=foo:bar&f[1]=foo:{{ raw_arguments.age }}');
  $view
    ->setArguments([
    22,
  ]);
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node?f%5B0%5D=foo%3Abar&f%5B1%5D=foo%3A22', $output, 'The read more link with href "/node?f[0]=foo:bar&f[1]=foo:22" was found.');

  // Test more link with arguments in path.
  $view->display_handler
    ->setOption('link_url', 'node/{{ raw_arguments.age }}?date={{ raw_arguments.age }}&foo=bar');
  $view
    ->setArguments([
    22,
  ]);
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node/22?date=22&foo=bar', $output, 'The read more link with href "/node/22?date=22&foo=bar" was found.');

  // Test more link with arguments in fragment.
  $view->display_handler
    ->setOption('link_url', 'node?date={{ raw_arguments.age }}&foo=bar#{{ raw_arguments.age }}');
  $view
    ->setArguments([
    22,
  ]);
  $this
    ->executeView($view);
  $output = $view
    ->preview();
  $output = $renderer
    ->renderRoot($output);
  $this
    ->assertStringContainsString('/node?date=22&foo=bar#22', $output, 'The read more link with href "/node?date=22&foo=bar#22" was found.');
}