diff --git a/trpcultivate_phenotypes/tests/src/Kernel/Validators/ValidatorValidDataFileTest.php b/trpcultivate_phenotypes/tests/src/Kernel/Validators/ValidatorValidDataFileTest.php index 1d51ab7a..8118fdfb 100644 --- a/trpcultivate_phenotypes/tests/src/Kernel/Validators/ValidatorValidDataFileTest.php +++ b/trpcultivate_phenotypes/tests/src/Kernel/Validators/ValidatorValidDataFileTest.php @@ -9,7 +9,6 @@ use Drupal\Tests\tripal_chado\Kernel\ChadoTestKernelBase; use Drupal\Tests\trpcultivate_phenotypes\Traits\PhenotypeImporterTestTrait; -use Drupal\file\Entity\File; /** * Tests Tripal Cultivate Phenotypes Data File Validator Plugins. @@ -83,15 +82,18 @@ protected function setUp(): void { 'file-valid' => [ 'ext' => 'tsv', 'mime' => 'text/tab-separated-values', - 'content' => implode("\t", ['Header 1', 'Header 2', 'Header 3']), - 'filesize' => 1024 + 'content' => [ + 'string' => implode("\t", ['Header 1', 'Header 2', 'Header 3']) + ], ], // A valid file type, an empty file. 'file-empty' => [ 'ext' => 'tsv', 'mime' => 'text/tab-separated-values', - 'content' => '', + 'content' => [ + 'string' => '' + ], 'filesize' => 0 ], @@ -99,34 +101,39 @@ protected function setUp(): void { 'file-alternative' => [ 'ext' => 'txt', 'mime' => 'text/plain', - 'content' => implode("\t", ['Header 1', 'Header 2', 'Header 3']), - 'filesize' => 1024, + 'content' => [ + 'string' => implode("\t", ['Header 1', 'Header 2', 'Header 3']) + ], ], // Not valid file. 'file-image' => [ 'ext' => 'png', 'mime' => 'image/png', - 'content' => '', - 'filesize' => 1024, - 'file' => 'png.png' // Can be found in the test Fixtures folder. + 'content' => [ + 'string' => '', + 'file' => 'png.png' // Can be found in the test Fixtures folder. + ] ], // Pretend tsv file. 'file-pretend' => [ 'ext' => 'tsv', 'mime' => 'application/pdf', - 'filesize' => 1024, - 'file' => 'pdf.txt' // Can be found in the test Fixtures folder. + 'content' => [ + 'string' => '', + 'file' => 'pdf.txt' // Can be found in the test Fixtures folder. + ] ], // Could not open the file - not permitted to read. 'file-locked' => [ 'ext' => 'tsv', 'mime' => 'text/tab-separated-values', - 'content' => implode("\t", ['Header 1', 'Header 2', 'Header 3']), - 'filesize' => 1024, - 'lock' => TRUE + 'content' => [ + 'string' => implode("\t", ['Header 1', 'Header 2', 'Header 3']) + ], + 'permissions' => 'none' ] ]; @@ -137,22 +144,9 @@ protected function setUp(): void { // Create the file for each test file scenario. foreach($test_file_scenario as $test_scenario => $file_properties) { $filename = 'test_data_file_' . $test_scenario . '.' . $file_properties['ext']; + $file_properties['filename'] = $filename; - $file = File::create([ - 'filename' => $filename, - 'filemime' => $file_properties['mime'], - 'uri' => 'public://' . $filename, - 'status' => 0 - ]); - - // Update test scenario file properties. - - // Set the file size. - if (isset($file_properties['filesize'])) { - $file->setSize($file_properties['filesize']); - } - - $file->save(); + $file = $this->createTestFile($file_properties); // Reference relevant file properties that will be used // to indicate attributes of the file that failed the validation. @@ -162,27 +156,6 @@ protected function setUp(): void { $file_filename = $file->getFileName(); $file_extension = pathinfo($file_filename, PATHINFO_EXTENSION); - // Write contents into the file. - if (!empty($file_properties['content'])) { - file_put_contents($file_uri, $file_properties['content']); - } - - // If an existing file was specified, move the file fixture into the uri - // to override the created file and use it in lieu of the created file. - if (!empty($file_properties['file'])) { - $path_to_fixtures = __DIR__ . '/../../Fixtures/'; - $full_path = $path_to_fixtures . $file_properties['file']; - $this->assertFileIsReadable($full_path, - "Unable to setup FILE ". $test_scenario . " because cannot access Fixture file at $full_path."); - - copy($full_path, $file_uri); - } - - // If file should be locked. - if (isset($file_properties['lock']) && $file_properties['lock']) { - chmod($file_uri, 0000); - } - // Create a test scenario file input parameter and attach the file properties. $test_file_param[ $test_scenario ] = [ 'test_param' =>[ @@ -510,15 +483,15 @@ public function provideFileForDataFileValidator() { */ public function testDataFileExceptionCase() { // The filename is set to a different name than the set filename of valid-file test file input scenario. - $filename = 'not-the-filename.tsv'; + $filename = 'not-the-filename.tsv'; $fid = $this->test_files['file-valid']['test_file']['fid']; $exception_caught = FALSE; $exception_message = ''; - + try { $this->validator_instance->validateFile($filename, $fid); - } + } catch (\Exception $e) { $exception_caught = TRUE; $exception_message = $e->getMessage(); @@ -529,7 +502,7 @@ public function testDataFileExceptionCase() { $exception_message, 'The filename provided does not match the filename set in the file object.', 'The exception message thrown by data file validator filename mismatch case does not match excepted message' - ); + ); } /** diff --git a/trpcultivate_phenotypes/tests/src/Traits/PhenotypeImporterTestTrait.php b/trpcultivate_phenotypes/tests/src/Traits/PhenotypeImporterTestTrait.php index 3598eea1..7c8c2000 100644 --- a/trpcultivate_phenotypes/tests/src/Traits/PhenotypeImporterTestTrait.php +++ b/trpcultivate_phenotypes/tests/src/Traits/PhenotypeImporterTestTrait.php @@ -183,69 +183,77 @@ protected function createTestFile($details) { // Set Defaults. $details['extension'] = @$details['extension'] ?: 'txt'; - $details['mime'] = @$details['mime'] ?: 'text/tab-separated-values'; $details['filename'] = @$details['filename'] ?: 'testFile.' . uniqid() . '.' . $details['extension']; + $details['mime'] = @$details['mime'] ?: 'text/tab-separated-values'; $details['is_temporary'] = @$details['is_temporary'] ?: FALSE; $details['content'] = @$details['content'] ?: ['string' => uniqid()]; - // Determine the fullpath to test files for use later - if (array_key_exists('file', $details['content'])) { - $path_to_fixtures = __DIR__ . '/../Fixtures/'; - $full_path = $path_to_fixtures . $details['content']['file']; - } - + // Set directory. $directory = ($details['is_temporary']) ? 'temporary://' : 'public://'; - $uri = $directory . $details['filename']; + $file_uri = $directory . $details['filename']; + // Create file object. $file = File::create([ 'filename' => $details['filename'], 'filemime' => $details['mime'], - 'uri' => $uri, + 'uri' => $file_uri, 'status' => 0, ]); - // Set the size of the file. - // This is usually used if the file is empty in which case this is 0 - if (isset($details['filesize'])) { - $file->setSize($details['filesize']); - } - else if (array_key_exists('file', $details['content'])) { - $size = @filesize($full_path); - // Set size unless there was an error. - $this->assertNotFalse($size, 'Unable to determine size of test file: ' . $full_path); - $file->setSize($size); - } + // Reference file attributes: + $file_id = $file->id(); + $file_uri = $file->getFileUri(); - // Save the file to Drupal. - $file->save(); - $id = $file->id(); + // If a test file fixture was provided, create a copy and set this file copy as + // the file uri value in the file object for this test file. + if (array_key_exists('file', $details['content']) && !empty($details['content']['file'])) { + $path_to_file_fixture = __DIR__ . '/../Fixtures/' . $details['content']['file']; + + $this->assertFileIsReadable( + $path_to_file_fixture, + 'Unable to setup FILE ' . $file_id . ' because cannot access Fixture file at ' . $path_to_file_fixture + ); + + copy($path_to_file_fixture, $file_uri); + } // Write something on file with content key set to a string. if (!empty($details['content']['string'])) { - $fileuri = $file->getFileUri(); - file_put_contents($fileuri, $details['content']['string']); + file_put_contents($file_uri, $details['content']['string']); } - // If an existing file was specified then we can add that in here. - if (!empty($details['content']['file'])) { - $fileuri = $file->getFileUri(); + // Set other file attributes: + + // Set the size of the file. + // This is usually used if the file is empty in which case this is 0. + if (isset($details['filesize'])) { + // File size was provided. + $file->setSize($details['filesize']); + } + else { + // File size is to be determined. + // Get the file size. + $file_size = @filesize($file_uri); - $this->assertFileIsReadable($full_path, - "Unable to setup FILE ". $id . " because cannot access Fixture file at $full_path."); + // Assert that a file size was established. + $this->assertNotFalse($file_size, 'Unable to determine size of test file: ' . $file_uri); - copy($full_path, $fileuri); + // Set the file size. + $file->setSize($file_size); } + // Save all set attributes. + $file->save(); + // Set file permissions if needed. if (!empty($details['permissions'])) { - $fileuri = $file->getFileUri(); if ($details['permissions'] == 'none') { - chmod($fileuri, octdec(0000)); + chmod($file_uri, octdec(0000)); } elseif (is_numeric($details['permissions'])) { $decoded = decoct(octdec($details['permissions'])); if ($details['permissions'] == $decoded) { - chmod($fileuri, $details['permissions']); + chmod($file_uri, $details['permissions']); } } }