comparison core/modules/image/src/Tests/ImageStylesPathAndUrlTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\image\Tests; 3 namespace Drupal\image\Tests;
4 4
5 use Drupal\image\Entity\ImageStyle; 5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\simpletest\WebTestBase; 7 use Drupal\simpletest\WebTestBase;
7 8
8 /** 9 /**
9 * Tests the functions for generating paths and URLs for image styles. 10 * Tests the functions for generating paths and URLs for image styles.
10 * 11 *
15 /** 16 /**
16 * Modules to enable. 17 * Modules to enable.
17 * 18 *
18 * @var array 19 * @var array
19 */ 20 */
20 public static $modules = ['image', 'image_module_test']; 21 public static $modules = ['image', 'image_module_test', 'language'];
21 22
22 /** 23 /**
24 * The image style.
25 *
23 * @var \Drupal\image\ImageStyleInterface 26 * @var \Drupal\image\ImageStyleInterface
24 */ 27 */
25 protected $style; 28 protected $style;
26 29
30 /**
31 * {@inheritdoc}
32 */
27 protected function setUp() { 33 protected function setUp() {
28 parent::setUp(); 34 parent::setUp();
29 35
30 $this->style = ImageStyle::create(['name' => 'style_foo', 'label' => $this->randomString()]); 36 $this->style = ImageStyle::create([
37 'name' => 'style_foo',
38 'label' => $this->randomString(),
39 ]);
31 $this->style->save(); 40 $this->style->save();
41
42 // Create a new language.
43 ConfigurableLanguage::createFromLangcode('fr')->save();
32 } 44 }
33 45
34 /** 46 /**
35 * Tests \Drupal\image\ImageStyleInterface::buildUri(). 47 * Tests \Drupal\image\ImageStyleInterface::buildUri().
36 */ 48 */
72 public function testImageStyleUrlAndPathPrivateUnclean() { 84 public function testImageStyleUrlAndPathPrivateUnclean() {
73 $this->doImageStyleUrlAndPathTests('private', FALSE); 85 $this->doImageStyleUrlAndPathTests('private', FALSE);
74 } 86 }
75 87
76 /** 88 /**
89 * Tests an image style URL with the "public://" schema and language prefix.
90 */
91 public function testImageStyleUrlAndPathPublicLanguage() {
92 $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE, 'fr');
93 }
94
95 /**
96 * Tests an image style URL with the "private://" schema and language prefix.
97 */
98 public function testImageStyleUrlAndPathPrivateLanguage() {
99 $this->doImageStyleUrlAndPathTests('private', TRUE, TRUE, 'fr');
100 }
101
102 /**
77 * Tests an image style URL with a file URL that has an extra slash in it. 103 * Tests an image style URL with a file URL that has an extra slash in it.
78 */ 104 */
79 public function testImageStyleUrlExtraSlash() { 105 public function testImageStyleUrlExtraSlash() {
80 $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE); 106 $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE);
81 } 107 }
91 } 117 }
92 118
93 /** 119 /**
94 * Tests building an image style URL. 120 * Tests building an image style URL.
95 */ 121 */
96 public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE) { 122 public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE, $langcode = FALSE) {
97 $this->prepareRequestForGenerator($clean_url); 123 $this->prepareRequestForGenerator($clean_url);
98 124
99 // Make the default scheme neither "public" nor "private" to verify the 125 // Make the default scheme neither "public" nor "private" to verify the
100 // functions work for other than the default scheme. 126 // functions work for other than the default scheme.
101 $this->config('system.file')->set('default_scheme', 'temporary')->save(); 127 $this->config('system.file')->set('default_scheme', 'temporary')->save();
102 128
103 // Create the directories for the styles. 129 // Create the directories for the styles.
104 $directory = $scheme . '://styles/' . $this->style->id(); 130 $directory = $scheme . '://styles/' . $this->style->id();
105 $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY); 131 $status = file_prepare_directory($directory, FILE_CREATE_DIRECTORY);
106 $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.'); 132 $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.');
133
134 // Override the language to build the URL for the correct language.
135 if ($langcode) {
136 $language_manager = \Drupal::service('language_manager');
137 $language = $language_manager->getLanguage($langcode);
138 $language_manager->setConfigOverrideLanguage($language);
139 }
107 140
108 // Create a working copy of the file. 141 // Create a working copy of the file.
109 $files = $this->drupalGetTestFiles('image'); 142 $files = $this->drupalGetTestFiles('image');
110 $file = array_shift($files); 143 $file = array_shift($files);
111 $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME); 144 $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
116 149
117 // Get the URL of a file that has not been generated and try to create it. 150 // Get the URL of a file that has not been generated and try to create it.
118 $generated_uri = $this->style->buildUri($original_uri); 151 $generated_uri = $this->style->buildUri($original_uri);
119 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); 152 $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
120 $generate_url = $this->style->buildUrl($original_uri, $clean_url); 153 $generate_url = $this->style->buildUrl($original_uri, $clean_url);
154
155 // Make sure that language prefix is never added to the image style URL.
156 if ($langcode) {
157 $this->assertTrue(strpos($generate_url, "/$langcode/") === FALSE, 'Langcode was not found in the image style URL.');
158 }
121 159
122 // Ensure that the tests still pass when the file is generated by accessing 160 // Ensure that the tests still pass when the file is generated by accessing
123 // a poorly constructed (but still valid) file URL that has an extra slash 161 // a poorly constructed (but still valid) file URL that has an extra slash
124 // in it. 162 // in it.
125 if ($extra_slash) { 163 if ($extra_slash) {
155 $image = $this->container->get('image.factory')->get($generated_uri); 193 $image = $this->container->get('image.factory')->get($generated_uri);
156 $this->assertEqual($this->drupalGetHeader('Content-Type'), $image->getMimeType(), 'Expected Content-Type was reported.'); 194 $this->assertEqual($this->drupalGetHeader('Content-Type'), $image->getMimeType(), 'Expected Content-Type was reported.');
157 $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.'); 195 $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.');
158 196
159 // Check that we did not download the original file. 197 // Check that we did not download the original file.
160 $original_image = $this->container->get('image.factory')->get($original_uri); 198 $original_image = $this->container->get('image.factory')
199 ->get($original_uri);
161 $this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize()); 200 $this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize());
162 201
163 if ($scheme == 'private') { 202 if ($scheme == 'private') {
164 $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); 203 $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
165 $this->assertNotEqual(strpos($this->drupalGetHeader('Cache-Control'), 'no-cache'), FALSE, 'Cache-Control header contains \'no-cache\' to prevent caching.'); 204 $this->assertNotEqual(strpos($this->drupalGetHeader('Cache-Control'), 'no-cache'), FALSE, 'Cache-Control header contains \'no-cache\' to prevent caching.');
190 $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.'); 229 $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.');
191 $generate_url_noaccess = $this->style->buildUrl($original_uri_noaccess); 230 $generate_url_noaccess = $this->style->buildUrl($original_uri_noaccess);
192 231
193 $this->drupalGet($generate_url_noaccess); 232 $this->drupalGet($generate_url_noaccess);
194 $this->assertResponse(403, 'Confirmed that access is denied for the private image style.'); 233 $this->assertResponse(403, 'Confirmed that access is denied for the private image style.');
195 // Verify that images are not appended to the response. Currently this test only uses PNG images. 234 // Verify that images are not appended to the response.
235 // Currently this test only uses PNG images.
196 if (strpos($generate_url, '.png') === FALSE) { 236 if (strpos($generate_url, '.png') === FALSE) {
197 $this->fail('Confirming that private image styles are not appended require PNG file.'); 237 $this->fail('Confirming that private image styles are not appended require PNG file.');
198 } 238 }
199 else { 239 else {
200 // Check for PNG-Signature (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) in the 240 // Check for PNG-Signature
201 // response body. 241 // (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2)
242 // in the response body.
202 $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.'); 243 $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.');
203 } 244 }
204 } 245 }
205 else { 246 else {
206 $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); 247 $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.');
213 } 254 }
214 } 255 }
215 256
216 // Allow insecure image derivatives to be created for the remainder of this 257 // Allow insecure image derivatives to be created for the remainder of this
217 // test. 258 // test.
218 $this->config('image.settings')->set('allow_insecure_derivatives', TRUE)->save(); 259 $this->config('image.settings')
260 ->set('allow_insecure_derivatives', TRUE)
261 ->save();
219 262
220 // Create another working copy of the file. 263 // Create another working copy of the file.
221 $files = $this->drupalGetTestFiles('image'); 264 $files = $this->drupalGetTestFiles('image');
222 $file = array_shift($files); 265 $file = array_shift($files);
223 $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME); 266 $original_uri = file_unmanaged_copy($file->uri, $scheme . '://', FILE_EXISTS_RENAME);
237 $this->assertResponse(200, 'Image was accessible at the URL with a missing token.'); 280 $this->assertResponse(200, 'Image was accessible at the URL with a missing token.');
238 281
239 // Stop supressing the security token in the URL. 282 // Stop supressing the security token in the URL.
240 $this->config('image.settings')->set('suppress_itok_output', FALSE)->save(); 283 $this->config('image.settings')->set('suppress_itok_output', FALSE)->save();
241 // Ensure allow_insecure_derivatives is enabled. 284 // Ensure allow_insecure_derivatives is enabled.
242 $this->assertEqual($this->config('image.settings')->get('allow_insecure_derivatives'), TRUE); 285 $this->assertEqual($this->config('image.settings')
286 ->get('allow_insecure_derivatives'), TRUE);
243 // Check that a security token is still required when generating a second 287 // Check that a security token is still required when generating a second
244 // image derivative using the first one as a source. 288 // image derivative using the first one as a source.
245 $nested_url = $this->style->buildUrl($generated_uri, $clean_url); 289 $nested_url = $this->style->buildUrl($generated_uri, $clean_url);
246 $matches_expected_url_format = (boolean) preg_match('/styles\/' . $this->style->id() . '\/' . $scheme . '\/styles\/' . $this->style->id() . '\/' . $scheme . '/', $nested_url); 290 $matches_expected_url_format = (boolean) preg_match('/styles\/' . $this->style->id() . '\/' . $scheme . '\/styles\/' . $this->style->id() . '\/' . $scheme . '/', $nested_url);
247 $this->assertTrue($matches_expected_url_format, "URL for a derivative of an image style matches expected format."); 291 $this->assertTrue($matches_expected_url_format, "URL for a derivative of an image style matches expected format.");