annotate core/modules/image/tests/src/Functional/ImageEffectsTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 129ea1e6d783
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\image\Functional;
Chris@0 4
Chris@0 5 use Drupal\image\Entity\ImageStyle;
Chris@0 6 use Drupal\FunctionalTests\Image\ToolkitTestBase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * Tests that the image effects pass parameters to the toolkit correctly.
Chris@0 10 *
Chris@0 11 * @group image
Chris@0 12 */
Chris@0 13 class ImageEffectsTest extends ToolkitTestBase {
Chris@0 14
Chris@0 15 /**
Chris@0 16 * Modules to enable.
Chris@0 17 *
Chris@0 18 * @var array
Chris@0 19 */
Chris@0 20 public static $modules = ['image', 'image_test', 'image_module_test'];
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The image effect manager.
Chris@0 24 *
Chris@0 25 * @var \Drupal\image\ImageEffectManager
Chris@0 26 */
Chris@0 27 protected $manager;
Chris@0 28
Chris@0 29 protected function setUp() {
Chris@0 30 parent::setUp();
Chris@0 31 $this->manager = $this->container->get('plugin.manager.image.effect');
Chris@0 32 }
Chris@0 33
Chris@0 34 /**
Chris@0 35 * Test the image_resize_effect() function.
Chris@0 36 */
Chris@0 37 public function testResizeEffect() {
Chris@0 38 $this->assertImageEffect('image_resize', [
Chris@0 39 'width' => 1,
Chris@0 40 'height' => 2,
Chris@0 41 ]);
Chris@0 42 $this->assertToolkitOperationsCalled(['resize']);
Chris@0 43
Chris@0 44 // Check the parameters.
Chris@0 45 $calls = $this->imageTestGetAllCalls();
Chris@0 46 $this->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
Chris@0 47 $this->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
Chris@0 48 }
Chris@0 49
Chris@0 50 /**
Chris@0 51 * Test the image_scale_effect() function.
Chris@0 52 */
Chris@0 53 public function testScaleEffect() {
Chris@0 54 // @todo: need to test upscaling.
Chris@0 55 $this->assertImageEffect('image_scale', [
Chris@0 56 'width' => 10,
Chris@0 57 'height' => 10,
Chris@0 58 ]);
Chris@0 59 $this->assertToolkitOperationsCalled(['scale']);
Chris@0 60
Chris@0 61 // Check the parameters.
Chris@0 62 $calls = $this->imageTestGetAllCalls();
Chris@0 63 $this->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
Chris@0 64 $this->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
Chris@0 65 }
Chris@0 66
Chris@0 67 /**
Chris@0 68 * Test the image_crop_effect() function.
Chris@0 69 */
Chris@0 70 public function testCropEffect() {
Chris@0 71 // @todo should test the keyword offsets.
Chris@0 72 $this->assertImageEffect('image_crop', [
Chris@0 73 'anchor' => 'top-1',
Chris@0 74 'width' => 3,
Chris@0 75 'height' => 4,
Chris@0 76 ]);
Chris@0 77 $this->assertToolkitOperationsCalled(['crop']);
Chris@0 78
Chris@0 79 // Check the parameters.
Chris@0 80 $calls = $this->imageTestGetAllCalls();
Chris@0 81 $this->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly');
Chris@0 82 $this->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly');
Chris@0 83 $this->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
Chris@0 84 $this->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
Chris@0 85 }
Chris@0 86
Chris@0 87 /**
Chris@0 88 * Tests the ConvertImageEffect plugin.
Chris@0 89 */
Chris@0 90 public function testConvertEffect() {
Chris@0 91 // Test jpeg.
Chris@0 92 $this->assertImageEffect('image_convert', [
Chris@0 93 'extension' => 'jpeg',
Chris@0 94 ]);
Chris@0 95 $this->assertToolkitOperationsCalled(['convert']);
Chris@0 96
Chris@0 97 // Check the parameters.
Chris@0 98 $calls = $this->imageTestGetAllCalls();
Chris@0 99 $this->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly');
Chris@0 100 }
Chris@0 101
Chris@0 102 /**
Chris@0 103 * Test the image_scale_and_crop_effect() function.
Chris@0 104 */
Chris@0 105 public function testScaleAndCropEffect() {
Chris@0 106 $this->assertImageEffect('image_scale_and_crop', [
Chris@0 107 'width' => 5,
Chris@0 108 'height' => 10,
Chris@0 109 ]);
Chris@0 110 $this->assertToolkitOperationsCalled(['scale_and_crop']);
Chris@0 111
Chris@0 112 // Check the parameters.
Chris@0 113 $calls = $this->imageTestGetAllCalls();
Chris@17 114 $this->assertEqual($calls['scale_and_crop'][0][0], 7.5, 'X was computed and passed correctly');
Chris@17 115 $this->assertEqual($calls['scale_and_crop'][0][1], 0, 'Y was computed and passed correctly');
Chris@17 116 $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
Chris@17 117 $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
Chris@17 118 }
Chris@17 119
Chris@17 120 /**
Chris@17 121 * Test the image_scale_and_crop_effect() function with an anchor.
Chris@17 122 */
Chris@17 123 public function testScaleAndCropEffectWithAnchor() {
Chris@17 124 $this->assertImageEffect('image_scale_and_crop', [
Chris@17 125 'anchor' => 'top-1',
Chris@17 126 'width' => 5,
Chris@17 127 'height' => 10,
Chris@17 128 ]);
Chris@17 129 $this->assertToolkitOperationsCalled(['scale_and_crop']);
Chris@17 130
Chris@17 131 // Check the parameters.
Chris@17 132 $calls = $this->imageTestGetAllCalls();
Chris@17 133 $this->assertEqual($calls['scale_and_crop'][0][0], 0, 'X was computed and passed correctly');
Chris@17 134 $this->assertEqual($calls['scale_and_crop'][0][1], 1, 'Y was computed and passed correctly');
Chris@17 135 $this->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
Chris@17 136 $this->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
Chris@0 137 }
Chris@0 138
Chris@0 139 /**
Chris@0 140 * Test the image_desaturate_effect() function.
Chris@0 141 */
Chris@0 142 public function testDesaturateEffect() {
Chris@0 143 $this->assertImageEffect('image_desaturate', []);
Chris@0 144 $this->assertToolkitOperationsCalled(['desaturate']);
Chris@0 145
Chris@0 146 // Check the parameters.
Chris@0 147 $calls = $this->imageTestGetAllCalls();
Chris@0 148 $this->assertEqual(count($calls['desaturate'][0]), 0, 'No parameters were passed.');
Chris@0 149 }
Chris@0 150
Chris@0 151 /**
Chris@0 152 * Test the image_rotate_effect() function.
Chris@0 153 */
Chris@0 154 public function testRotateEffect() {
Chris@0 155 // @todo: need to test with 'random' => TRUE
Chris@0 156 $this->assertImageEffect('image_rotate', [
Chris@0 157 'degrees' => 90,
Chris@0 158 'bgcolor' => '#fff',
Chris@0 159 ]);
Chris@0 160 $this->assertToolkitOperationsCalled(['rotate']);
Chris@0 161
Chris@0 162 // Check the parameters.
Chris@0 163 $calls = $this->imageTestGetAllCalls();
Chris@0 164 $this->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
Chris@0 165 $this->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly');
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Test image effect caching.
Chris@0 170 */
Chris@0 171 public function testImageEffectsCaching() {
Chris@0 172 $image_effect_definitions_called = &drupal_static('image_module_test_image_effect_info_alter');
Chris@0 173
Chris@0 174 // First call should grab a fresh copy of the data.
Chris@0 175 $manager = $this->container->get('plugin.manager.image.effect');
Chris@0 176 $effects = $manager->getDefinitions();
Chris@0 177 $this->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
Chris@0 178
Chris@0 179 // Second call should come from cache.
Chris@0 180 drupal_static_reset('image_module_test_image_effect_info_alter');
Chris@0 181 $cached_effects = $manager->getDefinitions();
Chris@0 182 $this->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.');
Chris@0 183
Chris@0 184 $this->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
Chris@0 185 }
Chris@0 186
Chris@0 187 /**
Chris@0 188 * Tests if validation errors are passed plugin form to the parent form.
Chris@0 189 */
Chris@0 190 public function testEffectFormValidationErrors() {
Chris@0 191 $account = $this->drupalCreateUser(['administer image styles']);
Chris@0 192 $this->drupalLogin($account);
Chris@0 193 /** @var \Drupal\image\ImageStyleInterface $style */
Chris@0 194 $style = ImageStyle::load('thumbnail');
Chris@0 195 // Image Scale is the only effect shipped with 'thumbnail', by default.
Chris@0 196 $uuids = $style->getEffects()->getInstanceIds();
Chris@0 197 $uuid = key($uuids);
Chris@0 198
Chris@0 199 // We are posting the form with both, width and height, empty.
Chris@0 200 $edit = ['data[width]' => '', 'data[height]' => ''];
Chris@0 201 $path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid;
Chris@0 202 $this->drupalPostForm($path, $edit, t('Update effect'));
Chris@0 203 // Check that the error message has been displayed.
Chris@0 204 $this->assertText(t('Width and height can not both be blank.'));
Chris@0 205 }
Chris@0 206
Chris@0 207 /**
Chris@0 208 * Asserts the effect processing of an image effect plugin.
Chris@0 209 *
Chris@0 210 * @param string $effect_name
Chris@0 211 * The name of the image effect to test.
Chris@0 212 * @param array $data
Chris@0 213 * The data to pass to the image effect.
Chris@0 214 *
Chris@0 215 * @return bool
Chris@0 216 * TRUE if the assertion succeeded, FALSE otherwise.
Chris@0 217 */
Chris@0 218 protected function assertImageEffect($effect_name, array $data) {
Chris@0 219 $effect = $this->manager->createInstance($effect_name, ['data' => $data]);
Chris@0 220 return $this->assertTrue($effect->applyEffect($this->image), 'Function returned the expected value.');
Chris@0 221 }
Chris@0 222
Chris@0 223 }