-
Notifications
You must be signed in to change notification settings - Fork 62
Description
Detailed description
Images are not uploaded automatically when some field in $imageFields has no options.
Context
For exemple:
class User extends Model {
use HasImageUploads;
protected static $imageFields = [
'cover', 'avatar'
];
}
There are no tests covering the automatic upload of images without options...
In method AutoUpload():
laravel-imageup/src/HasImageUploads.php
Lines 467 to 486 in fbf456f
protected function autoUpload() | |
{ | |
foreach ($this->getDefinedImageFields() as $field => $options) { | |
// check if global upload is allowed, then in override in option | |
$autoUploadAllowed = array_get($options, 'auto_upload', $this->canAutoUploadImages()); | |
if (is_array($options) && count($options) && $autoUploadAllowed) { | |
// get the input file name | |
$requestFileName = array_get($options, 'file_input', $field); | |
// if request has the file upload it | |
if (request()->hasFile($requestFileName)) { | |
$this->uploadImage( | |
request()->file($requestFileName), | |
$field | |
); | |
} | |
} | |
} | |
} |
Following the example, in the first foreach interaction, $field is 0 and $options has the value of "cover"
Note the condition of line 473! This image will never be sent! Because $options is string and not array.
These methods also have a bug:
- getImageFieldOptions()
- getImageFieldName()
If some field in $imageFields has no options, some tests will fail ... For example:
laravel-imageup/tests/ImageUpTest.php
Lines 132 to 143 in fbf456f
function it_returns_field_name_of_first_field() | |
{ | |
$user = new User(); | |
$fieldOption = [ | |
'avatar' => ['width' => 200], | |
'logo' => ['width' => 400, 'height' => 400] | |
]; | |
$user->setImagesField($fieldOption); | |
$this->assertEquals('avatar', $user->getImageFieldName()); | |
$this->assertEquals('logo', $user->getImageFieldName('logo')); | |
} |
If you replace the line value 136 from "'avatar' => ['width' => 200]" to '''avatar'''. The test will fail because the getImageFieldName() method will return 0 and not 'avatar'...
Possible implementation
I'm working to fix this. 😉
My environment
- Laradock (PHP 7.2):
- Operating System: Ubuntu 18.04