-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for ResourceImporter and sample_content (#4276)
- Loading branch information
Showing
8 changed files
with
283 additions
and
27 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
47 changes: 47 additions & 0 deletions
47
modules/harvest/tests/src/Functional/Transform/ResourceImporterTest.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,47 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\harvest\Functional\Transform; | ||
|
||
use Drupal\harvest\Transform\ResourceImporter; | ||
use Drupal\Tests\BrowserTestBase; | ||
|
||
/** | ||
* @covers \Drupal\harvest\Transform\ResourceImporter | ||
* @coversDefaultClass \Drupal\harvest\Transform\ResourceImporter | ||
* | ||
* @group dkan | ||
* @group harvest | ||
* @group functional | ||
* @group btb | ||
* | ||
* @todo Turn this into a kernel test when we have refactored | ||
* DrupalFiles::retrieveFile to allow for vfsStream. | ||
*/ | ||
class ResourceImporterTest extends BrowserTestBase { | ||
|
||
protected static $modules = [ | ||
'datastore', | ||
'metastore', | ||
]; | ||
|
||
protected $defaultTheme = 'stark'; | ||
|
||
/** | ||
* @covers ::saveFile | ||
*/ | ||
public function testSaveFileHappyPath() { | ||
$sample_content_module_path = $this->container->get('extension.list.module') | ||
->getPath('sample_content'); | ||
|
||
$importer = new ResourceImporter('harvest_plan_id'); | ||
|
||
$this->assertStringContainsString( | ||
'distribution/dataset/Bike_Lane.csv', | ||
$importer->saveFile( | ||
'file://' . $sample_content_module_path . '/files/Bike_Lane.csv', | ||
'dataset' | ||
) | ||
); | ||
} | ||
|
||
} |
101 changes: 101 additions & 0 deletions
101
modules/harvest/tests/src/Kernel/Transform/ResourceImporterTest.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,101 @@ | ||
<?php | ||
|
||
namespace Drupal\Tests\harvest\Kernel\Transform; | ||
|
||
use Drupal\common\Util\DrupalFiles; | ||
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' | ||
); | ||
} | ||
|
||
/** | ||
* @covers ::saveFile | ||
*/ | ||
public function testSaveFile() { | ||
// When DrupalFiles::retrieveFile fails, saveFile will return FALSE. | ||
$drupal_files = $this->getMockBuilder(DrupalFiles::class) | ||
->setConstructorArgs([ | ||
$this->container->get('file_system'), | ||
$this->container->get('stream_wrapper_manager'), | ||
]) | ||
->onlyMethods(['retrieveFile']) | ||
->getMock(); | ||
$drupal_files->expects($this->once()) | ||
->method('retrieveFile') | ||
->willReturn(FALSE); | ||
|
||
$this->container->set('dkan.common.drupal_files', $drupal_files); | ||
|
||
$importer = new ResourceImporter('harvest_plan_id'); | ||
$this->assertFalse($importer->saveFile('any_url', 'any_dataset')); | ||
} | ||
|
||
} |
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 |
---|---|---|
|
@@ -7,3 +7,4 @@ dependencies: | |
- dkan:common | ||
- drupal:basic_auth | ||
- drupal:content_moderation | ||
- drupal:node |
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.