TaxonomyFieldVidTest.php in Drupal 9
File
core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php
View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\Views;
use Drupal\Core\Render\RenderContext;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
use Drupal\taxonomy\Entity\Vocabulary;
class TaxonomyFieldVidTest extends ViewsKernelTestBase {
use TaxonomyTestTrait;
protected static $modules = [
'taxonomy',
'taxonomy_test_views',
'text',
'filter',
];
public static $testViews = [
'test_taxonomy_vid_field',
];
protected $term1;
protected $adminUser;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('taxonomy_term');
$this
->installEntitySchema('user');
$this
->installConfig([
'filter',
]);
$vocabulary = $this
->createVocabulary();
$this->term1 = $this
->createTerm($vocabulary);
$this->adminUser = User::create([
'name' => $this
->randomString(),
]);
$this->adminUser
->save();
$this->container
->get('current_user')
->setAccount($this->adminUser);
ViewTestData::createTestViews(static::class, [
'taxonomy_test_views',
]);
}
public function testViewsHandlerVidField() {
$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]);
});
$vocabulary = Vocabulary::load($this->term1
->bundle());
$expected = $vocabulary
->get('name');
$this
->assertEquals($expected, $actual);
}
}