Mercurial > hg > cmmr2012-drupal-site
diff core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php @ 5:12f9dff5fda9 tip
Update to Drupal core 8.7.1
author | Chris Cannam |
---|---|
date | Thu, 09 May 2019 15:34:47 +0100 |
parents | a9cd425dd02b |
children |
line wrap: on
line diff
--- a/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php Thu Feb 28 13:11:55 2019 +0000 +++ b/core/tests/Drupal/FunctionalJavascriptTests/DrupalSelenium2Driver.php Thu May 09 15:34:47 2019 +0100 @@ -3,6 +3,8 @@ namespace Drupal\FunctionalJavascriptTests; use Behat\Mink\Driver\Selenium2Driver; +use Behat\Mink\Exception\DriverException; +use WebDriver\Exception\UnknownError; use WebDriver\ServiceFactory; /** @@ -41,4 +43,69 @@ $this->getWebDriverSession()->setCookie($cookieArray); } + /** + * Uploads a file to the Selenium instance and returns the remote path. + * + * \Behat\Mink\Driver\Selenium2Driver::uploadFile() is a private method so + * that can't be used inside a test, but we need the remote path that is + * generated when uploading to make sure the file reference exists on the + * container running selenium. + * + * @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 uploadFileAndGetRemoteFilePath($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; + } + }