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
expand sample content commands test
  • Loading branch information
paul-m committed Sep 5, 2024
commit 52e07214ab4f133e73e8f88ad6d77f34a70df0fd
1 change: 1 addition & 0 deletions modules/harvest/harvest.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ core_version_requirement: ^10
package: DKAN
dependencies:
- dkan:common
- dkan:datastore
- dkan:metastore
5 changes: 2 additions & 3 deletions modules/harvest/src/Transform/ResourceImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Drupal\harvest\Transform;

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

/**
* Moves local files to public:// and alters the downloadUrl field.
*
* @codeCoverageIgnore
*/
class ResourceImporter extends Transform {

Expand All @@ -17,7 +16,7 @@ class ResourceImporter extends Transform {
*
* @var \Drupal\common\Util\DrupalFiles
*/
private $drupalFiles;
private DrupalFiles $drupalFiles;

/**
* Constructor.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Drupal\Tests\sample_content\Functional;

use Drupal\Tests\BrowserTestBase;
use Drupal\user\Entity\User;
use Drush\TestTraits\DrushTestTrait;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

/**
* @coversDefaultClass \Drupal\sample_content\Drush
Expand All @@ -25,17 +28,52 @@ class SampleContentCommandsTest extends BrowserTestBase {
'sample_content',
];

/**
* Get a Guzzle Client object ready for sending HTTP requests.
*
* @param \Drupal\user\Entity\User|null $authUser
* (Optional) A user object to use for authentication.
* @param bool $http_errors
* (Optional) Whether 4xx or 5xx response codes should throw an exception.
* Defaults to FALSE.
*
* @return \GuzzleHttp\Client
* Client ready for HTTP requests.
*
* @todo Move this to a trait or base class.
*/
protected function getApiClient(?User $authUser = NULL, $http_errors = FALSE): Client {
$options = [
'base_uri' => $this->baseUrl,
RequestOptions::HTTP_ERRORS => $http_errors,
];
if ($authUser) {
$options[RequestOptions::AUTH] = [$authUser->getAccountName(), $authUser->pass_raw];
}
return $this->container->get('http_client_factory')->fromOptions($options);
}

public function test() {
// No errors thrown on --help.
foreach ([
'dkan:sample-content:create',
'dkan:sample-content:remove',
] as $command) {
$this->drush($command . ' --help');
$this->assertEmpty(
$this->getSimplifiedErrorOutput()
);
}

$harvest_plan_name = 'sample_content';
/** @var \Drupal\harvest\HarvestService $harvest_service */
$harvest_service = $this->container->get('dkan.harvest.service');

// Run the create command.
$this->drush('dkan:sample-content:create');
$output = $this->getOutput();

/** @var \Drupal\harvest\HarvestService $harvest_service */
$harvest_service = $this->container->get('dkan.harvest.service');

// Start asserting.
// Start asserting. Admittedly, not the best set of assertions.
foreach ([
'run_id',
'processed',
Expand All @@ -59,6 +97,11 @@ public function test() {
$this->assertCount(10, $run_info['status']['extracted_items_ids'] ?? []);
$this->assertEmpty($run_info['error'] ?? []);

// What does the RESTful API say?
$response = $this->getApiClient()->get('/api/1/metastore/schemas/dataset/items');
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(10, json_decode($response->getBody()->getContents()));

// Run the remove command.
$this->drush('dkan:sample-content:remove');

Expand All @@ -71,6 +114,10 @@ public function test() {
] as $expected) {
$this->assertStringContainsString($expected, $output);
}

$response = $this->getApiClient()->get('/api/1/metastore/schemas/dataset/items');
$this->assertEquals(200, $response->getStatusCode());
$this->assertCount(0, json_decode($response->getBody()->getContents()));
}

}