View source
<?php
namespace Drupal\Tests\views\Kernel\Handler;
use Drupal\Core\Render\RenderContext;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
class FieldDropbuttonTest extends ViewsKernelTestBase {
use ContentTypeCreationTrait;
use UserCreationTrait;
use NodeCreationTrait;
public static $testViews = [
'test_dropbutton',
];
protected static $modules = [
'system',
'user',
'node',
'field',
'text',
'filter',
'views',
];
protected $node1;
protected $node2;
protected $node3;
protected $testUser;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp(FALSE);
$this
->installEntitySchema('node');
$this
->installEntitySchema('user');
$this
->installSchema('node', 'node_access');
$this
->installConfig('node');
$this
->installConfig('filter');
ViewTestData::createTestViews(static::class, [
'views_test_config',
]);
$this
->createContentType([
'type' => 'foo',
]);
$this
->createContentType([
'type' => 'bar',
]);
$admin = $this
->createUser();
$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,
]);
$this->testUser = $this
->createUser([
'access content overview',
'access content',
'edit any bar content',
'delete any bar content',
]);
$this->container
->get('account_switcher')
->switchTo($this->testUser);
}
public function testDropbuttonMarkupShouldNotLeakBetweenRows() {
$view = Views::getView('test_dropbutton');
$view
->setDisplay();
$view
->preExecute([]);
$view
->execute();
$renderer = $this->container
->get('renderer');
$dropbutton_output = [];
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;
}
}
}
$this
->assertStringContainsString($this->node3
->toUrl('edit-form')
->toString(), (string) $dropbutton_output[0]);
$this
->assertStringContainsString($this->node3
->toUrl('delete-form')
->toString(), (string) $dropbutton_output[0]);
$this
->assertStringNotContainsString($this->node3
->toUrl('edit-form')
->toString(), (string) $dropbutton_output[1]);
$this
->assertStringNotContainsString($this->node3
->toUrl('delete-form')
->toString(), (string) $dropbutton_output[1]);
$this
->assertStringNotContainsString($this->node2
->toUrl('edit-form')
->toString(), (string) $dropbutton_output[1]);
$this
->assertStringNotContainsString($this->node2
->toUrl('delete-form')
->toString(), (string) $dropbutton_output[1]);
$this
->assertStringContainsString($this->node1
->toUrl('edit-form')
->toString(), (string) $dropbutton_output[2]);
$this
->assertStringContainsString($this->node1
->toUrl('delete-form')
->toString(), (string) $dropbutton_output[2]);
}
}