Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / media / tests / src / FunctionalJavascript / MediaSourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\media\FunctionalJavascript;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\media\Entity\MediaType;
8
9 /**
10  * Base class for media source tests.
11  */
12 abstract class MediaSourceTestBase extends MediaJavascriptTestBase {
13
14   /**
15    * Creates storage and field instance, attached to a given media type.
16    *
17    * @param string $field_name
18    *   The field name.
19    * @param string $field_type
20    *   The field type.
21    * @param string $media_type_id
22    *   The media type config entity ID.
23    */
24   protected function createMediaTypeField($field_name, $field_type, $media_type_id) {
25     $storage = FieldStorageConfig::create([
26       'field_name' => $field_name,
27       'entity_type' => 'media',
28       'type' => $field_type,
29     ]);
30     $storage->save();
31
32     FieldConfig::create([
33       'field_storage' => $storage,
34       'bundle' => $media_type_id,
35     ])->save();
36
37     // Make the field widget visible in the form display.
38     $component = \Drupal::service('plugin.manager.field.widget')
39       ->prepareConfiguration($field_type, []);
40
41     // @todo Replace entity_get_form_display() when #2367933 is done.
42     // https://www.drupal.org/node/2872159.
43     $entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
44     $entity_form_display->setComponent($field_name, $component)
45       ->save();
46
47     // Use the default formatter and settings.
48     $component = \Drupal::service('plugin.manager.field.formatter')
49       ->prepareConfiguration($field_type, []);
50
51     // @todo Replace entity_get_display() when #2367933 is done.
52     // https://www.drupal.org/node/2872159.
53     $entity_display = entity_get_display('media', $media_type_id, 'default');
54     $entity_display->setComponent($field_name, $component)
55       ->save();
56   }
57
58   /**
59    * Create a set of fields in a media type.
60    *
61    * @param array $fields
62    *   An associative array where keys are field names and values field types.
63    * @param string $media_type_id
64    *   The media type config entity ID.
65    */
66   protected function createMediaTypeFields(array $fields, $media_type_id) {
67     foreach ($fields as $field_name => $field_type) {
68       $this->createMediaTypeField($field_name, $field_type, $media_type_id);
69     }
70   }
71
72   /**
73    * Hides a widget in the default form display config.
74    *
75    * @param string $field_name
76    *   The field name.
77    * @param string $media_type_id
78    *   The media type config entity ID.
79    */
80   protected function hideMediaTypeFieldWidget($field_name, $media_type_id) {
81     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */
82     $entity_form_display = entity_get_form_display('media', $media_type_id, 'default');
83     if ($entity_form_display->getComponent($field_name)) {
84       $entity_form_display->removeComponent($field_name)->save();
85     }
86   }
87
88   /**
89    * Test generic media type creation.
90    *
91    * @param string $media_type_id
92    *   The media type config entity ID.
93    * @param string $source_id
94    *   The media source ID.
95    * @param array $provided_fields
96    *   (optional) An array of field machine names this type provides.
97    *
98    * @return \Drupal\media\MediaTypeInterface
99    *   The created media type.
100    */
101   public function doTestCreateMediaType($media_type_id, $source_id, array $provided_fields = []) {
102     $session = $this->getSession();
103     $page = $session->getPage();
104     $assert_session = $this->assertSession();
105
106     $this->drupalGet('admin/structure/media/add');
107     $page->fillField('label', $media_type_id);
108     $this->getSession()
109       ->wait(5000, "jQuery('.machine-name-value').text() === '{$media_type_id}'");
110
111     // Make sure the source is available.
112     $assert_session->fieldExists('Media source');
113     $assert_session->optionExists('Media source', $source_id);
114     $page->selectFieldOption('Media source', $source_id);
115     $result = $assert_session->waitForElementVisible('css', 'fieldset[data-drupal-selector="edit-source-configuration"]');
116     $this->assertNotEmpty($result);
117
118     // Make sure the provided fields are visible on the form.
119     foreach ($provided_fields as $provided_field) {
120       $result = $assert_session->waitForElementVisible('css', 'select[name="field_map[' . $provided_field . ']"]');
121       $this->assertNotEmpty($result);
122     }
123
124     // Save the form to create the type.
125     $page->pressButton('Save');
126     $assert_session->pageTextContains('The media type ' . $media_type_id . ' has been added.');
127     $this->drupalGet('admin/structure/media');
128     $assert_session->pageTextContains($media_type_id);
129
130     // Bundle definitions are statically cached in the context of the test, we
131     // need to make sure we have updated information before proceeding with the
132     // actions on the UI.
133     \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
134
135     return MediaType::load($media_type_id);
136   }
137
138 }