3 namespace Drupal\Tests\taxonomy\Kernel\Views;
5 use Drupal\Core\Render\RenderContext;
6 use Drupal\Tests\taxonomy\Functional\TaxonomyTestTrait;
7 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
8 use Drupal\user\Entity\User;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\views\Views;
11 use Drupal\taxonomy\Entity\Vocabulary;
14 * Tests the taxonomy term VID field handler.
18 class TaxonomyFieldVidTest extends ViewsKernelTestBase {
20 use TaxonomyTestTrait;
27 public static $modules = ['taxonomy', 'taxonomy_test_views', 'text', 'filter'];
30 * Views used by this test.
34 public static $testViews = ['test_taxonomy_vid_field'];
37 * A taxonomy term to use in this test.
39 * @var \Drupal\taxonomy\Entity\Term
46 * @var \Drupal\user\Entity\User;
53 protected function setUp($import_test_views = TRUE) {
54 parent::setUp($import_test_views);
56 $this->installEntitySchema('taxonomy_term');
57 $this->installEntitySchema('user');
58 $this->installConfig(['filter']);
60 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
61 $vocabulary = $this->createVocabulary();
62 $this->term1 = $this->createTerm($vocabulary);
64 // Create user 1 and set is as the logged in user, so that the logged in
65 // user has the correct permissions to view the vocabulary name.
66 $this->adminUser = User::create(['name' => $this->randomString()]);
67 $this->adminUser->save();
68 $this->container->get('current_user')->setAccount($this->adminUser);
70 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
74 * Tests the field handling for the Vocabulary ID.
76 public function testViewsHandlerVidField() {
77 /** @var \Drupal\Core\Render\RendererInterface $renderer */
78 $renderer = \Drupal::service('renderer');
80 $view = Views::getView('test_taxonomy_vid_field');
81 $this->executeView($view);
83 $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
84 return $view->field['vid']->advancedRender($view->result[0]);
86 $vocabulary = Vocabulary::load($this->term1->bundle());
87 $expected = $vocabulary->get('name');
89 $this->assertEquals($expected, $actual);