diff --git a/modules/common/src/Util/DrupalFiles.php b/modules/common/src/Util/DrupalFiles.php index eda67b6a0a..f4839c4315 100644 --- a/modules/common/src/Util/DrupalFiles.php +++ b/modules/common/src/Util/DrupalFiles.php @@ -94,10 +94,36 @@ public function retrieveFile($url, $destination) { return $this->fileCreateUrl("{$destination}/{$filename}"); } else { - return system_retrieve_file($url, $destination, FALSE, FileSystemInterface::EXISTS_REPLACE); + return $this->systemRetrieveFile($url, $destination); } } + /** + * Attempts to get a file using Guzzle HTTP client and to store it locally. + * + * The destination file will never be a managed file. + * + * @param string $url + * The URL of the file to grab. + * @param string $destination + * Stream wrapper URI specifying where the file should be placed. If a + * directory path is provided, the file is saved into that directory under + * its original name. If the path contains a filename as well, that one will + * be used instead. + * If this value is omitted, the site's default files scheme will be used, + * usually "public://". + * + * @return mixed + * One of these possibilities: + * - If it succeeds the location where the file was saved. + * - If it fails, FALSE. + * + * @see \system_retrieve_file() + */ + protected function systemRetrieveFile($url, $destination = NULL) { + return system_retrieve_file($url, $destination, FALSE, FileSystemInterface::EXISTS_REPLACE); + } + /** * Given a URI like public:// retrieve the URL. */ diff --git a/modules/common/tests/src/Kernel/Util/DrupalFilesTest.php b/modules/common/tests/src/Kernel/Util/DrupalFilesTest.php new file mode 100644 index 0000000000..a280e26b5a --- /dev/null +++ b/modules/common/tests/src/Kernel/Util/DrupalFilesTest.php @@ -0,0 +1,76 @@ +container->get('dkan.common.drupal_files'); + $this->expectException(\Exception::class); + $this->expectExceptionMessage($exception_message); + $drupal_files->retrieveFile($url, $destination); + } + + public function provideRetrieve() { + return [ + ['http://'], + ['https://'], + ]; + } + + /** + * @covers ::retrieveFile + * + * @dataProvider provideRetrieve + */ + public function testHttpSource($url) { + // We're checking the internal logic of retrieveFile(), to make sure it + // calls systemRetrieveFile() given the inputs, and not testing whether the + // file is successfully retrieved. + // Mock a DrupalFiles object so that we can mock systemRetrieveFile(). + $drupal_files = $this->getMockBuilder(DrupalFiles::class) + ->setConstructorArgs([ + $this->container->get('file_system'), + $this->container->get('stream_wrapper_manager'), + ]) + ->onlyMethods(['systemRetrieveFile']) + ->getMock(); + $drupal_files->expects($this->once()) + ->method('systemRetrieveFile') + ->willReturn('/your/fake/path'); + + $this->assertEquals( + '/your/fake/path', + $drupal_files->retrieveFile($url, 'public://') + ); + } + +} diff --git a/modules/common/tests/src/Unit/Util/DrupalFilesTest.php b/modules/common/tests/src/Unit/Util/DrupalFilesTest.php index f100596fbe..8f4e2b7dd7 100644 --- a/modules/common/tests/src/Unit/Util/DrupalFilesTest.php +++ b/modules/common/tests/src/Unit/Util/DrupalFilesTest.php @@ -12,7 +12,9 @@ use Symfony\Component\DependencyInjection\ContainerInterface; /** - * + * @group dkan + * @group common + * @group unit */ class DrupalFilesTest extends TestCase {