3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\entity_browser\Element\EntityBrowserElement;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\file\Entity\File;
10 use Drupal\node\Entity\Node;
13 * Tests the image field widget.
15 * @group entity_browser
17 class ImageFieldTest extends EntityBrowserJavascriptTestBase {
20 * Created file entity.
22 * @var \Drupal\file\Entity\File
29 public function setUp() {
32 FieldStorageConfig::create([
33 'field_name' => 'field_image',
35 'entity_type' => 'node',
36 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
40 'field_name' => 'field_image',
41 'entity_type' => 'node',
42 'bundle' => 'article',
45 'file_extensions' => 'jpg',
46 'file_directory' => 'entity-browser-test',
47 'max_resolution' => '40x40',
48 'title_field' => TRUE,
52 file_unmanaged_copy(\Drupal::root() . '/core/modules/simpletest/files/image-test.jpg', 'public://example.jpg');
53 $this->image = File::create([
54 'uri' => 'public://example.jpg',
57 // Register usage for this file to avoid validation erros when referencing
58 // this file on node save.
59 \Drupal::service('file.usage')->add($this->image, 'entity_browser', 'test', '1');
61 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
62 $form_display = $this->container->get('entity_type.manager')
63 ->getStorage('entity_form_display')
64 ->load('node.article.default');
66 $form_display->setComponent('field_image', [
67 'type' => 'entity_browser_file',
69 'entity_browser' => 'test_entity_browser_iframe_view',
71 'field_widget_edit' => FALSE,
72 'field_widget_remove' => TRUE,
73 'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
74 'view_mode' => 'default',
75 'preview_image_style' => 'thumbnail',
82 'link_text' => 'Select images',
84 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
85 $browser = $this->container->get('entity_type.manager')
86 ->getStorage('entity_browser')
87 ->load('test_entity_browser_iframe_view');
88 $browser->setDisplay('iframe');
89 $browser->getDisplay()->setConfiguration($display_config);
91 // These settings should get overridden by our field settings.
93 'upload_location' => 'public://',
94 'extensions' => 'png',
97 'label' => 'Upload images',
100 $browser->setWidgetSelector('tabs');
103 $account = $this->drupalCreateUser([
104 'access test_entity_browser_iframe_view entity browser pages',
105 'create article content',
106 'edit own article content',
109 $this->drupalLogin($account);
113 * Tests basic usage for an image field.
115 public function testImageFieldUsage() {
116 $this->drupalGet('node/add/article');
117 $this->assertSession()->linkExists('Select images');
118 $this->getSession()->getPage()->clickLink('Select images');
119 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
120 $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $this->image->id() . ']');
121 $this->getSession()->getPage()->pressButton('Select entities');
122 $this->getSession()->getPage()->pressButton('Use selected');
123 $this->assertSession()->pageTextContains('example.jpg');
124 // Switch back to the main page.
125 $this->getSession()->switchToIFrame();
126 $this->waitForAjaxToFinish();
127 // Check if the image thumbnail exists.
128 $this->assertSession()->elementExists('xpath', '//*[@data-drupal-selector="edit-field-image-current-' . $this->image->id() . '-display"]');
129 // Test if the image filename is present.
130 $this->assertSession()->pageTextContains('example.jpg');
131 // Test specifying Alt and Title texts and saving the node.
132 $alt_text = 'Test alt text.';
133 $title_text = 'Test title text.';
134 $this->getSession()->getPage()->fillField('field_image[current][1][meta][alt]', $alt_text);
135 $this->getSession()->getPage()->fillField('field_image[current][1][meta][title]', $title_text);
136 $this->getSession()->getPage()->fillField('title[0][value]', 'Node 1');
137 $this->getSession()->getPage()->pressButton('Save');
138 $this->assertSession()->pageTextContains('Article Node 1 has been created.');
139 $node = Node::load(1);
140 $saved_alt = $node->get('field_image')[0]->alt;
141 $this->assertEquals($saved_alt, $alt_text);
142 $saved_title = $node->get('field_image')[0]->title;
143 $this->assertEquals($saved_title, $title_text);
144 // Test the Delete functionality.
145 $this->drupalGet('node/1/edit');
146 $this->assertSession()->buttonExists('Remove');
147 $this->getSession()->getPage()->pressButton('Remove');
148 $this->waitForAjaxToFinish();
149 // Image filename should not be present.
150 $this->assertSession()->pageTextNotContains('example.jpg');
151 $this->assertSession()->linkExists('Select entities');
155 * Tests that settings are passed from the image field to the upload widget.
157 public function testImageFieldSettings() {
158 $root = \Drupal::root();
159 $file_wrong_type = $root . '/core/misc/druplicon.png';
160 $file_too_big = $root . '/core/modules/simpletest/files/image-2.jpg';
161 $file_just_right = $root . '/core/modules/simpletest/files/image-test.jpg';
162 $this->drupalGet('node/add/article');
163 $this->assertSession()->linkExists('Select images');
164 $this->getSession()->getPage()->clickLink('Select images');
165 $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
166 // Switch to the image tab.
167 $this->clickLink('Upload images');
168 // Attempt to upload an invalid image type. The upload widget is configured
169 // to allow png but the field widget is configured to allow jpg, so we
170 // expect the field to override the widget.
171 $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_wrong_type);
172 $this->waitForAjaxToFinish();
173 $this->assertSession()->pageTextContains('Only files with the following extensions are allowed: jpg');
174 $this->assertSession()->pageTextContains('The specified file druplicon.png could not be uploaded');
175 // Upload an image bigger than the field widget's configured max size.
176 $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_too_big);
177 $this->waitForAjaxToFinish();
178 $this->assertSession()->pageTextContains('The image was resized to fit within the maximum allowed dimensions of 40x40 pixels.');
179 // Upload an image that passes validation and finish the upload.
180 $this->getSession()->getPage()->attachFileToField('files[upload][]', $file_just_right);
181 $this->waitForAjaxToFinish();
182 $this->getSession()->getPage()->pressButton('Select files');
183 $this->getSession()->getPage()->pressButton('Use selected');
184 $this->assertSession()->pageTextContains('image-test.jpg');
185 // Check that the file has uploaded to the correct sub-directory.
186 $this->getSession()->switchToIFrame();
187 $this->waitForAjaxToFinish();
188 $entity_id = $this->getSession()->evaluateScript('jQuery("#edit-field-image-wrapper [data-entity-id]").data("entity-id")');
189 $this->assertStringStartsWith('file:', $entity_id);
190 /** @var \Drupal\file\Entity\File $file */
191 $fid = explode(':', $entity_id)[1];
192 $file = File::load($fid);
193 $this->assertContains('entity-browser-test', $file->getFileUri());