ImageStylesPathAndUrlTest.php 6.93 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\image\Tests\ImageStylesPathAndUrlTest.
 */

namespace Drupal\image\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Tests the functions for generating paths and URLs for image styles.
 */
class ImageStylesPathAndUrlTest extends WebTestBase {
16 17 18 19 20 21 22 23

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('image', 'image_module_test');

24 25 26 27 28 29 30 31 32 33 34 35 36
  protected $style_name;
  protected $image_info;
  protected $image_filepath;

  public static function getInfo() {
    return array(
      'name' => 'Image styles path and URL functions',
      'description' => 'Tests functions for generating paths and URLs to image styles.',
      'group' => 'Image',
    );
  }

  function setUp() {
37
    parent::setUp();
38 39

    $this->style_name = 'style_foo';
40 41
    $style = entity_create('image_style', array('name' => $this->style_name, 'label' => $this->randomString()));
    $style->save();
42 43 44 45 46 47 48 49 50
  }

  /**
   * Test image_style_path().
   */
  function testImageStylePath() {
    $scheme = 'public';
    $actual = image_style_path($this->style_name, "$scheme://foo/bar.gif");
    $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
51
    $this->assertEqual($actual, $expected, 'Got the path for a file URI.');
52 53 54

    $actual = image_style_path($this->style_name, 'foo/bar.gif');
    $expected = "$scheme://styles/" . $this->style_name . "/$scheme/foo/bar.gif";
55
    $this->assertEqual($actual, $expected, 'Got the path for a relative file path.');
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  }

  /**
   * Test image_style_url() with a file using the "public://" scheme.
   */
  function testImageStyleUrlAndPathPublic() {
    $this->_testImageStyleUrlAndPath('public');
  }

  /**
   * Test image_style_url() with a file using the "private://" scheme.
   */
  function testImageStyleUrlAndPathPrivate() {
    $this->_testImageStyleUrlAndPath('private');
  }

  /**
   * Test image_style_url() with the "public://" scheme and unclean URLs.
   */
   function testImageStylUrlAndPathPublicUnclean() {
     $this->_testImageStyleUrlAndPath('public', FALSE);
   }

  /**
   * Test image_style_url() with the "private://" schema and unclean URLs.
   */
  function testImageStyleUrlAndPathPrivateUnclean() {
    $this->_testImageStyleUrlAndPath('private', FALSE);
  }

  /**
   * Test image_style_url().
   */
  function _testImageStyleUrlAndPath($scheme, $clean_url = TRUE) {
    $script_path_original = $GLOBALS['script_path'];
    $GLOBALS['script_path'] = $clean_url ? '' : 'index.php/';

    // Make the default scheme neither "public" nor "private" to verify the
    // functions work for other than the default scheme.
95
    config('system.file')->set('default_scheme', 'temporary')->save();
96 97 98 99

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

    // Create a working copy of the file.
    $files = $this->drupalGetTestFiles('image');
104
    $file = array_shift($files);
105 106 107 108
    $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().
109
    state()->set('image.test_file_download', $original_uri);
110
    $this->assertNotIdentical(FALSE, $original_uri, 'Created the generated image file.');
111 112 113

    // Get the URL of a file that has not been generated and try to create it.
    $generated_uri = $scheme . '://styles/' . $this->style_name . '/' . $scheme . '/'. drupal_basename($original_uri);
114
    $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
115 116 117 118 119 120 121 122
    $generate_url = image_style_url($this->style_name, $original_uri);

    if ($GLOBALS['script_path']) {
      $this->assertTrue(strpos($generate_url, $GLOBALS['script_path']) !== FALSE, 'When using non-clean URLS, the system path contains the script name.');
    }

    // Fetch the URL that generates the file.
    $this->drupalGet($generate_url);
123 124 125
    $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.');
126
    $generated_image_info = image_get_info($generated_uri);
127 128
    $this->assertEqual($this->drupalGetHeader('Content-Type'), $generated_image_info['mime_type'], 'Expected Content-Type was reported.');
    $this->assertEqual($this->drupalGetHeader('Content-Length'), $generated_image_info['file_size'], 'Expected Content-Length was reported.');
129
    if ($scheme == 'private') {
130 131 132
      $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
      $this->assertEqual($this->drupalGetHeader('Cache-Control'), 'no-cache, private', 'Cache-Control header was set to prevent caching.');
      $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', 'Expected custom header has been added.');
133 134 135 136

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

139 140 141 142 143 144
      // Make sure that access is denied for existing style files if we do not
      // have access.
      state()->delete('image.test_file_download');
      $this->drupalGet($generate_url);
      $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');

145 146 147 148 149
      // 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 . '/'. drupal_basename($original_uri_noaccess);
150
      $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.');
151 152 153
      $generate_url_noaccess = image_style_url($this->style_name, $original_uri_noaccess);

      $this->drupalGet($generate_url_noaccess);
154
      $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
155 156 157 158 159 160 161 162 163
      // 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.');
      }
164 165 166 167 168
    }

    $GLOBALS['script_path'] = $script_path_original;
  }
}