From 0d0f77529afa85563145a9766a0a8915933c0a32 Mon Sep 17 00:00:00 2001 From: Bizley Date: Fri, 26 Jul 2024 18:06:32 +0200 Subject: [PATCH] Fix #20231: Fix regression introduced in #20167 in `yii\validators\FileValidator` --- framework/CHANGELOG.md | 1 + framework/validators/FileValidator.php | 2 +- tests/framework/validators/FileValidatorTest.php | 16 ++++++++++------ 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0185dbb7835..c100209e75a 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -5,6 +5,7 @@ Yii Framework 2 Change Log ------------------------ - Bug #20232: Fix regression introduced in `GHSA-cjcc-p67m-7qxm` while attaching behavior defined by `__class` array key (erickskrauch) +- Bug #20231: Fix regression introduced in #20167 in `yii\validators\FileValidator` (bizley) 2.0.51 July 18, 2024 -------------------- diff --git a/framework/validators/FileValidator.php b/framework/validators/FileValidator.php index 758c8242e2e..28a2df0fa32 100644 --- a/framework/validators/FileValidator.php +++ b/framework/validators/FileValidator.php @@ -209,7 +209,7 @@ public function validateAttribute($model, $attribute) { $files = $this->filterFiles(is_array($model->$attribute) ? $model->$attribute : [$model->$attribute]); $filesCount = count($files); - if ($filesCount === 0 && $this->minFiles > 0) { + if ($filesCount === 0) { $this->addError($model, $attribute, $this->uploadRequired); return; diff --git a/tests/framework/validators/FileValidatorTest.php b/tests/framework/validators/FileValidatorTest.php index 0b42b54967e..9e05edf4d23 100644 --- a/tests/framework/validators/FileValidatorTest.php +++ b/tests/framework/validators/FileValidatorTest.php @@ -115,10 +115,11 @@ public function testValidateAttributeMultiple() ]); $m = FakedValidationModel::createWithAttributes(['attr_files' => 'path']); $val->validateAttribute($m, 'attr_files'); - $this->assertFalse($m->hasErrors('attr_files')); + $this->assertTrue($m->hasErrors('attr_files')); $m = FakedValidationModel::createWithAttributes(['attr_files' => []]); $val->validateAttribute($m, 'attr_files'); - $this->assertFalse($m->hasErrors('attr_files')); + $this->assertTrue($m->hasErrors('attr_files')); + $this->assertSame($val->uploadRequired, current($m->getErrors('attr_files'))); $m = FakedValidationModel::createWithAttributes( [ @@ -334,7 +335,7 @@ public function testValidateArrayAttributeWithMinMaxOneAndOneFile() 'type' => 'image/png', ], ] - )[0]; + )[0]; // <-- only one file $model = FakedValidationModel::createWithAttributes(['attr_images' => [$files]]); $validator->validateAttribute($model, 'attr_images'); @@ -422,7 +423,8 @@ public function testValidateAttribute() $val->validateAttribute($m, 'attr_files'); $this->assertFalse($m->hasErrors()); $val->validateAttribute($m, 'attr_files_empty'); - $this->assertFalse($m->hasErrors('attr_files_empty')); + $this->assertTrue($m->hasErrors('attr_files_empty')); + $this->assertSame($val->uploadRequired, current($m->getErrors('attr_files_empty'))); // single File with skipOnEmpty = false $val = new FileValidator(['skipOnEmpty' => false]); @@ -430,7 +432,8 @@ public function testValidateAttribute() $val->validateAttribute($m, 'attr_files'); $this->assertFalse($m->hasErrors()); $val->validateAttribute($m, 'attr_files_empty'); - $this->assertFalse($m->hasErrors('attr_files_empty')); + $this->assertTrue($m->hasErrors('attr_files_empty')); + $this->assertSame($val->uploadRequired, current($m->getErrors('attr_files_empty'))); $m = $this->createModelForAttributeTest(); // too big @@ -689,7 +692,8 @@ public function testValidateMimeTypeCaseInsensitive($mask, $fileMimeType, $expec $this->assertEquals($expected, $validator->validate($file), sprintf('Mime type validate fail: "%s" / "%s"', $mask, $fileMimeType)); } - public function mimeTypeCaseInsensitive() { + public function mimeTypeCaseInsensitive() + { return [ ['Image/*', 'image/jp2', true], ['image/*', 'Image/jp2', true],