3 namespace Drupal\image_widget_crop\Tests;
5 use Drupal\node\Entity\Node;
6 use Drupal\simpletest\WebTestBase;
9 * Minimal test case for the image_widget_crop module.
11 * @group image_widget_crop
15 class ImageWidgetCropTest extends WebTestBase {
18 * User with permissions to create content.
20 * @var \Drupal\user\Entity\User
29 public static $modules = [
37 * Prepares environment for the tests.
39 protected function setUp() {
42 $this->drupalCreateContentType(['name' => 'Crop test', 'type' => 'crop_test']);
44 $this->user = $this->createUser([
45 'access content overview',
46 'administer content types',
47 'edit any crop_test content',
49 $this->drupalLogin($this->user);
53 * Test Image Widget Crop UI.
55 public function testCropUi() {
56 // Test that when a crop has more than one usage we have a warning.
57 $this->createImageField('field_image_crop_test', 'crop_test', 'image_widget_crop', [], [], ['crop_list' => ['crop_16_9' => 'crop_16_9'], 'crop_types_required' => []]);
58 $this->drupalGetTestFiles('image');
60 $this->drupalGet('node/add/crop_test');
62 'title[0][value]' => $this->randomMachineName(),
63 'files[field_image_crop_test_0]' => \Drupal::service('file_system')->realpath('public://image-test.jpg'),
65 $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
67 $node = Node::create([
68 'title' => '2nd node using it',
69 'type' => 'crop_test',
70 'field_image_crop_test' => 1,
71 'alt' => $this->randomMachineName(),
75 /** @var \Drupal\file\FileUsage\FileUsageInterface $usage */
76 $usage = \Drupal::service('file.usage');
77 $usage->add(\Drupal::service('entity_type.manager')->getStorage('file')->load(1), 'image_widget_crop', 'node', $node->id());
79 $this->drupalGet('node/1/edit');
81 $this->assertRaw(t('This crop definition affects more usages of this image'));
86 * Test Image Widget Crop.
88 public function testImageWidgetCrop() {
89 // Test that crop widget works properly.
90 $this->createImageField('field_image_crop_test', 'crop_test', 'image_widget_crop', [], [], ['crop_list' => ['crop_16_9' => 'crop_16_9'], 'crop_types_required' => []]);
91 $this->drupalGetTestFiles('image');
93 $this->drupalGet('node/add/crop_test');
95 // Assert that there is no crop widget, neither 'Alternative text' text
96 // filed nor 'Remove' button yet.
97 $raw = '<summary role="button" aria-controls="edit-field-image-crop-test-0-image-crop-crop-wrapper" aria-expanded="false" aria-pressed="false">Crop image</summary>';
98 $this->assertNoRaw($raw);
99 $this->assertNoText('Alternative text');
100 $this->assertNoFieldByName('field_image_crop_test_0_remove_button');
103 // Upload an image in field_image_crop_test_0.
104 $image['files[field_image_crop_test_0]'] = $this->container->get('file_system')->realpath('public://image-test.jpg');
105 $this->drupalPostAjaxForm(NULL, $image, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
107 // Assert that now crop widget and 'Alternative text' text field appear and
108 // that 'Remove' button exists.
109 $this->assertRaw($raw);
110 $this->assertText('Alternative text');
111 $this->assertFieldByName('field_image_crop_test_0_remove_button');
113 // Set title and 'Alternative text' text field and save.
114 $title = $this->randomMachineName();
116 'title[0][value]' => $title,
117 'field_image_crop_test[0][alt]' => $this->randomMachineName(),
119 $this->drupalPostForm(NULL, $edit, 'Save');
120 $this->assertText('Crop test ' . $title . ' has been created.');
121 $url = $this->getUrl();
122 $nid = substr($url, -1, strrpos($url, '/'));
125 $this->drupalGet('node/' . $nid . '/edit');
127 // Verify that the 'Remove' button works properly.
128 $this->assertText('Alternative text');
129 $this->drupalPostForm(NULL, NULL, 'Remove');
130 $this->assertNoText('Alternative text');
132 // Re-upload the image and set the 'Alternative text'.
133 $this->drupalPostAjaxForm(NULL, $image, $this->getButtonName('//input[@type="submit" and @value="Upload" and @data-drupal-selector="edit-field-image-crop-test-0-upload-button"]'));
135 // Verify that the 'Preview' button works properly.
136 $this->drupalPostForm(NULL, $edit, 'Preview');
137 $this->assertLink('Back to content editing');
138 $this->clickLink('Back to content editing');
140 // Verify that there is an image style preview.
141 $this->assertFieldByName('field_image_crop_test[0][width]', '40');
142 $this->assertFieldByName('field_image_crop_test[0][height]', '20');
147 * Gets IEF button name.
149 * @param string $xpath
150 * Xpath of the button.
153 * The name of the button.
155 protected function getButtonName($xpath) {
158 /** @var \SimpleXMLElement[] $elements */
159 if ($elements = $this->xpath($xpath)) {
160 foreach ($elements[0]->attributes() as $name => $value) {
161 if ($name == 'name') {
162 $retval = (string) $value;
171 * Create a new image field.
173 * @param string $name
174 * The name of the new field (all lowercase), exclude the "field_" prefix.
175 * @param string $type_name
176 * The node type that this field will be added to.
177 * @param string $widget_name
178 * The name of the widget.
179 * @param array $storage_settings
180 * A list of field storage settings that will be added to the defaults.
181 * @param array $field_settings
182 * A list of instance settings that will be added to the instance defaults.
183 * @param array $widget_settings
184 * A list of widget settings that will be added to the widget defaults.
186 protected function createImageField($name, $type_name, $widget_name, array $storage_settings = [], array $field_settings = [], array $widget_settings = []) {
187 \Drupal::entityTypeManager()->getStorage('field_storage_config')->create([
188 'field_name' => $name,
189 'entity_type' => 'node',
191 'settings' => $storage_settings,
192 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
195 $field_config = \Drupal::entityTypeManager()->getStorage('field_config')->create([
196 'field_name' => $name,
198 'entity_type' => 'node',
199 'bundle' => $type_name,
200 'required' => !empty($field_settings['required']),
201 'settings' => $field_settings,
203 $field_config->save();
205 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
206 $form_display = \Drupal::entityTypeManager()->getStorage('entity_form_display')->load('node.' . $type_name . '.default');
207 $form_display->setComponent($name, [
208 'type' => $widget_name,
209 'settings' => $widget_settings,
212 /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
213 $view_display = \Drupal::entityTypeManager()->getStorage('entity_view_display')->load('node.' . $type_name . '.default');
214 $view_display->setComponent($name)