Problem/Motivation

FileValidationConstraintValidator is a wrapper around the file_validate() function.
It currently assumes that it is being used to validate a \Drupal\Core\Field\Plugin\Field\FieldType\EntityReferenceItem.
EntityReferenceItem::getTarget() can return either the typed data object representing the entity being referenced, or NULL if there is no entity.

All EntityReferenceItems also have a constraint of ValidReference which ensures that the entity exists.

If the entity (file) does not exist, the ValidReference validator will set the appropriate violation.
However, the FileValidation constraint will still run even when there is no file.

Proposed resolution

Respect the possible NULL return value of \Drupal\Core\TypedData\DataReferenceInterface::getTarget() before checking the file.
Do not append any additional violations if the file is not found, as the other constraint will handle it.

Remaining tasks

User interface changes

API changes

Data model changes

Comments

tim.plunkett created an issue. See original summary.

tim.plunkett’s picture

Status: Active » Needs review
StatusFileSize
new965 bytes

Would help if I upload the patch.

borisson_’s picture

It looks like there is no dedicated test for the FileValidationConstraintValidator, this seems like the ideal time to add that test? If you disagree and this is out of scope for this issue, it does look very solid.

tim.plunkett’s picture

StatusFileSize
new1.35 KB
new2.29 KB

There is a test class that claims to explicitly cover FileValidationConstraintValidator, but it is not named FileValidationConstraintValidatorTest.
Expanded that coverage.

The last submitted patch, 4: 2956814-file-4-FAIL.patch, failed testing. View results

andypost’s picture

Status: Needs review » Reviewed & tested by the community

Looks great!

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Crediting @borisson_ for asking for a test.

Committed and pushed 37b9e45ac0 to 8.6.x and d58b19b0ac to 8.5.x. Thanks!

  • alexpott committed 37b9e45 on 8.6.x
    Issue #2956814 by tim.plunkett: FileValidationConstraintValidator...

  • alexpott committed d58b19b on 8.5.x
    Issue #2956814 by tim.plunkett: FileValidationConstraintValidator...

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

viappidu’s picture

Version: 8.6.x-dev » 8.5.x-dev

Has this been applied on 8.5.3 too?

koppie’s picture

@viappidu I don't think so; this only made it into 8.5.7 & higher.

quietone credited Dinesh18.

quietone’s picture

Issue tags: +Bug Smash Initiative

Closed #2900675: File validation fails when file does not exist. as a duplicate, adding credit.