View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManager;
use Drupal\image\Entity\ImageStyle;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class ImageStylesPathAndUrlTest extends BrowserTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
compareFiles as drupalCompareFiles;
}
protected static $modules = [
'image',
'image_module_test',
'language',
];
protected $defaultTheme = 'stark';
protected $style;
protected function setUp() : void {
parent::setUp();
$this->style = ImageStyle::create([
'name' => 'style_foo',
'label' => $this
->randomString(),
]);
$this->style
->save();
ConfigurableLanguage::createFromLangcode('fr')
->save();
}
public function testImageStylePath() {
$scheme = 'public';
$actual = $this->style
->buildUri("{$scheme}://foo/bar.gif");
$expected = "{$scheme}://styles/" . $this->style
->id() . "/{$scheme}/foo/bar.gif";
$this
->assertEquals($expected, $actual, 'Got the path for a file URI.');
$actual = $this->style
->buildUri('foo/bar.gif');
$expected = "{$scheme}://styles/" . $this->style
->id() . "/{$scheme}/foo/bar.gif";
$this
->assertEquals($expected, $actual, 'Got the path for a relative file path.');
}
public function testImageStyleUrlAndPathPublic() {
$this
->doImageStyleUrlAndPathTests('public');
}
public function testImageStyleUrlAndPathPrivate() {
$this
->doImageStyleUrlAndPathTests('private');
}
public function testImageStyleUrlAndPathPublicUnclean() {
$this
->doImageStyleUrlAndPathTests('public', FALSE);
}
public function testImageStyleUrlAndPathPrivateUnclean() {
$this
->doImageStyleUrlAndPathTests('private', FALSE);
}
public function testImageStyleUrlAndPathPublicLanguage() {
$this
->doImageStyleUrlAndPathTests('public', TRUE, TRUE, 'fr');
}
public function testImageStyleUrlAndPathPrivateLanguage() {
$this
->doImageStyleUrlAndPathTests('private', TRUE, TRUE, 'fr');
}
public function testImageStyleUrlExtraSlash() {
$this
->doImageStyleUrlAndPathTests('public', TRUE, TRUE);
}
public function testImageStyleUrlForMissingSourceImage() {
$non_existent_uri = 'public://foo.png';
$generated_url = $this->style
->buildUrl($non_existent_uri);
$this
->drupalGet($generated_url);
$this
->assertSession()
->statusCodeEquals(404);
}
public function doImageStyleUrlAndPathTests($scheme, $clean_url = TRUE, $extra_slash = FALSE, $langcode = FALSE) {
$this
->prepareRequestForGenerator($clean_url);
$this
->config('system.file')
->set('default_scheme', 'temporary')
->save();
$directory = $scheme . '://styles/' . $this->style
->id();
$status = \Drupal::service('file_system')
->prepareDirectory($directory, FileSystemInterface::CREATE_DIRECTORY);
$this
->assertNotFalse($status, 'Created the directory for the generated images for the test style.');
if ($langcode) {
$language_manager = \Drupal::service('language_manager');
$language = $language_manager
->getLanguage($langcode);
$language_manager
->setConfigOverrideLanguage($language);
}
$files = $this
->drupalGetTestFiles('image');
$file = array_shift($files);
$file_system = \Drupal::service('file_system');
$original_uri = $file_system
->copy($file->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME);
\Drupal::state()
->set('image.test_file_download', $original_uri);
$this
->assertNotFalse($original_uri, 'Created the generated image file.');
$generated_uri = $this->style
->buildUri($original_uri);
$this
->assertFileDoesNotExist($generated_uri);
$generate_url = $this->style
->buildUrl($original_uri, $clean_url);
if ($langcode) {
$this
->assertStringNotContainsString("/{$langcode}/", $generate_url, 'Langcode was not found in the image style URL.');
}
if ($extra_slash) {
$modified_uri = str_replace('://', ':///', $original_uri);
$this
->assertNotEquals($original_uri, $modified_uri, 'An extra slash was added to the generated file URI.');
$generate_url = $this->style
->buildUrl($modified_uri, $clean_url);
}
if (!$clean_url) {
$this
->assertStringContainsString('index.php/', $generate_url, 'When using non-clean URLS, the system path contains the script name.');
}
$this
->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url));
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $generate_url));
$this
->assertSession()
->statusCodeEquals(404);
$this
->config('system.file')
->set('default_scheme', $scheme)
->save();
$relative_path = StreamWrapperManager::getTarget($original_uri);
$generate_url_from_relative_path = $this->style
->buildUrl($relative_path, $clean_url);
$this
->assertEquals($generate_url, $generate_url_from_relative_path);
$this
->config('system.file')
->set('default_scheme', 'temporary')
->save();
$this
->drupalGet($generate_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertFileExists($generated_uri);
$this
->assertNotEmpty(file_get_contents($generated_uri), 'URL returns expected file.');
$image = $this->container
->get('image.factory')
->get($generated_uri);
$this
->assertSession()
->responseHeaderEquals('Content-Type', $image
->getMimeType());
$this
->assertSession()
->responseHeaderEquals('Content-Length', (string) $image
->getFileSize());
$original_image = $this->container
->get('image.factory')
->get($original_uri);
$this
->assertSession()
->responseHeaderNotEquals('Content-Length', (string) $original_image
->getFileSize());
if ($scheme == 'private') {
$this
->assertSession()
->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
$this
->assertSession()
->responseHeaderContains('Cache-Control', 'no-cache');
$this
->assertSession()
->responseHeaderEquals('X-Image-Owned-By', 'image_module_test');
$this
->drupalGet($generate_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertSession()
->responseHeaderEquals('Content-Length', (string) $image
->getFileSize());
$this
->assertSession()
->responseHeaderNotEquals('Content-Length', (string) $original_image
->getFileSize());
\Drupal::state()
->delete('image.test_file_download');
$this
->drupalGet($generate_url);
$this
->assertSession()
->statusCodeEquals(403);
$file_noaccess = array_shift($files);
$original_uri_noaccess = $file_system
->copy($file_noaccess->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME);
$generated_uri_noaccess = $scheme . '://styles/' . $this->style
->id() . '/' . $scheme . '/' . $file_system
->basename($original_uri_noaccess);
$this
->assertFileDoesNotExist($generated_uri_noaccess);
$generate_url_noaccess = $this->style
->buildUrl($original_uri_noaccess);
$this
->drupalGet($generate_url_noaccess);
$this
->assertSession()
->statusCodeEquals(403);
if (strpos($generate_url, '.png') === FALSE) {
$this
->fail('Confirming that private image styles are not appended require PNG file.');
}
else {
$raw = $this
->getSession()
->getPage()
->getContent();
$this
->assertStringNotContainsString(chr(137) . chr(80) . chr(78) . chr(71) . chr(13) . chr(10) . chr(26) . chr(10), $raw);
}
}
else {
$this
->assertSession()
->responseHeaderEquals('Expires', 'Sun, 19 Nov 1978 05:00:00 GMT');
$this
->assertSession()
->responseHeaderNotContains('Cache-Control', 'no-cache');
if ($clean_url) {
$this
->drupalGet(str_replace(IMAGE_DERIVATIVE_TOKEN . '=', IMAGE_DERIVATIVE_TOKEN . '=Zo', $generate_url));
$this
->assertSession()
->statusCodeEquals(200);
}
}
$this
->config('image.settings')
->set('allow_insecure_derivatives', TRUE)
->save();
$files = $this
->drupalGetTestFiles('image');
$file = array_shift($files);
$original_uri = $file_system
->copy($file->uri, $scheme . '://', FileSystemInterface::EXISTS_RENAME);
\Drupal::state()
->set('image.test_file_download', $original_uri);
$this
->config('image.settings')
->set('suppress_itok_output', TRUE)
->save();
$generated_uri = $this->style
->buildUri($original_uri);
$this
->assertFileDoesNotExist($generated_uri);
$generate_url = $this->style
->buildUrl($original_uri, $clean_url);
$this
->assertStringNotContainsString(IMAGE_DERIVATIVE_TOKEN . '=', $generate_url, 'The security token does not appear in the image style URL.');
$this
->drupalGet($generate_url);
$this
->assertSession()
->statusCodeEquals(200);
$this
->config('image.settings')
->set('suppress_itok_output', FALSE)
->save();
$this
->assertEquals(TRUE, $this
->config('image.settings')
->get('allow_insecure_derivatives'));
$nested_url = $this->style
->buildUrl($generated_uri, $clean_url);
$matches_expected_url_format = (bool) preg_match('/styles\\/' . $this->style
->id() . '\\/' . $scheme . '\\/styles\\/' . $this->style
->id() . '\\/' . $scheme . '/', $nested_url);
$this
->assertTrue($matches_expected_url_format, "URL for a derivative of an image style matches expected format.");
$nested_url_with_wrong_token = str_replace(IMAGE_DERIVATIVE_TOKEN . '=', 'wrongparam=', $nested_url);
$this
->drupalGet($nested_url_with_wrong_token);
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet(substr_replace($nested_url_with_wrong_token, '//styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/')));
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet(substr_replace($nested_url_with_wrong_token, '////styles/', strrpos($nested_url_with_wrong_token, '/styles/'), strlen('/styles/')));
$this
->assertSession()
->statusCodeEquals(404);
$this
->drupalGet($nested_url);
$this
->assertSession()
->statusCodeEquals(200);
$directory = $scheme . '://styles/' . $this->style
->id() . '/' . $scheme . '/' . $this
->randomMachineName();
$this
->drupalGet(\Drupal::service('file_url_generator')
->generateAbsoluteString($directory . '/' . $this
->randomString()));
$this
->assertDirectoryDoesNotExist($directory);
}
}