View source
<?php
namespace Drupal\Tests\imagemagick\Functional;
use Drupal\Core\Cache\Cache;
use Drupal\file_mdm\FileMetadataInterface;
use Drupal\imagemagick\ImagemagickExecArguments;
use Drupal\imagemagick\Plugin\ImageToolkit\ImagemagickToolkit;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\imagemagick\Kernel\ToolkitSetupTrait;
class EventSubscriberTest extends BrowserTestBase {
use ToolkitSetupTrait;
public static $modules = [
'system',
'imagemagick',
'file_mdm',
];
public function testEventSubscriber($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->setUpToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$this
->prepareImageFileHandling();
$fmdm = \Drupal::service('file_metadata_manager');
\Drupal::configFactory()
->getEditable('imagemagick.settings')
->set('advanced.colorspace', 'GRAY')
->save();
$image_uri = "public://image-test.png";
$image = $this->imageFactory
->get($image_uri);
if (!$image
->isValid()) {
$this
->fail("Could not load image {$image_uri}.");
}
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame('SRGB', $image
->getToolkit()
->getColorspace());
}
else {
$this
->assertNull($image
->getToolkit()
->getColorspace());
}
$image
->getToolkit()
->arguments()
->add("-resize 100x75!")
->add("-quality 75");
$image
->save($image_uri . '.derived');
if (substr(PHP_OS, 0, 3) === 'WIN') {
$expected = "-resize 100x75! -quality 75 -colorspace \"GRAY\"";
}
else {
$expected = "-resize 100x75! -quality 75 -colorspace 'GRAY'";
}
$this
->assertSame($expected, $image
->getToolkit()
->arguments()
->toString(ImagemagickExecArguments::POST_SOURCE));
Cache::InvalidateTags([
'config:imagemagick.file_metadata_plugin.imagemagick_identify',
]);
$fmdm
->release($image_uri . '.derived');
$image_md = $fmdm
->uri($image_uri . '.derived');
$image = $this->imageFactory
->get($image_uri . '.derived');
$this
->assertSame(FileMetadataInterface::LOADED_FROM_FILE, $image_md
->isMetadataLoaded(ImagemagickToolkit::FILE_METADATA_PLUGIN_ID));
if ($toolkit_settings['binaries'] === 'imagemagick') {
$this
->assertSame('GRAY', $image
->getToolkit()
->getColorspace());
}
else {
$this
->assertNull($image
->getToolkit()
->getColorspace());
}
\Drupal::configFactory()
->getEditable('imagemagick.settings')
->set('prepend', '-debug All')
->set('prepend_pre_source', TRUE)
->save();
$image = $this->imageFactory
->get($image_uri);
$image
->getToolkit()
->arguments()
->add("-resize 100x75!")
->add("-quality 75");
$image
->save($image_uri . '.derived');
if (substr(PHP_OS, 0, 3) === 'WIN') {
$expected = "-resize 100x75! -quality 75 -colorspace \"GRAY\"";
}
else {
$expected = "-resize 100x75! -quality 75 -colorspace 'GRAY'";
}
$this
->assertSame('-debug All', $image
->getToolkit()
->arguments()
->toString(ImagemagickExecArguments::PRE_SOURCE));
$this
->assertSame($expected, $image
->getToolkit()
->arguments()
->toString(ImagemagickExecArguments::POST_SOURCE));
\Drupal::configFactory()
->getEditable('imagemagick.settings')
->set('prepend_pre_source', FALSE)
->save();
$image = $this->imageFactory
->get($image_uri);
$image
->getToolkit()
->arguments()
->add("-resize 100x75!")
->add("-quality 75");
$image
->save($image_uri . '.derived');
if (substr(PHP_OS, 0, 3) === 'WIN') {
$expected = "-debug All -resize 100x75! -quality 75 -colorspace \"GRAY\"";
}
else {
$expected = "-debug All -resize 100x75! -quality 75 -colorspace 'GRAY'";
}
$this
->assertSame('', $image
->getToolkit()
->arguments()
->toString(ImagemagickExecArguments::PRE_SOURCE));
$this
->assertSame($expected, $image
->getToolkit()
->arguments()
->toString(ImagemagickExecArguments::POST_SOURCE));
}
}