You are here

public function AspectSwitcherTest::testAspectSwitcherEffect in Image Effects 8.3

Same name and namespace in other branches
  1. 8 tests/src/Functional/Effect/AspectSwitcherTest.php \Drupal\Tests\image_effects\Functional\Effect\AspectSwitcherTest::testAspectSwitcherEffect()
  2. 8.2 tests/src/Functional/Effect/AspectSwitcherTest.php \Drupal\Tests\image_effects\Functional\Effect\AspectSwitcherTest::testAspectSwitcherEffect()

AspectSwitcher effect test.

@dataProvider providerToolkits

Parameters

string $toolkit_id: The id of the toolkit to set up.

string $toolkit_config: The config object of the toolkit to set up.

array $toolkit_settings: The settings of the toolkit to set up.

File

tests/src/Functional/Effect/AspectSwitcherTest.php, line 75

Class

AspectSwitcherTest
AspectSwitcher effect test.

Namespace

Drupal\Tests\image_effects\Functional\Effect

Code

public function testAspectSwitcherEffect($toolkit_id, $toolkit_config, array $toolkit_settings) {
  $this
    ->changeToolkit($toolkit_id, $toolkit_config, $toolkit_settings);
  $image_factory = $this->container
    ->get('image.factory');
  $test_landscape_file = 'core/tests/fixtures/files/image-test.png';
  $original_landscape_uri = $this->fileSystem
    ->copy($test_landscape_file, 'public://', FileSystemInterface::EXISTS_RENAME);
  $img_portrait = imagerotate(imagecreatefrompng($original_landscape_uri), 90, 0);
  $generated_uri = \Drupal::service('file_system')
    ->realpath('public://image-test-portrait.png');
  imagepng($img_portrait, $generated_uri);
  $test_portrait_file = $generated_uri;
  $original_portrait_uri = $this->fileSystem
    ->copy($test_portrait_file, 'public://', FileSystemInterface::EXISTS_RENAME);

  // Add aspect switcher effect.
  $effect = [
    'id' => 'image_effects_aspect_switcher',
    'data' => [
      'landscape_image_style' => 'L (landscape_image_style_test)',
      'portrait_image_style' => 'L (portrait_image_style_test)',
      'ratio_adjustment' => 0.99,
    ],
  ];
  $uuid = $this
    ->addEffectToTestStyle($effect);

  // Load Image Style.
  $image_style = ImageStyle::load($this->testImageStyleName);

  // Check that effect's configuration is as expected.
  $aspect_switcher_effect_configuration = $image_style
    ->getEffect($uuid)
    ->getConfiguration()['data'];
  $this
    ->assertEquals('landscape_image_style_test', $aspect_switcher_effect_configuration['landscape_image_style']);
  $this
    ->assertEquals('portrait_image_style_test', $aspect_switcher_effect_configuration['portrait_image_style']);
  $this
    ->assertEquals(0.99, $aspect_switcher_effect_configuration['ratio_adjustment']);

  // Check that dependent image style have been added to configuration
  // dependencies.
  $expected_config_dependencies = [
    'image.style.landscape_image_style_test',
    'image.style.portrait_image_style_test',
  ];
  $this
    ->assertEquals($expected_config_dependencies, $image_style
    ->getDependencies()['config']);

  // Check that landscape image style is applied when source image is
  // landscape.
  // Check that ::transformDimensions returns expected dimensions.
  $original_landscape_image = $image_factory
    ->get($original_landscape_uri);
  $derivative_landscape_url = file_url_transform_relative($this->testImageStyle
    ->buildUrl($original_landscape_uri));
  $variables = [
    '#theme' => 'image_style',
    '#style_name' => $this->testImageStyleName,
    '#uri' => $original_landscape_uri,
    '#width' => $original_landscape_image
      ->getWidth(),
    '#height' => $original_landscape_image
      ->getHeight(),
  ];
  $this
    ->assertRegExp("/\\<img src=\"" . preg_quote($derivative_landscape_url, '/') . "\" width=\"{$this->effects['landscape']['data']['width']}\" height=\"{$this->effects['landscape']['data']['height']}\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
    ->getImageTag($variables));

  // Check that ::applyEffect returns expected dimensions.
  $dest_uri = $image_style
    ->buildUri($original_landscape_uri);
  $this
    ->assertTrue($image_style
    ->createDerivative($original_landscape_uri, $dest_uri));
  $image = $image_factory
    ->get($dest_uri);
  $this
    ->assertEquals($this->effects['landscape']['data']['width'], $image
    ->getWidth());
  $this
    ->assertEquals($this->effects['landscape']['data']['height'], $image
    ->getHeight());

  // Check that portrait image style is applied when source image is
  // portrait.
  // Check that ::transformDimensions returns expected dimensions.
  $original_portrait_image = $image_factory
    ->get($original_portrait_uri);
  $derivative_portrait_url = file_url_transform_relative($this->testImageStyle
    ->buildUrl($original_portrait_uri));
  $variables = [
    '#theme' => 'image_style',
    '#style_name' => $this->testImageStyleName,
    '#uri' => $original_portrait_uri,
    '#width' => $original_portrait_image
      ->getWidth(),
    '#height' => $original_portrait_image
      ->getHeight(),
  ];
  $this
    ->assertRegExp("/\\<img src=\"" . preg_quote($derivative_portrait_url, '/') . "\" width=\"{$this->effects['portrait']['data']['width']}\" height=\"{$this->effects['portrait']['data']['height']}\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
    ->getImageTag($variables));

  // Check that ::applyEffect returns expected dimensions.
  $dest_uri = $image_style
    ->buildUri($original_portrait_uri);
  $image_style
    ->createDerivative($original_portrait_uri, $dest_uri);
  $image = $image_factory
    ->get($dest_uri);
  $this
    ->assertEquals($this->effects['portrait']['data']['width'], $image
    ->getWidth());
  $this
    ->assertEquals($this->effects['portrait']['data']['height'], $image
    ->getHeight());

  // Check that flushing a dependent style, the parent one gets flushed too,
  // and the invalidation of the parent image style cache tag is changed.
  $pre_flush_invalidations_parent = $this
    ->getImageStyleCacheTagInvalidations($this->testImageStyleName);
  $pre_flush_invalidations_child = $this
    ->getImageStyleCacheTagInvalidations('portrait_image_style_test');
  $this
    ->assertNotEquals(0, count($this->fileSystem
    ->scanDirectory('public://styles/' . $this->testImageStyleName, '/.*/')));
  $portrait_image_style = ImageStyle::load('portrait_image_style_test');
  $portrait_image_style
    ->flush();
  $this
    ->assertFalse(is_dir('public://styles/' . $this->testImageStyleName));
  $this
    ->assertNotEquals($this
    ->getImageStyleCacheTagInvalidations($this->testImageStyleName), $pre_flush_invalidations_parent);
  $this
    ->assertNotEquals($this
    ->getImageStyleCacheTagInvalidations('portrait_image_style_test'), $pre_flush_invalidations_child);

  // Test an aspect switcher effect with no portrait sub-style specified.
  $this
    ->removeEffectFromTestStyle($uuid);

  // Add aspect switcher effect.
  $effect = [
    'id' => 'image_effects_aspect_switcher',
    'data' => [
      'landscape_image_style' => 'L (landscape_image_style_test)',
      'ratio_adjustment' => 1,
    ],
  ];
  $uuid = $this
    ->addEffectToTestStyle($effect);

  // Load Image Style.
  $image_style = ImageStyle::load($this->testImageStyleName);

  // Check that dependent image style have been added to configuration
  // dependencies.
  $expected_config_dependencies = [
    'image.style.landscape_image_style_test',
  ];
  $this
    ->assertEquals($expected_config_dependencies, $image_style
    ->getDependencies()['config']);

  // Check that no changes are made when source image is portrait.
  // Check that ::transformDimensions returns expected dimensions.
  $original_portrait_image = $image_factory
    ->get($original_portrait_uri);
  $derivative_portrait_url = file_url_transform_relative($this->testImageStyle
    ->buildUrl($original_portrait_uri));
  $variables = [
    '#theme' => 'image_style',
    '#style_name' => $this->testImageStyleName,
    '#uri' => $original_portrait_uri,
    '#width' => $original_portrait_image
      ->getWidth(),
    '#height' => $original_portrait_image
      ->getHeight(),
  ];
  $this
    ->assertRegExp("/\\<img src=\"" . preg_quote($derivative_portrait_url, '/') . "\" width=\"20\" height=\"40\" alt=\"\" .*class=\"image\\-style\\-image\\-effects\\-test\" \\/\\>/", $this
    ->getImageTag($variables));

  // Check that ::applyEffect returns expected dimensions.
  $dest_uri = $image_style
    ->buildUri($original_portrait_uri);
  $image_style
    ->createDerivative($original_portrait_uri, $dest_uri);
  $image = $image_factory
    ->get($dest_uri);
  $this
    ->assertEquals(20, $image
    ->getWidth());
  $this
    ->assertEquals(40, $image
    ->getHeight());
}