3 namespace Drupal\Tests\entity_browser\FunctionalJavascript;
5 use Drupal\user\Entity\Role;
8 * Tests the Upload Widget.
10 * @group entity_browser
12 class UploadWidgetTest extends EntityBrowserJavascriptTestBase {
17 public function setUp() {
20 // Grant permission to this user to use also the EB page we are testing.
21 /** @var \Drupal\user\RoleInterface $role */
22 $role = Role::load('authenticated');
23 $this->grantPermissions($role, ['access test_entity_browser_standalone_upload entity browser pages']);
28 * Tests Entity Browser upload widget.
30 public function testUploadWidget() {
32 /** @var \Drupal\entity_browser\EntityBrowserInterface $browser */
33 $browser = $this->container->get('entity_type.manager')
34 ->getStorage('entity_browser')
35 ->load('test_entity_browser_standalone_upload');
37 $page = $this->getSession()->getPage();
39 // Make sure the test file is not present beforehand.
40 $this->assertFileNotExists('public://druplicon.png');
42 // Go to the widget standalone page and test the upload.
43 $this->drupalGet($browser->getDisplay()->path());
44 $page->attachFileToField('edit-upload-upload', \Drupal::root() . '/core/misc/druplicon.png');
45 $this->waitForAjaxToFinish();
46 $this->assertSession()->fieldExists('druplicon.png');
47 $page->pressButton('Select files');
48 $this->assertSession()->statusCodeEquals(200);
50 // Check if the file was correctly uploaded to the EB destination.
51 $this->assertFileExists('public://druplicon.png');
53 // Now change upload location and submit label and check again.
54 $widget = $browser->getWidget('2dc1ab07-2f8f-42c9-aab7-7eef7f8b7d87');
55 $config = $widget->getConfiguration();
56 $config['settings']['upload_location'] = 'public://some_location';
57 $config['settings']['submit_text'] = 'Fancy submit';
58 $widget->setConfiguration($config);
61 $this->drupalGet($browser->getDisplay()->path());
62 $page->attachFileToField('edit-upload-upload', \Drupal::root() . '/core/misc/druplicon.png');
63 $this->waitForAjaxToFinish();
64 $this->assertSession()->fieldExists('druplicon.png');
65 $page->pressButton('Fancy submit');
66 $this->assertSession()->statusCodeEquals(200);
68 // Check if the file was correctly uploaded to the EB destination.
69 $this->assertFileExists('public://some_location/druplicon.png');