Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / crop / tests / src / Kernel / CropEffectTest.php
1 <?php
2
3 namespace Drupal\Tests\crop\Kernel;
4
5 /**
6  * Tests the crop image effect.
7  *
8  * @group crop
9  */
10 class CropEffectTest extends CropUnitTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = ['user', 'image', 'crop', 'file', 'system'];
18
19   /**
20    * Tests manual crop image effect.
21    */
22   public function testCropEffect() {
23     // Create image to be cropped.
24     $file = $this->getTestFile();
25     $file->save();
26
27     // Create crop.
28     $values = [
29       'type' => $this->cropType->id(),
30       'entity_id' => $file->id(),
31       'entity_type' => 'file',
32       'uri' => $file->getFileUri(),
33       'x' => '190',
34       'y' => '120',
35       'width' => '50',
36       'height' => '50',
37     ];
38     /** @var \Drupal\crop\CropInterface $crop */
39     $crop = $this->container->get('entity.manager')->getStorage('crop')->create($values);
40     $crop->save();
41
42     $derivative_uri = $this->testStyle->buildUri($file->getFileUri());
43     $this->testStyle->createDerivative($file->getFileUri(), $derivative_uri);
44
45     $this->assertTrue(file_exists($derivative_uri), 'Image derivative file exists on the filesystem.');
46
47     // Test if cropped version looks like expected. Basically loop pixels,
48     // in derivative image and check if they look the same as pixels,
49     // in corresponding region on original image.
50     $original_image = imagecreatefrompng($file->getFileUri());
51     $derivative_image = imagecreatefrompng($derivative_uri);
52     $orig_start = $crop->anchor();
53     $matches = TRUE;
54     for ($x = 0; $x < $values['width']; $x++) {
55       for ($y = 0; $y < $values['height']; $y++) {
56         if (imagecolorat($derivative_image, $x, $y) != imagecolorat($original_image, $orig_start['x'] + $x, $orig_start['y'] + $y)) {
57           $matches = FALSE;
58           break;
59         }
60       }
61     }
62     $this->assertTrue($matches, 'Cropped image looks the same as region on original.');
63   }
64
65   /**
66    * Test image crop effect dimensions.
67    */
68   public function testCropDimenssions() {
69     // Create image to be cropped.
70     $file = $this->getTestFile();
71     $file->save();
72     $file_uri = $file->getFileUri();
73
74     // Create crop.
75     $values = [
76       'type' => $this->cropType->id(),
77       'entity_id' => $file->id(),
78       'entity_type' => 'file',
79       'uri' => $file_uri,
80       'x' => '190',
81       'y' => '120',
82       'width' => '50',
83       'height' => '50',
84     ];
85     $dimensions = ['width' => 0, 'height' => 0];
86
87     /** @var \Drupal\crop\CropInterface $crop */
88     $crop = $this->container->get('entity_type.manager')->getStorage('crop')->create($values);
89     $crop->save();
90
91     /** @var $effect \Drupal\crop\Plugin\ImageEffect\CropEffect */
92     $effect = $this->imageEffectManager->createInstance('crop_crop', ['data' => ['crop_type' => $this->cropType->id()]]);
93     $effect->transformDimensions($dimensions, $file_uri);
94
95     $this->assertEquals($crop->size(), $dimensions, t('CropEffect::transformDimensions() transform image dimensions correctly.'));
96   }
97
98 }