You are here

public function EntityEmbedFilterOverridesTest::testOverridesAltAndTitleForImageMedia in Entity Embed 8

Tests overriding of `alt` and `title` for image media items.

File

tests/src/Kernel/EntityEmbedFilterOverridesTest.php, line 91

Class

EntityEmbedFilterOverridesTest
Tests that entity embeds can have per-embed overrides for e.g. `alt`.

Namespace

Drupal\Tests\entity_embed\Kernel

Code

public function testOverridesAltAndTitleForImageMedia() {
  $this
    ->createMediaType('image', [
    'id' => 'image',
  ]);

  // The `alt` field property is enabled by default, the `title` one is not.
  // Since we want to test it, enable it.
  $source_field = FieldConfig::load('media.image.field_media_image');
  $source_field
    ->setSetting('title_field', TRUE);
  $source_field
    ->save();
  $this->container
    ->get('current_user')
    ->addRole($this
    ->drupalCreateRole([
    'view media',
  ]));
  $media = Media::create([
    'bundle' => 'image',
    'name' => 'Screaming hairy armadillo',
    'field_media_image' => [
      [
        'target_id' => $this->image
          ->id(),
        'alt' => 'default alt',
        'title' => 'default title',
      ],
    ],
  ]);
  $media
    ->save();
  $base = [
    'data-entity-embed-display' => 'view_mode:media.full',
    'data-entity-embed-display-settings' => '',
    'data-entity-type' => 'media',
    'data-entity-uuid' => $media
      ->uuid(),
  ];
  $input = $this
    ->createEmbedCode($base);
  $input .= $this
    ->createEmbedCode([
    'alt' => 'alt 1',
    'title' => 'title 1',
  ] + $base);
  $input .= $this
    ->createEmbedCode([
    'alt' => 'alt 2',
    'title' => 'title 2',
  ] + $base);
  $input .= $this
    ->createEmbedCode([
    'alt' => 'alt 3',
    'title' => 'title 3',
  ] + $base);
  $this
    ->applyFilter($input);
  $img_nodes = $this
    ->cssSelect('img');
  $this
    ->assertCount(4, $img_nodes);
  $this
    ->assertHasAttributes($img_nodes[0], [
    'alt' => 'default alt',
  ]);
  $this
    ->assertHasAttributes($img_nodes[1], [
    'alt' => 'alt 1',
    'title' => 'title 1',
  ]);
  $this
    ->assertHasAttributes($img_nodes[2], [
    'alt' => 'alt 2',
    'title' => 'title 2',
  ]);
  $this
    ->assertHasAttributes($img_nodes[3], [
    'alt' => 'alt 3',
    'title' => 'title 3',
  ]);
}