f6b4ab36e92f2a95fba0dc5499e682a110a88726
[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\FunctionalTests\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], 7.5, 'X was computed and passed correctly');
115     $this->assertEqual($calls['scale_and_crop'][0][1], 0, 'Y was computed and passed correctly');
116     $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
117     $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
118   }
119
120   /**
121    * Test the image_scale_and_crop_effect() function with an anchor.
122    */
123   public function testScaleAndCropEffectWithAnchor() {
124     $this->assertImageEffect('image_scale_and_crop', [
125       'anchor' => 'top-1',
126       'width' => 5,
127       'height' => 10,
128     ]);
129     $this->assertToolkitOperationsCalled(['scale_and_crop']);
130
131     // Check the parameters.
132     $calls = $this->imageTestGetAllCalls();
133     $this->assertEqual($calls['scale_and_crop'][0][0], 0, 'X was computed and passed correctly');
134     $this->assertEqual($calls['scale_and_crop'][0][1], 1, 'Y was computed and passed correctly');
135     $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
136     $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
137   }
138
139   /**
140    * Test the image_desaturate_effect() function.
141    */
142   public function testDesaturateEffect() {
143     $this->assertImageEffect('image_desaturate', []);
144     $this->assertToolkitOperationsCalled(['desaturate']);
145
146     // Check the parameters.
147     $calls = $this->imageTestGetAllCalls();
148     $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
149   }
150
151   /**
152    * Test the image_rotate_effect() function.
153    */
154   public function testRotateEffect() {
155     // @todo: need to test with 'random' => TRUE
156     $this->assertImageEffect('image_rotate', [
157       'degrees' => 90,
158       'bgcolor' => '#fff',
159     ]);
160     $this->assertToolkitOperationsCalled(['rotate']);
161
162     // Check the parameters.
163     $calls = $this->imageTestGetAllCalls();
164     $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
165     $this->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly');
166   }
167
168   /**
169    * Test image effect caching.
170    */
171   public function testImageEffectsCaching() {
172     $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
173
174     // First call should grab a fresh copy of the data.
175     $manager = $this->container->get('plugin.manager.image.effect');
176     $effects = $manager->getDefinitions();
177     $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
178
179     // Second call should come from cache.
180     drupal_static_reset('image_module_test_image_effect_info_alter');
181     $cached_effects = $manager->getDefinitions();
182     $this->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.');
183
184     $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
185   }
186
187   /**
188    * Tests if validation errors are passed plugin form to the parent form.
189    */
190   public function testEffectFormValidationErrors() {
191     $account = $this->drupalCreateUser(['administer image styles']);
192     $this->drupalLogin($account);
193     /** @var \Drupal\image\ImageStyleInterface $style */
194     $style = ImageStyle::load('thumbnail');
195     // Image Scale is the only effect shipped with 'thumbnail', by default.
196     $uuids = $style->getEffects()->getInstanceIds();
197     $uuid = key($uuids);
198
199     // We are posting the form with both, width and height, empty.
200     $edit = ['data[width]' => '', 'data[height]' => ''];
201     $path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid;
202     $this->drupalPostForm($path, $edit, t('Update effect'));
203     // Check that the error message has been displayed.
204     $this->assertText(t('Width and height can not both be blank.'));
205   }
206
207   /**
208    * Asserts the effect processing of an image effect plugin.
209    *
210    * @param string $effect_name
211    *   The name of the image effect to test.
212    * @param array $data
213    *   The data to pass to the image effect.
214    *
215    * @return bool
216    *   TRUE if the assertion succeeded, FALSE otherwise.
217    */
218   protected function assertImageEffect($effect_name, array $data) {
219     $effect = $this->manager->createInstance($effect_name, ['data' => $data]);
220     return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');
221   }
222
223 }