You are here

function FileEntityAltTitleTestCase::testFileEntityAltTitle in File Entity (fieldable files) 7.2

Same name and namespace in other branches
  1. 7.3 file_entity.test \FileEntityAltTitleTestCase::testFileEntityAltTitle()

Create an "image" file and verify its associated alt and title text.

File

./file_entity.test, line 914
Test integration for the file_entity module.

Class

FileEntityAltTitleTestCase
Tests image alt and title text.

Code

function testFileEntityAltTitle() {
  $test_file = $this
    ->getTestFile('image');
  $alt_field_name = 'field_file_image_alt_text';

  // Name of the default alt text field added to the image file type.
  $title_field_name = 'field_file_image_title_text';

  // Name of the default title text field added to the image file type.
  // Create a file.
  $edit = array();
  $edit['files[upload]'] = drupal_realpath($test_file->uri);
  $this
    ->drupalPost('file/add', $edit, t('Next'));

  // Step 2: Scheme selection.
  if ($this
    ->xpath('//input[@name="scheme"]')) {
    $this
      ->drupalPost(NULL, array(), t('Next'));
  }

  // Step 3: Attached fields.
  $alt = 'Quote" Amp& ' . 'Файл для тестирования ' . $this
    ->randomName();

  // Generate alt text containing HTML entities, spaces and non-latin characters.
  $title = 'Quote" Amp& ' . 'Файл для тестирования ' . $this
    ->randomName();

  // Generate title text containing HTML entities, spaces and non-latin characters.
  $edit = array();
  $edit[$alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $alt;
  $edit[$title_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $title;
  $this
    ->drupalPost(NULL, $edit, t('Save'));

  // Check that the image file has been uploaded.
  $this
    ->assertRaw(t('!type %name was uploaded.', array(
    '!type' => 'Image',
    '%name' => $test_file->filename,
  )), t('Image file uploaded.'));

  // Check that the file exists in the database.
  $file = $this
    ->getFileByFilename($test_file->filename);
  $this
    ->assertTrue($file, t('File found in database.'));

  // Check that the alt and title text was loaded from the fields.
  $this
    ->assertEqual($file->alt, $alt, t('Alt text was stored as file metadata.'));
  $this
    ->assertEqual($file->title, $title, t('Title text was stored as file metadata.'));

  // Verify that the alt and title text is present on the page.
  $image_info = array(
    'path' => $file->uri,
    'alt' => $alt,
    'title' => $title,
    'width' => $file->width,
    'height' => $file->height,
  );
  $default_output = theme('image', $image_info);
  $this
    ->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');

  // Verify that the alt and title text can be edited.
  $new_alt = $this
    ->randomName();
  $new_title = $this
    ->randomName();
  $edit = array();
  $edit[$alt_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $new_alt;
  $edit[$title_field_name . '[' . LANGUAGE_NONE . '][0][value]'] = $new_title;
  $this
    ->drupalPost('file/' . $file->fid . '/edit', $edit, t('Save'));
  $image_info = array(
    'path' => $file->uri,
    'alt' => $new_alt,
    'title' => $new_title,
    'width' => $file->width,
    'height' => $file->height,
  );
  $default_output = theme('image', $image_info);
  $this
    ->assertRaw($default_output, 'Image displayed using updated alt and title attributes.');
}