Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / taxonomy / tests / src / Kernel / Views / TaxonomyTestBase.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Kernel\Views;
4
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;
14
15 /**
16  * Base class for views kernel taxonomy tests.
17  */
18 abstract class TaxonomyTestBase extends ViewsKernelTestBase {
19
20   use EntityReferenceTestTrait;
21
22   use NodeCreationTrait {
23     createNode as drupalCreateNode;
24   }
25
26   use ContentTypeCreationTrait {
27     createContentType as drupalCreateContentType;
28   }
29
30   /**
31    * Modules to enable.
32    *
33    * @var array
34    */
35   public static $modules = [
36     'taxonomy',
37     'taxonomy_test_views',
38     'text',
39     'node',
40     'field',
41     'filter',
42   ];
43
44   /**
45    * Stores the nodes used for the different tests.
46    *
47    * @var \Drupal\node\NodeInterface[]
48    */
49   protected $nodes = [];
50
51   /**
52    * The vocabulary used for creating terms.
53    *
54    * @var \Drupal\taxonomy\VocabularyInterface
55    */
56   protected $vocabulary;
57
58   /**
59    * Stores the first term used in the different tests.
60    *
61    * @var \Drupal\taxonomy\TermInterface
62    */
63   protected $term1;
64
65   /**
66    * Stores the second term used in the different tests.
67    *
68    * @var \Drupal\taxonomy\TermInterface
69    */
70   protected $term2;
71
72   /**
73    * {@inheritdoc}
74    */
75   protected function setUp($import_test_views = TRUE) {
76     parent::setUp($import_test_views);
77
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();
83
84     if ($import_test_views) {
85       ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
86     }
87
88     $this->term1 = $this->createTerm();
89     $this->term2 = $this->createTerm();
90
91     $node = [];
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);
97   }
98
99   /**
100    * Provides a workaround for the inability to use the standard profile.
101    *
102    * @see https://www.drupal.org/node/1708692
103    */
104   protected function mockStandardInstall() {
105     $this->drupalCreateContentType([
106       'type' => 'article',
107     ]);
108
109     // Create the vocabulary for the tag field.
110     $this->vocabulary = Vocabulary::create([
111       'name' => 'Views testing tags',
112       'vid' => 'views_testing_tags',
113     ]);
114     $this->vocabulary->save();
115     $field_name = 'field_' . $this->vocabulary->id();
116
117     $handler_settings = [
118       'target_bundles' => [
119         $this->vocabulary->id() => $this->vocabulary->id(),
120       ],
121       'auto_create' => TRUE,
122     ];
123
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');
127     $entity_type_manager
128       ->getStorage('entity_form_display')
129       ->load('node.article.default')
130       ->setComponent($field_name, [
131         'type' => 'entity_reference_autocomplete_tags',
132         'weight' => -4,
133       ])
134       ->save();
135
136     $view_modes = [
137       'default',
138       'teaser',
139     ];
140     foreach ($view_modes as $view_mode) {
141       $entity_type_manager
142         ->getStorage('entity_view_display')
143         ->load("node.article.{$view_mode}")
144         ->setComponent($field_name, [
145           'type' => 'entity_reference_label',
146           'weight' => 10,
147         ])
148         ->save();
149     }
150   }
151
152   /**
153    * Creates and returns a taxonomy term.
154    *
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.
164    *
165    * @return \Drupal\taxonomy\Entity\Term
166    *   The created taxonomy term.
167    */
168   protected function createTerm(array $settings = []) {
169     $filter_formats = filter_formats();
170     $format = array_pop($filter_formats);
171     $settings += [
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,
178     ];
179     $term = Term::create($settings);
180     $term->save();
181     return $term;
182   }
183
184 }