Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#209] Supported additional entity properties when creating files. #210

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions tests/behat/bootstrap/FeatureContextTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

}
5 changes: 5 additions & 0 deletions tests/behat/features/file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down