1 image.test public ImageFieldValidateTestCase::testTypeSupport()

Test for supported image types.

File

core/modules/image/tests/image.test, line 1153
Tests for image.module.

Class

ImageFieldValidateTestCase
Test class to check for various validations.

Code

public function testTypeSupport() {
  $field_name = strtolower($this->randomName());
  $instance_settings = array(
    'file_extensions' => 'jpg webp',
  );
  $this->createImageField($field_name, 'post', array(), $instance_settings);

  $path = backdrop_get_path('module', 'image') . '/tests/images';
  $filename = 'testimage.webp';
  $files = file_scan_directory($path, '/' . $filename . '/');
  $webp_image = reset($files);
  $nid = $this->uploadNodeImage($webp_image, $field_name, 'post');
  $refused_message = t('The specified file %file could not be uploaded. Only files with the following extensions are allowed: %extensions.', array(
    '%file' => $filename,
    '%extensions' => 'jpg',
  ));

  // Both, PHP and GD must support WebP. PHP ships with support since 7.1.+,
  // the bundled GD version still might not support it.
  $gd_info = gd_info();
  $webp_supported = isset($gd_info['WebP Support']) && $gd_info['WebP Support'] == TRUE;
  if (version_compare(PHP_VERSION, '7.1.0', '<') || !$webp_supported) {
    $this->assertText(strip_tags($refused_message), 'WebP image type has been refused');
    // Also check the field description.
    $this->assertText('Allowed file types: jpg.', 'Field allows only jpg extension');
  }
  else {
    // Successful upload and node creation.
    $this->assertNoText(strip_tags($refused_message), 'WebP image type has not been refused');
    $node = node_load($nid);
    $uploaded_filename = $node->{$field_name}[LANGUAGE_NONE][0]['filename'];
    $this->assertEqual($uploaded_filename, $filename, 'New node contains the file');
    // Thumbnail image has been created.
    $image_uri = $node->{$field_name}[LANGUAGE_NONE][0]['uri'];
    $this->backdropGet(image_style_url('thumbnail', $image_uri));
    $this->assertResponse(200, 'Received a 200 response for derivative image.');
    $this->assertNoText('Error generating image.');
  }
}