View source
<?php
namespace Drupal\Tests\image\Functional;
use Drupal\image\Entity\ImageStyle;
use Drupal\FunctionalTests\Image\ToolkitTestBase;
class ImageEffectsTest extends ToolkitTestBase {
public static $modules = [
'image',
'image_test',
'image_module_test',
];
protected $defaultTheme = 'stark';
protected $manager;
protected function setUp() {
parent::setUp();
$this->manager = $this->container
->get('plugin.manager.image.effect');
}
public function testResizeEffect() {
$this
->assertImageEffect('image_resize', [
'width' => 1,
'height' => 2,
]);
$this
->assertToolkitOperationsCalled([
'resize',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['resize'][0][0], 1, 'Width was passed correctly');
$this
->assertEqual($calls['resize'][0][1], 2, 'Height was passed correctly');
}
public function testScaleEffect() {
$this
->assertImageEffect('image_scale', [
'width' => 10,
'height' => 10,
]);
$this
->assertToolkitOperationsCalled([
'scale',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['scale'][0][0], 10, 'Width was passed correctly');
$this
->assertEqual($calls['scale'][0][1], 10, 'Height was based off aspect ratio and passed correctly');
}
public function testCropEffect() {
$this
->assertImageEffect('image_crop', [
'anchor' => 'top-1',
'width' => 3,
'height' => 4,
]);
$this
->assertToolkitOperationsCalled([
'crop',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['crop'][0][0], 0, 'X was passed correctly');
$this
->assertEqual($calls['crop'][0][1], 1, 'Y was passed correctly');
$this
->assertEqual($calls['crop'][0][2], 3, 'Width was passed correctly');
$this
->assertEqual($calls['crop'][0][3], 4, 'Height was passed correctly');
}
public function testConvertEffect() {
$this
->assertImageEffect('image_convert', [
'extension' => 'jpeg',
]);
$this
->assertToolkitOperationsCalled([
'convert',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['convert'][0][0], 'jpeg', 'Extension was passed correctly');
}
public function testScaleAndCropEffect() {
$this
->assertImageEffect('image_scale_and_crop', [
'width' => 5,
'height' => 10,
]);
$this
->assertToolkitOperationsCalled([
'scale_and_crop',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['scale_and_crop'][0][0], 7.5, 'X was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][1], 0, 'Y was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
}
public function testScaleAndCropEffectWithAnchor() {
$this
->assertImageEffect('image_scale_and_crop', [
'anchor' => 'top-1',
'width' => 5,
'height' => 10,
]);
$this
->assertToolkitOperationsCalled([
'scale_and_crop',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['scale_and_crop'][0][0], 0, 'X was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][1], 1, 'Y was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][2], 5, 'Width was computed and passed correctly');
$this
->assertEqual($calls['scale_and_crop'][0][3], 10, 'Height was computed and passed correctly');
}
public function testDesaturateEffect() {
$this
->assertImageEffect('image_desaturate', []);
$this
->assertToolkitOperationsCalled([
'desaturate',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertCount(0, $calls['desaturate'][0], 'No parameters were passed.');
}
public function testRotateEffect() {
$this
->assertImageEffect('image_rotate', [
'degrees' => 90,
'bgcolor' => '#fff',
]);
$this
->assertToolkitOperationsCalled([
'rotate',
]);
$calls = $this
->imageTestGetAllCalls();
$this
->assertEqual($calls['rotate'][0][0], 90, 'Degrees were passed correctly');
$this
->assertEqual($calls['rotate'][0][1], '#fff', 'Background color was passed correctly');
}
public function testImageEffectsCaching() {
$image_effect_definitions_called =& drupal_static('image_module_test_image_effect_info_alter');
$manager = $this->container
->get('plugin.manager.image.effect');
$effects = $manager
->getDefinitions();
$this
->assertTrue($image_effect_definitions_called === 1, 'image_effect_definitions() generated data.');
drupal_static_reset('image_module_test_image_effect_info_alter');
$cached_effects = $manager
->getDefinitions();
$this
->assertTrue($image_effect_definitions_called === 0, 'image_effect_definitions() returned data from cache.');
$this
->assertTrue($effects == $cached_effects, 'Cached effects are the same as generated effects.');
}
public function testEffectFormValidationErrors() {
$account = $this
->drupalCreateUser([
'administer image styles',
]);
$this
->drupalLogin($account);
$style = ImageStyle::load('thumbnail');
$uuids = $style
->getEffects()
->getInstanceIds();
$uuid = key($uuids);
$edit = [
'data[width]' => '',
'data[height]' => '',
];
$path = 'admin/config/media/image-styles/manage/thumbnail/effects/' . $uuid;
$this
->drupalPostForm($path, $edit, t('Update effect'));
$this
->assertText(t('Width and height can not both be blank.'));
}
protected function assertImageEffect($effect_name, array $data) {
$effect = $this->manager
->createInstance($effect_name, [
'data' => $data,
]);
return $this
->assertTrue($effect
->applyEffect($this->image), 'Function returned the expected value.');
}
}