class TaxonomyFieldVidTest
Same name and namespace in other branches
- 11.x core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest
- 10 core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest
- 9 core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest
- 8.9.x core/modules/taxonomy/tests/src/Kernel/Views/TaxonomyFieldVidTest.php \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest
Tests the taxonomy term VID field handler.
Attributes
#[Group('taxonomy')]
#[RunTestsInSeparateProcesses]
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\Tests\DrupalTestCaseTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\PhpUnitCompatibilityTrait, \Prophecy\PhpUnit\ProphecyTrait, \Drupal\TestTools\Extension\DeprecationBridge\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\taxonomy\Kernel\Views\TaxonomyFieldVidTest uses \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait, \Drupal\Tests\user\Traits\UserCreationTrait extends \Drupal\Tests\views\Kernel\ViewsKernelTestBase
- class \Drupal\Tests\views\Kernel\ViewsKernelTestBase uses \Drupal\views\Tests\ViewResultAssertionTrait extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of TaxonomyFieldVidTest
File
-
core/
modules/ taxonomy/ tests/ src/ Kernel/ Views/ TaxonomyFieldVidTest.php, line 20
Namespace
Drupal\Tests\taxonomy\Kernel\ViewsView source
class TaxonomyFieldVidTest extends ViewsKernelTestBase {
use TaxonomyTestTrait;
use UserCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'taxonomy',
'taxonomy_test_views',
'text',
'filter',
];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = [
'test_taxonomy_vid_field',
];
/**
* An array of taxonomy term used in this test.
*
* @var \Drupal\taxonomy\Entity\Term[]
*/
protected $terms;
/**
* An admin user.
*
* @var \Drupal\user\Entity\User
*/
protected $adminUser;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('user');
$this->installConfig([
'filter',
]);
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary */
$vocabulary = $this->createVocabulary([
'vid' => 'aaa',
]);
$term = $this->createTerm($vocabulary);
$this->terms[$term->id()] = $term;
/** @var \Drupal\taxonomy\Entity\Vocabulary $vocabulary2 */
$vocabulary2 = $this->createVocabulary([
'vid' => 'bbb',
]);
$term = $this->createTerm($vocabulary2);
$this->terms[$term->id()] = $term;
$this->adminUser = $this->createUser([
'administer taxonomy',
]);
$this->container
->get('current_user')
->setAccount($this->adminUser);
ViewTestData::createTestViews(static::class, [
'taxonomy_test_views',
]);
}
/**
* Tests the field handling for the Vocabulary ID.
*/
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.