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

Add tests for ResourceImporter and sample_content #4276

Draft
wants to merge 17 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
some unit test
  • Loading branch information
paul-m committed Sep 6, 2024
commit 9f8f2548cdc90e3a31eaf928afb79d3b00e40200
17 changes: 16 additions & 1 deletion modules/harvest/src/Transform/ResourceImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
namespace Drupal\harvest\Transform;

use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\File\FileUrlGeneratorInterface;
use Drupal\common\Util\DrupalFiles;
use Harvest\ETL\Transform\Transform;

/**
* Moves local files to public:// and alters the downloadUrl field.
*
* Used by the sample_content harvest.
*
* @see modules/sample_content/harvest_plan.json
*/
class ResourceImporter extends Transform {

Expand All @@ -18,12 +23,20 @@ class ResourceImporter extends Transform {
*/
private DrupalFiles $drupalFiles;

/**
* File URL generator service.
*
* @var \Drupal\Core\File\FileUrlGeneratorInterface
*/
private FileUrlGeneratorInterface $fileUrlGenerator;

/**
* Constructor.
*/
public function __construct($harvest_plan) {
parent::__construct($harvest_plan);
$this->drupalFiles = \Drupal::service('dkan.common.drupal_files');
$this->fileUrlGenerator = \Drupal::service('file_url_generator');
}

/**
Expand Down Expand Up @@ -99,6 +112,8 @@ protected function updateDownloadUrl($dataset, $dist) {
* `$settings['file_public_base_url']` must be configured in `settings.php`,
* otherwise 'default' will be used as the hostname in the new URL.
*
* @todo Is the above comment true?
*
* @param string $url
* External file URL.
* @param string $dataset_id
Expand All @@ -121,7 +136,7 @@ public function saveFile($url, $dataset_id) {
}

if (is_object($path)) {
return \Drupal::service('file_url_generator')->generateAbsoluteString($path->uri->value);
return $this->fileUrlGenerator->generateAbsoluteString($path->uri->value);
}
else {
return $this->drupalFiles->fileCreateUrl($path);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Drupal\Tests\harvest\Kernel\Transform;

use Drupal\KernelTests\KernelTestBase;
use Drupal\harvest\Transform\ResourceImporter;

/**
* @covers \Drupal\harvest\Transform\ResourceImporter
* @coversDefaultClass \Drupal\harvest\Transform\ResourceImporter
*
* @group dkan
* @group harvest
* @group kernel
*/
class ResourceImporterTest extends KernelTestBase {

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

/**
* @covers ::updateDistributions
*/
public function testUpdateDistributions() {
// Calling with an empty dataset should result in the same empty dataset.
$dataset = (object) [];
$importer = new ResourceImporter('harvest_plan_id');
$this->assertIsObject($result = $importer->run($dataset));
$this->assertEquals($dataset, $result);

// Calling with a distribution with no download url should result in the
// same object being returned.
$dataset = (object) [
'distribution' => [
(object) [
'title' => 'my title',
],
],
];
$this->assertIsObject($result = $importer->run($dataset));
$this->assertEquals($dataset, $result);
}

/**
* @covers ::updateDownloadUrl
*/
public function testUpdateDownloadUrl() {
// Mock saveFile so we don't actually have to worry about the file system.
$importer = $this->getMockBuilder(ResourceImporter::class)
->setConstructorArgs(['harvest_plan_id'])
->onlyMethods(['saveFile'])
->getMock();
$importer->expects($this->any())
->method('saveFile')
// Adds '_saved' to whatever URL was passed in.
->willReturnCallback(function ($url, $dataset_id): string {
return $url . '_saved';
});

// Prepare a dataset with a downloadURL.
$dataset = (object) [
'identifier' => 'identifier',
'distribution' => [
(object) [
'downloadURL' => 'my_url',
'title' => 'my title',
],
],
];
$this->assertIsObject($result = $importer->run($dataset));
$this->assertEquals(
'my_url_saved',
$result->distribution[0]->downloadURL ?? 'nope'
);
}

}