function DisplayPageTest::testReadMore

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php \Drupal\Tests\views\Kernel\Plugin\DisplayPageTest::testReadMore()
  2. 8.9.x core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php \Drupal\Tests\views\Kernel\Plugin\DisplayPageTest::testReadMore()
  3. 10 core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php \Drupal\Tests\views\Kernel\Plugin\DisplayPageTest::testReadMore()

Tests the readmore functionality.

File

core/modules/views/tests/src/Kernel/Plugin/DisplayPageTest.php, line 165

Class

DisplayPageTest
Tests the page display plugin.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

public function testReadMore() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $this->container
        ->get('renderer');
    $expected_more_text = 'custom more text';
    $view = Views::getView('test_display_more');
    $this->executeView($view);
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->setRawContent($output);
    $result = $this->xpath('//div[@class=:class]/a', [
        ':class' => 'more-link',
    ]);
    $this->assertEquals(Url::fromRoute('view.test_display_more.page_1')->toString(), $result[0]->attributes()->href, 'The right more link is shown.');
    $this->assertEquals($expected_more_text, trim((string) $result[0][0]), 'The right link text is shown.');
    // Test the renderMoreLink method directly. This could be directly unit
    // tested.
    $more_link = $view->display_handler
        ->renderMoreLink();
    $more_link = $renderer->renderRoot($more_link);
    $this->setRawContent($more_link);
    $result = $this->xpath('//div[@class=:class]/a', [
        ':class' => 'more-link',
    ]);
    $this->assertEquals(Url::fromRoute('view.test_display_more.page_1')->toString(), $result[0]->attributes()->href, 'The right more link is shown.');
    $this->assertEquals($expected_more_text, trim((string) $result[0][0]), 'The right link text is shown.');
    // Test the useMoreText method directly. This could be directly unit
    // tested.
    $more_text = $view->display_handler
        ->useMoreText();
    $this->assertEquals($expected_more_text, $more_text, 'The right more text is chosen.');
    $view = Views::getView('test_display_more');
    $view->setDisplay();
    $view->display_handler
        ->setOption('use_more', 0);
    $this->executeView($view);
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->setRawContent($output);
    $result = $this->xpath('//div[@class=:class]/a', [
        ':class' => 'more-link',
    ]);
    $this->assertEmpty($result, 'The more link is not shown.');
    $view = Views::getView('test_display_more');
    $view->setDisplay();
    $view->display_handler
        ->setOption('use_more', 0);
    $view->display_handler
        ->setOption('use_more_always', 0);
    $view->display_handler
        ->setOption('pager', [
        'type' => 'some',
        'options' => [
            'items_per_page' => 1,
            'offset' => 0,
        ],
    ]);
    $this->executeView($view);
    $output = $view->preview();
    $output = $renderer->renderRoot($output);
    $this->setRawContent($output);
    $result = $this->xpath('//div[@class=:class]/a', [
        ':class' => 'more-link',
    ]);
    $this->assertEmpty($result, 'The more link is not shown when view has more records.');
    // Test the default value of use_more_always.
    $view = View::create()->getExecutable();
    $this->assertTrue($view->getDisplay()
        ->getOption('use_more_always'), 'Always display the more link by default.');
}

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