Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / crop / tests / src / Kernel / CropCRUDTest.php
1 <?php
2
3 namespace Drupal\Tests\crop\Kernel;
4
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\crop\Entity\Crop;
7
8 /**
9  * Tests the crop entity CRUD operations.
10  *
11  * @group crop
12  */
13 class CropCRUDTest extends CropUnitTestBase {
14
15   /**
16    * Modules to enable.
17    *
18    * @var array
19    */
20   public static $modules = ['user', 'image', 'crop', 'file', 'system'];
21
22   /**
23    * Tests crop type save.
24    */
25   public function testCropTypeSave() {
26     $values = [
27       'id' => $this->randomMachineName(),
28       'label' => $this->randomString(),
29       'description' => $this->randomGenerator->sentences(8),
30     ];
31     $crop_type = $this->cropTypeStorage->create($values);
32
33     try {
34       $crop_type->save();
35       $this->assertTrue(TRUE, 'Crop type saved correctly.');
36     }
37     catch (\Exception $exception) {
38       $this->assertTrue(FALSE, 'Crop type not saved correctly.');
39     }
40
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]));
44     }
45   }
46
47   /**
48    * Tests crop save.
49    */
50   public function testCropSave() {
51     // Test file.
52     $file = $this->getTestFile();
53     $file->save();
54
55     /** @var \Drupal\crop\CropInterface $crop */
56     $values = [
57       'type' => $this->cropType->id(),
58       'entity_id' => $file->id(),
59       'entity_type' => $file->getEntityTypeId(),
60       'uri' => $file->getFileUri(),
61       'x' => '100',
62       'y' => '150',
63       'width' => '200',
64       'height' => '250',
65     ];
66     $crop = $this->cropStorage->create($values);
67
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.'));
71
72     $crop->setPosition(10, 10);
73     $crop->setSize(20, 20);
74
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.'));
78
79     $crop->setPosition($values['x'], $values['y']);
80     $crop->setSize($values['width'], $values['height']);
81
82     try {
83       $crop->save();
84       $this->assertTrue(TRUE, 'Crop saved correctly.');
85     }
86     catch (\Exception $exception) {
87       $this->assertTrue(FALSE, 'Crop not saved correctly.');
88     }
89
90     $loaded_crop = $this->cropStorage->loadUnchanged(1);
91     foreach ($values as $key => $value) {
92       switch ($key) {
93         case 'type':
94           $this->assertEquals($loaded_crop->{$key}->target_id, $value, new FormattableMarkup('Correct value for @field found.', ['@field' => $key]));
95           break;
96
97         default:
98           $this->assertEquals($loaded_crop->{$key}->value, $value, new FormattableMarkup('Correct value for @field found.', ['@field' => $key]));
99           break;
100       }
101     }
102
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.'));
107
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.'));
115   }
116
117   /**
118    * Tests automatic removal of orphaned crops.
119    */
120   public function testOrphanRemoval() {
121     $this->installSchema('file', ['file_usage']);
122     $file = $this->getTestFile();
123     $file->save();
124
125     $values = [
126       'type' => $this->cropType->id(),
127       'entity_id' => $file->id(),
128       'entity_type' => $file->getEntityTypeId(),
129       'x' => '100',
130       'y' => '150',
131       'width' => '200',
132       'height' => '250',
133     ];
134     /** @var \Drupal\crop\CropInterface $crop */
135     $crop = $this->cropStorage->create($values);
136     $crop->save();
137
138     // Check if the crop is automatically removed at file removal.
139     $file->delete();
140     $crops = $this->cropStorage->loadByProperties(['uri' => $crop->uri->value]);
141     $this->assertEquals([], $crops, 'Crop deleted correctly.');
142   }
143
144 }