Commit 06914f5f authored by catch's avatar catch
Browse files

Issue #3319601 by pooja_sharma, sunlix, bkosborne, quietone, smustgrave,...

Issue #3319601 by pooja_sharma, sunlix, bkosborne, quietone, smustgrave, longwave, lendude: Media image thumbnail incorrectly ends up as NULL when it should be an empty string

(cherry picked from commit 22ee9bdb)
parent a64c9e97
Loading
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -144,7 +144,7 @@ public function getMetadata(MediaInterface $media, $name) {
        return $uri;

      case 'thumbnail_alt_value':
        return $media->get($this->configuration['source_field'])->alt ?: parent::getMetadata($media, $name);
        return $media->get($this->configuration['source_field'])->alt ?? parent::getMetadata($media, $name);
    }

    return parent::getMetadata($media, $name);
+51 −0
Original line number Diff line number Diff line
@@ -8,6 +8,9 @@
use Drupal\media\Entity\Media;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\Tests\TestFileCreationTrait;

/**
 * Tests the Media overview page.
@@ -16,6 +19,8 @@
 */
class MediaOverviewPageTest extends MediaFunctionalTestBase {

  use TestFileCreationTrait;

  /**
   * {@inheritdoc}
   */
@@ -185,4 +190,50 @@ public function testMediaOverviewPage(): void {
    $assert_session->linkByHrefExists('/media/' . $media3->id());
  }

  /**
   * Tests the display of the alt attribute.
   */
  public function testImageAltTextDisplay(): void {
    $this->drupalLogin($this->adminUser);
    $media_type = $this->createMediaType('image');
    $media_type_id = $media_type->id();
    $media_type->setFieldMap(['name' => 'name']);
    $media_type->save();

    /** @var \Drupal\field\FieldConfigInterface $field */
    $field = FieldConfig::load("media.$media_type_id.field_media_image");
    $settings = $field->getSettings();
    $settings['alt_field'] = TRUE;
    $settings['alt_field_required'] = FALSE;
    $field->set('settings', $settings);
    $field->save();

    $file = File::create([
      'uri' => $this->getTestFiles('image')[0]->uri,
    ]);
    $file->save();

    // Set the alt text to an empty string.
    $media = Media::create([
      'name' => 'Custom name',
      'bundle' => $media_type_id,
      'field_media_image' => [
        [
          'target_id' => $file->id(),
          'alt' => '',
          'title' => 'default title',
        ],
      ],
    ]);
    $media->save();

    $this->drupalGet('/admin/content/media');

    // Confirm that the alt text attribute is present.
    $assert_session = $this->assertSession();
    $element = $assert_session->elementAttributeExists('css', 'td.views-field-thumbnail__target-id img', 'alt');
    $this->assertSame('', (string) $element->getAttribute('alt'));

  }

}