Commit 14afb776 authored by alexpott's avatar alexpott

Issue #2530634 by larowlan, Lendude, acbramley, webflo, dawehner, alexpott:...

Issue #2530634 by larowlan, Lendude, acbramley, webflo, dawehner, alexpott: Value of last_render_text leaks into the next Dropbutton
parent d558fbef
......@@ -101,6 +101,13 @@ abstract class FieldPluginBase extends HandlerBase implements FieldHandlerInterf
*/
protected $renderer;
/**
* Keeps track of the last render index.
*
* @var int|NULL
*/
protected $lastRenderIndex;
/**
* {@inheritdoc}
*/
......@@ -1122,6 +1129,10 @@ public function postRender(ResultRow $row, $output) {
* {@inheritdoc}
*/
public function advancedRender(ResultRow $values) {
// Clean up values from previous render calls.
if ($this->lastRenderIndex != $values->index) {
$this->last_render_text = '';
}
if ($this->allowAdvancedRender() && $this instanceof MultiItemsFieldHandlerInterface) {
$raw_items = $this->getItems($values);
// If there are no items, set the original value to NULL.
......@@ -1181,7 +1192,10 @@ public function advancedRender(ResultRow $values) {
$this->last_render = $this->renderText($alter);
}
}
// If we rendered something, update the last render index.
if ((string) $this->last_render !== '') {
$this->lastRenderIndex = $values->index;
}
return $this->last_render;
}
......
......@@ -21,6 +21,8 @@ display:
display_options:
access:
type: perm
options:
perm: 'access content overview'
cache:
type: tag
query:
......@@ -159,6 +161,108 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: false
edit_node:
id: edit_node
table: node
field: edit_node
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: true
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
text: edit
entity_type: node
plugin_id: entity_link_edit
delete_node:
id: delete_node
table: node
field: delete_node
relationship: none
group_type: group
admin_label: ''
label: ''
exclude: true
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: 0
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
text: delete
entity_type: node
plugin_id: entity_link_delete
dropbutton:
id: dropbutton
table: views
......@@ -211,6 +315,8 @@ display:
fields:
title: title
nothing: nothing
edit_node: edit_node
delete_node: delete_node
nid: '0'
destination: true
filters:
......
<?php
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Core\Render\RenderContext;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\NodeCreationTrait;
use Drupal\simpletest\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
/**
* Tests the core Drupal\views\Plugin\views\field\Dropbutton handler.
*
* @group views
*/
class FieldDropbuttonTest extends ViewsKernelTestBase {
use ContentTypeCreationTrait;
use UserCreationTrait;
use NodeCreationTrait;
/**
* {@inheritdoc}
*/
public static $testViews = ['test_dropbutton'];
/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'user',
'node',
'field',
'text',
'filter',
'views',
];
/**
* Test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $node1;
/**
* Test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $node2;
/**
* Test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $node3;
/**
* Test user.
*
* @var \Drupal\user\UserInterface
*/
protected $testUser;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp(FALSE);
$this->installEntitySchema('node');
$this->installEntitySchema('user');
$this->installSchema('node', 'node_access');
$this->installConfig('node');
$this->installConfig('filter');
ViewTestData::createTestViews(get_class($this), ['views_test_config']);
// Create two node types.
$this->createContentType(['type' => 'foo']);
$this->createContentType(['type' => 'bar']);
// Create user 1.
$admin = $this->createUser();
// And three nodes.
$this->node1 = $this->createNode([
'type' => 'bar',
'title' => 'bazs',
'status' => 1,
'uid' => $admin->id(),
'created' => REQUEST_TIME - 10,
]);
$this->node2 = $this->createNode([
'type' => 'foo',
'title' => 'foos',
'status' => 1,
'uid' => $admin->id(),
'created' => REQUEST_TIME - 5,
]);
$this->node3 = $this->createNode([
'type' => 'bar',
'title' => 'bars',
'status' => 1,
'uid' => $admin->id(),
'created' => REQUEST_TIME,
]);
// Now create a user with the ability to edit bar but not foo.
$this->testUser = $this->createUser([
'access content overview',
'access content',
'edit any bar content',
'delete any bar content',
]);
// And switch to that user.
$this->container->get('account_switcher')->switchTo($this->testUser);
}
/**
* Tests that dropbutton markup doesn't leak between rows.
*/
public function testDropbuttonMarkupShouldNotLeakBetweenRows() {
$view = Views::getView('test_dropbutton');
$view->setDisplay();
$view->preExecute([]);
$view->execute();
$renderer = $this->container->get('renderer');
$dropbutton_output = [];
// Render each row and field in turn - the dropbutton plugin relies on
// output being set in previous versions.
foreach ($view->result as $index => $row) {
foreach (array_keys($view->field) as $field) {
$output = $renderer->executeInRenderContext(new RenderContext(), function () use ($view, $row, $field) {
return $view->field[$field]->advancedRender($row);
});
if ($field === 'dropbutton') {
$dropbutton_output[] = $output;
}
}
}
// The first row should contain edit links to node 3, as the user has
// access.
$this->assertContains($this->node3->toUrl('edit-form')->toString(), (string) $dropbutton_output[0]);
$this->assertContains($this->node3->toUrl('delete-form')->toString(), (string) $dropbutton_output[0]);
// Second row should be not contain links to edit/delete any content as user
// has no edit/delete permissions.
// It most certainly should not contain links to node 3, as node 2 is the
// entity that forms this row.
$this->assertNotContains($this->node3->toUrl('edit-form')->toString(), (string) $dropbutton_output[1]);
$this->assertNotContains($this->node3->toUrl('delete-form')->toString(), (string) $dropbutton_output[1]);
$this->assertNotContains($this->node2->toUrl('edit-form')->toString(), (string) $dropbutton_output[1]);
$this->assertNotContains($this->node2->toUrl('delete-form')->toString(), (string) $dropbutton_output[1]);
// Third row should contain links for node 1.
$this->assertContains($this->node1->toUrl('edit-form')->toString(), (string) $dropbutton_output[2]);
$this->assertContains($this->node1->toUrl('delete-form')->toString(), (string) $dropbutton_output[2]);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment