diff -u b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php --- b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php @@ -3,8 +3,7 @@ namespace Drupal\FunctionalJavascriptTests; use Behat\Mink\Driver\Selenium2Driver; -use Behat\Mink\Exception\DriverException; -use WebDriver\Exception\UnknownError; +use Drupal\Tests\Traits\Core\SeleniumUploadRemoteFileTrait; use WebDriver\ServiceFactory; /** @@ -12,6 +11,8 @@ */ class DrupalSelenium2Driver extends Selenium2Driver { + use SeleniumUploadRemoteFileTrait; + /** * {@inheritdoc} */ @@ -45,64 +46,2 @@ - /** - * Uploads a file to the Selenium instance and returns the remote path. - * - * See \Behat\Mink\Driver\Selenium2Driver::uploadFile(). - * - * @param string $path - * The path to the file to upload. - * - * @return string - * The remote path. - * - * @throws \Behat\Mink\Exception\DriverException - * When PHP is compiled without zip support, or the file doesn't exist. - * @throws \WebDriver\Exception\UnknownError; - * When an unknown error occurred during file upload. - * @throws \Exception - * When a known error occurred during file upload. - */ - public function getRemoteFilePath($path) { - if (!is_file($path)) { - throw new DriverException('File does not exist locally and cannot be uploaded to the remote instance.'); - } - - if (!class_exists('ZipArchive')) { - throw new DriverException('Could not compress file, PHP is compiled without zip support.'); - } - - // Selenium only accepts uploads that are compressed as a Zip archive. - $tempFilename = tempnam('', 'WebDriverZip'); - - $archive = new \ZipArchive(); - $result = $archive->open($tempFilename, \ZipArchive::CREATE); - if (!$result) { - throw new DriverException('Zip archive could not be created. Error ' . $result); - } - $result = $archive->addFile($path, basename($path)); - if (!$result) { - throw new DriverException('File could not be added to zip archive.'); - } - $result = $archive->close(); - if (!$result) { - throw new DriverException('Zip archive could not be closed.'); - } - - try { - $remotePath = $this->getWebDriverSession()->file(['file' => base64_encode(file_get_contents($tempFilename))]); - - // If no path is returned the file upload failed silently. - if (empty($remotePath)) { - throw new UnknownError; - } - } - catch (\Exception $e) { - throw $e; - } - finally { - unlink($tempFilename); - } - - return $remotePath; - } - } diff -u b/core/tests/Drupal/FunctionalJavascriptTests/Tests/DrupalSelenium2DriverTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Tests/DrupalSelenium2DriverTest.php --- b/core/tests/Drupal/FunctionalJavascriptTests/Tests/DrupalSelenium2DriverTest.php +++ b/core/tests/Drupal/FunctionalJavascriptTests/Tests/DrupalSelenium2DriverTest.php @@ -27,27 +27,11 @@ protected static $modules = ['file', 'field_ui', 'entity_test']; /** - * WebDriver. - * - * @var \Drupal\FunctionalJavascriptTests\DrupalSelenium2Driver - */ - protected $webDriver; - - /** - * The file system service. - * - * @var \Drupal\Core\File\FileSystem - */ - protected $fileSystem; - - /** * {@inheritdoc} */ protected function setUp() { parent::setUp(); - $this->webDriver = $this->getSession()->getDriver(); - $this->fileSystem = \Drupal::service('file_system'); - $storage_settings = ['cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED]; + $storage_settings = ['cardinality' => 3]; $this->createFileField('field_file', 'entity_test', 'entity_test', $storage_settings); $this->drupalLogin($this->drupalCreateUser([ 'administer entity_test content', @@ -57,8 +41,6 @@ /** * Tests uploading remote files. - * - * @covers ::getRemoteFilePath */ public function testGetRemoteFilePath() { $entity = EntityTest::create(); @@ -67,14 +49,14 @@ $files = array_slice($this->getTestFiles('text'), 0, 3); $real_paths = []; foreach ($files as $file) { - $real_paths[] = $this->fileSystem->realpath($file->uri); + $real_paths[] = \Drupal::service('file_system')->realpath($file->uri); } $remote_paths = []; foreach ($real_paths as $path) { - $remote_paths[] = $this->webDriver->getRemoteFilePath($path); + $remote_paths[] = $this->getSession()->getDriver()->uploadFileAndGetRemoteFilePath($path); } - // Tests that uploading remote files works with remote path. + // Tests that uploading multiple remote files works with remote path. $this->drupalGet($entity->toUrl('edit-form')); $multiple_field = $this->xpath('//input[@multiple]')[0]; $multiple_field->setValue(implode("\n", $remote_paths)); @@ -82,23 +64,6 @@ $this->getSession()->getPage()->findButton('Save')->click(); $entity = EntityTest::load($entity->id()); $this->assertCount(3, $entity->field_file); - - // Tests that upload remote files doesn't works without remote path. - $entity = EntityTest::create(); - $entity->save(); - $this->drupalGet($entity->toUrl('edit-form')); - $multiple_field = $this->xpath('//input[@multiple]')[0]; - try { - $multiple_field->setValue(implode("\n", $real_paths)); - $this->assertSession()->assertWaitOnAjaxRequest(); - $this->getSession()->getPage()->findButton('Save')->click(); - $entity = EntityTest::load($entity->id()); - $this->assertCount(0, $entity->field_file); - $this->fail('setValue() works with real path without problem! Can we remove our workaround with getRemoteFilePath() method?'); - } - catch (\Exception $e) { - $this->assertContains('File not found', $e->getMessage()); - } } } only in patch2: unchanged: --- /dev/null +++ b/core/tests/Drupal/Tests/Traits/Core/SeleniumUploadRemoteFileTrait.php @@ -0,0 +1,78 @@ +open($tempFilename, \ZipArchive::CREATE); + if (!$result) { + throw new DriverException('Zip archive could not be created. Error ' . $result); + } + $result = $archive->addFile($path, basename($path)); + if (!$result) { + throw new DriverException('File could not be added to zip archive.'); + } + $result = $archive->close(); + if (!$result) { + throw new DriverException('Zip archive could not be closed.'); + } + + try { + $remotePath = $this->getWebDriverSession()->file(['file' => base64_encode(file_get_contents($tempFilename))]); + + // If no path is returned the file upload failed silently. + if (empty($remotePath)) { + throw new UnknownError; + } + } + catch (\Exception $e) { + throw $e; + } + finally { + unlink($tempFilename); + } + + return $remotePath; + } + +}