You are here

public function FileEntityCreationTest::testImageAltTitleFields in File Entity (fieldable files) 8.2

Test the Title Text and Alt Text fields of to the predefined Image type.

File

tests/src/Functional/FileEntityCreationTest.php, line 104

Class

FileEntityCreationTest
Tests creating and saving a file.

Namespace

Drupal\Tests\file_entity\Functional

Code

public function testImageAltTitleFields() {

  // Disable private path to avoid irrelevant second form step.
  new Settings([
    'file_private_path' => NULL,
  ] + Settings::getAll());
  $this
    ->rebuildContainer();

  // Create an image.
  $test_file = $this
    ->getTestFile('image');
  $edit = array(
    'files[upload]' => \Drupal::service('file_system')
      ->realpath($test_file->uri),
  );
  $this
    ->drupalPostForm('file/add', $edit, t('Next'));
  $data = array(
    'field_image_title_text' => 'My image',
    'field_image_alt_text' => 'A test image',
  );

  // Find the alt and title fields on the next step.
  foreach ($data as $field => $value) {
    $this
      ->assertFieldByXPath('//input[@name="' . $field . '[0][value]"]');
  }

  // Set fields.
  $edit = array();
  foreach ($data as $field => $value) {
    $edit[$field . '[0][value]'] = $value;
  }
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $file = $this
    ->getFileByFilename('image-test_0.png');
  $this
    ->drupalGet('file/' . $file
    ->id());
  $this
    ->assertRaw('alt="A test image"', 'Alt attribute is shown and has the correct value.');
  $this
    ->assertRaw('title="My image"', 'Title attribute is shown and has the correct value.');

  // Make sure the field values are saved.
  $created_file = FileEntity::load(1)
    ->getTranslation(LanguageInterface::LANGCODE_DEFAULT);
  foreach ($data as $field => $value) {
    $this
      ->assertEqual($value, $created_file
      ->get($field)->value);
  }
}