function TaxonomyFieldVidTest::testViewsHandlerVidField

Same name in other branches
  1. 9 core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest::testViewsHandlerVidField()
  2. 8.9.x core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest::testViewsHandlerVidField()
  3. 11.x core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest::testViewsHandlerVidField()

Tests the field handling for the Vocabulary ID.

File

core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php, line 84

Class

TaxonomyFieldVidTest
Tests the taxonomy term VID field handler.

Namespace

Drupal\Tests\taxonomy\Kernel\Views

Code

public function testViewsHandlerVidField() : void {
    
    /** @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = \Drupal::service('renderer');
    $view = Views::getView('test_taxonomy_vid_field');
    $this->executeView($view);
    $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['vid']
            ->advancedRender($view->result[0]);
    });
    $tid = $view->result[0]->_entity
        ->id();
    $vocabulary = Vocabulary::load($this->terms[$tid]
        ->bundle());
    $expected = $vocabulary->get('name');
    $this->assertEquals($expected, $actual, 'Displayed vocabulary name should match that loaded from the term.');
    $this->assertEquals('aaa', $vocabulary->id(), 'First result should be vocabulary "aaa", due to ASC sorting.');
    // Reverse sorting.
    $view = Views::getView('test_taxonomy_vid_field');
    $view->setHandlerOption('default', 'sort', 'vid', 'order', 'DESC');
    $this->executeView($view);
    $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['vid']
            ->advancedRender($view->result[0]);
    });
    $tid = $view->result[0]->_entity
        ->id();
    $vocabulary = Vocabulary::load($this->terms[$tid]
        ->bundle());
    $expected = $vocabulary->get('name');
    $this->assertEquals($expected, $actual, 'Displayed vocabulary name should match that loaded from the term.');
    $this->assertEquals('bbb', $vocabulary->id(), 'First result should be vocabulary "bbb", due to DESC sorting.');
    // Test with user without 'view vocabulary labels' permission.
    $this->setUpCurrentUser();
    $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['vid']
            ->advancedRender($view->result[0]);
    });
    $expected = '';
    $this->assertEquals($expected, $actual);
    // Test with user with 'view vocabulary labels' permissions.
    $this->setUpCurrentUser([], [
        'view vocabulary labels',
    ]);
    $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['vid']
            ->advancedRender($view->result[0]);
    });
    $expected = $vocabulary->label();
    $this->assertEquals($expected, $actual);
    // Test with user with 'administer taxonomy' and 'access taxonomy overview'
    // permissions. Label should be displayed for either permission.
    $this->setUpCurrentUser([], [
        'administer taxonomy',
        'access taxonomy overview',
    ]);
    $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
        return $view->field['vid']
            ->advancedRender($view->result[0]);
    });
    $expected = $vocabulary->label();
    $this->assertEquals($expected, $actual);
}

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