-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
FileFetcher should always reflect use-local-file config (#4035)
- Loading branch information
Showing
12 changed files
with
487 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Drupal\common\FileFetcher; | ||
|
||
use FileFetcher\FileFetcher; | ||
|
||
/** | ||
* Allows FileFetcher to be reconfigured for using existing local files. | ||
* | ||
* This DKAN-specific extension of the FileFetcher (which comes from an | ||
* external library) applies the DKAN configuration | ||
* common.settings.always_use_existing_local_perspective | ||
* when selecting the processor. The configuration itself is retrieved | ||
* in FileFetcherFactory and passed to DkanFileFetcher on getInstance(). | ||
*/ | ||
class DkanFileFetcher extends FileFetcher { | ||
|
||
/** | ||
* Tell this file fetcher whether to use local files if they exist. | ||
* | ||
* @param bool $use_local_file | ||
* (optional) Whether to use the local file. If TRUE, we'll use the file | ||
* processor that prefers to use local files. Defaults to TRUE. | ||
* | ||
* @return self | ||
* Fluent interface. | ||
* | ||
* @see https://dkan.readthedocs.io/en/2.x/user-guide/guide_local_files.html | ||
*/ | ||
public function setAlwaysUseExistingLocalPerspective(bool $use_local_file = TRUE) : self { | ||
// @todo Re-computing the custom processor classes should be in another | ||
// method that is in the parent class. | ||
if ($use_local_file) { | ||
$this->dkanUseLocalFileProcessor(); | ||
} | ||
else { | ||
$this->dkanUseDefaultFileProcessor(); | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* Configure the processor to respect the local file if it already exists. | ||
*/ | ||
protected function dkanUseLocalFileProcessor() { | ||
// Set the state/config to use our remote class. | ||
$this->setProcessors(['processors' => [FileFetcherRemoteUseExisting::class]]); | ||
$this->setStateProperty('processor', FileFetcherRemoteUseExisting::class); | ||
// At this very early stage, update the status if the file already exists. | ||
/** @var \Drupal\common\FileFetcher\FileFetcherRemoteUseExisting $processor */ | ||
$processor = $this->getProcessor(); | ||
$existing_status = $processor->discoverStatusForExistingFile( | ||
$this->getState(), | ||
$this->getResult() | ||
); | ||
$this->setState($existing_status['state']); | ||
} | ||
|
||
/** | ||
* Configure the processor to use its default behavior. | ||
*/ | ||
protected function dkanUseDefaultFileProcessor() { | ||
// @todo This ignores any other custom processor classes that might have | ||
// been configured. Improve this situation. | ||
$this->customProcessorClasses = []; | ||
$state = $this->getState(); | ||
foreach ($this->getProcessors() as $processor) { | ||
if ($processor->isServerCompatible($state)) { | ||
$state['processor'] = get_class($processor); | ||
break; | ||
} | ||
} | ||
$this->getResult()->setData(json_encode($state)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
modules/common/tests/src/Unit/FileFetcher/FileFetcherRemoteUseExistingTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\common\Unit\FileFetcher; | ||
|
||
use Drupal\common\FileFetcher\FileFetcherRemoteUseExisting; | ||
use org\bovigo\vfs\vfsStream; | ||
use PHPUnit\Framework\TestCase; | ||
use Procrastinator\Result; | ||
|
||
/** | ||
* @covers \Drupal\common\FileFetcher\FileFetcherRemoteUseExisting | ||
* @coversDefaultClass \Drupal\common\FileFetcher\FileFetcherRemoteUseExisting | ||
*/ | ||
class FileFetcherRemoteUseExistingTest extends TestCase { | ||
|
||
/** | ||
* @covers ::copy | ||
*/ | ||
public function testCopy() { | ||
$result = new Result(); | ||
$remote = new FileFetcherRemoteUseExisting(); | ||
|
||
// Set up a file system. | ||
$root = vfsStream::setup(); | ||
$file = $root->url() . '/nine_bytes.csv'; | ||
$file_contents = '0123,4567'; | ||
|
||
// Config for processor. | ||
$state = [ | ||
'destination' => $file, | ||
'source' => 'https://example.com/bad_path.csv', | ||
]; | ||
|
||
// Run it for error condition because the file doesn't already exist, so it | ||
// will try to copy the source, but the source URL is bad. | ||
$result_state = $remote->copy($state, $result); | ||
$this->assertEquals(Result::ERROR, $result_state['result']->getStatus()); | ||
|
||
// Add existing file contents. | ||
file_put_contents($file, $file_contents); | ||
|
||
// Run it for re-use of existing file. This will succeed because the file | ||
// is there. | ||
$result_state = $remote->copy($state, $result); | ||
|
||
$this->assertEquals(Result::DONE, $result_state['result']->getStatus()); | ||
$this->assertEquals(9, $result_state['state']['total_bytes']); | ||
$this->assertEquals(9, $result_state['state']['total_bytes_copied']); | ||
$this->assertStringEqualsFile($file, $file_contents); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.