function FieldDropbuttonTest::testDropbuttonMarkupShouldNotLeakBetweenRows

Same name in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php \Drupal\Tests\views\Kernel\Handler\FieldDropbuttonTest::testDropbuttonMarkupShouldNotLeakBetweenRows()
  2. 8.9.x core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php \Drupal\Tests\views\Kernel\Handler\FieldDropbuttonTest::testDropbuttonMarkupShouldNotLeakBetweenRows()
  3. 11.x core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php \Drupal\Tests\views\Kernel\Handler\FieldDropbuttonTest::testDropbuttonMarkupShouldNotLeakBetweenRows()

Tests that dropbutton markup doesn't leak between rows.

File

core/modules/views/tests/src/Kernel/Handler/FieldDropbuttonTest.php, line 129

Class

FieldDropbuttonTest
Tests the core Drupal\views\Plugin\views\field\Dropbutton handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testDropbuttonMarkupShouldNotLeakBetweenRows() : void {
    $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->assertStringContainsString($this->node3
        ->toUrl('edit-form')
        ->toString(), (string) $dropbutton_output[0]);
    $this->assertStringContainsString($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->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]);
    // Third row should contain links for node 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]);
}

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