Skip to content

Commit

Permalink
Merge pull request #966 from cakephp/3.x-fix-false-positive-file-fields
Browse files Browse the repository at this point in the history
3.x - Fix fields being incorrectly detected as file fields.
  • Loading branch information
ADmad authored Dec 24, 2023
2 parents 82cb1d2 + a56f43e commit 41b243c
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Command/ModelCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ protected function getEmptyMethod(string $fieldName, array $metaData, string $pr
return $prefix . 'EmptyDateTime';
}

if (preg_match('/file|image/', $fieldName)) {
if (preg_match('/(^|\s|_|-)(attachment|file|image)$/i', $fieldName)) {
return $prefix . 'EmptyFile';
}

Expand Down
85 changes: 85 additions & 0 deletions tests/TestCase/Command/ModelCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1338,6 +1338,91 @@ public function testGetValidationExcludeForeignKeysSigned()
$this->assertEquals($expected, $result);
}

public function testGetValidationOfPossibleFileFields(): void
{
$model = $this->getTableLocator()->get('TodoItems');
$model->setSchema([
// matches
'attachment' => ['type' => 'custom', 'null' => true],
'file' => ['type' => 'string', 'null' => true],
'image' => ['type' => 'string', 'null' => true],
'PDF_ATTACHMENT' => ['type' => 'string', 'null' => true],
'pdf-file' => ['type' => 'string', 'null' => true],
'pdf image' => ['type' => 'string', 'null' => true],
// non-matches
'attachment_size' => ['type' => 'integer', 'null' => true],
'file_location' => ['type' => 'string', 'null' => true],
'image_width' => ['type' => 'integer', 'null' => true],
'profile_id' => ['type' => 'integer', 'null' => true],
'filesize' => ['type' => 'integer', 'null' => true],
'pilgrimage' => ['type' => 'string', 'null' => true],
'reattachments' => ['type' => 'integer', 'null' => true],
]);

$associations = [];
$command = new ModelCommand();
$args = new Arguments([], [], []);

$result = $command->getValidation($model, $associations, $args);

$expected = [
// matches
'attachment' => [
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
'file' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
'image' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
'PDF_ATTACHMENT' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
'pdf-file' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
'pdf image' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyFile', 'args' => []],
],
// non-matches
'attachment_size' => [
'integer' => ['rule' => 'integer', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'file_location' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'image_width' => [
'integer' => ['rule' => 'integer', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'profile_id' => [
'integer' => ['rule' => 'integer', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'filesize' => [
'integer' => ['rule' => 'integer', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'pilgrimage' => [
'scalar' => ['rule' => 'scalar', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
'reattachments' => [
'integer' => ['rule' => 'integer', 'args' => []],
'allowEmpty' => ['rule' => 'allowEmptyString', 'args' => []],
],
];
$this->assertEquals($expected, $result);
}

/**
* test getting validation rules with the no-rules param.
*
Expand Down

0 comments on commit 41b243c

Please sign in to comment.