3 namespace Drupal\crop\Tests;
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\simpletest\WebTestBase;
12 * Functional tests for crop API.
16 class CropFunctionalTest extends WebTestBase {
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.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->assertResponse(403);
70 $this->drupalGet('admin/config/media/crop/add');
71 $this->assertResponse(403);
73 // Can access pages if logged in and no crop types exist.
74 $this->drupalLogin($this->adminUser);
75 $this->drupalGet('admin/config/media/crop');
76 $this->assertResponse(200);
77 $this->assertText(t('No crop types available.'));
78 $this->assertLink(t('Add crop type'));
80 // Can access add crop type form.
81 $this->clickLink(t('Add crop type'));
82 $this->assertResponse(200);
83 $this->assertUrl('admin/config/media/crop/add');
86 $crop_type_id = strtolower($this->randomMachineName());
88 'id' => $crop_type_id,
89 'label' => $this->randomMachineName(),
90 'description' => $this->randomGenerator->sentences(10),
92 $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
93 $this->assertRaw(t('The crop type %name has been added.', ['%name' => $edit['label']]));
94 $this->cropType = CropType::load($crop_type_id);
95 $this->assertUrl('admin/config/media/crop');
96 $label = $this->xpath("//td[contains(concat(' ',normalize-space(@class),' '),' menu-label ')]");
97 $this->assert(strpos($label[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on listing page.');
98 $this->assertText($edit['description']);
101 $this->clickLink(t('Edit'));
102 $this->assertText(t('Edit @name crop type', ['@name' => $edit['label']]));
103 $this->assertRaw($edit['id']);
104 $this->assertFieldById('edit-label', $edit['label']);
105 $this->assertRaw($edit['description']);
107 // See if crop type appears on image effect configuration form.
108 $this->drupalGet('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop');
109 $option = $this->xpath("//select[@id='edit-data-crop-type']/option");
110 $this->assert(strpos($option[0]->asXML(), $edit['label']) !== FALSE, 'Crop type label found on image effect page.');
111 $this->drupalPostForm('admin/config/media/image-styles/manage/' . $this->testStyle->id() . '/add/crop_crop', ['data[crop_type]' => $edit['id']], t('Add effect'));
112 $this->assertText(t('The image effect was successfully applied.'));
113 $this->assertText(t('Manual crop uses @name crop type', ['@name' => $edit['label']]));
114 $this->testStyle = $this->container->get('entity.manager')->getStorage('image_style')->loadUnchanged($this->testStyle->id());
115 $this->assertEqual($this->testStyle->getEffects()->count(), 1, 'One image effect added to test image style.');
116 $effect_configuration = $this->testStyle->getEffects()->getIterator()->current()->getConfiguration();
117 $this->assertEqual($effect_configuration['data'], ['crop_type' => $edit['id']], 'Manual crop effect uses correct image style.');
119 // Tests the image URI is extended with shortened hash in case of image
120 // style and corresponding crop existence.
121 $this->doTestFileUriAlter();
123 // Try to access edit form as anonymous user.
124 $this->drupalLogout();
125 $this->drupalGet('admin/config/media/crop/manage/' . $edit['id']);
126 $this->assertResponse(403);
127 $this->drupalLogin($this->adminUser);
129 // Try to create crop type with same machine name.
130 $this->drupalPostForm('admin/config/media/crop/add', $edit, t('Save crop type'));
131 $this->assertText(t('The machine-readable name is already in use. It must be unique.'));
134 $this->drupalGet('admin/config/media/crop');
135 $this->assertLink('Test image style');
136 $this->clickLink(t('Delete'));
137 $this->assertText(t('Are you sure you want to delete the crop type @name?', ['@name' => $edit['label']]));
138 $this->drupalPostForm('admin/config/media/crop/manage/' . $edit['id'] . '/delete', [], t('Delete'));
139 $this->assertRaw(t('The crop type %name has been deleted.', ['%name' => $edit['label']]));
140 $this->assertText(t('No crop types available.'));
144 * Asserts a shortened hash is added to the file URI.
146 * Tests crop_file_url_alter().
148 protected function doTestFileUriAlter() {
149 // Get the test file.
150 file_unmanaged_copy(drupal_get_path('module', 'crop') . '/tests/files/sarajevo.png', PublicStream::basePath());
151 $file_uri = 'public://sarajevo.png';
152 $file = File::create(['uri' => $file_uri, 'status' => FILE_STATUS_PERMANENT]);
155 /** @var \Drupal\crop\CropInterface $crop */
157 'type' => $this->cropType->id(),
158 'entity_id' => $file->id(),
159 'entity_type' => $file->getEntityTypeId(),
160 'uri' => 'public://sarajevo.png',
166 $crop = Crop::create($values);
169 // Build an image style derivative for the file URI.
170 $image_style_uri = $this->testStyle->buildUri($file_uri);
171 // Build an image style URL.
172 $image_style_url = $this->testStyle->buildUrl($image_style_uri);
173 // This triggers crop_file_url_alter().
174 $altered_image_style_url = file_create_url($image_style_url);
176 $shortened_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
177 $this->assertTrue(strpos($altered_image_style_url, $shortened_hash) !== FALSE, 'The image style URL contains a shortened hash.');
179 // Update the crop to assert the hash has changed.
180 $crop->setPosition('80', '80')->save();
181 $old_hash = $shortened_hash;
182 $new_hash = substr(md5(implode($crop->position()) . implode($crop->anchor())), 0, 8);
183 $altered_image_style_url = file_create_url($image_style_url);
184 $this->assertFalse(strpos($altered_image_style_url, $old_hash) !== FALSE, 'The image style URL does not contain the old hash.');
185 $this->assertTrue(strpos($altered_image_style_url, $new_hash) !== FALSE, 'The image style URL contains an updated hash.');
187 // Delete the file and the crop entity associated,
188 // the crop entity are auto cleaned by crop_file_delete().
191 // Check if crop entity are correctly deleted.
192 $this->assertFalse(Crop::cropExists($file_uri), 'The Crop entity are correctly deleted after file delete.');