1.20.x image.test ImageFieldDisplayTestCase::testImageFieldSettings()

Tests for image field settings.

File

modules/image/tests/image.test, line 818
Tests for image.module.

Class

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

Code

function testImageFieldSettings() {
  $test_image = current($this->backdropGetTestFiles('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_dimensions' => '100x100',
    'min_dimensions' => '10x10',
    'title_field' => 1,
  );
  $widget_settings = array(
    'preview_image_style' => 'medium',
  );
  $field = $this->createImageField($field_name, 'post', array(), $instance_settings, $widget_settings);
  $field['deleted'] = 0;
  $table = _field_sql_storage_tablename($field);
  $schema = backdrop_get_schema($table, TRUE);
  $instance = field_info_instance('node', $field_name, 'post');

  $this->backdropGet('node/add/post');
  $this->assertText(t('Files must be less than 50 KB.'), 'Image widget max file size is displayed on post form.');
  $this->assertText(t('Allowed file types: ' . $test_image_extension . '.'), 'Image widget allowed file types displayed on post form.');
  $this->assertText(t('Images larger than 100x100 pixels will be scaled down.'), 'Image widget allowed maximum dimensions displayed on post form.');
  $this->assertText(t('Images must be at least 10x10 pixels.'), 'Image widget allowed mimimum dimensions displayed on post form.');

  // We have to create the post 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, 'post');
  $this->backdropGet('node/' . $nid . '/edit');
  $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][alt]', '', t('Alt field displayed on post form.'));
  $this->assertFieldByName($field_name . '[' . LANGUAGE_NONE . '][0][title]', '', t('Title field displayed on post form.'));
  // Verify that the attached image is being previewed using the 'medium'
  // style.
  $node = node_load($nid, NULL, TRUE);
  $image_info = array(
    'style_name' => 'medium',
    'uri' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
    'width' => '300',
    'height' => '150',
  );
  $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' => $node->{$field_name}[LANGUAGE_NONE][0]['uri'],
    'alt' => $this->randomName(),
    'title' => $this->randomName(),
    'width' => 40,
    'height' => 20,
  );
  $edit = array(
    $field_name . '[' . LANGUAGE_NONE . '][0][alt]' => $image_info['alt'],
    $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $image_info['title'],
  );
  $this->backdropPost('node/' . $nid . '/edit', $edit, t('Save'));
  $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_NONE . '][0][alt]' => $this->randomName($test_size),
    $field_name . '[' . LANGUAGE_NONE . '][0][title]' => $this->randomName($test_size),
  );
  $this->backdropPost('node/' . $nid . '/edit', $edit, t('Save'));
  $this->assertRaw(t('Alternate text cannot be longer than %max characters but is currently %length characters long.', array(
    '%max' => $schema['fields'][$field_name . '_alt']['length'],
    '%length' => $test_size,
  )));
  $this->assertRaw(t('Title cannot be longer than %max characters but is currently %length characters long.', array(
    '%max' => $schema['fields'][$field_name . '_title']['length'],
    '%length' => $test_size,
  )));

  // Check that image minimum size is not larger than maximum size.
  $edit = array(
    'instance[settings][max_dimensions][x]' => 100,
    'instance[settings][min_dimensions][x]' => 200,
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $field_name, $edit, t('Save settings'));
  $this->assertText(t('The minimum image dimensions cannot be bigger than its maximum dimensions.'));
  // Try again with unacceptable image heights instead.
  $edit = array(
    'instance[settings][max_dimensions][x]' => '',
    'instance[settings][min_dimensions][x]' => '',
    'instance[settings][max_dimensions][y]' => 100,
    'instance[settings][min_dimensions][y]' => 200,
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $field_name, $edit, t('Save settings'));
  $this->assertText(t('The minimum image dimensions cannot be bigger than its maximum dimensions.'));
  // Try again with acceptable values.
  $edit = array(
    'instance[settings][max_dimensions][x]' => '',
    'instance[settings][min_dimensions][x]' => '',
    'instance[settings][max_dimensions][y]' => 300,
    'instance[settings][min_dimensions][y]' => 200,
  );
  $this->backdropPost('admin/structure/types/manage/post/fields/' . $field_name, $edit, t('Save settings'));
  $this->assertRaw(t('Saved %field_name configuration.', array('%field_name' => $field_name)));
}