function ImageFieldDisplayTest::testImageFieldSettings

Tests for image field settings.

File

drupal/core/modules/image/lib/Drupal/image/Tests/ImageFieldDisplayTest.php, line 135
Definition of Drupal\image\Tests\ImageFieldDisplayTest.

Class

ImageFieldDisplayTest
Test class to check that formatters and display settings are working.

Namespace

Drupal\image\Tests

Code

function testImageFieldSettings() {
  $test_image = current($this
    ->drupalGetTestFiles('image'));
  list(, $test_image_extension) = explode('.', $test_image->filename);
  $field_name = strtolower($this
    ->randomName());
  $instance_settings = array(
    'alt_field' => 1,
    'file_extensions' => $test_image_extension,
    'max_filesize' => '50 KB',
    'max_resolution' => '100x100',
    'min_resolution' => '10x10',
    'title_field' => 1,
    'description' => '[site:name]_description',
  );
  $widget_settings = array(
    'preview_image_style' => 'medium',
  );
  $instance = $this
    ->createImageField($field_name, 'article', array(), $instance_settings, $widget_settings);
  $this
    ->drupalGet('node/add/article');
  $this
    ->assertText(t('Files must be less than 50 KB.'), 'Image widget max file size is displayed on article form.');
  $this
    ->assertText(t('Allowed file types: ' . $test_image_extension . '.'), 'Image widget allowed file types displayed on article form.');
  $this
    ->assertText(t('Images must be between 10x10 and 100x100 pixels.'), 'Image widget allowed resolution displayed on article form.');

  // We have to create the article first and then edit it because the alt
  // and title fields do not display until the image has been attached.
  $nid = $this
    ->uploadNodeImage($test_image, $field_name, 'article');
  $this
    ->drupalGet('node/' . $nid . '/edit');
  $this
    ->assertFieldByName($field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][alt]', '', 'Alt field displayed on article form.');
  $this
    ->assertFieldByName($field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][title]', '', 'Title field displayed on article form.');

  // Verify that the attached image is being previewed using the 'medium'
  // style.
  $node = node_load($nid, TRUE);
  $image_info = array(
    'uri' => file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid'])->uri,
    'width' => 220,
    'height' => 110,
    'style_name' => 'medium',
  );
  $default_output = theme('image_style', $image_info);
  $this
    ->assertRaw($default_output, "Preview image is displayed using 'medium' style.");

  // Add alt/title fields to the image and verify that they are displayed.
  $image_info = array(
    'uri' => file_load($node->{$field_name}[Language::LANGCODE_NOT_SPECIFIED][0]['fid'])->uri,
    'alt' => $this
      ->randomName(),
    'title' => $this
      ->randomName(),
    'width' => 40,
    'height' => 20,
  );
  $edit = array(
    $field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][alt]' => $image_info['alt'],
    $field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][title]' => $image_info['title'],
  );
  $this
    ->drupalPost('node/' . $nid . '/edit', $edit, t('Save and keep published'));
  $default_output = theme('image', $image_info);
  $this
    ->assertRaw($default_output, 'Image displayed using user supplied alt and title attributes.');

  // Verify that alt/title longer than allowed results in a validation error.
  $test_size = 2000;
  $edit = array(
    $field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][alt]' => $this
      ->randomName($test_size),
    $field_name . '[' . Language::LANGCODE_NOT_SPECIFIED . '][0][title]' => $this
      ->randomName($test_size),
  );
  $this
    ->drupalPost('node/' . $nid . '/edit', $edit, t('Save and keep published'));
  $schema = $instance
    ->getField()
    ->getSchema();
  $this
    ->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
    '%max' => $schema['columns']['alt']['length'],
    '%length' => $test_size,
  )));
  $this
    ->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
    '%max' => $schema['columns']['title']['length'],
    '%length' => $test_size,
  )));
}