Add test to prove mime-type parsing of UploadFile #3735
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While writing a test using an
Illuminate\Http\UploadedFile
with a set MIME type, I encountered an issue where the MIME type does not propagate correctly through the Media Library package.Although it's not directly within the scope of the package to handle
Illuminate\Http\UploadedFile
, I believe it could be useful. As @freekmurze mentioned in #3469, he is open to a PR to support this use case.Current Behavior:
In my test (
Feature/FileAdder/MediaConversions/MediaCollectionTest.php
), I demonstrate the current behavior, which I believe is incorrect. However, I'm uncertain whether this should be addressed in Laravel Medialibrary. The issue arises because when usingUploadedFile::fake()
for testing in Laravel, the generated file is given a random temporary name (e.g., phpzcNPGn). Note that this name on disk does not contain the extension, even if you provide one.Results in something like
/private/var/folders/yb/1b4wqmkj1wx_hzx_jwb38_hc0000gn/T/phpzcNPGn
on disk.The package responsible for determining the MIME type is Symfony’s
MimeTypes.php
, which might (I did not check that) rely on the file extension when it cannot determine the MIME type from the file content. At the point where the MIME type is assigned inMediaCollections/FileAdder.php
, only the file path is available:Given this, it’s impossible to know whether the file is a fake upload, making it challenging to adjust the MIME type resolution.
Workaround:
The current workaround is to avoid using
UploadedFile::fake()
and instead use an actual stub file (similar to the approach used in other tests in this package).Suggestion:
It may be best to document this behavior rather than modifying the package. However, if a PR is considered, it might involve additional checks to detect fake uploads and adjust MIME type resolution accordingly.
TL;DR
Illuminate\Http\UploadedFile
in tests.Update
A workaround using a bit of both works...
By using an actual mp3 and providing that as the content for the
UploadedFile::fake()
works and propagates the correct MIME type. Will comment on the discussion as well.