X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fimage%2Ftests%2Fsrc%2FFunctional%2FImageDimensionsTest.php;fp=web%2Fcore%2Fmodules%2Fimage%2Ftests%2Fsrc%2FFunctional%2FImageDimensionsTest.php;h=4a11bf956eb55af909b3e20062bc7a8b993ced5b;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=0000000000000000000000000000000000000000;hpb=74df008bdbb3a11eeea356744f39b802369bda3c;p=yaffs-website
diff --git a/web/core/modules/image/tests/src/Functional/ImageDimensionsTest.php b/web/core/modules/image/tests/src/Functional/ImageDimensionsTest.php
new file mode 100644
index 000000000..4a11bf956
--- /dev/null
+++ b/web/core/modules/image/tests/src/Functional/ImageDimensionsTest.php
@@ -0,0 +1,299 @@
+container->get('image.factory');
+ // Create a working copy of the file.
+ $files = $this->drupalGetTestFiles('image');
+ $file = reset($files);
+ $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
+
+ // Create a style.
+ /** @var $style \Drupal\image\ImageStyleInterface */
+ $style = ImageStyle::create(['name' => 'test', 'label' => 'Test']);
+ $style->save();
+ $generated_uri = 'public://styles/test/public/' . \Drupal::service('file_system')->basename($original_uri);
+ $url = file_url_transform_relative($style->buildUrl($original_uri));
+
+ $variables = [
+ '#theme' => 'image_style',
+ '#style_name' => 'test',
+ '#uri' => $original_uri,
+ '#width' => 40,
+ '#height' => 20,
+ ];
+ // Verify that the original image matches the hard-coded values.
+ $image_file = $image_factory->get($original_uri);
+ $this->assertEqual($image_file->getWidth(), $variables['#width']);
+ $this->assertEqual($image_file->getHeight(), $variables['#height']);
+
+ // Scale an image that is wider than it is high.
+ $effect = [
+ 'id' => 'image_scale',
+ 'data' => [
+ 'width' => 120,
+ 'height' => 90,
+ 'upscale' => TRUE,
+ ],
+ 'weight' => 0,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 120);
+ $this->assertEqual($image_file->getHeight(), 60);
+
+ // Rotate 90 degrees anticlockwise.
+ $effect = [
+ 'id' => 'image_rotate',
+ 'data' => [
+ 'degrees' => -90,
+ 'random' => FALSE,
+ ],
+ 'weight' => 1,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 60);
+ $this->assertEqual($image_file->getHeight(), 120);
+
+ // Scale an image that is higher than it is wide (rotated by previous effect).
+ $effect = [
+ 'id' => 'image_scale',
+ 'data' => [
+ 'width' => 120,
+ 'height' => 90,
+ 'upscale' => TRUE,
+ ],
+ 'weight' => 2,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 45);
+ $this->assertEqual($image_file->getHeight(), 90);
+
+ // Test upscale disabled.
+ $effect = [
+ 'id' => 'image_scale',
+ 'data' => [
+ 'width' => 400,
+ 'height' => 200,
+ 'upscale' => FALSE,
+ ],
+ 'weight' => 3,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 45);
+ $this->assertEqual($image_file->getHeight(), 90);
+
+ // Add a desaturate effect.
+ $effect = [
+ 'id' => 'image_desaturate',
+ 'data' => [],
+ 'weight' => 4,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 45);
+ $this->assertEqual($image_file->getHeight(), 90);
+
+ // Add a random rotate effect.
+ $effect = [
+ 'id' => 'image_rotate',
+ 'data' => [
+ 'degrees' => 180,
+ 'random' => TRUE,
+ ],
+ 'weight' => 5,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+
+ // Add a crop effect.
+ $effect = [
+ 'id' => 'image_crop',
+ 'data' => [
+ 'width' => 30,
+ 'height' => 30,
+ 'anchor' => 'center-center',
+ ],
+ 'weight' => 6,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 30);
+ $this->assertEqual($image_file->getHeight(), 30);
+
+ // Rotate to a non-multiple of 90 degrees.
+ $effect = [
+ 'id' => 'image_rotate',
+ 'data' => [
+ 'degrees' => 57,
+ 'random' => FALSE,
+ ],
+ 'weight' => 7,
+ ];
+
+ $effect_id = $style->addImageEffect($effect);
+ $style->save();
+ // @todo Uncomment this once
+ // https://www.drupal.org/project/drupal/issues/2670966 is resolved.
+ // $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ // @todo Uncomment this once
+ // https://www.drupal.org/project/drupal/issues/2670966 is resolved.
+ // $this->assertEqual($image_file->getWidth(), 41);
+ // $this->assertEqual($image_file->getHeight(), 41);
+
+ $effect_plugin = $style->getEffect($effect_id);
+ $style->deleteImageEffect($effect_plugin);
+
+ // Ensure that an effect can unset dimensions.
+ $effect = [
+ 'id' => 'image_module_test_null',
+ 'data' => [],
+ 'weight' => 8,
+ ];
+
+ $style->addImageEffect($effect);
+ $style->save();
+ $this->assertEqual($this->getImageTag($variables), '
');
+
+ // Test URI dependent image effect.
+ $style = ImageStyle::create(['name' => 'test_uri', 'label' => 'Test URI']);
+ $effect = [
+ 'id' => 'image_module_test_uri_dependent',
+ 'data' => [],
+ 'weight' => 0,
+ ];
+ $style->addImageEffect($effect);
+ $style->save();
+ $variables = [
+ '#theme' => 'image_style',
+ '#style_name' => 'test_uri',
+ '#uri' => $original_uri,
+ '#width' => 40,
+ '#height' => 20,
+ ];
+ // PNG original image. Should be resized to 100x100.
+ $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
+ $url = file_url_transform_relative($style->buildUrl($original_uri));
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 100);
+ $this->assertEqual($image_file->getHeight(), 100);
+ // GIF original image. Should be resized to 50x50.
+ $file = $files[1];
+ $original_uri = file_unmanaged_copy($file->uri, 'public://', FILE_EXISTS_RENAME);
+ $generated_uri = 'public://styles/test_uri/public/' . \Drupal::service('file_system')->basename($original_uri);
+ $url = file_url_transform_relative($style->buildUrl($original_uri));
+ $variables['#uri'] = $original_uri;
+ $this->assertEqual($this->getImageTag($variables), '
');
+ $this->assertFalse(file_exists($generated_uri), 'Generated file does not exist.');
+ $this->drupalGet($this->getAbsoluteUrl($url));
+ $this->assertResponse(200, 'Image was generated at the URL.');
+ $this->assertTrue(file_exists($generated_uri), 'Generated file does exist after we accessed it.');
+ $image_file = $image_factory->get($generated_uri);
+ $this->assertEqual($image_file->getWidth(), 50);
+ $this->assertEqual($image_file->getHeight(), 50);
+ }
+
+ /**
+ * Render an image style element.
+ *
+ * Function drupal_render() alters the passed $variables array by adding a new
+ * key '#printed' => TRUE. This prevents next call to re-render the element.
+ * We wrap drupal_render() in a helper protected method and pass each time a
+ * fresh array so that $variables won't get altered and the element is
+ * re-rendered each time.
+ */
+ protected function getImageTag($variables) {
+ return str_replace("\n", NULL, \Drupal::service('renderer')->renderRoot($variables));
+ }
+
+}