3 namespace Drupal\Tests\image\Functional;
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\system\Tests\Image\ToolkitTestBase;
9 * Tests that the image effects pass parameters to the toolkit correctly.
13 class ImageEffectsTest extends ToolkitTestBase {
20 public static $modules = ['image', 'image_test', 'image_module_test'];
23 * The image effect manager.
25 * @var \Drupal\image\ImageEffectManager
29 protected function setUp() {
31 $this->manager = $this->container->get('plugin.manager.image.effect');
35 * Test the image_resize_effect() function.
37 public function testResizeEffect() {
38 $this->assertImageEffect('image_resize', [
42 $this->assertToolkitOperationsCalled(['resize']);
44 // Check the parameters.
45 $calls = $this->imageTestGetAllCalls();
46 $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
47 $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
51 * Test the image_scale_effect() function.
53 public function testScaleEffect() {
54 // @todo: need to test upscaling.
55 $this->assertImageEffect('image_scale', [
59 $this->assertToolkitOperationsCalled(['scale']);
61 // Check the parameters.
62 $calls = $this->imageTestGetAllCalls();
63 $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
64 $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
68 * Test the image_crop_effect() function.
70 public function testCropEffect() {
71 // @todo should test the keyword offsets.
72 $this->assertImageEffect('image_crop', [
77 $this->assertToolkitOperationsCalled(['crop']);
79 // Check the parameters.
80 $calls = $this->imageTestGetAllCalls();
81 $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly');
82 $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly');
83 $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
84 $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
88 * Tests the ConvertImageEffect plugin.
90 public function testConvertEffect() {
92 $this->assertImageEffect('image_convert', [
93 'extension' => 'jpeg',
95 $this->assertToolkitOperationsCalled(['convert']);
97 // Check the parameters.
98 $calls = $this->imageTestGetAllCalls();
99 $this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly');
103 * Test the image_scale_and_crop_effect() function.
105 public function testScaleAndCropEffect() {
106 $this->assertImageEffect('image_scale_and_crop', [
110 $this->assertToolkitOperationsCalled(['scale_and_crop']);
112 // Check the parameters.
113 $calls = $this->imageTestGetAllCalls();
114 $this->assertEqual($calls['scale_and_crop'][0][0], 5, 'Width was computed and passed correctly');
115 $this->assertEqual($calls['scale_and_crop'][0][1], 10, 'Height was computed and passed correctly');
119 * Test the image_desaturate_effect() function.
121 public function testDesaturateEffect() {
122 $this->assertImageEffect('image_desaturate', []);
123 $this->assertToolkitOperationsCalled(['desaturate']);
125 // Check the parameters.
126 $calls = $this->imageTestGetAllCalls();
127 $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
131 * Test the image_rotate_effect() function.
133 public function testRotateEffect() {
134 // @todo: need to test with 'random' => TRUE
135 $this->assertImageEffect('image_rotate', [
139 $this->assertToolkitOperationsCalled(['rotate']);
141 // Check the parameters.
142 $calls = $this->imageTestGetAllCalls();
143 $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
144 $this->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly');
148 * Test image effect caching.
150 public function testImageEffectsCaching() {
151 $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
153 // First call should grab a fresh copy of the data.
154 $manager = $this->container->get('plugin.manager.image.effect');
155 $effects = $manager->getDefinitions();
156 $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
158 // Second call should come from cache.
159 drupal_static_reset('image_module_test_image_effect_info_alter');
160 $cached_effects = $manager->getDefinitions();
161 $this->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.');
163 $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
167 * Tests if validation errors are passed plugin form to the parent form.
169 public function testEffectFormValidationErrors() {
170 $account = $this->drupalCreateUser(['administer image styles']);
171 $this->drupalLogin($account);
172 /** @var \Drupal\image\ImageStyleInterface $style */
173 $style = ImageStyle::load('thumbnail');
174 // Image Scale is the only effect shipped with 'thumbnail', by default.
175 $uuids = $style->getEffects()->getInstanceIds();
178 // We are posting the form with both, width and height, empty.
179 $edit = ['data[width]' => '', 'data[height]' => ''];
180 $path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid;
181 $this->drupalPostForm($path, $edit, t('Update effect'));
182 // Check that the error message has been displayed.
183 $this->assertText(t('Width and height can not both be blank.'));
187 * Asserts the effect processing of an image effect plugin.
189 * @param string $effect_name
190 * The name of the image effect to test.
192 * The data to pass to the image effect.
195 * TRUE if the assertion succeeded, FALSE otherwise.
197 protected function assertImageEffect($effect_name, array $data) {
198 $effect = $this->manager->createInstance($effect_name, ['data' => $data]);
199 return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');