Skip to content

Commit

Permalink
Merge pull request #168 from Metadrop/feature/context-savedata
Browse files Browse the repository at this point in the history
Issue #147: Deprecated function file_save_data
  • Loading branch information
omarlopesino authored Sep 29, 2023
2 parents 7f7a41b + ac33e8a commit b15733e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/Behat/Cores/Drupal7.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Metadrop\Behat\Cores;

use Drupal\Core\File\FileSystemInterface;
use Metadrop\Behat\Cores\Traits\EntityTrait;
use NuvoleWeb\Drupal\Driver\Cores\Drupal7 as OriginalDrupal7;
use Metadrop\Behat\Cores\Traits\UsersTrait;
Expand Down Expand Up @@ -377,4 +378,11 @@ public function getLanguagePrefix($language) {
throw new PendingException('Pending to implement method in Drupal 7');
}

/**
* {@inheritdoc}
*/
public function fileSaveData(string $data, $destination = NULL, int $replace = FileSystemInterface::EXISTS_RENAME) {
return file_save_data($data, $destination, $replace);
}

}
29 changes: 29 additions & 0 deletions src/Behat/Cores/Drupal8.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Metadrop\Behat\Cores;

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Drupal\file\FileInterface;
Expand Down Expand Up @@ -30,6 +31,27 @@ class Drupal8 extends OriginalDrupal8 implements CoreInterface {
use EntityTrait;
use StringTranslationTrait;

/**
* The File Repository service.
*
* @var \Drupal\file\FileRepositoryInterface
*/
protected $fileRepository;

/**
* Gets the file repository service.
*
* @return \Drupal\file\FileRepositoryInterface
* The file repository service.
*/
protected function getFileRepository() {
if (!$this->fileRepository) {
$this->fileRepository = \Drupal::service('file.repository');
}

return $this->fileRepository;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -542,4 +564,11 @@ public function getLanguagePrefix($language) {
return $prefixes[array_key_first($found)];
}

/**
* {@inheritdoc}
*/
public function fileSaveData(string $data, $destination = NULL, int $replace = FileSystemInterface::EXISTS_RENAME) {
return $this->getFileRepository()->writeData($data, $destination, $replace);
}

}
30 changes: 29 additions & 1 deletion src/Behat/Cores/Traits/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function createFileWithName($file_path, $directory = NULL) {
$data = file_get_contents($file_path);

// Existing files are replaced.
$file = file_save_data($data, $destination, 1);
$file = $this->fileSaveData($data, $destination, 1);
if (!$file) {
throw new \Exception("Error: file could not be copied to directory");
}
Expand All @@ -48,4 +48,32 @@ public function createFileWithName($file_path, $directory = NULL) {
*/
abstract public function getDefaultFileScheme();

/**
* Saves a file to the specified destination and creates a database entry.
*
* @param string $data
* A string containing the contents of the file.
* @param string|null $destination
* (optional) A string containing the destination URI. This must be a stream
* wrapper URI. If no value or NULL is provided, a randomized name will be
* generated and the file will be saved using Drupal's default files scheme,
* usually "public://".
* @param int $replace
* (optional) The replace behavior when the destination file already exists.
* Possible values include:
* - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a
* managed file with the destination name exists, then its database entry
* will be updated. If no database entry is found, then a new one will be
* created.
* - FileSystemInterface::EXISTS_RENAME: (default) Append
* _{incrementing number} until the filename is unique.
* - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE.
*
* @return \Drupal\file\FileInterface|false
* A file entity, or FALSE on error.
*
* @see \Drupal\Core\File\FileSystemInterface::saveData()
*/
abstract public function fileSaveData(string $data, $destination = NULL, int $replace = FileSystemInterface::EXISTS_RENAME);

}

0 comments on commit b15733e

Please sign in to comment.