Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor DrupalFiles to remove deprecated system_retrieve_file() #4283

Draft
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion modules/common/src/Util/DrupalFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
76 changes: 76 additions & 0 deletions modules/common/tests/src/Kernel/Util/DrupalFilesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

namespace Drupal\Tests\common\Kernel\Util;

use Drupal\common\Util\DrupalFiles;
use Drupal\KernelTests\KernelTestBase;

/**
* @covers \Drupal\common\Util\DrupalFiles
* @coversDefaultClass \Drupal\common\Util\DrupalFiles
*
* @group dkan
* @group common
* @group kernel
*/
class DrupalFilesTest extends KernelTestBase {

protected static $modules = [
'common',
];

public function provideExceptions() {
return [
['Only file:// and http(s) urls are supported', 'badscheme://', 'any_destination'],
["Only moving files to Drupal's public directory (public://) is supported", 'file://', 'badscheme://'],
];
}

/**
* @covers ::retrieveFile
*
* @dataProvider provideExceptions
*/
public function testExceptions($exception_message, $url, $destination) {
/** @var \Drupal\common\Util\DrupalFiles $drupal_files */
$drupal_files = $this->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://')
);
}

}
4 changes: 3 additions & 1 deletion modules/common/tests/src/Unit/Util/DrupalFilesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
*
* @group dkan
* @group common
* @group unit
*/
class DrupalFilesTest extends TestCase {

Expand Down