Skip to content
Snippets Groups Projects
Commit ad38a0b5 authored by Mohit Aghera's avatar Mohit Aghera Committed by Jibran Ijaz
Browse files

Issue #3448925 by mohit_aghera, mstrelan, jibran, pghaemim: Add getFileUrl...

Issue #3448925 by mohit_aghera, mstrelan, jibran, pghaemim: Add getFileUrl method to FileDestinationBase and provide cache buster
parent db5890f1
Branches
Tags 7.x-1.2
1 merge request!27Add method to the base class for fetching the url
Pipeline #343055 failed
{
"name": "drupal/data_pipelines",
"description": "Provides the ability to ingest, validate, transform and index (ElasticSearch) arbitrary datasets",
"type": "drupal-module",
"license": "GPL-2.0-or-later",
"minimum-stability": "dev",
......
......@@ -12,6 +12,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\data_pipelines\Destination\DatasetDestinationPluginBase;
use Drupal\data_pipelines\Entity\DatasetInterface;
use Drupal\data_pipelines\Entity\DestinationInterface;
......@@ -186,6 +187,27 @@ abstract class FileDestinationBase extends DatasetDestinationPluginBase implemen
return $this->getDirPath() . $this->getFilename($dataset);
}
/**
* Get the Url object for the file of the dataset.
*
* @param \Drupal\data_pipelines\Entity\DatasetInterface $dataset
* The dataset.
*
* @return \Drupal\Core\Url
* File Url.
*/
public function getFileUrl(DatasetInterface $dataset): Url {
$fileUri = $this->getFilePath($dataset);
$filePath = $this->fileSystem->realpath($fileUri);
// Add the timestamp as a cache-bursting parameter.
$timestamp = file_exists($filePath) ? filemtime($filePath) : time();
return Url::fromUri($fileUri, [
'query' => [
't' => $timestamp,
],
]);
}
/**
* Get the dataset filename.
*
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\data_pipelines\Kernel\Destination;
use Drupal\Core\Url;
use Drupal\data_pipelines\Plugin\DatasetDestination\JsonDestination;
use Drupal\Tests\data_pipelines\Kernel\DatasetKernelTestBase;
/**
* Tests the getFileUrl of the FileDestinationBase class.
*
* @group data_pipelines
*
* @see \Drupal\data_pipelines\Plugin\DatasetDestination\FileDestinationBase
*/
class DatasetDestinationUrlTest extends DatasetKernelTestBase {
/**
* Tests that filemtime is added to getUrl method.
*/
public function testFileUrlForDestination(): void {
$destinationId = $this->randomMachineName();
$destination = $this->createTestMemoryDestination(['id' => $destinationId]);
$dataset = $this->createTestDataset(['destinations' => $destination]);
$fileSystem = \Drupal::service('file_system');
$streamWrapperManager = \Drupal::service('stream_wrapper_manager');
$configuration = [
'scheme' => 'public',
'dir' => '',
];
$plugin_id = 'foo';
$plugin_definition = [];
$destinationPlugin = new JsonDestination($configuration, $plugin_id, $plugin_definition, $fileSystem, $streamWrapperManager);
$file_path = $destinationPlugin->getFilePath($dataset);
// Create.
$this->assertTrue($destinationPlugin->beginProcessing($dataset));
$this->assertTrue($destinationPlugin->processChunk($dataset, iterator_to_array($dataset->getDataIterator())));
$this->assertEquals('[{"should_we":true,"full_name":"bloggs, joe"},{"should_we":false,"full_name":"bloggs, betty"}]', file_get_contents($file_path));
$url = $destinationPlugin->getFileUrl($dataset);
assert($url instanceof Url);
$this->assertArrayHasKey('t', $url->getOption('query'));
$timestamp1 = $url->getOption('query')['t'];
$this->assertEquals($timestamp1, filemtime(\Drupal::service('file_system')->realpath($destinationPlugin->getFilePath($dataset))));
sleep(2);
// Update and test the timestamp again.
$dataset->csv_text[0]->value .= "\nY,robin,scherbatsky";
$dataset->save();
$this->assertTrue($destinationPlugin->beginProcessing($dataset));
$this->assertTrue($destinationPlugin->processChunk($dataset, iterator_to_array($dataset->getDataIterator())));
$this->assertEquals('[{"should_we":true,"full_name":"bloggs, joe"},{"should_we":false,"full_name":"bloggs, betty"},{"should_we":true,"full_name":"scherbatsky, robin"}]', file_get_contents($file_path));
$url = $destinationPlugin->getFileUrl($dataset);
$timestamp2 = $url->getOption('query')['t'];
$this->assertEquals($timestamp2, filemtime(\Drupal::service('file_system')->realpath($destinationPlugin->getFilePath($dataset))));
$this->assertNotEquals($timestamp2, $timestamp1);
}
}
[
{
"should_we": "Y",
"firstname": "joe",
"lastname": "bloggs"
},
{
"should_we": "N",
"firstname": "betty",
"lastname": "bloggs"
},
{
"should_we": "N",
"firstname": "john",
"lastname": "Doe"
}
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment