View source
<?php
namespace Drupal\Tests\media\FunctionalJavascript;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\Entity\Media;
use Drupal\media\Entity\MediaType;
use Drupal\media\Plugin\media\Source\Image;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class MediaSourceImageTest extends MediaSourceTestBase {
protected $defaultTheme = 'classy';
public function testMediaImageSource() {
$media_type_id = 'test_media_image_type';
$source_field_id = 'field_media_image';
$provided_fields = [
Image::METADATA_ATTRIBUTE_WIDTH,
Image::METADATA_ATTRIBUTE_HEIGHT,
];
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$this
->doTestCreateMediaType($media_type_id, 'image', $provided_fields);
$fields = [
'field_string_width' => 'string',
'field_string_height' => 'string',
];
$this
->createMediaTypeFields($fields, $media_type_id);
$this
->hideMediaTypeFieldWidget('name', $media_type_id);
$this
->drupalGet("admin/structure/media/manage/{$media_type_id}");
$page
->selectFieldOption("field_map[" . Image::METADATA_ATTRIBUTE_WIDTH . "]", 'field_string_width');
$page
->selectFieldOption("field_map[" . Image::METADATA_ATTRIBUTE_HEIGHT . "]", 'field_string_height');
$page
->pressButton('Save');
$this
->drupalGet("media/add/{$media_type_id}");
$page
->attachFileToField("files[{$source_field_id}_0]", $this->root . '/core/modules/media/tests/fixtures/example_1.jpeg');
$result = $assert_session
->waitForButton('Remove');
$this
->assertNotEmpty($result);
$page
->fillField("{$source_field_id}[0][alt]", 'Image Alt Text 1');
$page
->pressButton('Save');
$assert_session
->addressEquals('admin/content/media');
$this
->drupalGet($this
->assertLinkToCreatedMedia());
$image_element = $assert_session
->elementExists('css', '.field--name-field-media-image img');
$file_url_generator = \Drupal::service('file_url_generator');
$expected_image_src = $file_url_generator
->generateString(\Drupal::token()
->replace('public://styles/large/public/[date:custom:Y]-[date:custom:m]/example_1.jpeg'));
$this
->assertStringContainsString($expected_image_src, $image_element
->getAttribute('src'));
$field = $assert_session
->elementExists('css', '.field--name-field-media-image');
$assert_session
->elementExists('css', '.field__label.visually-hidden', $field);
$assert_session
->elementNotExists('css', 'a', $field);
$this
->assertSame('Image Alt Text 1', $image_element
->getAttribute('alt'));
$media = Media::load(1);
$this
->assertSame('example_1.jpeg', $media
->getName());
$this
->assertSame('200', $media
->get('field_string_width')->value);
$this
->assertSame('89', $media
->get('field_string_height')->value);
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
'access media overview',
'administer media',
'administer media types',
'administer media display',
'view media',
'access content',
]));
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
ImageStyle::load('large')
->delete();
$this
->drupalGet('admin/structure/media/add');
$page
->fillField('label', 'Madame Bonacieux');
$this
->assertNotEmpty($assert_session
->waitForText('Machine name: madame_bonacieux'));
$page
->selectFieldOption('source', 'image');
$this
->assertNotEmpty($assert_session
->waitForText('Field mapping'));
$page
->pressButton('Save');
$this
->assertViewDisplayConfigured('madame_bonacieux');
$this
->drupalLogin($this
->drupalCreateUser([
'administer site configuration',
'access media overview',
'administer media',
'administer media types',
'view media',
'access content',
]));
$this
->drupalGet('/admin/reports/status');
$assert_session
->pageTextContains('The default display for the Madame Bonacieux media type is not currently using an image style on the Image field. Not using an image style can lead to much larger file downloads.');
$assert_session
->linkNotExists('add an image style to the Image field');
$assert_session
->linkByHrefNotExists('/admin/structure/media/manage/madame_bonacieux/display');
Role::load(RoleInterface::AUTHENTICATED_ID)
->grantPermission('administer media display')
->save();
$this
->drupalGet('/admin/reports/status');
$assert_session
->pageTextContains('The default display for the Madame Bonacieux media type is not currently using an image style on the Image field. Not using an image style can lead to much larger file downloads. If you would like to change this, add an image style to the Image field.');
$assert_session
->linkExists('add an image style to the Image field');
$assert_session
->linkByHrefExists('/admin/structure/media/manage/madame_bonacieux/display');
$this->container
->get('module_installer')
->uninstall([
'field_ui',
]);
$this
->drupalGet('/admin/reports/status');
$assert_session
->pageTextContains('The default display for the Madame Bonacieux media type is not currently using an image style on the Image field. Not using an image style can lead to much larger file downloads.');
$assert_session
->linkNotExists('add an image style to the Image field');
$assert_session
->linkByHrefNotExists('/admin/structure/media/manage/madame_bonacieux/display');
}
protected function assertViewDisplayConfigured($media_type_id) {
$assert_session = $this
->assertSession();
$type = MediaType::load($media_type_id);
$display = EntityViewDisplay::load('media.' . $media_type_id . '.' . EntityDisplayRepositoryInterface::DEFAULT_DISPLAY_MODE);
$this
->assertInstanceOf(EntityViewDisplay::class, $display);
$source_field_definition = $type
->getSource()
->getSourceFieldDefinition($type);
$component = $display
->getComponent($source_field_definition
->getName());
$this
->assertSame('visually_hidden', $component['label']);
if (ImageStyle::load('large')) {
$this
->assertSame('large', $component['settings']['image_style']);
}
else {
$this
->assertEmpty($component['settings']['image_style']);
}
$this
->assertEmpty($component['settings']['image_link']);
$this
->drupalGet('/admin/structure/media/manage/' . $media_type_id . '/display');
$assert_session
->elementExists('css', 'input[name="' . $source_field_definition
->getName() . '_settings_edit"]');
$assert_session
->elementsCount('css', 'input[name$="_settings_edit"]', 1);
}
}