1.20.x image.test ImageStylesPathAndUrlUnitTest::_testImageStyleUrlAndPath($scheme, $clean_url = TRUE)

Test image_style_url().

File

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

Class

ImageStylesPathAndUrlUnitTest
Tests the functions for generating paths and URLs for image styles.

Code

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.
  config_set('system.core', 'file_default_scheme', 'temporary');
  config_set('system.core', 'clean_url', $clean_url);
  backdrop_static_reset('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, 'Created the directory for the generated images for the test style.');

  // Create a working copy of the file.
  $files = $this->backdropGetTestFiles('image');
  $file = array_shift($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().
  state_set('image_module_test_file_download', $original_uri);
  $this->assertNotIdentical(FALSE, $original_uri, '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 . '/' . backdrop_basename($original_uri);
  $this->assertFalse(file_exists($generated_uri), '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.');
  }

  // Check that the generated URL is the same when we pass in a relative path
  // rather than a URI. We need to temporarily switch the default scheme to
  // match the desired scheme before testing this, then switch it back to the
  // "temporary" scheme used throughout this test afterwards.
  config_set('system.core', 'file_default_scheme', $scheme);
  $relative_path = file_uri_target($original_uri);
  $generate_url_from_relative_path = image_style_url($this->style_name, $relative_path);
  $this->assertEqual($generate_url, $generate_url_from_relative_path, 'Generated URL is the same regardless of whether it came from a relative path or a file URI.');
  config_set('system.core', 'file_default_scheme', 'temporary');

  // Fetch the URL that generates the file.
  $this->backdropGet($generate_url);
  $this->assertResponse(200, 'Image was generated at the URL.');
  $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
  $this->assertRaw(file_get_contents($generated_uri), 'URL returns expected file.');
  $generated_image_info = image_get_info($generated_uri);
  $this->assertEqual($this->backdropGetHeader('Content-Type'), $generated_image_info['mime_type'], 'Expected Content-Type was reported.');
  $this->assertEqual($this->backdropGetHeader('Content-Length'), $generated_image_info['file_size'], 'Expected Content-Length was reported.');
  if ($scheme == 'private') {
    $this->assertEqual($this->backdropGetHeader('Expires'), 'Fri, 16 Jan 2015 07:50:00 GMT', 'Expires header was sent.');
    $this->assertEqual($this->backdropGetHeader('Cache-Control'), 'no-cache, must-revalidate', 'Cache-Control header was set to prevent caching.');
    $this->assertEqual($this->backdropGetHeader('X-Image-Owned-By'), 'image_module_test', 'Expected custom header has been added.');

    // Make sure that a second request to the already existing derivate works
    // too.
    $this->backdropGet($generate_url);
    $this->assertResponse(200, 'Image was generated at the URL.');

    // Make sure that access is denied for existing style files if we do not
    // have access.
    state_set('image_module_test_file_download', FALSE);
    $this->backdropGet($generate_url);
    $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');

    // Repeat this with a different file that we do not have access to and
    // make sure that access is denied.
    $file_noaccess = array_shift($files);
    $original_uri_noaccess = file_unmanaged_copy($file_noaccess->uri, $scheme . '://', FILE_EXISTS_RENAME);
    $generated_uri_noaccess = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/' . backdrop_basename($original_uri_noaccess);
    $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.');
    $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);

    $this->backdropGet($generate_url_noaccess);
    $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
    // Verify that images are not appended to the response. Currently this test only uses PNG images.
    if (strpos($generate_url, '.png') === FALSE) {
      $this->fail('Confirming that private image styles are not appended require PNG file.');
    }
    else {
      // Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the
      // response body.
      $this->assertNoRaw(chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), 'No PNG signature found in the response body.');
    }
  }
}