annotate core/modules/image/tests/src/Functional/ImageEffectsTest.php @ 8:50b0d041100e

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