You are here

public function SvgFormatterTest::testAltAndTitleAttributes in SVG Formatter 8

Tests alt and title attributes.

File

tests/src/Functional/SvgFormatterTest.php, line 304

Class

SvgFormatterTest
Simple test to ensure that basic functionality of the module works.

Namespace

Drupal\Tests\svg_formatter\Functional

Code

public function testAltAndTitleAttributes() {
  $entity_type_manager = $this->container
    ->get('entity_type.manager');
  foreach ([
    'field_alt_string',
    'field_title_string',
  ] as $field_name) {
    $entity_type_manager
      ->getStorage('field_storage_config')
      ->create([
      'field_name' => $field_name,
      'entity_type' => 'media',
      'type' => 'string',
      'module' => 'svg_formatter',
      'settings' => [],
      'cardinality' => 1,
    ])
      ->save();
    $entity_type_manager
      ->getStorage('field_config')
      ->create([
      'field_name' => $field_name,
      'entity_type' => 'media',
      'bundle' => 'svg',
      'label' => 'Field ' . $field_name,
    ])
      ->save();
  }
  $media = $this
    ->createMediaEntity();
  $media
    ->set('field_alt_string', 'thisisthealttext');
  $media
    ->set('field_title_string', 'thisisthetitletext');
  $media
    ->save();
  $display = $this->container
    ->get('entity_type.manager')
    ->getStorage('entity_view_display')
    ->load('media.svg.default');
  $component = $display
    ->getComponent('field_media_file');
  $component['settings'] = [
    'inline' => FALSE,
    'sanitize' => TRUE,
    'apply_dimensions' => TRUE,
    'width' => 100,
    'height' => 100,
    'enable_alt' => TRUE,
    'alt_string' => '[media:field_alt_string]',
    'enable_title' => TRUE,
    'title_string' => '[media:field_title_string]',
  ];
  $display
    ->setComponent('field_media_file', $component)
    ->save();
  $this
    ->drupalGet('media/1');
  $this
    ->assertSession()
    ->elementAttributeContains('css', 'main > div > div > div:nth-child(3) > div:nth-child(4) > div:nth-child(2) > img', 'alt', 'thisisthealttext');
  $this
    ->assertSession()
    ->elementAttributeContains('css', 'main > div > div > div:nth-child(3) > div:nth-child(4) > div:nth-child(2) > img', 'title', 'thisisthetitletext');
  $media
    ->set('field_alt_string', NULL);
  $media
    ->set('field_title_string', NULL);
  $media
    ->save();
  $this
    ->drupalGet('media/1');
  $this
    ->assertFalse($this
    ->assertSession()
    ->elementExists('css', 'main > div > div > div:nth-child(3) > div:nth-child(4) > div:nth-child(2) > img')
    ->hasAttribute('alt'));
  $this
    ->assertFalse($this
    ->assertSession()
    ->elementExists('css', 'main > div > div > div:nth-child(3) > div:nth-child(4) > div:nth-child(2) > img')
    ->hasAttribute('title'));
}