Skip to content

Commit

Permalink
[#127] Fixed inconsistent format on "Given no managed files" step to …
Browse files Browse the repository at this point in the history
…support custom field name.
  • Loading branch information
tannguyen04 authored Feb 5, 2024
1 parent 1a350ea commit 935f3b6
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,11 @@ public function fileCleanAll(AfterScenarioScope $scope): void {
}

/**
* Delete managed files defined by provided properties.
* Delete managed files defined by provided properties/fields.
*
* Example: filename, uri, status, uid and more.
*
* @see Drupal\file\Entity\File
*
* @code
* Given no managed files:
Expand All @@ -145,15 +149,22 @@ public function fileCleanAll(AfterScenarioScope $scope): void {
* | otherfile.jpg |
* @endcode
*
* @code
* Given no managed files:
* | uri |
* | public://myfile.jpg |
* | public://otherfile.jpg |
* @endcode
*
* @Given no managed files:
*/
public function fileDeleteManagedFiles(TableNode $nodesTable): void {
$storage = \Drupal::entityTypeManager()->getStorage('file');
$filenames = $nodesTable->getColumn(0);
// Get rid of the column header.
array_shift($filenames);
foreach ($filenames as $filename) {
$ids = $this->fileLoadMultiple(['filename' => $filename]);
$field_values = $nodesTable->getColumn(0);
// Get field name of the column header.
$field_name = array_shift($field_values);
foreach ($field_values as $field_value) {
$ids = $this->fileLoadMultiple([$field_name => $field_value]);
$entities = $storage->loadMultiple($ids);
$storage->delete($entities);
}
Expand Down
58 changes: 57 additions & 1 deletion tests/behat/features/file.feature
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Feature: Check that FileTrait works for or D9
And "example_audio.mp3" file object exists

@api
Scenario: Assert "@Given no managed files:"
Scenario: Assert "@Given no managed files: With filename"
When I am logged in as a user with the "administrator" role
Given managed file:
| path |
Expand All @@ -25,14 +25,70 @@ Feature: Check that FileTrait works for or D9
And "example_image.png" file object exists
And "example_audio.mp3" file object exists
Given no managed files:
| filename |
| example_document.pdf |
| example_image.png |
| example_audio.mp3 |
Then no "example_document.pdf" file object exists
And no "example_image.png" file object exists
And no "example_audio.mp3" file object exists

@api
Scenario: Assert "@Given no managed files: With uri"
When I am logged in as a user with the "administrator" role
Given managed file:
| path |
| example_document.pdf |
| example_image.png |
| example_audio.mp3 |
And "example_document.pdf" file object exists
And "example_image.png" file object exists
And "example_audio.mp3" file object exists
Given no managed files:
| uri |
| public://example_document.pdf |
| public://example_image.png |
| public://example_audio.mp3 |
Then no "example_document.pdf" file object exists
And no "example_image.png" file object exists
And no "example_audio.mp3" file object exists

@api
Scenario: Assert "@Given no managed files: With status"
When I am logged in as a user with the "administrator" role
Given managed file:
| path |
| example_document.pdf |
| example_image.png |
| example_audio.mp3 |
And "example_document.pdf" file object exists
And "example_image.png" file object exists
And "example_audio.mp3" file object exists
Given no managed files:
| status |
| 1 |
Then no "example_document.pdf" file object exists
And no "example_image.png" file object exists
And no "example_audio.mp3" file object exists

@api
Scenario: Assert "@Given no managed files: With filemime"
When I am logged in as a user with the "administrator" role
Given managed file:
| path |
| example_document.pdf |
| example_image.png |
| example_audio.mp3 |
And "example_document.pdf" file object exists
And "example_image.png" file object exists
And "example_audio.mp3" file object exists
Given no managed files:
| filemime |
| image/png |
And "example_document.pdf" file object exists
And no "example_image.png" file object exists
And "example_audio.mp3" file object exists

@api
Scenario: Assert unmanaged files step definitions
Given unmanaged file "public://test1.txt" does not exist
Expand Down

0 comments on commit 935f3b6

Please sign in to comment.