1 image.test | public ImageStylesPathAndUrlUnitTest::_testImageStyleUrlAndPath($scheme, $clean_url = TRUE) |
Test image_style_url().
File
- core/
modules/ image/ tests/ image.test, line 215 - Tests for image.module.
Class
- ImageStylesPathAndUrlUnitTest
- Tests the functions for generating paths and URLs for image styles.
Code
public 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.
$raster_files = $this->backdropGetTestFiles('image');
$raster_file = array_shift($raster_files);
$svg_files = $this->backdropGetTestFiles('svg');
$svg_file = array_shift($svg_files);
foreach (array($raster_file, $svg_file) as $file) {
$image_info = image_get_info($file->uri);
$image_is_svg = image_is_svg($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 && !$image_is_svg) {
$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.
if (!$image_is_svg) {
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.');
if (!$image_is_svg) {
$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.');
// Some web servers may return a "chunked" response and ignore the
// Content-Length headers returned by image_style_deliver(). Allow
// either as an acceptable response.
// See https://github.com/backdrop/backdrop-issues/issues/6459.
$content_length = $this->backdropGetHeader('Content-Length');
$transfer_encoding = $this->backdropGetHeader('Transfer-Encoding');
if ($transfer_encoding == 'chunked') {
$this->pass('Response was chunked without a Content-Length.');
}
elseif ($content_length == $generated_image_info['file_size']) {
$this->pass('Expected Content-Length was reported.');
}
else {
$this->fail('Expected Content-Length or Chunked response was not returned.');
}
}
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_no_access = $image_is_svg ? array_shift($svg_files) : array_shift($raster_files);
$original_uri_no_access = file_unmanaged_copy($file_no_access->uri, $scheme . '://', FILE_EXISTS_RENAME);
$generated_uri_no_access = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/' . backdrop_basename($original_uri_no_access);
$this->assertFalse(file_exists($generated_uri_no_access), 'Generated file does not exist.');
$generate_url_no_access = image_style_url($this->style_name, $original_uri_no_access);
$this->backdropGet($generate_url_no_access);
$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 && strpos($generate_url, '.svg') === FALSE) {
$this->fail('Confirming that private image styles are not appended require PNG file.');
}
elseif (strpos($generate_url, '.png') === TRUE) {
// 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.');
}
}
}
}