View source
<?php
namespace Drupal\Tests\imagemagick\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\file_mdm\FileMetadataInterface;
use Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\imagemagick\Kernel\ToolkitSetupTrait;
class ToolkitImagemagickFileMetadataTest extends BrowserTestBase {
use ToolkitSetupTrait;
public static $modules = [
'system',
'imagemagick',
'file_mdm',
'file_test',
];
public function setUp() {
parent::setUp();
$this->imageFactory = $this->container
->get('image.factory');
$this->fileSystem = $this->container
->get('file_system');
$this->testDirectory = 'public://imagetest';
$this->imageFactory
->setToolkitId('imagemagick');
}
public function testFileMetadata($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->setUpToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$this
->prepareImageFileHandling();
$config = \Drupal::configFactory()
->getEditable('imagemagick.settings');
$config_mdm = \Drupal::configFactory()
->getEditable('file_mdm.settings');
$config_mdm
->set('metadata_cache.enabled', TRUE)
->set('metadata_cache.disallowed_paths', [])
->save();
$files = [
'public://image-test.png' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/png',
'colorspace' => 'SRGB',
'profiles' => [],
],
'public://image-test.gif' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/gif',
'colorspace' => 'SRGB',
'profiles' => [],
],
'dummy-remote://image-test.jpg' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/jpeg',
'colorspace' => 'SRGB',
'profiles' => [],
],
'public://test-multi-frame.gif' => [
'skip_dimensions_check' => TRUE,
'frames' => 13,
'mimetype' => 'image/gif',
'colorspace' => 'SRGB',
'profiles' => [],
],
'public://test-exif.jpeg' => [
'skip_dimensions_check' => TRUE,
'frames' => 1,
'mimetype' => 'image/jpeg',
'colorspace' => 'SRGB',
'profiles' => [
'exif',
],
],
'public://test-exif-icc.jpeg' => [
'skip_dimensions_check' => TRUE,
'frames' => 1,
'mimetype' => 'image/jpeg',
'colorspace' => 'SRGB',
'profiles' => [
'exif',
'icc',
],
],
];
$operations = [
'resize' => [
'function' => 'resize',
'arguments' => [
'width' => 20,
'height' => 10,
],
'width' => 20,
'height' => 10,
],
'scale_x' => [
'function' => 'scale',
'arguments' => [
'width' => 20,
],
'width' => 20,
'height' => 10,
],
'rotate_5' => [
'function' => 'rotate',
'arguments' => [
'degrees' => 5,
'background' => '#FF00FF',
],
'width' => 41,
'height' => 23,
],
'convert_jpg' => [
'function' => 'convert',
'width' => 40,
'height' => 20,
'arguments' => [
'extension' => 'jpeg',
],
'mimetype' => 'image/jpeg',
],
];
$fmdm = $this->container
->get('file_metadata_manager');
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-multi-frame.gif', 'public://', FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-exif.jpeg', 'public://', FILE_EXISTS_REPLACE);
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-exif-icc.jpeg', 'public://', FILE_EXISTS_REPLACE);
$config_mdm
->set('metadata_cache.enabled', FALSE)
->save();
foreach ($files as $source_uri => $source_image_data) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri), $source_uri);
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
if ($first) {
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
$this
->assertFalse($fmdm
->has($saved_uri));
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'width'));
}
$fmdm
->release($saved_uri);
$first = FALSE;
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
$config_mdm
->set('metadata_cache.enabled', TRUE)
->save();
foreach ($files as $source_uri => $source_image_data) {
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri));
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$source_image = $this->imageFactory
->get($source_uri);
if ($first) {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
$this
->assertFalse($fmdm
->has($saved_uri));
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'width'));
}
$fmdm
->release($saved_uri);
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
$first = FALSE;
}
}
$directory_scan = file_scan_directory('temporary://', '/ima.*/');
$this
->assertGreaterThan(0, count($directory_scan));
foreach ($directory_scan as $file) {
file_unmanaged_delete($file->uri);
}
$directory_scan = file_scan_directory('temporary://', '/ima.*/');
$this
->assertEquals(0, count($directory_scan));
foreach ($files as $source_uri => $source_image_data) {
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
$this
->assertFalse($fmdm
->has($saved_uri));
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'width'));
}
$fmdm
->release($saved_uri);
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-multi-frame.gif', 'temporary://', FILE_EXISTS_REPLACE);
$source_uri = 'temporary://test-multi-frame.gif';
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
Cache::InvalidateTags([
'config:imagemagick.file_metadata_plugin.imagemagick_identify',
'config:file_mdm.file_metadata_plugin.getimagesize',
]);
$config_mdm
->set('metadata_cache.disallowed_paths', [
'public://imagetest/*',
])
->save();
foreach ($files as $source_uri => $source_image_data) {
$fmdm
->release($source_uri);
}
foreach ($files as $source_uri => $source_image_data) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri));
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
if ($first) {
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
}
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
$this
->assertFalse($fmdm
->has($saved_uri));
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
$this
->assertEquals($source_image_data['profiles'], $source_image
->getToolkit()
->getProfiles());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
$saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID, 'width'));
}
$fmdm
->release($saved_uri);
$first = FALSE;
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
}
public function providerFileMetadataTestLegacy() {
return [
[
'imagemagick',
'getimagesize',
],
[
'graphicsmagick',
'getimagesize',
],
];
}
public function testFileMetadataLegacy($binaries, $parsing_method) {
$config = \Drupal::configFactory()
->getEditable('imagemagick.settings');
$config_mdm = \Drupal::configFactory()
->getEditable('file_mdm.settings');
$config_mdm
->set('metadata_cache.enabled', TRUE)
->set('metadata_cache.disallowed_paths', [])
->save();
$config
->set('binaries', $binaries)
->set('use_identify', $parsing_method === 'imagemagick_identify')
->save();
$status = \Drupal::service('image.toolkit.manager')
->createInstance('imagemagick')
->getExecManager()
->checkPath('');
if (!empty($status['errors'])) {
$this
->markTestSkipped("Tests for '{$binaries}' cannot run because the binaries are not available on the shell path.");
}
$files = [
'public://image-test.png' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/png',
'colorspace' => 'SRGB',
],
'public://image-test.gif' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/gif',
'colorspace' => 'SRGB',
],
'dummy-remote://image-test.jpg' => [
'width' => 40,
'height' => 20,
'frames' => 1,
'mimetype' => 'image/jpeg',
'colorspace' => 'SRGB',
],
'public://test-multi-frame.gif' => [
'skip_dimensions_check' => TRUE,
'frames' => 13,
'mimetype' => 'image/gif',
'colorspace' => 'SRGB',
],
];
$operations = [
'resize' => [
'function' => 'resize',
'arguments' => [
'width' => 20,
'height' => 10,
],
'width' => 20,
'height' => 10,
],
'scale_x' => [
'function' => 'scale',
'arguments' => [
'width' => 20,
],
'width' => 20,
'height' => 10,
],
'rotate_5' => [
'function' => 'rotate',
'arguments' => [
'degrees' => 5,
'background' => '#FF00FF',
],
'width' => 41,
'height' => 23,
],
'convert_jpg' => [
'function' => 'convert',
'width' => 40,
'height' => 20,
'arguments' => [
'extension' => 'jpeg',
],
'mimetype' => 'image/jpeg',
],
];
$fmdm = $this->container
->get('file_metadata_manager');
$this
->getTestFiles('image');
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-multi-frame.gif', 'public://', FILE_EXISTS_REPLACE);
$config_mdm
->set('metadata_cache.enabled', FALSE)
->save();
foreach ($files as $source_uri => $source_image_data) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri));
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
if ($first) {
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded($parsing_method));
}
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded($parsing_method));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $source_image
->getToolkit()
->getFrames() === 1) {
$this
->assertTrue($fmdm
->has($saved_uri));
}
else {
$this
->assertFalse($fmdm
->has($saved_uri));
}
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $saved_image
->getToolkit()
->getFrames() === 1 && !($values['function'] === 'convert' && $source_image_data['frames'] > 1)) {
$this
->assertSame(FileMetadataInterface::LOADED_BY_CODE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded($parsing_method));
$saved_image_md
->getMetadata($parsing_method);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
switch ($parsing_method) {
case 'imagemagick_identify':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 'width'));
}
break;
case 'getimagesize':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 1));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 0));
}
break;
}
$fmdm
->release($saved_uri);
$first = FALSE;
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
$config_mdm
->set('metadata_cache.enabled', TRUE)
->save();
foreach ($files as $source_uri => $source_image_data) {
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri));
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded($parsing_method));
$source_image = $this->imageFactory
->get($source_uri);
if ($first) {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $source_image_md
->isMetadataLoaded($parsing_method));
}
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $source_image
->getToolkit()
->getFrames() === 1) {
$this
->assertTrue($fmdm
->has($saved_uri));
}
else {
$this
->assertFalse($fmdm
->has($saved_uri));
}
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $saved_image
->getToolkit()
->getFrames() === 1 && !($values['function'] === 'convert' && $source_image_data['frames'] > 1)) {
$this
->assertSame(FileMetadataInterface::LOADED_BY_CODE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded($parsing_method));
$saved_image_md
->getMetadata($parsing_method);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $saved_image_md
->isMetadataLoaded($parsing_method));
switch ($parsing_method) {
case 'imagemagick_identify':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 'width'));
}
break;
case 'getimagesize':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 1));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 0));
}
break;
}
$fmdm
->release($saved_uri);
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
$first = FALSE;
}
}
$directory_scan = file_scan_directory('temporary://', '/ima.*/');
$this
->assertGreaterThan(0, count($directory_scan));
foreach ($directory_scan as $file) {
file_unmanaged_delete($file->uri);
}
$directory_scan = file_scan_directory('temporary://', '/ima.*/');
$this
->assertEquals(0, count($directory_scan));
foreach ($files as $source_uri => $source_image_data) {
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded($parsing_method));
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $source_image_md
->isMetadataLoaded($parsing_method));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $source_image
->getToolkit()
->getFrames() === 1) {
$this
->assertTrue($fmdm
->has($saved_uri));
}
else {
$this
->assertFalse($fmdm
->has($saved_uri));
}
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $saved_image
->getToolkit()
->getFrames() === 1 && !($values['function'] === 'convert' && $source_image_data['frames'] > 1)) {
$this
->assertSame(FileMetadataInterface::LOADED_BY_CODE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded($parsing_method));
$saved_image_md
->getMetadata($parsing_method);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_CACHE, $saved_image_md
->isMetadataLoaded($parsing_method));
switch ($parsing_method) {
case 'imagemagick_identify':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 'width'));
}
break;
case 'getimagesize':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 1));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 0));
}
break;
}
$fmdm
->release($saved_uri);
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
if ($parsing_method === 'imagemagick_identify') {
file_unmanaged_copy(drupal_get_path('module', 'imagemagick') . '/misc/test-multi-frame.gif', 'temporary://', FILE_EXISTS_REPLACE);
$source_uri = 'temporary://test-multi-frame.gif';
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded('imagemagick_identify'));
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded('imagemagick_identify'));
$fmdm
->release($source_uri);
$source_image_md = $fmdm
->uri($source_uri);
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded('imagemagick_identify'));
}
Cache::InvalidateTags([
'config:imagemagick.file_metadata_plugin.imagemagick_identify',
'config:file_mdm.file_metadata_plugin.getimagesize',
]);
$config_mdm
->set('metadata_cache.disallowed_paths', [
'public://imagetest/*',
])
->save();
foreach ($files as $source_uri => $source_image_data) {
$fmdm
->release($source_uri);
}
foreach ($files as $source_uri => $source_image_data) {
$this
->assertFalse($fmdm
->has($source_uri));
$source_image_md = $fmdm
->uri($source_uri);
$this
->assertTrue($fmdm
->has($source_uri));
$first = TRUE;
file_unmanaged_delete_recursive($this->testDirectory);
file_prepare_directory($this->testDirectory, FILE_CREATE_DIRECTORY);
foreach ($operations as $op => $values) {
if ($first) {
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $source_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded($parsing_method));
}
$source_image = $this->imageFactory
->get($source_uri);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $source_image_md
->isMetadataLoaded($parsing_method));
$this
->assertSame($source_image_data['mimetype'], $source_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertSame($source_image_data['height'], $source_image
->getHeight());
$this
->assertSame($source_image_data['width'], $source_image
->getWidth());
}
$source_image
->apply($values['function'], $values['arguments']);
$saved_uri = $this->testDirectory . '/' . $op . substr($source_uri, -4);
$this
->assertFalse($fmdm
->has($saved_uri));
$this
->assertTrue($source_image
->save($saved_uri));
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $source_image
->getToolkit()
->getFrames() === 1) {
$this
->assertTrue($fmdm
->has($saved_uri));
}
else {
$this
->assertFalse($fmdm
->has($saved_uri));
}
$saved_image_md = $fmdm
->uri($saved_uri);
$saved_image = $this->imageFactory
->get($saved_uri);
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify' && $saved_image
->getToolkit()
->getFrames() === 1 && !($values['function'] === 'convert' && $source_image_data['frames'] > 1)) {
$this
->assertSame(FileMetadataInterface::LOADED_BY_CODE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
else {
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
}
$this
->assertSame($values['function'] === 'convert' ? $values['mimetype'] : $source_image_data['mimetype'], $saved_image
->getMimeType());
if ($binaries === 'imagemagick' && $parsing_method === 'imagemagick_identify') {
$this
->assertSame($source_image_data['colorspace'], $source_image
->getToolkit()
->getColorspace());
}
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image
->getHeight());
$this
->assertEquals($values['width'], $saved_image
->getWidth());
}
$fmdm
->release($saved_uri);
$saved_image_md = $fmdm
->uri($saved_uri);
$this
->assertSame(FileMetadataInterface::NOT_LOADED, $saved_image_md
->isMetadataLoaded($parsing_method));
$saved_image_md
->getMetadata($parsing_method);
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $saved_image_md
->isMetadataLoaded($parsing_method));
switch ($parsing_method) {
case 'imagemagick_identify':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 'height'));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 'width'));
}
break;
case 'getimagesize':
if (!isset($source_image_data['skip_dimensions_check'])) {
$this
->assertEquals($values['height'], $saved_image_md
->getMetadata($parsing_method, 1));
$this
->assertEquals($values['width'], $saved_image_md
->getMetadata($parsing_method, 0));
}
break;
}
$fmdm
->release($saved_uri);
$first = FALSE;
}
$fmdm
->release($source_uri);
$this
->assertFalse($fmdm
->has($source_uri));
}
}
public function testSourceLocalPath($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->setUpToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$this
->prepareImageFileHandling();
$config = \Drupal::configFactory()
->getEditable('imagemagick.settings');
$config_mdm = \Drupal::configFactory()
->getEditable('file_mdm.settings');
$fmdm = $this->container
->get('file_metadata_manager');
$source_uri = 'public://image-test.png';
$config_mdm
->set('metadata_cache.enabled', TRUE)
->save();
$config
->set('use_identify', TRUE)
->save();
$image = $this->imageFactory
->get($source_uri);
$this
->assertEquals($source_uri, $image
->getToolkit()
->getSource());
$this
->assertEquals($this->fileSystem
->realpath($source_uri), $image
->getToolkit()
->arguments()
->getSourceLocalPath());
$fmdm
->release($source_uri);
$image1 = $this->imageFactory
->get($source_uri);
$this
->assertEquals($source_uri, $image1
->getToolkit()
->getSource());
$this
->assertEquals($this->fileSystem
->realpath($source_uri), $image1
->getToolkit()
->ensureSourceLocalPath());
}
}