diff --git a/src/FileTrait.php b/src/FileTrait.php index 67589bd7..0c32863c 100644 --- a/src/FileTrait.php +++ b/src/FileTrait.php @@ -110,7 +110,20 @@ protected function fileCreateEntity(\StdClass $stub): FileInterface { } } - return \Drupal::service('file.repository')->writeData(file_get_contents($path), $destination, FileSystemInterface::EXISTS_REPLACE); + $entity = \Drupal::service('file.repository')->writeData(file_get_contents($path), $destination, FileSystemInterface::EXISTS_REPLACE); + $fields = get_object_vars($stub); + + foreach ($fields as $property => $value) { + // If path or URI has been specified then the value has already been + // handled. + if (in_array($property, ['path', 'uri'])) { + continue; + } + $entity->set($property, $value); + } + + $entity->save(); + return $entity; } /** diff --git a/tests/behat/bootstrap/FeatureContextTrait.php b/tests/behat/bootstrap/FeatureContextTrait.php index a27a3c28..c7c631b5 100644 --- a/tests/behat/bootstrap/FeatureContextTrait.php +++ b/tests/behat/bootstrap/FeatureContextTrait.php @@ -198,4 +198,15 @@ public function noFileObjectExist($file_name) { } } + /** + * @Then :entity_type entity exists with UUID :uuid + */ + public function entityExistsByUuid($entity_type, $uuid) { + $entity = \Drupal::service('entity.repository')->loadEntityByUuid($entity_type, $uuid); + + if (!$entity) { + throw new \Exception(sprintf('Entity of type "%s" does not exist in DB with UUID "%s", but it should', $entity_type, $uuid)); + } + } + } diff --git a/tests/behat/features/file.feature b/tests/behat/features/file.feature index 85923779..1b692cbf 100644 --- a/tests/behat/features/file.feature +++ b/tests/behat/features/file.feature @@ -9,9 +9,14 @@ Feature: Check that FileTrait works for or D9 | example_document.pdf | | example_image.png | | example_audio.mp3 | + And managed file: + | uuid | path | + | 9cb1b484-db7b-4496-bd63-8c702e207704 | example_text.txt | And "example_document.pdf" file object exists And "example_image.png" file object exists And "example_audio.mp3" file object exists + And "example_text.txt" file object exists + And "file" entity exists with UUID "9cb1b484-db7b-4496-bd63-8c702e207704" @api Scenario: Assert "@Given no managed files: With filename"