View source
<?php
namespace Drupal\Tests\taxonomy\Kernel\Views;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
use Drupal\Tests\node\Traits\NodeCreationTrait;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
abstract class TaxonomyTestBase extends ViewsKernelTestBase {
use EntityReferenceTestTrait;
use UserCreationTrait;
use NodeCreationTrait {
createNode as drupalCreateNode;
}
use ContentTypeCreationTrait {
createContentType as drupalCreateContentType;
}
protected static $modules = [
'taxonomy',
'taxonomy_test_views',
'text',
'node',
'field',
'filter',
];
protected $nodes = [];
protected $vocabulary;
protected $term1;
protected $term2;
protected function setUp($import_test_views = TRUE) : void {
parent::setUp($import_test_views);
$this
->installEntitySchema('node');
$this
->installConfig([
'node',
'filter',
'taxonomy',
]);
$this
->installEntitySchema('user');
$this
->installEntitySchema('taxonomy_term');
$this
->mockStandardInstall();
if ($import_test_views) {
ViewTestData::createTestViews(static::class, [
'taxonomy_test_views',
]);
}
$this->term1 = $this
->createTerm();
$this->term2 = $this
->createTerm();
$node = [];
$node['type'] = 'article';
$node['field_views_testing_tags'][]['target_id'] = $this->term1
->id();
$node['field_views_testing_tags'][]['target_id'] = $this->term2
->id();
$this->nodes[] = $this
->drupalCreateNode($node);
$this->nodes[] = $this
->drupalCreateNode($node);
}
protected function mockStandardInstall() {
$this
->drupalCreateContentType([
'type' => 'article',
]);
$this->vocabulary = Vocabulary::create([
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
]);
$this->vocabulary
->save();
$field_name = 'field_' . $this->vocabulary
->id();
$handler_settings = [
'target_bundles' => [
$this->vocabulary
->id() => $this->vocabulary
->id(),
],
'auto_create' => TRUE,
];
$this
->installEntitySchema('node');
$this
->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
$entity_type_manager = $this->container
->get('entity_type.manager');
$entity_type_manager
->getStorage('entity_form_display')
->load('node.article.default')
->setComponent($field_name, [
'type' => 'entity_reference_autocomplete_tags',
'weight' => -4,
])
->save();
$view_modes = [
'default',
'teaser',
];
foreach ($view_modes as $view_mode) {
$entity_type_manager
->getStorage('entity_view_display')
->load("node.article.{$view_mode}")
->setComponent($field_name, [
'type' => 'entity_reference_label',
'weight' => 10,
])
->save();
}
}
protected function createTerm(array $settings = []) {
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
$settings += [
'name' => $this
->randomMachineName(),
'description' => $this
->randomMachineName(),
'format' => $format
->id(),
'vid' => $this->vocabulary
->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
];
$term = Term::create($settings);
$term
->save();
return $term;
}
}