function RowRenderCacheTest::doTestRenderedOutput

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

Check whether the rendered output matches expectations.

Parameters

\Drupal\Core\Session\AccountInterface $account: The user account to tests rendering with.

bool $check_cache: (optional) Whether explicitly test render cache entries.

1 call to RowRenderCacheTest::doTestRenderedOutput()
RowRenderCacheTest::testAdvancedCaching in core/modules/views/tests/src/Kernel/Plugin/RowRenderCacheTest.php
Tests complex field rewriting and uncacheable field handlers.

File

core/modules/views/tests/src/Kernel/Plugin/RowRenderCacheTest.php, line 146

Class

RowRenderCacheTest
Tests row render caching.

Namespace

Drupal\Tests\views\Kernel\Plugin

Code

protected function doTestRenderedOutput(AccountInterface $account, $check_cache = FALSE) : void {
    $this->setCurrentUser($account);
    $view = Views::getView('test_row_render_cache');
    $view->setDisplay();
    $view->preview();
    
    /** @var \Drupal\Core\Render\RenderCacheInterface $render_cache */
    $render_cache = $this->container
        ->get('render_cache');
    
    /** @var \Drupal\views\Plugin\views\cache\CachePluginBase $cache_plugin */
    $cache_plugin = $view->display_handler
        ->getPlugin('cache');
    // Retrieve nodes and sort them in alphabetical order to match view results.
    $nodes = Node::loadMultiple();
    usort($nodes, function (NodeInterface $a, NodeInterface $b) {
        return strcmp($a->label(), $b->label());
    });
    $index = 0;
    foreach ($nodes as $node) {
        $nid = $node->id();
        $access = $node->access('update');
        $counter = $index + 1;
        $expected = "{$nid}: {$counter} (just in case: {$nid})";
        $counter_output = $view->style_plugin
            ->getField($index, 'counter');
        $this->assertSame($expected, (string) $counter_output);
        $node_url = $node->toUrl()
            ->toString();
        $expected = "<a href=\"{$node_url}\"><span class=\"da-title\">{$node->label()}</span> <span class=\"counter\">{$counter_output}</span></a>";
        $output = $view->style_plugin
            ->getField($index, 'title');
        $this->assertSame($expected, (string) $output);
        $expected = $access ? "<a href=\"{$node_url}/edit?destination=/\" hreflang=\"en\">edit</a>" : "";
        $output = $view->style_plugin
            ->getField($index, 'edit_node');
        $this->assertSame($expected, (string) $output);
        $expected = $access ? "<a href=\"{$node_url}/delete?destination=/\" hreflang=\"en\">delete</a>" : "";
        $output = $view->style_plugin
            ->getField($index, 'delete_node');
        $this->assertSame($expected, (string) $output);
        $expected = $access ? '  <div class="dropbutton-wrapper" data-drupal-ajax-container><div class="dropbutton-widget"><ul class="dropbutton">' . '<li><a href="' . $node_url . '/edit?destination=/" aria-label="Edit ' . $node->label() . '" hreflang="en">Edit</a></li>' . '<li><a href="' . $node_url . '/delete?destination=/" aria-label="Delete ' . $node->label() . '" class="use-ajax" data-dialog-type="modal" data-dialog-options="' . Html::escape(Json::encode([
            'width' => 880,
        ])) . '" hreflang="en">Delete</a></li>' . '</ul></div></div>' : '';
        $output = $view->style_plugin
            ->getField($index, 'operations');
        $this->assertSame($expected, (string) $output);
        if ($check_cache) {
            $keys = $cache_plugin->getRowCacheKeys($view->result[$index]);
            $cache = [
                '#cache' => [
                    'keys' => $keys,
                    'contexts' => [
                        'languages:language_interface',
                        'theme',
                        'user.permissions',
                    ],
                ],
            ];
            $element = $render_cache->get($cache);
            $this->assertNotEmpty($element);
        }
        $index++;
    }
}

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