Skip to content

Commit

Permalink
Merge pull request #130 from TripalCultivate/g4.110-repairValidDataFile
Browse files Browse the repository at this point in the history
Revises valid data file validator
  • Loading branch information
laceysanderson authored Dec 20, 2024
2 parents bf615b7 + 820d5ce commit 6437caa
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 311 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ public function formValidate($form, &$form_state) {
// Set failures for this validator name to an empty array to signal that
// this validator has been run.
$failures[$validator_name] = [];
$result = $validator->validateFile('', $file_id);
$result = $validator->validateFile($file_id);

// Check if validation failed and save the results if it did.
if (array_key_exists('valid', $result) && $result['valid'] === FALSE) {
Expand Down
79 changes: 15 additions & 64 deletions trpcultivate_phenotypes/src/Plugin/Validators/ValidDataFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,14 @@ public static function create(ContainerInterface $container, array $configuratio
* Validate that the input file is a valid file.
*
* Checks include:
* - Parameter filename or file id is valid.
* - File ID parameter is positive non-zero integer, and cannot be null.
* - Has Drupal File Id number assigned and can be loaded.
* - File extension and mime type are configured by the importer.
* - File exists and is not empty.
* - File can be opened.
*
* @param string $filename
* The full path to a file within the file system (Absolute file path).
* @param int $fid
* [OPTIONAL] The unique identifier (fid) of a file that is managed by
* The unique identifier (fid) of a file that is managed by
* Drupal File System.
*
* @return array
Expand All @@ -96,84 +94,37 @@ public static function create(ContainerInterface $container, array $configuratio
* - 'fid': The fid of the provided file.
* - 'mime': The mime type of the input file if it is not supported.
* - 'extension': The extension of the input file if not supported.
*
* @throws \Exception
* - If parameters $filename and $fid are both provided, but do not point to
* the same file object.
*/
public function validateFile(string $filename, int|null $fid = NULL) {
public function validateFile(int|null $fid) {

// Parameter check, verify that the filename/file path is valid.
if (empty($filename) && is_null($fid)) {
// Parameter check, verify the file id number is not null, 0 or
// a negative value.
if (is_null($fid) || $fid <= 0) {
return [
'case' => 'Filename is empty string',
'case' => 'Invalid file id number',
'valid' => FALSE,
'failedItems' => [
'filename' => '',
'fid' => $fid,
],
];
}

// Parameter check, verify the file id number is not 0 or a negative value.
if (!is_null($fid) && $fid <= 0) {
// Load the file object by fid number.
$file_object = $this->service_EntityTypeManager
->getStorage('file')
->load($fid);

// Check that the file input provided returned a file object.
if (!$file_object) {
return [
'case' => 'Invalid file id number',
'case' => 'File id failed to load a file object',
'valid' => FALSE,
'failedItems' => [
'filename' => $filename,
'fid' => $fid,
],
];
}

// Holds the file object when file is a managed file.
$file_object = NULL;

// Load file object.
if (is_numeric($fid) && $fid > 0) {
// Load the file object by fid number.
$file_object = $this->service_EntityTypeManager
->getStorage('file')
->load($fid);

// Verify that the filename (if provided) matches the filename in the file
// object returned by the file id.
if (!empty($filename) && $file_object->getFileName() != pathinfo($filename, PATHINFO_FILENAME)) {
throw new \Exception('The filename provided does not match the filename set in the file object.');
}
}
elseif ($filename) {
// Locate the file entity by uri and load the file object using the
// returned file id number that matched.
$file_object = $this->service_EntityTypeManager
->getStorage('file')
->loadByProperties(['uri' => $filename]);
$file_object = reset($file_object);
}

// Check that the file input provided returned a file object.
if (!$file_object) {
if (is_null($fid)) {
return [
'case' => 'Filename failed to load a file object',
'valid' => FALSE,
'failedItems' => [
'filename' => $filename,
],
];
}
else {
return [
'case' => 'File id failed to load a file object',
'valid' => FALSE,
'failedItems' => [
'fid' => $fid,
],
];
}
}

// File object loaded successfully. Any subsequent failed checks will
// reference the filename and file id from the established file object.
$file_filename = $file_object->getFileName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function validateMetadata(array $form_values) {
/**
* {@inheritdoc}
*/
public function validateFile(string $filename, int $fid) {
public function validateFile(int|null $fid) {
$plugin_name = $this->getValidatorName();
throw new \Exception("Method validateFile() from base class called for $plugin_name. If this plugin wants to support this type of validation then they need to override it.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ public function validateMetadata(array $form_values);
* This should validate the file object (e.g. it exists, is readable) but
* should not validate the contents in any way.
*
* @param array $filename
* The full path and filename with extension of the file to validate.
* @param int $fid
* The file ID of the file object.
*
Expand All @@ -86,7 +84,7 @@ public function validateMetadata(array $form_values);
* then this array should contain a key indicating what failed, and the
* resulting value from checking its mime-type/extension.
*/
public function validateFile(string $filename, int $fid);
public function validateFile(int|null $fid);

/**
* Validates rows within the data file submitted to an importer.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,8 @@ public function testValidatorValidateMethods() {
$exception_caught = NULL;
$exception_message = NULL;
try {
$filename = 'public://does_not_exist.txt';
$fid = 123;
$instance->validateFile($filename, $fid);
$instance->validateFile($fid);
}
catch (\Exception $e) {
$exception_caught = TRUE;
Expand Down
Loading

0 comments on commit 6437caa

Please sign in to comment.