ImageStylesPathAndUrlUnitTest::_testImageStyleUrlAndPath

  1. drupal
    1. 8
    2. 7
Versions
7 ImageStylesPathAndUrlUnitTest::_testImageStyleUrlAndPath($scheme, $clean_url = TRUE)

Test image_style_url().

Code

modules/image/image.test, line 173

<?php
function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE) {
  // Make the default scheme neither "public" nor "private" to verify the
  // functions work for other than the default scheme.
  variable_set('file_default_scheme', 'temporary');
  variable_set('clean_url', $clean_url);

  // Create the directories for the styles.
  $directory = $scheme . '://styles/' . $this->style_name;
  $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
  $this->assertNotIdentical(FALSE, $status, t('Created the directory for the generated images for the test style.'));

  // Create a working copy of the file.
  $files = $this->drupalGetTestFiles('image');
  $file = reset($files);
  $image_info = image_get_info($file->uri);
  $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
  // Let the image_module_test module know about this file, so it can claim
  // ownership in hook_file_download().
  variable_set('image_module_test_file_download', $original_uri);
  $this->assertNotIdentical(FALSE, $original_uri, t('Created the generated image file.'));

  // Get the URL of a file that has not been generated and try to create it.
  $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/' . basename($original_uri);
  $this->assertFalse(file_exists($generated_uri), t('Generated file does not exist.'));
  $generate_url = image_style_url($this->style_name, $original_uri);

  if (!$clean_url) {
    $this->assertTrue(strpos($generate_url, '?q=') !== FALSE, 'When using non-clean URLS, the system path contains the query string.');
  }

  // Fetch the URL that generates the file.
  $this->drupalGet($generate_url);
  $this->assertResponse(200, t('Image was generated at the URL.'));
  $this->assertTrue(file_exists($generated_uri), t('Generated file does exist after we accessed it.'));
  $this->assertRaw(file_get_contents($generated_uri), t('URL returns expected file.'));
  $generated_image_info = image_get_info($generated_uri);
  $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], t('Expected Content-Type was reported.'));
  $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], t('Expected Content-Length was reported.'));
  if ($scheme == 'private') {
    $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', t('Expected custom header has been added.'));
  }
}
?>