3 namespace Drupal\Tests\crop\Functional;
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;
12 * Functional tests for crop API.
16 class CropFunctionalTest extends BrowserTestBase {
23 public static $modules = ['crop', 'file'];
28 * @var \Drupal\user\UserInterface
35 * @var \Drupal\image\ImageStyleInterface
42 * @var \Drupal\crop\CropInterface
49 protected function setUp() {
52 $this->adminUser = $this->drupalCreateUser(['administer crop types', 'administer image styles']);
54 // Create test image style.
55 $this->testStyle = $this->container->get('entity_type.manager')->getStorage('image_style')->create([
57 'label' => 'Test image style',
60 $this->testStyle->save();
64 * Tests crop type crud pages.
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);
71 $this->drupalGet('admin/config/media/crop/add');
72 $this->assertSession()->statusCodeEquals(403);
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'));
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');
89 $crop_type_id = strtolower($this->randomMachineName());
91 'id' => $crop_type_id,
92 'label' => $this->randomMachineName(),
93 'description' => $this->randomGenerator->sentences(10),
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']);
105 $this->clickLink(t('Edit'));
106 $this->assertSession()->pageTextContains(t('Edit @name crop type', ['@name' => $edit['label']]));
108 $this->assertSession()->responseContains($edit['id']);
109 $this->assertSession()->fieldExists('edit-label');
110 $this->assertSession()->responseContains($edit['description']);
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.');
125 // Tests the image URI is extended with shortened hash in case of image
126 // style and corresponding crop existence.
127 $this->doTestFileUriAlter();
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);
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.'));
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']]));
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.'));
153 * Asserts a shortened hash is added to the file URI.
155 * Tests crop_file_url_alter().
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]);
164 /** @var \Drupal\crop\CropInterface $crop */
166 'type' => $this->cropType->id(),
167 'entity_id' => $file->id(),
168 'entity_type' => $file->getEntityTypeId(),
169 'uri' => 'public://sarajevo.png',
175 $crop = Crop::create($values);
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);
183 // Build an image style derivative for the file URI.
184 $image_style_uri = $this->testStyle->buildUri($file_uri);
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.');
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.');
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);
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.');
201 // Delete the file and the crop entity associated,
202 // the crop entity are auto cleaned by crop_file_delete().
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.');