3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Drupal\file\Entity\File;
8 * Entity Browser views widget tests.
10 * @group entity_browser
11 * @see \Drupal\entity_browser\Plugin\EntityBrowser\Widget\View
13 class EntityBrowserViewsWidgetTest extends EntityBrowserJavascriptTestBase {
20 public static $modules = [
23 'entity_browser_test',
29 protected function setUp() {
32 $user = $this->drupalCreateUser([
33 'access test_entity_browser_file entity browser pages',
35 $this->drupalLogin($user);
39 * Tests Entity Browser views widget.
41 public function testViewsWidget() {
42 // Create a file so that our test View isn't empty.
43 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example.jpg');
44 /** @var \Drupal\file\FileInterface $file */
45 $file = File::create([
46 'uri' => 'public://example.jpg',
50 // Visit a test entity browser page that defaults to using a View widget.
51 $this->drupalGet('/entity-browser/iframe/test_entity_browser_file');
52 $field = 'entity_browser_select[file:' . $file->id() . ']';
54 // Test exposed filters.
55 $this->assertSession()->pageTextContains('example.jpg');
56 $this->assertSession()->fieldExists($field);
57 $this->getSession()->getPage()->fillField('filename', 'llama');
58 $this->getSession()->getPage()->pressButton('Apply');
59 $this->waitForAjaxToFinish();
60 $this->assertSession()->fieldNotExists($field);
61 $this->assertSession()->pageTextNotContains('example.jpg');
62 $this->getSession()->getPage()->fillField('filename', 'example');
63 $this->getSession()->getPage()->pressButton('Apply');
64 $this->waitForAjaxToFinish();
65 $this->assertSession()->pageTextContains('example.jpg');
66 $this->assertSession()->fieldExists($field);
71 ], t('Select entities'));
72 $this->assertSession()->pageTextContains($file->getFilename());
74 // Create another file to test bulk select form.
75 file_unmanaged_copy(\Drupal::root() . '/core/misc/druplicon.png', 'public://example_1.jpg');
76 /** @var \Drupal\file\FileInterface $file */
77 $new_file = File::create([
78 'uri' => 'public://example_1.jpg',
81 // Visit entity browser test page again.
82 $this->drupalGet('/entity-browser/iframe/test_entity_browser_file');
83 $new_field = 'entity_browser_select[file:' . $new_file->id() . ']';
84 // Assert both checkbox fields are there.
85 $check_old = $this->assertSession()->fieldExists($field);
86 $check_new = $this->assertSession()->fieldExists($new_field);
87 // Compare value attributes of checkboxes and assert they not equal.
88 $this->assertNotEquals($check_old->getAttribute('value'), $check_new->getAttribute('value'));
90 $uuid = \Drupal::service('uuid')->generate();
91 \Drupal::service('entity_browser.selection_storage')->setWithExpire(
93 ['validators' => ['cardinality' => ['cardinality' => 1]]],
96 $this->drupalGet('/entity-browser/iframe/test_entity_browser_file', ['query' => ['uuid' => $uuid]]);
97 $this->getSession()->getPage()->fillField('entity_browser_select[file:1]', TRUE);
98 $this->getSession()->getPage()->fillField('entity_browser_select[file:2]', TRUE);
99 $this->getSession()->getPage()->pressButton('Select entities');
101 $this->assertSession()->pageTextContains('You can not select more than 1 entity.');
102 $this->assertSession()->checkboxNotChecked('entity_browser_select[file:1]');
103 $this->assertSession()->checkboxNotChecked('entity_browser_select[file:2]');