Skip to content

Commit

Permalink
OPENEUROPA-2025: Fix QA remarks.
Browse files Browse the repository at this point in the history
  • Loading branch information
imanoleguskiza committed Jul 8, 2019
1 parent 54628cf commit 74a9997
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ image.effect.retina_image_scale:
mapping:
multiplier:
type: integer
label: 'Multiplier'
label: 'Multiplier'
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ class RetinaScaleImageEffect extends ScaleImageEffect {
* {@inheritdoc}
*/
public function applyEffect(ImageInterface $image) {
$target_with = $this->configuration['width'];
$target_height = $this->configuration['height'];
// If we are not upscaling the image, check to see if it's smaller
// than the defined dimensions.
if (!$upscale = $this->configuration['upscale']) {
if (
(!empty($target_with) && $target_with > $image->getWidth()) ||
(!empty($target_height) && $target_height > $image->getHeight())
(!empty($target_with = $this->configuration['width']) && $target_with > $image->getWidth()) ||
(!empty($target_height = $this->configuration['height']) && $target_height > $image->getHeight())
) {
// If the image is smaller than the defined dimensions,
// upscale it according to the defined multiplier.
Expand All @@ -57,14 +55,12 @@ public function applyEffect(ImageInterface $image) {
*/
public function transformDimensions(array &$dimensions, $uri) {
if ($dimensions['width'] && $dimensions['height']) {
$target_with = $this->configuration['width'];
$target_height = $this->configuration['height'];
// If we are not upscaling the image, check to see if it's smaller
// than the defined dimensions.
if (!$upscale = $this->configuration['upscale']) {
if (
(!empty($target_with) && $this->configuration['width'] > $dimensions['width']) ||
(!empty($target_height) && $this->configuration['height'] > $dimensions['height'])
(!empty($target_with = $this->configuration['width']) && $this->configuration['width'] > $dimensions['width']) ||
(!empty($target_height = $this->configuration['height']) && $this->configuration['height'] > $dimensions['height'])
) {
// If the image is smaller than the defined dimensions,
// upscale it according to the defined multiplier.
Expand Down Expand Up @@ -108,14 +104,14 @@ public function buildConfigurationForm(array $form, FormStateInterface $form_sta
$form = parent::buildConfigurationForm($form, $form_state);
$form['multiplier'] = [
'#type' => 'select',
'#title' => t('Multiplier'),
'#title' => $this->t('Multiplier'),
'#options' => [
2 => '2x',
3 => '3x',
],
'#default_value' => $this->configuration['multiplier'],
'#required' => TRUE,
'#description' => t("The image will be upscaled according to this multiplier if it is smaller than the dimensions defined in the 'Width' and 'Height' properties."),
'#description' => $this->t("The image will be upscaled according to this multiplier if it is smaller than the dimensions defined in the 'Width' and 'Height' properties."),
];

return $form;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@
* @group image
*/
class RetinaScaleEffectTest extends ToolkitTestBase {

/**
* Modules to enable.
*
* @var array
* {@inheritdoc}
*/
public static $modules = ['image', 'oe_theme_helper'];

Expand All @@ -29,15 +28,15 @@ class RetinaScaleEffectTest extends ToolkitTestBase {
/**
* {@inheritdoc}
*/
protected function setUp() {
protected function setUp(): void {
parent::setUp();
$this->manager = $this->container->get('plugin.manager.image.effect');
}

/**
* Test the retina_image_scale_effect() function.
*/
public function testRetinaScaleEffect() {
public function testRetinaScaleEffect(): void {
$this->assertImageEffect('retina_image_scale', [
// Set the desired width to be much higher than the image width.
'width' => $this->image->getWidth() * 10,
Expand All @@ -53,7 +52,7 @@ public function testRetinaScaleEffect() {
/**
* Test the image_scale_effect() function using a multiplier of 3.
*/
public function testTripleMultiplierRetinaScaleEffect() {
public function testTripleMultiplierRetinaScaleEffect(): void {
$this->assertImageEffect('retina_image_scale', [
// Set the desired width to be much higher than the image width.
'width' => $this->image->getWidth() * 10,
Expand All @@ -68,21 +67,35 @@ public function testTripleMultiplierRetinaScaleEffect() {
$this->assertEqual($calls['scale'][0][1], $this->image->getHeight() * 3, 'Height is triple the original size.');
}

/**
* Test the image_scale_effect() function using a big enough image.
*/
public function testScaleEffect() {
$this->assertImageEffect('image_scale', [
// Set the image width to be smaller than the image width.
'width' => 10,
'height' => 10,
]);
$this->assertToolkitOperationsCalled(['scale']);

// Check the parameters.
$calls = $this->imageTestGetAllCalls();
$this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
$this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
}

/**
* Asserts the effect processing of an image effect plugin.
*
* @param string $effect_name
* The name of the image effect to test.
* @param array $data
* The data to pass to the image effect.
*
* @return bool
* TRUE if the assertion succeeded, FALSE otherwise.
*/
protected function assertImageEffect($effect_name, array $data) {
protected function assertImageEffect($effect_name, array $data): void {
/** @var \Drupal\image\ImageEffectInterface $effect */
$effect = $this->manager->createInstance($effect_name, ['data' => $data]);
return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');
$this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');
}

}

0 comments on commit 74a9997

Please sign in to comment.