Skip to content

Commit

Permalink
Fix regression in FileValidator
Browse files Browse the repository at this point in the history
  • Loading branch information
bizley committed Jul 25, 2024
1 parent 6abe5bf commit dd37f90
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------------------
Expand Down
2 changes: 1 addition & 1 deletion framework/validators/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Check warning on line 212 in framework/validators/FileValidator.php

View check run for this annotation

Codecov / codecov/patch

framework/validators/FileValidator.php#L212

Added line #L212 was not covered by tests
$this->addError($model, $attribute, $this->uploadRequired);

return;
Expand Down
16 changes: 10 additions & 6 deletions tests/framework/validators/FileValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
[
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -422,15 +423,17 @@ 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]);
$m = $this->createModelForAttributeTest();
$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
Expand Down Expand Up @@ -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],
Expand Down

0 comments on commit dd37f90

Please sign in to comment.