View source
<?php
namespace Drupal\Tests\image_effects\Functional;
use Drupal\Core\Database\Database;
use Drupal\Core\Image\ImageInterface;
use Drupal\Tests\BrowserTestBase;
use Drupal\image\Entity\ImageStyle;
use Drupal\image_effects\Component\GdImageAnalysis;
abstract class ImageEffectsTestBase extends BrowserTestBase {
public static $modules = [
'image',
'image_effects',
'simpletest',
'imagemagick',
];
protected $testImageStyle;
protected $testImageStyleName = 'image_effects_test';
protected $testImageStyleLabel = 'Image Effects Test';
protected $imageFactory;
protected $black = [
0,
0,
0,
0,
];
protected $red = [
255,
0,
0,
0,
];
protected $green = [
0,
255,
0,
0,
];
protected $blue = [
0,
0,
255,
0,
];
protected $yellow = [
255,
255,
0,
0,
];
protected $fuchsia = [
255,
0,
255,
0,
];
protected $cyan = [
0,
255,
255,
0,
];
protected $white = [
255,
255,
255,
0,
];
protected $grey = [
128,
128,
128,
0,
];
protected $transparent = [
0,
0,
0,
127,
];
public function setUp() {
parent::setUp();
$this->imageFactory = $this->container
->get('image.factory');
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'administer image styles',
]);
$this
->drupalLogin($this->adminUser);
$this->testImageStyle = ImageStyle::create([
'name' => $this->testImageStyleName,
'label' => $this->testImageStyleLabel,
]);
$this
->assertEqual(SAVED_NEW, $this->testImageStyle
->save());
}
protected function addEffectToTestStyle(array $effect) {
$image_style_pre = ImageStyle::load($this->testImageStyleName);
$this
->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testImageStyleName, [
'new' => $effect['id'],
], t('Add'));
if (!empty($effect['data'])) {
$effect_edit = [];
foreach ($effect['data'] as $field => $value) {
$effect_edit['data[' . $field . ']'] = $value;
}
$this
->drupalPostForm(NULL, $effect_edit, t('Add effect'));
}
$this->testImageStyle = ImageStyle::load($this->testImageStyleName);
foreach ($this->testImageStyle
->getEffects() as $uuid => $effect) {
if (!$image_style_pre
->getEffects()
->has($uuid)) {
return $uuid;
}
}
return NULL;
}
protected function removeEffectFromTestStyle($uuid) {
$effect = $this->testImageStyle
->getEffect($uuid);
$this->testImageStyle
->deleteImageEffect($effect);
$this
->assertEqual(SAVED_UPDATED, $this->testImageStyle
->save());
}
protected function getImageTag($variables) {
return str_replace("\n", NULL, \Drupal::service('renderer')
->renderRoot($variables));
}
public function providerToolkits() {
return [
'GD' => [
'toolkit_id' => 'gd',
'toolkit_config' => 'system.image.gd',
'toolkit_settings' => [
'jpeg_quality' => 100,
],
],
'ImageMagick-imagemagick' => [
'toolkit_id' => 'imagemagick',
'toolkit_config' => 'imagemagick.settings',
'toolkit_settings' => [
'binaries' => 'imagemagick',
'quality' => 100,
'debug' => TRUE,
],
],
'ImageMagick-graphicsmagick' => [
'toolkit_id' => 'imagemagick',
'toolkit_config' => 'imagemagick.settings',
'toolkit_settings' => [
'binaries' => 'graphicsmagick',
'quality' => 100,
'debug' => TRUE,
],
],
];
}
protected function changeToolkit($toolkit_id, $toolkit_config, array $toolkit_settings) {
\Drupal::configFactory()
->getEditable('system.image')
->set('toolkit', $toolkit_id)
->save();
$config = \Drupal::configFactory()
->getEditable($toolkit_config);
foreach ($toolkit_settings as $setting => $value) {
$config
->set($setting, $value);
}
$config
->save();
if ($toolkit_id === 'imagemagick') {
$status = \Drupal::service('image.toolkit.manager')
->createInstance('imagemagick')
->checkPath('');
if (!empty($status['errors'])) {
$this
->markTestSkipped("Tests for '{$toolkit_settings['binaries']}' cannot run because the binaries are not available on the shell path.");
}
}
$this->container
->get('image.factory')
->setToolkitId($toolkit_id);
}
protected function getTestImageCopyUri($path, $name = NULL, $type = 'module') {
$test_directory = 'public://test-images/';
file_prepare_directory($test_directory, FILE_CREATE_DIRECTORY);
$source_uri = $name ? drupal_get_path($type, $name) : '';
$source_uri .= $path;
$target_uri = $test_directory . \Drupal::service('file_system')
->basename($source_uri);
return file_unmanaged_copy($source_uri, $target_uri, FILE_EXISTS_REPLACE);
}
public function assertColorsAreEqual(array $actual, array $expected) {
$this
->assertColorsAreClose($actual, $expected, 0);
}
public function assertColorsAreNotEqual(array $actual, array $expected) {
if ($expected[3] == 127) {
$this
->assertNotEquals(127, $actual[3]);
return;
}
$this
->assertColorsAreNotClose($actual, $expected, 0);
}
public function assertColorsAreClose(array $actual, array $expected, $tolerance) {
if ($actual[3] == 127 && $expected[3] == 127) {
return;
}
$distance = pow($actual[0] - $expected[0], 2) + pow($actual[1] - $expected[1], 2) + pow($actual[2] - $expected[2], 2) + pow($actual[3] - $expected[3], 2);
$this
->assertLessThanOrEqual($tolerance, $distance, "Actual: {" . implode(',', $actual) . "}, Expected: {" . implode(',', $expected) . "}, Distance: " . $distance . ", Tolerance: " . $tolerance);
}
public function assertColorsAreNotClose(array $actual, array $expected, $tolerance) {
$distance = pow($actual[0] - $expected[0], 2) + pow($actual[1] - $expected[1], 2) + pow($actual[2] - $expected[2], 2) + pow($actual[3] - $expected[3], 2);
$this
->assertGreaterThan($tolerance, $distance, "Actual: {" . implode(',', $actual) . "}, Expected: {" . implode(',', $expected) . "}, Distance: " . $distance . ", Tolerance: " . $tolerance);
}
protected function getPixelColor(ImageInterface $image, $x, $y) {
$toolkit = $image
->getToolkit();
$color_index = imagecolorat($toolkit
->getResource(), $x, $y);
$transparent_index = imagecolortransparent($toolkit
->getResource());
if ($color_index == $transparent_index) {
return [
0,
0,
0,
127,
];
}
return array_values(imagecolorsforindex($toolkit
->getResource(), $color_index));
}
protected function getImageStyleCacheTagInvalidations($image_style_name) {
$query = Database::getConnection()
->select('cachetags', 'a');
$query
->addField('a', 'invalidations');
$query
->condition('tag', 'config:image.style.' . $image_style_name);
return (int) $query
->execute()
->fetchColumn();
}
protected function assertTextOverlay($image, $width, $height) {
$w_error = abs($image
->getWidth() - $width);
$h_error = abs($image
->getHeight() - $height);
$tolerance = 0.1;
$this
->assertTrue($w_error < $width * $tolerance && $h_error < $height * $tolerance, "Width and height ({$image->getWidth()}x{$image->getHeight()}) approximate expected results ({$width}x{$height})");
}
protected function assertImagesAreEqual(ImageInterface $expected_image, ImageInterface $actual_image, $max_diff = 1, $message = NULL) {
$this
->assertSame('gd', $expected_image
->getToolkitId());
$this
->assertSame('gd', $actual_image
->getToolkitId());
$this
->assertSame($expected_image
->getWidth(), $actual_image
->getWidth());
$this
->assertSame($expected_image
->getHeight(), $actual_image
->getHeight());
$difference = GdImageAnalysis::difference($expected_image
->getToolkit()
->getResource(), $actual_image
->getToolkit()
->getResource());
$mean = GdImageAnalysis::mean($difference);
imagedestroy($difference);
$this
->assertTrue($mean < $max_diff, $message);
}
}