07dbe075337f1b2a8775b3a49f2c3358f6ad604a
[yaffs-website] / web / modules / contrib / entity_browser / tests / src / FunctionalJavascript / ImageFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
4
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;
11
12 /**
13  * Tests the image field widget.
14  *
15  * @group entity_browser
16  */
17 class ImageFieldTest extends EntityBrowserJavascriptTestBase {
18
19   /**
20    * Created file entity.
21    *
22    * @var \Drupal\file\Entity\File
23    */
24   protected $image;
25
26   /**
27    * {@inheritdoc}
28    */
29   public function setUp() {
30     parent::setUp();
31
32     FieldStorageConfig::create([
33       'field_name' => 'field_image',
34       'type' => 'image',
35       'entity_type' => 'node',
36       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
37     ])->save();
38
39     FieldConfig::create([
40       'field_name' => 'field_image',
41       'entity_type' => 'node',
42       'bundle' => 'article',
43       'label' => 'Images',
44       'settings' => [
45         'file_extensions' => 'jpg',
46         'title_field' => TRUE,
47       ],
48     ])->save();
49
50     file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
51     $this->image = File::create([
52       'uri' => 'public://example.jpg',
53     ]);
54     $this->image->save();
55     // Register usage for this file to avoid validation erros when referencing
56     // this file on node save.
57     \Drupal::service('file.usage')->add($this->image, 'entity_browser', 'test', '1');
58
59     /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $form_display */
60     $form_display = $this->container->get('entity_type.manager')
61       ->getStorage('entity_form_display')
62       ->load('node.article.default');
63
64     $form_display->setComponent('field_image', [
65       'type' => 'entity_browser_file',
66       'settings' => [
67         'entity_browser' => 'test_entity_browser_iframe_view',
68         'open' => TRUE,
69         'field_widget_edit' => FALSE,
70         'field_widget_remove' => TRUE,
71         'selection_mode' => EntityBrowserElement::SELECTION_MODE_APPEND,
72         'view_mode' => 'default',
73         'preview_image_style' => 'thumbnail',
74       ],
75     ])->save();
76
77     $display_config = [
78       'width' => '650',
79       'height' => '500',
80       'link_text' => 'Select images',
81     ];
82     /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
83     $browser = $this->container->get('entity_type.manager')
84       ->getStorage('entity_browser')
85       ->load('test_entity_browser_iframe_view');
86     $browser->setDisplay('iframe');
87     $browser->getDisplay()->setConfiguration($display_config);
88     $browser->save();
89
90     $account = $this->drupalCreateUser([
91       'access test_entity_browser_iframe_view entity browser pages',
92       'create article content',
93       'edit own article content',
94       'access content',
95     ]);
96     $this->drupalLogin($account);
97   }
98
99   /**
100    * Tests basic usage for an image field.
101    */
102   public function testImageFieldUsage() {
103
104     $this->drupalGet('node/add/article');
105     $this->assertSession()->linkExists('Select images');
106     $this->getSession()->getPage()->clickLink('Select images');
107     $this->getSession()->switchToIFrame('entity_browser_iframe_test_entity_browser_iframe_view');
108     $this->getSession()->getPage()->checkField('entity_browser_select[file:' . $this->image->id() . ']');
109     $this->getSession()->getPage()->pressButton('Select entities');
110     $this->getSession()->getPage()->pressButton('Use selected');
111     $this->assertSession()->pageTextContains('example.jpg');
112     // Switch back to the main page.
113     $this->getSession()->switchToIFrame();
114     $this->waitForAjaxToFinish();
115     // Check if the image thumbnail exists.
116     $this->assertSession()->elementExists('xpath', '//*[@data-drupal-selector="edit-field-image-current-' . $this->image->id() . '-display"]');
117     // Test if the image filename is present.
118     $this->assertSession()->pageTextContains('example.jpg');
119     // Test specifying Alt and Title texts and saving the node.
120     $alt_text = 'Test alt text.';
121     $title_text = 'Test title text.';
122     $this->getSession()->getPage()->fillField('field_image[current][1][meta][alt]', $alt_text);
123     $this->getSession()->getPage()->fillField('field_image[current][1][meta][title]', $title_text);
124     $this->getSession()->getPage()->fillField('title[0][value]', 'Node 1');
125     $this->getSession()->getPage()->pressButton('Save');
126     $this->assertSession()->pageTextContains('Article Node 1 has been created.');
127     $node = Node::load(1);
128     $saved_alt = $node->get('field_image')[0]->alt;
129     $this->assertEquals($saved_alt, $alt_text);
130     $saved_title = $node->get('field_image')[0]->title;
131     $this->assertEquals($saved_title, $title_text);
132     // Test the Delete functionality.
133     $this->drupalGet('node/1/edit');
134     $this->assertSession()->buttonExists('Remove');
135     $this->getSession()->getPage()->pressButton('Remove');
136     $this->waitForAjaxToFinish();
137     // Image filename should not be present.
138     $this->assertSession()->pageTextNotContains('example.jpg');
139     $this->assertSession()->linkExists('Select entities');
140   }
141
142 }