2fde283e32512efd5b342c6903e04293b7d92335
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Views / TaxonomyFieldVidTest.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Views;
4
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;
12
13 /**
14  * Tests the taxonomy term VID field handler.
15  *
16  * @group taxonomy
17  */
18 class TaxonomyFieldVidTest extends ViewsKernelTestBase {
19
20   use TaxonomyTestTrait;
21
22   /**
23    * Modules to enable.
24    *
25    * @var array
26    */
27   public static $modules = ['taxonomy', 'taxonomy_test_views', 'text', 'filter'];
28
29   /**
30    * Views used by this test.
31    *
32    * @var array
33    */
34   public static $testViews = ['test_taxonomy_vid_field'];
35
36   /**
37    * A taxonomy term to use in this test.
38    *
39    * @var \Drupal\taxonomy\Entity\Term
40    */
41   protected $term1;
42
43   /**
44    * An admin user.
45    *
46    * @var \Drupal\user\Entity\User;
47    */
48   protected $adminUser;
49
50   /**
51    * {@inheritdoc}
52    */
53   protected function setUp($import_test_views = TRUE) {
54     parent::setUp($import_test_views);
55
56     $this->installEntitySchema('taxonomy_term');
57     $this->installEntitySchema('user');
58     $this->installConfig(['filter']);
59
60     /** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
61     $vocabulary = $this->createVocabulary();
62     $this->term1 = $this->createTerm($vocabulary);
63
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);
69
70     ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
71   }
72
73   /**
74    * Tests the field handling for the Vocabulary ID.
75    */
76   public function testViewsHandlerVidField() {
77     /** @var \Drupal\Core\Render\RendererInterface $renderer */
78     $renderer = \Drupal::service('renderer');
79
80     $view = Views::getView('test_taxonomy_vid_field');
81     $this->executeView($view);
82
83     $actual = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
84       return $view->field['vid']->advancedRender($view->result[0]);
85     });
86     $vocabulary = Vocabulary::load($this->term1->bundle());
87     $expected = $vocabulary->get('name');
88
89     $this->assertEquals($expected, $actual);
90   }
91
92 }