3 namespace Drupal\Tests\crop\Kernel;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\crop\Entity\Crop;
9 * Tests the crop entity CRUD operations.
13 class CropCRUDTest extends CropUnitTestBase {
20 public static $modules = ['user', 'image', 'crop', 'file', 'system'];
23 * Tests crop type save.
25 public function testCropTypeSave() {
27 'id' => $this->randomMachineName(),
28 'label' => $this->randomString(),
29 'description' => $this->randomGenerator->sentences(8),
31 $crop_type = $this->cropTypeStorage->create($values);
35 $this->assertTrue(TRUE, 'Crop type saved correctly.');
37 catch (\Exception $exception) {
38 $this->assertTrue(FALSE, 'Crop type not saved correctly.');
41 $loaded = $this->container->get('config.factory')->get('crop.type.' . $values['id'])->get();
42 foreach ($values as $key => $value) {
43 $this->assertEquals($loaded[$key], $value, new FormattableMarkup('Correct value for @field found.', ['@field' => $key]));
50 public function testCropSave() {
52 $file = $this->getTestFile();
55 /** @var \Drupal\crop\CropInterface $crop */
57 'type' => $this->cropType->id(),
58 'entity_id' => $file->id(),
59 'entity_type' => $file->getEntityTypeId(),
60 'uri' => $file->getFileUri(),
66 $crop = $this->cropStorage->create($values);
68 $this->assertEquals(['x' => 100, 'y' => 150], $crop->position(), t('Position correctly returned.'));
69 $this->assertEquals(['width' => 200, 'height' => 250], $crop->size(), t('Size correctly returned.'));
70 $this->assertEquals(['x' => 0, 'y' => 25], $crop->anchor(), t('Anchor correctly returned.'));
72 $crop->setPosition(10, 10);
73 $crop->setSize(20, 20);
75 $this->assertEquals(['x' => 10, 'y' => 10], $crop->position(), t('Position correctly returned.'));
76 $this->assertEquals(['width' => 20, 'height' => 20], $crop->size(), t('Size correctly returned.'));
77 $this->assertEquals(['x' => 0, 'y' => 0], $crop->anchor(), t('Anchor correctly returned.'));
79 $crop->setPosition($values['x'], $values['y']);
80 $crop->setSize($values['width'], $values['height']);
84 $this->assertTrue(TRUE, 'Crop saved correctly.');
86 catch (\Exception $exception) {
87 $this->assertTrue(FALSE, 'Crop not saved correctly.');
90 $loaded_crop = $this->cropStorage->loadUnchanged(1);
91 foreach ($values as $key => $value) {
94 $this->assertEquals($loaded_crop->{$key}->target_id, $value, new FormattableMarkup('Correct value for @field found.', ['@field' => $key]));
98 $this->assertEquals($loaded_crop->{$key}->value, $value, new FormattableMarkup('Correct value for @field found.', ['@field' => $key]));
103 $this->assertTrue(Crop::cropExists($file->getFileUri()), t('Crop::cropExists() correctly found saved crop.'));
104 $this->assertTrue(Crop::cropExists($file->getFileUri(), $this->cropType->id()), t('Crop::cropExists() correctly found saved crop.'));
105 $this->assertFalse(Crop::cropExists($file->getFileUri(), 'nonexistent_type'), t('Crop::cropExists() correctly handled wrong type.'));
106 $this->assertFalse(Crop::cropExists('public://nonexistent.png'), t('Crop::cropExists() correctly handled wrong uri.'));
108 $loaded_crop = Crop::findCrop($file->getFileUri(), $this->cropType->id());
109 $this->assertEquals($crop->id(), $loaded_crop->id(), t('Crop::findCrop() correctly loaded crop entity.'));
110 $this->assertEquals($crop->position(), $loaded_crop->position(), t('Crop::findCrop() correctly loaded crop entity.'));
111 $this->assertEquals($crop->size(), $loaded_crop->size(), t('Crop::findCrop() correctly loaded crop entity.'));
112 $this->assertEquals($crop->uri->value, $loaded_crop->uri->value, t('Crop::findCrop() correctly loaded crop entity.'));
113 $this->assertNull(Crop::findCrop('public://nonexistent.png', $this->cropType->id()), t('Crop::findCrop() correctly handled nonexistent crop.'));
114 $this->assertNull(Crop::findCrop('public://nonexistent.png', 'nonexistent_crop'), t('Crop::findCrop() correctly handled nonexistent crop.'));
118 * Tests automatic removal of orphaned crops.
120 public function testOrphanRemoval() {
121 $this->installSchema('file', ['file_usage']);
122 $file = $this->getTestFile();
126 'type' => $this->cropType->id(),
127 'entity_id' => $file->id(),
128 'entity_type' => $file->getEntityTypeId(),
134 /** @var \Drupal\crop\CropInterface $crop */
135 $crop = $this->cropStorage->create($values);
138 // Check if the crop is automatically removed at file removal.
140 $crops = $this->cropStorage->loadByProperties(['uri' => $crop->uri->value]);
141 $this->assertEquals([], $crops, 'Crop deleted correctly.');