Skip to content

Commit

Permalink
Test handle method returns false on invalid resolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
unclexo committed Jan 28, 2023
1 parent 3a99822 commit ace2fc6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
13 changes: 12 additions & 1 deletion app/Jobs/ImageUploadAndResizingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function storage()
*/
public function handle()
{
if (! $this->isValidImage())
if (! $this->isFilteredResolutions() || ! $this->isValidImage())
return false;

$path = sprintf(
Expand All @@ -82,6 +82,17 @@ public function handle()
return $paths;
}

private function isFilteredResolutions()
{
return array_filter((array) $this->resolutions, function ($value, $key) {
return is_array($value) &&
count($value) === 2 &&
is_string($key) &&
$value[0] > 0 &&
$value[1] > 0;
}, ARRAY_FILTER_USE_BOTH);
}

private function isValidImage()
{
if (
Expand Down
23 changes: 23 additions & 0 deletions tests/Feature/JobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,27 @@ public function handle_method_returns_false_on_invalid_image_content()

$this->assertFalse($job->handle());
}

/** @test */
public function handle_method_returns_false_on_invalid_resolutions()
{
Storage::fake('public');

$image = UploadedFile::fake()
->image('image.jpg', 50, 50)
->mimeType('image/jpeg');

$job = new ImageUploadAndResizingJob($image->getMimeType(), base64_encode($image->getContent()));

$job->resolutions = [];

$this->assertFalse($job->handle());

$job->resolutions = [
'50x50' => [-50, 50], // Note the negative number
'640x480' => [640], // Note the height is not provided
];

$this->assertFalse($job->handle());
}
}

0 comments on commit ace2fc6

Please sign in to comment.