View source
<?php
namespace Drupal\Tests\views\Kernel\Plugin;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
class RowRenderCacheTest extends ViewsKernelTestBase {
use UserCreationTrait;
protected static $modules = [
'user',
'node',
];
public static $testViews = [
'test_row_render_cache',
'test_row_render_cache_none',
];
protected $editorUser;
protected $powerUser;
protected $regularUser;
protected function setUpFixtures() {
parent::setUpFixtures();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installSchema('node', 'node_access');
$type = NodeType::create([
'type' => 'test',
]);
$type
->save();
$this->editorUser = $this
->createUser([
'bypass node access',
]);
$this->powerUser = $this
->createUser([
'access content',
'create test content',
'edit own test content',
'delete own test content',
]);
$this->regularUser = $this
->createUser([
'access content',
]);
for ($i = 0; $i < 5; $i++) {
Node::create([
'title' => 'b' . $i . $this
->randomMachineName(),
'type' => 'test',
])
->save();
}
Node::create([
'title' => 'z' . $this
->randomMachineName(),
'uid' => $this->powerUser
->id(),
'type' => 'test',
])
->save();
}
public function testAdvancedCaching() {
$this
->doTestRenderedOutput($this->editorUser);
$this
->doTestRenderedOutput($this->editorUser, TRUE);
$this
->doTestRenderedOutput($this->powerUser);
$this
->doTestRenderedOutput($this->powerUser, TRUE);
$this
->doTestRenderedOutput($this->regularUser);
$this
->doTestRenderedOutput($this->regularUser, TRUE);
$this
->doTestRenderedOutput($this->editorUser);
$node = Node::load(6);
$node
->setTitle('a' . $this
->randomMachineName());
$node
->save();
$this
->doTestRenderedOutput($this->editorUser);
}
public function testNoCaching() {
$this
->setCurrentUser($this->regularUser);
$view = Views::getView('test_row_render_cache_none');
$view
->setDisplay();
$view
->preview();
$render_cache = $this->container
->get('render_cache');
$cache_plugin = $view->display_handler
->getPlugin('cache');
foreach ($view->result as $row) {
$keys = $cache_plugin
->getRowCacheKeys($row);
$cache = [
'#cache' => [
'keys' => $keys,
'contexts' => [
'languages:language_interface',
'theme',
'user.permissions',
],
],
];
$element = $render_cache
->get($cache);
$this
->assertFalse($element);
}
}
protected function doTestRenderedOutput(AccountInterface $account, $check_cache = FALSE) {
$this
->setCurrentUser($account);
$view = Views::getView('test_row_render_cache');
$view
->setDisplay();
$view
->preview();
$render_cache = $this->container
->get('render_cache');
$cache_plugin = $view->display_handler
->getPlugin('cache');
$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
->assertEquals($expected, $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
->assertEquals($expected, $output);
$expected = $access ? "<a href=\"{$node_url}/edit?destination=/\" hreflang=\"en\">edit</a>" : "";
$output = $view->style_plugin
->getField($index, 'edit_node');
$this
->assertEquals($expected, $output);
$expected = $access ? "<a href=\"{$node_url}/delete?destination=/\" hreflang=\"en\">delete</a>" : "";
$output = $view->style_plugin
->getField($index, 'delete_node');
$this
->assertEquals($expected, $output);
$expected = $access ? ' <div class="dropbutton-wrapper"><div class="dropbutton-widget"><ul class="dropbutton">' . '<li><a href="' . $node_url . '/edit?destination=/" hreflang="en">Edit</a></li>' . '<li><a href="' . $node_url . '/delete?destination=/" hreflang="en">Delete</a></li>' . '</ul></div></div>' : '';
$output = $view->style_plugin
->getField($index, 'operations');
$this
->assertEquals($expected, $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++;
}
}
}