e9cb157abe155953d5821e400a85396aefda2021
[yaffs-website] / web / core / modules / image / tests / src / Functional / ImageEffectsTest.php
1 <?php
2
3 namespace Drupal\Tests\image\Functional;
4
5 use Drupal\image\Entity\ImageStyle;
6 use Drupal\system\Tests\Image\ToolkitTestBase;
7
8 /**
9  * Tests that the image effects pass parameters to the toolkit correctly.
10  *
11  * @group image
12  */
13 class ImageEffectsTest extends ToolkitTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['image', 'image_test', 'image_module_test'];
21
22   /**
23    * The image effect manager.
24    *
25    * @var \Drupal\image\ImageEffectManager
26    */
27   protected $manager;
28
29   protected function setUp() {
30     parent::setUp();
31     $this->manager = $this->container->get('plugin.manager.image.effect');
32   }
33
34   /**
35    * Test the image_resize_effect() function.
36    */
37   public function testResizeEffect() {
38     $this->assertImageEffect('image_resize', [
39       'width' => 1,
40       'height' => 2,
41     ]);
42     $this->assertToolkitOperationsCalled(['resize']);
43
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');
48   }
49
50   /**
51    * Test the image_scale_effect() function.
52    */
53   public function testScaleEffect() {
54     // @todo: need to test upscaling.
55     $this->assertImageEffect('image_scale', [
56       'width' => 10,
57       'height' => 10,
58     ]);
59     $this->assertToolkitOperationsCalled(['scale']);
60
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');
65   }
66
67   /**
68    * Test the image_crop_effect() function.
69    */
70   public function testCropEffect() {
71     // @todo should test the keyword offsets.
72     $this->assertImageEffect('image_crop', [
73       'anchor' => 'top-1',
74       'width' => 3,
75       'height' => 4,
76     ]);
77     $this->assertToolkitOperationsCalled(['crop']);
78
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');
85   }
86
87   /**
88    * Tests the ConvertImageEffect plugin.
89    */
90   public function testConvertEffect() {
91     // Test jpeg.
92     $this->assertImageEffect('image_convert', [
93       'extension' => 'jpeg',
94     ]);
95     $this->assertToolkitOperationsCalled(['convert']);
96
97     // Check the parameters.
98     $calls = $this->imageTestGetAllCalls();
99     $this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly');
100   }
101
102   /**
103    * Test the image_scale_and_crop_effect() function.
104    */
105   public function testScaleAndCropEffect() {
106     $this->assertImageEffect('image_scale_and_crop', [
107       'width' => 5,
108       'height' => 10,
109     ]);
110     $this->assertToolkitOperationsCalled(['scale_and_crop']);
111
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');
116   }
117
118   /**
119    * Test the image_desaturate_effect() function.
120    */
121   public function testDesaturateEffect() {
122     $this->assertImageEffect('image_desaturate', []);
123     $this->assertToolkitOperationsCalled(['desaturate']);
124
125     // Check the parameters.
126     $calls = $this->imageTestGetAllCalls();
127     $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
128   }
129
130   /**
131    * Test the image_rotate_effect() function.
132    */
133   public function testRotateEffect() {
134     // @todo: need to test with 'random' => TRUE
135     $this->assertImageEffect('image_rotate', [
136       'degrees' => 90,
137       'bgcolor' => '#fff',
138     ]);
139     $this->assertToolkitOperationsCalled(['rotate']);
140
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');
145   }
146
147   /**
148    * Test image effect caching.
149    */
150   public function testImageEffectsCaching() {
151     $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
152
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.');
157
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.');
162
163     $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
164   }
165
166   /**
167    * Tests if validation errors are passed plugin form to the parent form.
168    */
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();
176     $uuid = key($uuids);
177
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.'));
184   }
185
186   /**
187    * Asserts the effect processing of an image effect plugin.
188    *
189    * @param string $effect_name
190    *   The name of the image effect to test.
191    * @param array $data
192    *   The data to pass to the image effect.
193    *
194    * @return bool
195    *   TRUE if the assertion succeeded, FALSE otherwise.
196    */
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.');
200   }
201
202 }