diff -u b/core/modules/media/src/Plugin/media/Source/File.php b/core/modules/media/src/Plugin/media/Source/File.php --- b/core/modules/media/src/Plugin/media/Source/File.php +++ b/core/modules/media/src/Plugin/media/Source/File.php @@ -65,19 +65,18 @@ * Gets the thumbnail image URI based on a file entity. * * @param \Drupal\file\FileInterface $file - * The file entity. + * A file entity. * * @return string * File URI of the thumbnail image. */ protected function getThumbnail(FileInterface $file) { $icon_base = $this->configFactory->get('media.settings')->get('icon_base'); - $thumbnail = FALSE; - // We try to magically use the most specific icon present in the $icon_base - // directory, based on the MIME information. For instance, if an icon file - // named "pdf.png" is present, it will be used if the file matches this - // MIME type. + // We try to automatically use the most specific icon present in the + // $icon_base directory, based on the MIME type. For instance, if an + // icon file named "pdf.png" is present, it will be used if the file + // matches this MIME type. $mimetype = $file->getMimeType(); $mimetype = explode('/', $mimetype); diff -u b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php --- b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceFileTest.php @@ -12,10 +12,10 @@ class MediaSourceFileTest extends MediaSourceTestBase { /** - * Just dummy test to check generic methods. + * Tests the file media source. */ public function testMediaFileSource() { - $type_name = 'test_media_file_type'; + $type_id = 'test_media_file_type'; $source_field_id = 'field_media_file'; $provided_fields = [ 'mime', @@ -27,36 +27,40 @@ $assert_session = $this->assertSession(); // Create file media source. - $this->createMediaTypeTest($type_name, 'file', $provided_fields); + $this->createMediaTypeTest($type_id, 'file', $provided_fields); - // Create a supported and a non-supported field. + // Create a custom field for the media type to store the mime metadata + // attribute. $fields = [ 'field_string_mime' => 'string', + 'field_string_size' => 'string', ]; - $this->createMediaFields($fields, $type_name); + $this->createMediaTypeFields($fields, $type_id); - // Hide the media name to test default name generation. - $this->hideMediaField('name', $type_name); + // Hide the name field widget to test default name generation. + $this->hideMediaTypeFieldWidget('name', $type_id); - $this->drupalGet("admin/structure/media/manage/{$type_name}"); + $this->drupalGet("admin/structure/media/manage/{$type_id}"); $page->selectFieldOption("field_map[mime]", 'field_string_mime'); + $page->selectFieldOption("field_map[size]", 'field_string_size'); $page->pressButton('Save'); // Create a media item. - $this->drupalGet("media/add/{$type_name}"); + $this->drupalGet("media/add/{$type_id}"); $page->attachFileToField("files[{$source_field_id}_0]", \Drupal::root() . '/sites/README.txt'); $assert_session->assertWaitOnAjaxRequest(); $page->pressButton('Save and publish'); $assert_session->addressEquals('media/1'); - // Make sure the thumbnail is created. + // Make sure the thumbnail is displayed. $assert_session->elementAttributeContains('css', '.image-style-thumbnail', 'src', 'generic.png'); // Load the media and check that all fields are properly populated. $media = Media::load(1); $this->assertEquals('README.txt', $media->label()); $this->assertEquals('text/plain', $media->get('field_string_mime')->value); + $this->assertEquals(filesize(\Drupal::root() . '/sites/README.txt'), $media->get('field_string_size')->value); } } diff -u b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php --- b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php +++ b/core/modules/media/tests/src/FunctionalJavascript/MediaSourceTestBase.php @@ -13,16 +13,16 @@ abstract class MediaSourceTestBase extends MediaJavascriptTestBase { /** - * Create storage and field instance, attached to a given media type. + * Creates storage and field instance, attached to a given media type. * * @param string $field_name * The field name. * @param string $field_type * The field storage type. - * @param string $type_name - * The media type machine name. + * @param string $type_id + * The media type config entity ID. */ - protected function createMediaField($field_name, $field_type, $type_name) { + protected function createMediaTypeField($field_name, $field_type, $type_id) { $storage = FieldStorageConfig::create([ 'field_name' => $field_name, 'entity_type' => 'media', @@ -30,24 +30,23 @@ ]); $storage->save(); - $config = FieldConfig::create([ + FieldConfig::create([ 'field_storage' => $storage, - 'bundle' => $type_name, - ]); - $config->save(); + 'bundle' => $type_id, + ])->save(); - // Make the field visible in the form display. + // Make the field widget visible in the form display. $component = \Drupal::service('plugin.manager.field.widget') ->prepareConfiguration($field_type, []); /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */ $entity_form_display = \Drupal::entityTypeManager() ->getStorage('entity_form_display') - ->load('media.' . $type_name . '.default'); + ->load('media.' . $type_id . '.default'); if (!$entity_form_display) { $entity_form_display = EntityFormDisplay::create(array( 'targetEntityType' => 'media', - 'bundle' => $type_name, + 'bundle' => $type_id, 'mode' => 'default', 'status' => TRUE, )); @@ -57,40 +56,40 @@ } /** - * Helper to create a set of fields in a media type. + * Create a set of fields in a media type. * * @param array $fields * An associative array where keys are field names and values field types. - * @param string $type_name - * The type machine name. + * @param string $type_id + * The media type config entity ID. */ - protected function createMediaFields(array $fields, $type_name) { + protected function createMediaTypeFields(array $fields, $type_id) { foreach ($fields as $field_name => $field_type) { - $this->createMediaField($field_name, $field_type, $type_name); + $this->createMediaTypeField($field_name, $field_type, $type_id); } } /** - * Hide a component from the default form display config. + * Hides a widget in the default form display config. * * @param string $field_name * The field name. - * @param string $type_name - * The media type machine name. + * @param string $type_id + * The media type config entity ID. */ - protected function hideMediaField($field_name, $type_name) { + protected function hideMediaTypeFieldWidget($field_name, $type_id) { /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $entity_form_display */ $entity_form_display = \Drupal::entityTypeManager() ->getStorage('entity_form_display') - ->load('media.' . $type_name . '.default'); + ->load('media.' . $type_id . '.default'); $entity_form_display->removeComponent($field_name)->save(); } /** - * Helper to test a generic type (bundle) creation. + * Test generic media type creation. * - * @param string $type_name - * The type machine name. + * @param string $type_id + * The media type config entity ID. * @param string $source_id * The media source ID. * @param array $provided_fields @@ -99,14 +98,14 @@ * @return \Drupal\media\MediaTypeInterface * The type created. */ - public function createMediaTypeTest($type_name, $source_id, array $provided_fields = []) { + public function createMediaTypeTest($type_id, $source_id, array $provided_fields = []) { $session = $this->getSession(); $page = $session->getPage(); $assert_session = $this->assertSession(); $this->drupalGet('admin/structure/media/add'); - $page->fillField('label', $type_name); - $this->assertJsCondition("jQuery('.machine-name-value').text() === '{$type_name}'"); + $page->fillField('label', $type_id); + $this->assertJsCondition("jQuery('.machine-name-value').text() === '{$type_id}'"); // Make sure the source is available. $this->assertSession()->fieldExists('Media source'); @@ -124,16 +123,16 @@ // Save the page to create the type. $page->pressButton('Save'); $assert_session->statusCodeEquals(200); - $assert_session->pageTextContains('The media type ' . $type_name . ' has been added.'); + $assert_session->pageTextContains('The media type ' . $type_id . ' has been added.'); $this->drupalGet('admin/structure/media'); - $assert_session->pageTextContains($type_name); + $assert_session->pageTextContains($type_id); // Bundle definitions are statically cached in the context of the test, we // need to make sure we have updated information before proceeding with the // actions on the UI. \Drupal::service('entity_type.bundle.info')->clearCachedBundles(); - return MediaType::load($type_name); + return MediaType::load($type_id); } }