View source
<?php
namespace Drupal\Tests\image_effects\Functional\Effect;
use Drupal\Component\Utility\NestedArray;
use Drupal\Tests\image_effects\Functional\ImageEffectsTestBase;
class RelativeCropTest extends ImageEffectsTestBase {
public function testRelativeCrop($toolkit_id, $toolkit_config, array $toolkit_settings) {
$this
->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
$test_files = [
'border',
'border-flat',
'border-no-bottom',
'border-no-left',
'border-no-right',
'border-no-top',
'border-thin',
];
foreach ($test_files as $filename) {
$test_file_uris[$filename] = $this
->getTestImageCopyUri("/tests/images/{$filename}.png", 'image_effects');
$test_images[$filename] = $this->imageFactory
->get($test_file_uris[$filename], 'gd');
}
$image = $this->imageFactory
->get($test_file_uris['border']);
$this
->assertEquals(14, $image
->getWidth());
$this
->assertEquals(14, $image
->getHeight());
$image_gd = $this->imageFactory
->get($test_file_uris['border'], 'gd');
$this
->assertEquals(14, $image_gd
->getWidth());
$this
->assertEquals(14, $image_gd
->getHeight());
$derivative_uri = $this->testImageStyle
->buildUri($test_file_uris['border']);
$derivative_url = file_url_transform_relative($this->testImageStyle
->buildUrl($test_file_uris['border']));
$test_data['Matching ratio'] = [
'effect_data' => [
'width' => 1,
'height' => 1,
],
'expected_image' => 'border',
'expected_width' => 14,
'expected_height' => 14,
];
$test_data['Crop width center'] = [
'effect_data' => [
'width' => 6,
'height' => 7,
],
'expected_image' => 'border-thin',
'expected_width' => 12,
'expected_height' => 14,
];
$test_data['Crop width left'] = NestedArray::mergeDeep($test_data['Crop width center'], [
'effect_data' => [
'anchor][width' => 'left',
],
'expected_image' => 'border-no-right',
]);
$test_data['Crop width right'] = NestedArray::mergeDeep($test_data['Crop width center'], [
'effect_data' => [
'anchor][width' => 'right',
],
'expected_image' => 'border-no-left',
]);
$test_data['Crop width rounding up'] = NestedArray::mergeDeep($test_data['Crop width center'], [
'effect_data' => [
'width' => 23,
'height' => 28,
],
]);
$test_data['Crop width rounding down'] = NestedArray::mergeDeep($test_data['Crop width center'], [
'effect_data' => [
'width' => 49,
'height' => 56,
],
]);
$test_data['Crop height center'] = [
'effect_data' => [
'width' => 7,
'height' => 6,
],
'expected_image' => 'border-flat',
'expected_width' => 14,
'expected_height' => 12,
];
$test_data['Crop height top'] = NestedArray::mergeDeep($test_data['Crop height center'], [
'effect_data' => [
'anchor][height' => 'top',
],
'expected_image' => 'border-no-bottom',
]);
$test_data['Crop height bottom'] = NestedArray::mergeDeep($test_data['Crop height center'], [
'effect_data' => [
'anchor][height' => 'bottom',
],
'expected_image' => 'border-no-top',
]);
$test_data['Crop height rounding up'] = NestedArray::mergeDeep($test_data['Crop height center'], [
'effect_data' => [
'width' => 28,
'height' => 23,
],
]);
$test_data['Crop height rounding down'] = NestedArray::mergeDeep($test_data['Crop height center'], [
'effect_data' => [
'width' => 56,
'height' => 49,
],
]);
foreach ($test_data as $test_description => $test) {
$effect = [
'id' => 'image_effects_relative_crop',
'data' => $test['effect_data'],
];
$uuid = $this
->addEffectToTestStyle($effect);
$summary = $test['effect_data']['width'] . ':' . $test['effect_data']['height'];
$this
->assertSession()
->pageTextContains('Relative crop ' . $summary);
$variables = [
'#theme' => 'image_style',
'#style_name' => 'image_effects_test',
'#uri' => $test_file_uris['border'],
'#width' => $image
->getWidth(),
'#height' => $image
->getHeight(),
];
$this
->assertRegExp("/\\<img src=\"" . preg_quote($derivative_url, '/') . "\" width=\"{$test['expected_width']}\" height=\"{$test['expected_height']}\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
->getImageTag($variables), $test_description);
$this->testImageStyle
->createDerivative($test_file_uris['border'], $derivative_uri);
$derivative = $this->imageFactory
->get($derivative_uri);
$this
->assertEquals($test['expected_width'], $derivative
->getWidth(), $test_description);
$this
->assertEquals($test['expected_height'], $derivative
->getHeight(), $test_description);
$gd_derivative = $this->imageFactory
->get($derivative_uri, 'gd');
$this
->assertImagesAreEqual($test_images[$test['expected_image']], $gd_derivative, 1, $test_description);
$this
->removeEffectFromTestStyle($uuid);
}
}
}