3 namespace Drupal\Tests\taxonomy\Kernel\Views;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
8 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
9 use Drupal\Tests\node\Traits\NodeCreationTrait;
10 use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
11 use Drupal\views\Tests\ViewTestData;
12 use Drupal\taxonomy\Entity\Vocabulary;
13 use Drupal\taxonomy\Entity\Term;
16 * Base class for views kernel taxonomy tests.
18 abstract class TaxonomyTestBase extends ViewsKernelTestBase {
20 use EntityReferenceTestTrait;
22 use NodeCreationTrait {
23 createNode as drupalCreateNode;
26 use ContentTypeCreationTrait {
27 createContentType as drupalCreateContentType;
35 public static $modules = [
37 'taxonomy_test_views',
45 * Stores the nodes used for the different tests.
47 * @var \Drupal\node\NodeInterface[]
49 protected $nodes = [];
52 * The vocabulary used for creating terms.
54 * @var \Drupal\taxonomy\VocabularyInterface
56 protected $vocabulary;
59 * Stores the first term used in the different tests.
61 * @var \Drupal\taxonomy\TermInterface
66 * Stores the second term used in the different tests.
68 * @var \Drupal\taxonomy\TermInterface
75 protected function setUp($import_test_views = TRUE) {
76 parent::setUp($import_test_views);
78 // Install node config to create body field.
79 $this->installConfig(['node', 'filter']);
80 $this->installEntitySchema('user');
81 $this->installEntitySchema('taxonomy_term');
82 $this->mockStandardInstall();
84 if ($import_test_views) {
85 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
88 $this->term1 = $this->createTerm();
89 $this->term2 = $this->createTerm();
92 $node['type'] = 'article';
93 $node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
94 $node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
95 $this->nodes[] = $this->drupalCreateNode($node);
96 $this->nodes[] = $this->drupalCreateNode($node);
100 * Provides a workaround for the inability to use the standard profile.
102 * @see https://www.drupal.org/node/1708692
104 protected function mockStandardInstall() {
105 $this->drupalCreateContentType([
109 // Create the vocabulary for the tag field.
110 $this->vocabulary = Vocabulary::create([
111 'name' => 'Views testing tags',
112 'vid' => 'views_testing_tags',
114 $this->vocabulary->save();
115 $field_name = 'field_' . $this->vocabulary->id();
117 $handler_settings = [
118 'target_bundles' => [
119 $this->vocabulary->id() => $this->vocabulary->id(),
121 'auto_create' => TRUE,
124 $this->installEntitySchema('node');
125 $this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
126 $entity_type_manager = $this->container->get('entity_type.manager');
128 ->getStorage('entity_form_display')
129 ->load('node.article.default')
130 ->setComponent($field_name, [
131 'type' => 'entity_reference_autocomplete_tags',
140 foreach ($view_modes as $view_mode) {
142 ->getStorage('entity_view_display')
143 ->load("node.article.{$view_mode}")
144 ->setComponent($field_name, [
145 'type' => 'entity_reference_label',
153 * Creates and returns a taxonomy term.
155 * @param array $settings
156 * (optional) An array of values to override the following default
157 * properties of the term:
158 * - name: A random string.
159 * - description: A random string.
160 * - format: First available text format.
161 * - vid: Vocabulary ID of self::$vocabulary object.
162 * - langcode: LANGCODE_NOT_SPECIFIED.
163 * Defaults to an empty array.
165 * @return \Drupal\taxonomy\Entity\Term
166 * The created taxonomy term.
168 protected function createTerm(array $settings = []) {
169 $filter_formats = filter_formats();
170 $format = array_pop($filter_formats);
172 'name' => $this->randomMachineName(),
173 'description' => $this->randomMachineName(),
174 // Use the first available text format.
175 'format' => $format->id(),
176 'vid' => $this->vocabulary->id(),
177 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
179 $term = Term::create($settings);