Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / crop / tests / src / Functional / CropFunctionalTest.php
1 <?php
2
3 namespace Drupal\Tests\crop\Functional;
4
5 use Drupal\Core\StreamWrapper\PublicStream;
6 use Drupal\crop\Entity\Crop;
7 use Drupal\crop\Entity\CropType;
8 use Drupal\file\Entity\File;
9 use Drupal\Tests\BrowserTestBase;
10
11 /**
12  * Functional tests for crop API.
13  *
14  * @group crop
15  */
16 class CropFunctionalTest extends BrowserTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['crop', 'file'];
24
25   /**
26    * Admin user.
27    *
28    * @var \Drupal\user\UserInterface
29    */
30   protected $adminUser;
31
32   /**
33    * Test image style.
34    *
35    * @var \Drupal\image\ImageStyleInterface
36    */
37   protected $testStyle;
38
39   /**
40    * Test crop type.
41    *
42    * @var \Drupal\crop\CropInterface
43    */
44   protected $cropType;
45
46   /**
47    * {@inheritdoc}
48    */
49   protected function setUp() {
50     parent::setUp();
51
52     $this->adminUser = $this->drupalCreateUser(['administer crop types', 'administer image styles']);
53
54     // Create test image style.
55     $this->testStyle = $this->container->get('entity_type.manager')->getStorage('image_style')->create([
56       'name' => 'test',
57       'label' => 'Test image style',
58       'effects' => [],
59     ]);
60     $this->testStyle->save();
61   }
62
63   /**
64    * Tests crop type crud pages.
65    */
66   public function testCropTypeCrud() {
67     // Anonymous users don't have access to crop type admin pages.
68     $this->drupalGet('admin/config/media/crop');
69     $this->assertSession()->statusCodeEquals(403);
70
71     $this->drupalGet('admin/config/media/crop/add');
72     $this->assertSession()->statusCodeEquals(403);
73
74
75     // Can access pages if logged in and no crop types exist.
76     $this->drupalLogin($this->adminUser);
77     $this->drupalGet('admin/config/media/crop');
78     $this->assertSession()->statusCodeEquals(200);
79     $this->assertSession()->pageTextContains(t('No crop types available.'));
80     $this->assertSession()->linkExists(t('Add crop type'));
81
82     // Can access add crop type form.
83     $this->clickLink(t('Add crop type'));
84     $this->assertSession()->statusCodeEquals(200);
85     $this->assertSession()->addressEquals('admin/config/media/crop/add');
86
87
88     // Create crop type.
89     $crop_type_id = strtolower($this->randomMachineName());
90     $edit = [
91       'id' => $crop_type_id,
92       'label' => $this->randomMachineName(),
93       'description' => $this->randomGenerator->sentences(10),
94     ];
95     $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
96     $this->assertSession()->responseContains(t('The crop type %name has been added.', ['%name' => $edit['label']]));
97     $this->cropType = CropType::load($crop_type_id);
98     $this->assertSession()->addressEquals('admin/config/media/crop');
99     $label = $this->xpath("//td[contains(concat(' ',normalize-space(@class),' '),' menu-label ')]");
100     self::assertTrue(strpos($label[0]->getText(), $edit['label']) !== FALSE, 'Crop type label found on listing page.');
101     $this->assertSession()->pageTextContains($edit['description']);
102
103
104     // Check edit form.
105     $this->clickLink(t('Edit'));
106     $this->assertSession()->pageTextContains(t('Edit @name crop type', ['@name' => $edit['label']]));
107
108     $this->assertSession()->responseContains($edit['id']);
109     $this->assertSession()->fieldExists('edit-label');
110     $this->assertSession()->responseContains($edit['description']);
111
112
113     // See if crop type appears on image effect configuration form.
114     $this->drupalGet('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop');
115     $option = $this->xpath("//select[@id='edit-data-crop-type']/option");
116     self::assertTrue(strpos($option[0]->getText(), $edit['label']) !== FALSE, 'Crop type label found on image effect page.');
117     $this->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop', ['data[crop_type]' => $edit['id']], t('Add effect'));
118     $this->assertSession()->pageTextContains(t('The image effect was successfully applied.'));
119     $this->assertSession()->pageTextContains(t('Manual crop uses @name crop type', ['@name' => $edit['label']]));
120     $this->testStyle = $this->container->get('entity_type.manager')->getStorage('image_style')->loadUnchanged($this->testStyle->id());
121     self::assertEquals($this->testStyle->getEffects()->count(), 1, 'One image effect added to test image style.');
122     $effect_configuration = $this->testStyle->getEffects()->getIterator()->current()->getConfiguration();
123     self::assertEquals($effect_configuration['data'], ['crop_type' => $edit['id']], 'Manual crop effect uses correct image style.');
124
125     // Tests the image URI is extended with shortened hash in case of image
126     // style and corresponding crop existence.
127     $this->doTestFileUriAlter();
128
129     // Try to access edit form as anonymous user.
130     $this->drupalLogout();
131     $this->drupalGet('admin/config/media/crop/manage/' . $edit['id']);
132     $this->assertSession()->statusCodeEquals(403);
133     $this->drupalLogin($this->adminUser);
134
135     // Try to create crop type with same machine name.
136     $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
137     $this->assertSession()->pageTextContains(t('The machine-readable name is already in use. It must be unique.'));
138
139
140     // Delete crop type.
141     $this->drupalGet('admin/config/media/crop');
142     $this->assertSession()->linkExists('Test image style');
143     $this->clickLink(t('Delete'));
144     $this->assertSession()->pageTextContains(t('Are you sure you want to delete the crop type @name?', ['@name' => $edit['label']]));
145
146     $this->drupalPostForm('admin/config/media/crop/manage/' . $edit['id'] . '/delete', [], t('Delete'));
147     $this->assertSession()->responseContains(t('The crop type %name has been deleted.', ['%name' => $edit['label']]));
148     $this->assertSession()->pageTextContains(t('No crop types available.'));
149
150   }
151
152   /**
153    * Asserts a shortened hash is added to the file URI.
154    *
155    * Tests crop_file_url_alter().
156    */
157   protected function doTestFileUriAlter() {
158     // Get the test file.
159     file_unmanaged_copy(drupal_get_path('module', 'crop') . '/tests/files/sarajevo.png', PublicStream::basePath());
160     $file_uri = 'public://sarajevo.png';
161     $file = File::create(['uri' => $file_uri, 'status' => FILE_STATUS_PERMANENT]);
162     $file->save();
163
164     /** @var \Drupal\crop\CropInterface $crop */
165     $values = [
166       'type' => $this->cropType->id(),
167       'entity_id' => $file->id(),
168       'entity_type' => $file->getEntityTypeId(),
169       'uri' => 'public://sarajevo.png',
170       'x' => '100',
171       'y' => '150',
172       'width' => '200',
173       'height' => '250',
174     ];
175     $crop = Crop::create($values);
176     $crop->save();
177
178     // Test that the hash is appended both when a URL is created and passed
179     // through file_create_url() and when a URL is created, without additional
180     // file_create_url() calls.
181     $shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
182
183     // Build an image style derivative for the file URI.
184     $image_style_uri = $this->testStyle->buildUri($file_uri);
185
186     $image_style_uri_url = file_create_url($image_style_uri);
187     $this->assertTrue(strpos($image_style_uri_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');
188
189     $image_style_url = $this->testStyle->buildUrl($file_uri);
190     $this->assertTrue(strpos($image_style_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');
191
192     // Update the crop to assert the hash has changed.
193     $crop->setPosition('80', '80')->save();
194     $old_hash = $shortened_hash;
195     $new_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
196
197     $image_style_url = $this->testStyle->buildUrl($file_uri);
198     $this->assertFalse(strpos($image_style_url, $old_hash) !== FALSE, 'The image style URL does not contain the old hash.');
199     $this->assertTrue(strpos($image_style_url, $new_hash) !== FALSE, 'The image style URL contains an updated hash.');
200
201     // Delete the file and the crop entity associated,
202     // the crop entity are auto cleaned by crop_file_delete().
203     $file->delete();
204
205     // Check that the crop entity is correctly deleted.
206     $this->assertFalse(Crop::cropExists($file_uri), 'The Crop entity was correctly deleted after file delete.');
207   }
208
209 }