Backup of db before drupal security update
[yaffs-website] / web / core / modules / taxonomy / tests / src / Functional / Views / TaxonomyTestBase.php
1 <?php
2
3 namespace Drupal\Tests\taxonomy\Functional\Views;
4
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;
12
13 /**
14  * Base class for all taxonomy tests.
15  */
16 abstract class TaxonomyTestBase extends ViewTestBase {
17
18   use EntityReferenceTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['taxonomy', 'taxonomy_test_views'];
26
27   /**
28    * Stores the nodes used for the different tests.
29    *
30    * @var \Drupal\node\NodeInterface[]
31    */
32   protected $nodes = [];
33
34   /**
35    * The vocabulary used for creating terms.
36    *
37    * @var \Drupal\taxonomy\VocabularyInterface
38    */
39   protected $vocabulary;
40
41   /**
42    * Stores the first term used in the different tests.
43    *
44    * @var \Drupal\taxonomy\TermInterface
45    */
46   protected $term1;
47
48   /**
49    * Stores the second term used in the different tests.
50    *
51    * @var \Drupal\taxonomy\TermInterface
52    */
53   protected $term2;
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function setUp($import_test_views = TRUE) {
59     parent::setUp($import_test_views);
60     $this->mockStandardInstall();
61
62     if ($import_test_views) {
63       ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
64     }
65
66     $this->term1 = $this->createTerm();
67     $this->term2 = $this->createTerm();
68
69     $node = [];
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);
75   }
76
77   /**
78    * Provides a workaround for the inability to use the standard profile.
79    *
80    * @see https://www.drupal.org/node/1708692
81    */
82   protected function mockStandardInstall() {
83     $this->drupalCreateContentType([
84       'type' => 'article',
85     ]);
86     // Create the vocabulary for the tag field.
87     $this->vocabulary = Vocabulary::create([
88       'name' => 'Views testing tags',
89       'vid' => 'views_testing_tags',
90     ]);
91     $this->vocabulary->save();
92     $field_name = 'field_' . $this->vocabulary->id();
93
94     $handler_settings = [
95       'target_bundles' => [
96         $this->vocabulary->id() => $this->vocabulary->id(),
97       ],
98       'auto_create' => TRUE,
99     ];
100     $this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
101
102     entity_get_form_display('node', 'article', 'default')
103       ->setComponent($field_name, [
104         'type' => 'entity_reference_autocomplete_tags',
105         'weight' => -4,
106       ])
107       ->save();
108
109     entity_get_display('node', 'article', 'default')
110       ->setComponent($field_name, [
111         'type' => 'entity_reference_label',
112         'weight' => 10,
113       ])
114       ->save();
115     entity_get_display('node', 'article', 'teaser')
116       ->setComponent($field_name, [
117         'type' => 'entity_reference_label',
118         'weight' => 10,
119       ])
120       ->save();
121   }
122
123   /**
124    * Creates and returns a taxonomy term.
125    *
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.
135    *
136    * @return \Drupal\taxonomy\Entity\Term
137    *   The created taxonomy term.
138    */
139   protected function createTerm(array $settings = []) {
140     $filter_formats = filter_formats();
141     $format = array_pop($filter_formats);
142     $settings += [
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,
149     ];
150     $term = Term::create($settings);
151     $term->save();
152     return $term;
153   }
154
155 }