1<?php 2 3namespace Drupal\Tests\taxonomy\Kernel\Views; 4 5use Drupal\Core\Link; 6use Drupal\Core\Render\RenderContext; 7use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait; 8use Drupal\Tests\views\Kernel\ViewsKernelTestBase; 9use Drupal\views\Tests\ViewTestData; 10use Drupal\views\Views; 11 12/** 13 * Tests the taxonomy term TID field handler. 14 * 15 * @group taxonomy 16 */ 17class TaxonomyFieldTidTest extends ViewsKernelTestBase { 18 19 use TaxonomyTestTrait; 20 21 /** 22 * {@inheritdoc} 23 */ 24 public static $modules = [ 25 'taxonomy', 26 'taxonomy_test_views', 27 'text', 28 'filter', 29 ]; 30 31 /** 32 * {@inheritdoc} 33 */ 34 public static $testViews = ['test_taxonomy_tid_field']; 35 36 /** 37 * A taxonomy term to use in this test. 38 * 39 * @var \Drupal\taxonomy\TermInterface 40 */ 41 protected $term1; 42 43 /** 44 * {@inheritdoc} 45 */ 46 protected function setUp($import_test_views = TRUE) { 47 parent::setUp($import_test_views); 48 49 $this->installEntitySchema('taxonomy_term'); 50 $this->installConfig(['filter']); 51 52 /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */ 53 $vocabulary = $this->createVocabulary(); 54 $this->term1 = $this->createTerm($vocabulary); 55 56 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']); 57 } 58 59 /** 60 * Tests the taxonomy field handler. 61 */ 62 public function testViewsHandlerTidField() { 63 /** @var \Drupal\Core\Render\RendererInterface $renderer */ 64 $renderer = \Drupal::service('renderer'); 65 66 $view = Views::getView('test_taxonomy_tid_field'); 67 $this->executeView($view); 68 69 $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) { 70 return $view->field['name']->advancedRender($view->result[0]); 71 }); 72 $expected = Link::fromTextAndUrl($this->term1->label(), $this->term1->toUrl()); 73 74 $this->assertEquals($expected->toString(), $actual); 75 } 76 77} 78