3 namespace Drupal\Tests\taxonomy\Functional\Views;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\Core\Language\LanguageInterface;
7 use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
8 use Drupal\Tests\views\Functional\ViewTestBase;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\taxonomy\Entity\Vocabulary;
11 use Drupal\taxonomy\Entity\Term;
14 * Base class for all taxonomy tests.
16 abstract class TaxonomyTestBase extends ViewTestBase {
18 use EntityReferenceTestTrait;
25 public static $modules = ['taxonomy', 'taxonomy_test_views'];
28 * Stores the nodes used for the different tests.
30 * @var \Drupal\node\NodeInterface[]
32 protected $nodes = [];
35 * The vocabulary used for creating terms.
37 * @var \Drupal\taxonomy\VocabularyInterface
39 protected $vocabulary;
42 * Stores the first term used in the different tests.
44 * @var \Drupal\taxonomy\TermInterface
49 * Stores the second term used in the different tests.
51 * @var \Drupal\taxonomy\TermInterface
58 protected function setUp($import_test_views = TRUE) {
59 parent::setUp($import_test_views);
60 $this->mockStandardInstall();
62 if ($import_test_views) {
63 ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
66 $this->term1 = $this->createTerm();
67 $this->term2 = $this->createTerm();
70 $node['type'] = 'article';
71 $node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
72 $node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
73 $this->nodes[] = $this->drupalCreateNode($node);
74 $this->nodes[] = $this->drupalCreateNode($node);
78 * Provides a workaround for the inability to use the standard profile.
80 * @see https://www.drupal.org/node/1708692
82 protected function mockStandardInstall() {
83 $this->drupalCreateContentType([
86 // Create the vocabulary for the tag field.
87 $this->vocabulary = Vocabulary::create([
88 'name' => 'Views testing tags',
89 'vid' => 'views_testing_tags',
91 $this->vocabulary->save();
92 $field_name = 'field_' . $this->vocabulary->id();
96 $this->vocabulary->id() => $this->vocabulary->id(),
98 'auto_create' => TRUE,
100 $this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
102 entity_get_form_display('node', 'article', 'default')
103 ->setComponent($field_name, [
104 'type' => 'entity_reference_autocomplete_tags',
109 entity_get_display('node', 'article', 'default')
110 ->setComponent($field_name, [
111 'type' => 'entity_reference_label',
115 entity_get_display('node', 'article', 'teaser')
116 ->setComponent($field_name, [
117 'type' => 'entity_reference_label',
124 * Creates and returns a taxonomy term.
126 * @param array $settings
127 * (optional) An array of values to override the following default
128 * properties of the term:
129 * - name: A random string.
130 * - description: A random string.
131 * - format: First available text format.
132 * - vid: Vocabulary ID of self::$vocabulary object.
133 * - langcode: LANGCODE_NOT_SPECIFIED.
134 * Defaults to an empty array.
136 * @return \Drupal\taxonomy\Entity\Term
137 * The created taxonomy term.
139 protected function createTerm(array $settings = []) {
140 $filter_formats = filter_formats();
141 $format = array_pop($filter_formats);
143 'name' => $this->randomMachineName(),
144 'description' => $this->randomMachineName(),
145 // Use the first available text format.
146 'format' => $format->id(),
147 'vid' => $this->vocabulary->id(),
148 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
150 $term = Term::create($settings);