Chris@16: style = ImageStyle::create([ Chris@16: 'name' => 'style_foo', Chris@16: 'label' => $this->randomString(), Chris@16: ]); Chris@16: $this->style->save(); Chris@16: Chris@16: // Create a new language. Chris@16: ConfigurableLanguage::createFromLangcode('fr')->save(); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests \Drupal\image\ImageStyleInterface::buildUri(). Chris@16: */ Chris@16: public function testImageStylePath() { Chris@16: $scheme = 'public'; Chris@16: $actual = $this->style->buildUri("$scheme://foo/bar.gif"); Chris@16: $expected = "$scheme://styles/" . $this->style->id() . "/$scheme/foo/bar.gif"; Chris@16: $this->assertEqual($actual, $expected, 'Got the path for a file URI.'); Chris@16: Chris@16: $actual = $this->style->buildUri('foo/bar.gif'); Chris@16: $expected = "$scheme://styles/" . $this->style->id() . "/$scheme/foo/bar.gif"; Chris@16: $this->assertEqual($actual, $expected, 'Got the path for a relative file path.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL using the "public://" scheme. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPublic() { Chris@16: $this->doImageStyleUrlAndPathTests('public'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL using the "private://" scheme. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPrivate() { Chris@16: $this->doImageStyleUrlAndPathTests('private'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL with the "public://" scheme and unclean URLs. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPublicUnclean() { Chris@16: $this->doImageStyleUrlAndPathTests('public', FALSE); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL with the "private://" schema and unclean URLs. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPrivateUnclean() { Chris@16: $this->doImageStyleUrlAndPathTests('private', FALSE); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL with the "public://" schema and language prefix. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPublicLanguage() { Chris@16: $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE, 'fr'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL with the "private://" schema and language prefix. Chris@16: */ Chris@16: public function testImageStyleUrlAndPathPrivateLanguage() { Chris@16: $this->doImageStyleUrlAndPathTests('private', TRUE, TRUE, 'fr'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests an image style URL with a file URL that has an extra slash in it. Chris@16: */ Chris@16: public function testImageStyleUrlExtraSlash() { Chris@16: $this->doImageStyleUrlAndPathTests('public', TRUE, TRUE); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests that an invalid source image returns a 404. Chris@16: */ Chris@16: public function testImageStyleUrlForMissingSourceImage() { Chris@16: $non_existent_uri = 'public://foo.png'; Chris@16: $generated_url = $this->style->buildUrl($non_existent_uri); Chris@16: $this->drupalGet($generated_url); Chris@16: $this->assertResponse(404, 'Accessing an image style URL with a source image that does not exist provides a 404 error response.'); Chris@16: } Chris@16: Chris@16: /** Chris@16: * Tests building an image style URL. Chris@16: */ Chris@16: public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE, $langcode = FALSE) { Chris@16: $this->prepareRequestForGenerator($clean_url); Chris@16: Chris@16: // Make the default scheme neither "public" nor "private" to verify the Chris@16: // functions work for other than the default scheme. Chris@16: $this->config('system.file')->set('default_scheme', 'temporary')->save(); Chris@16: Chris@16: // Create the directories for the styles. Chris@16: $directory = $scheme . '://styles/' . $this->style->id(); Chris@18: $status = \Drupal::service('file_system')->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY); Chris@16: $this->assertNotIdentical(FALSE, $status, 'Created the directory for the generated images for the test style.'); Chris@16: Chris@16: // Override the language to build the URL for the correct language. Chris@16: if ($langcode) { Chris@16: $language_manager = \Drupal::service('language_manager'); Chris@16: $language = $language_manager->getLanguage($langcode); Chris@16: $language_manager->setConfigOverrideLanguage($language); Chris@16: } Chris@16: Chris@16: // Create a working copy of the file. Chris@16: $files = $this->drupalGetTestFiles('image'); Chris@16: $file = array_shift($files); Chris@18: /** @var \Drupal\Core\File\FileSystemInterface $file_system */ Chris@18: $file_system = \Drupal::service('file_system'); Chris@18: $original_uri = $file_system->copy($file->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME); Chris@16: // Let the image_module_test module know about this file, so it can claim Chris@16: // ownership in hook_file_download(). Chris@16: \Drupal::state()->set('image.test_file_download', $original_uri); Chris@16: $this->assertNotIdentical(FALSE, $original_uri, 'Created the generated image file.'); Chris@16: Chris@16: // Get the URL of a file that has not been generated and try to create it. Chris@16: $generated_uri = $this->style->buildUri($original_uri); Chris@16: $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); Chris@16: $generate_url = $this->style->buildUrl($original_uri, $clean_url); Chris@16: Chris@16: // Make sure that language prefix is never added to the image style URL. Chris@16: if ($langcode) { Chris@16: $this->assertTrue(strpos($generate_url, "/$langcode/") === FALSE, 'Langcode was not found in the image style URL.'); Chris@16: } Chris@16: Chris@16: // Ensure that the tests still pass when the file is generated by accessing Chris@16: // a poorly constructed (but still valid) file URL that has an extra slash Chris@16: // in it. Chris@16: if ($extra_slash) { Chris@16: $modified_uri = str_replace('://', ':///', $original_uri); Chris@16: $this->assertNotEqual($original_uri, $modified_uri, 'An extra slash was added to the generated file URI.'); Chris@16: $generate_url = $this->style->buildUrl($modified_uri, $clean_url); Chris@16: } Chris@16: if (!$clean_url) { Chris@16: $this->assertTrue(strpos($generate_url, 'index.php/') !== FALSE, 'When using non-clean URLS, the system path contains the script name.'); Chris@16: } Chris@16: // Add some extra chars to the token. Chris@16: $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url)); Chris@16: $this->assertResponse(404, 'Image was inaccessible at the URL with an invalid token.'); Chris@16: // Change the parameter name so the token is missing. Chris@16: $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $generate_url)); Chris@16: $this->assertResponse(404, 'Image was inaccessible at the URL with a missing token.'); Chris@16: Chris@16: // Check that the generated URL is the same when we pass in a relative path Chris@16: // rather than a URI. We need to temporarily switch the default scheme to Chris@16: // match the desired scheme before testing this, then switch it back to the Chris@16: // "temporary" scheme used throughout this test afterwards. Chris@16: $this->config('system.file')->set('default_scheme', $scheme)->save(); Chris@16: $relative_path = file_uri_target($original_uri); Chris@16: $generate_url_from_relative_path = $this->style->buildUrl($relative_path, $clean_url); Chris@16: $this->assertEqual($generate_url, $generate_url_from_relative_path); Chris@16: $this->config('system.file')->set('default_scheme', 'temporary')->save(); Chris@16: Chris@16: // Fetch the URL that generates the file. Chris@16: $this->drupalGet($generate_url); Chris@16: $this->assertResponse(200, 'Image was generated at the URL.'); Chris@16: $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.'); Chris@16: // assertRaw can't be used with string containing non UTF-8 chars. Chris@16: $this->assertNotEmpty(file_get_contents($generated_uri), 'URL returns expected file.'); Chris@16: $image = $this->container->get('image.factory')->get($generated_uri); Chris@16: $this->assertEqual($this->drupalGetHeader('Content-Type'), $image->getMimeType(), 'Expected Content-Type was reported.'); Chris@16: $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize(), 'Expected Content-Length was reported.'); Chris@16: Chris@16: // Check that we did not download the original file. Chris@16: $original_image = $this->container->get('image.factory') Chris@16: ->get($original_uri); Chris@16: $this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize()); Chris@16: Chris@16: if ($scheme == 'private') { Chris@16: $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); Chris@16: $this->assertNotEqual(strpos($this->drupalGetHeader('Cache-Control'), 'no-cache'), FALSE, 'Cache-Control header contains \'no-cache\' to prevent caching.'); Chris@16: $this->assertEqual($this->drupalGetHeader('X-Image-Owned-By'), 'image_module_test', 'Expected custom header has been added.'); Chris@16: Chris@16: // Make sure that a second request to the already existing derivative Chris@16: // works too. Chris@16: $this->drupalGet($generate_url); Chris@16: $this->assertResponse(200, 'Image was generated at the URL.'); Chris@16: Chris@16: // Check that the second request also returned the generated image. Chris@16: $this->assertEqual($this->drupalGetHeader('Content-Length'), $image->getFileSize()); Chris@16: Chris@16: // Check that we did not download the original file. Chris@16: $this->assertNotEqual($this->drupalGetHeader('Content-Length'), $original_image->getFileSize()); Chris@16: Chris@16: // Make sure that access is denied for existing style files if we do not Chris@16: // have access. Chris@16: \Drupal::state()->delete('image.test_file_download'); Chris@16: $this->drupalGet($generate_url); Chris@16: $this->assertResponse(403, 'Confirmed that access is denied for the private image style.'); Chris@16: Chris@16: // Repeat this with a different file that we do not have access to and Chris@16: // make sure that access is denied. Chris@16: $file_noaccess = array_shift($files); Chris@18: $original_uri_noaccess = $file_system->copy($file_noaccess->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME); Chris@18: $generated_uri_noaccess = $scheme . '://styles/' . $this->style->id() . '/' . $scheme . '/' . $file_system->basename($original_uri_noaccess); Chris@16: $this->assertFalse(file_exists($generated_uri_noaccess), 'Generated file does not exist.'); Chris@16: $generate_url_noaccess = $this->style->buildUrl($original_uri_noaccess); Chris@16: Chris@16: $this->drupalGet($generate_url_noaccess); Chris@16: $this->assertResponse(403, 'Confirmed that access is denied for the private image style.'); Chris@16: // Verify that images are not appended to the response. Chris@16: // Currently this test only uses PNG images. Chris@16: if (strpos($generate_url, '.png') === FALSE) { Chris@16: $this->fail('Confirming that private image styles are not appended require PNG file.'); Chris@16: } Chris@16: else { Chris@16: // Check for PNG-Signature Chris@16: // (cf. http://www.libpng.org/pub/png/book/chapter08.html#png.ch08.div.2) Chris@16: // in the response body. Chris@16: $raw = $this->getSession()->getPage()->getContent(); Chris@16: $this->assertFalse(strpos($raw, chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10))); Chris@16: } Chris@16: } Chris@16: else { Chris@16: $this->assertEqual($this->drupalGetHeader('Expires'), 'Sun, 19 Nov 1978 05:00:00 GMT', 'Expires header was sent.'); Chris@16: $this->assertEqual(strpos($this->drupalGetHeader('Cache-Control'), 'no-cache'), FALSE, 'Cache-Control header contains \'no-cache\' to prevent caching.'); Chris@16: Chris@16: if ($clean_url) { Chris@16: // Add some extra chars to the token. Chris@16: $this->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url)); Chris@16: $this->assertResponse(200, 'Existing image was accessible at the URL with an invalid token.'); Chris@16: } Chris@16: } Chris@16: Chris@16: // Allow insecure image derivatives to be created for the remainder of this Chris@16: // test. Chris@16: $this->config('image.settings') Chris@16: ->set('allow_insecure_derivatives', TRUE) Chris@16: ->save(); Chris@16: Chris@16: // Create another working copy of the file. Chris@16: $files = $this->drupalGetTestFiles('image'); Chris@16: $file = array_shift($files); Chris@18: $original_uri = $file_system->copy($file->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME); Chris@16: // Let the image_module_test module know about this file, so it can claim Chris@16: // ownership in hook_file_download(). Chris@16: \Drupal::state()->set('image.test_file_download', $original_uri); Chris@16: Chris@16: // Suppress the security token in the URL, then get the URL of a file that Chris@16: // has not been created and try to create it. Check that the security token Chris@16: // is not present in the URL but that the image is still accessible. Chris@16: $this->config('image.settings')->set('suppress_itok_output', TRUE)->save(); Chris@16: $generated_uri = $this->style->buildUri($original_uri); Chris@16: $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.'); Chris@16: $generate_url = $this->style->buildUrl($original_uri, $clean_url); Chris@16: $this->assertIdentical(strpos($generate_url, IMAGE_DERIVATIVE_TOKEN . '='), FALSE, 'The security token does not appear in the image style URL.'); Chris@16: $this->drupalGet($generate_url); Chris@16: $this->assertResponse(200, 'Image was accessible at the URL with a missing token.'); Chris@16: Chris@17: // Stop suppressing the security token in the URL. Chris@16: $this->config('image.settings')->set('suppress_itok_output', FALSE)->save(); Chris@16: // Ensure allow_insecure_derivatives is enabled. Chris@16: $this->assertEqual($this->config('image.settings') Chris@16: ->get('allow_insecure_derivatives'), TRUE); Chris@16: // Check that a security token is still required when generating a second Chris@16: // image derivative using the first one as a source. Chris@16: $nested_url = $this->style->buildUrl($generated_uri, $clean_url); Chris@16: $matches_expected_url_format = (boolean) preg_match('/styles\/' . $this->style->id() . '\/' . $scheme . '\/styles\/' . $this->style->id() . '\/' . $scheme . '/', $nested_url); Chris@16: $this->assertTrue($matches_expected_url_format, "URL for a derivative of an image style matches expected format."); Chris@16: $nested_url_with_wrong_token = str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $nested_url); Chris@16: $this->drupalGet($nested_url_with_wrong_token); Chris@16: $this->assertResponse(404, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token.'); Chris@16: // Check that this restriction cannot be bypassed by adding extra slashes Chris@16: // to the URL. Chris@16: $this->drupalGet(substr_replace($nested_url_with_wrong_token, '//styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/'))); Chris@16: $this->assertResponse(404, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token, even with an extra forward slash in the URL.'); Chris@16: $this->drupalGet(substr_replace($nested_url_with_wrong_token, '////styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/'))); Chris@16: $this->assertResponse(404, 'Image generated from an earlier derivative was inaccessible at the URL with a missing token, even with multiple forward slashes in the URL.'); Chris@16: // Make sure the image can still be generated if a correct token is used. Chris@16: $this->drupalGet($nested_url); Chris@16: $this->assertResponse(200, 'Image was accessible when a correct token was provided in the URL.'); Chris@16: Chris@16: // Check that requesting a nonexistent image does not create any new Chris@16: // directories in the file system. Chris@16: $directory = $scheme . '://styles/' . $this->style->id() . '/' . $scheme . '/' . $this->randomMachineName(); Chris@16: $this->drupalGet(file_create_url($directory . '/' . $this->randomString())); Chris@16: $this->assertFalse(file_exists($directory), 'New directory was not created in the filesystem when requesting an unauthorized image.'); Chris@16: } Chris@16: Chris@16: }