function DisplayTest::testReadMoreCustomURL

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testReadMoreCustomURL()
  2. 8.9.x core/modules/views/tests/src/Functional/Plugin/DisplayTest.php \Drupal\Tests\views\Functional\Plugin\DisplayTest::testReadMoreCustomURL()
  3. 10 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 223

Class

DisplayTest
Tests the basic display plugin.

Namespace

Drupal\Tests\views\Functional\Plugin

Code

public function testReadMoreCustomURL() : void {
    
    /** @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 = (string) $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 = (string) $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://example.com');
    $this->executeView($view);
    $output = $view->preview();
    $output = (string) $renderer->renderRoot($output);
    $this->assertStringContainsString('http://example.com', $output, 'The read more link with href "http://example.com" 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 = (string) $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 = (string) $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 = (string) $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 = (string) $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 = (string) $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 = (string) $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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.