View source
<?php
namespace Drupal\Tests\crop\Kernel;
class CropEffectTest extends CropUnitTestBase {
public static $modules = [
'user',
'image',
'crop',
'file',
'system',
];
public function testCropEffect() {
$file = $this
->getTestFile();
$file
->save();
$values = [
'type' => $this->cropType
->id(),
'entity_id' => $file
->id(),
'entity_type' => 'file',
'uri' => $file
->getFileUri(),
'x' => '190',
'y' => '120',
'width' => '50',
'height' => '50',
];
$crop = $this->container
->get('entity.manager')
->getStorage('crop')
->create($values);
$crop
->save();
$derivative_uri = $this->testStyle
->buildUri($file
->getFileUri());
$this->testStyle
->createDerivative($file
->getFileUri(), $derivative_uri);
$this
->assertTrue(file_exists($derivative_uri), 'Image derivative file exists on the filesystem.');
$original_image = imagecreatefrompng($file
->getFileUri());
$derivative_image = imagecreatefrompng($derivative_uri);
$orig_start = $crop
->anchor();
$matches = TRUE;
for ($x = 0; $x < $values['width']; $x++) {
for ($y = 0; $y < $values['height']; $y++) {
if (imagecolorat($derivative_image, $x, $y) != imagecolorat($original_image, $orig_start['x'] + $x, $orig_start['y'] + $y)) {
$matches = FALSE;
break;
}
}
}
$this
->assertTrue($matches, 'Cropped image looks the same as region on original.');
}
public function testCropDimenssions() {
$file = $this
->getTestFile();
$file
->save();
$file_uri = $file
->getFileUri();
$values = [
'type' => $this->cropType
->id(),
'entity_id' => $file
->id(),
'entity_type' => 'file',
'uri' => $file_uri,
'x' => '190',
'y' => '120',
'width' => '50',
'height' => '50',
];
$dimensions = [
'width' => 0,
'height' => 0,
];
$crop = $this->container
->get('entity_type.manager')
->getStorage('crop')
->create($values);
$crop
->save();
$effect = $this->imageEffectManager
->createInstance('crop_crop', [
'data' => [
'crop_type' => $this->cropType
->id(),
],
]);
$effect
->transformDimensions($dimensions, $file_uri);
$this
->assertEquals($crop
->size(), $dimensions, t('CropEffect::transformDimensions() transform image dimensions correctly.'));
}
}