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 Drush command to apply a dictionary to a resource #4320

Open
wants to merge 5 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 13 additions & 0 deletions docs/source/drush_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ dkan:datastore:import

~~~~~~

dkan:datastore:apply-dictionary
-----------------------

Apply the configured data dictionary to a datastore resource.

This will apply the dictionary in the describedBy field of the resource.

**Arguments**

- **resource_identifier** The identifier for the resource.

~~~~~~

dkan:datastore:localize
-----------------------

Expand Down
8 changes: 8 additions & 0 deletions modules/datastore/drush.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,11 @@ services:
- '@dkan.datastore.logger_channel'
tags:
- { name: drush.command }
datastore.dictionary.commands:
class: \Drupal\datastore\Commands\DictionaryCommands
arguments:
- '@dkan.metastore.resource_mapper'
- '@dkan.datastore.service.resource_processor.dictionary_enforcer'
- '@dkan.metastore.data_dictionary_discovery'
tags:
- { name: drush.command }
81 changes: 81 additions & 0 deletions modules/datastore/src/Commands/DictionaryCommands.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Drupal\datastore\Commands;

use Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer;
use Drupal\datastore\Service\ResourceProcessor\ResourceDoesNotHaveDictionary;
use Drupal\metastore\DataDictionary\DataDictionaryDiscoveryInterface;
use Drupal\metastore\ResourceMapper;
use Drush\Commands\DrushCommands;
use Drush\Psysh\DrushCommand;

/**
* Dictionary Drush commands.
*
* @codeCoverageIgnore
*/
class DictionaryCommands extends DrushCommands {

/**
* Resource mapper service.
*
* @var \Drupal\metastore\ResourceMapper
*/
protected ResourceMapper $resourceMapper;

/**
* Dictionary enforcer service.
*
* @var \Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer
*/
protected DictionaryEnforcer $dictionaryEnforcer;

/**
* Data dictionary discovery service.
*
* @var \Drupal\metastore\DataDictionary\DataDictionaryDiscoveryInterface
*/
protected DataDictionaryDiscoveryInterface $dataDictionaryDiscovery;

/**
* Constructor.
*/
public function __construct(
ResourceMapper $resourceMapper,
DictionaryEnforcer $dictionaryEnforcer,
DataDictionaryDiscoveryInterface $dataDictionaryDiscovery,
) {
parent::__construct();
$this->resourceMapper = $resourceMapper;
$this->dictionaryEnforcer = $dictionaryEnforcer;
$this->dataDictionaryDiscovery = $dataDictionaryDiscovery;
}

/**
* Apply the configured data dictionary to a datastore resource.
*
* @param string $resource_identifier
* Datastore resource identifier, e.g., "b210fb966b5f68be0421b928631e5d51".
*
* @command dkan:datastore:apply-dictionary
*/
public function applyDictionary(string $resource_identifier) {
if ($this->dataDictionaryDiscovery->getDataDictionaryMode() === DataDictionaryDiscoveryInterface::MODE_NONE) {
$this->logger()->notice('This site is not configured to use data dictionaries.');
return DrushCommand::SUCCESS;
}
try {
$this->dictionaryEnforcer->process(
$this->resourceMapper->get($resource_identifier)
);
}
catch (ResourceDoesNotHaveDictionary $exception) {
// If there's no associated dictionary that's not really a problem.
$this->logger()->notice($exception->getMessage());
return DrushCommand::SUCCESS;
}
$this->logger()->notice('Applied dictionary for ' . $resource_identifier);
return DrushCommand::SUCCESS;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Thrown when a resource does not have an associated data dictionary.
*
* @see Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer::getDataDictionaryForResource()
* @see \Drupal\datastore\Service\ResourceProcessor\DictionaryEnforcer::getDataDictionaryForResource()
*/
class ResourceDoesNotHaveDictionary extends \RuntimeException {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

declare(strict_types=1);

namespace Drupal\Tests\harvest\Functional\Commands;

use Drupal\Tests\BrowserTestBase;
use Drush\Psysh\DrushCommand;
use Drush\TestTraits\DrushTestTrait;

/**
* @group dkan
* @group datastore
* @group btb
* @group functional
*/
class DatastoreCommandsTest extends BrowserTestBase {

use DrushTestTrait;

protected static $modules = [
'datastore',
'metastore',
'node',
];

protected $defaultTheme = 'stark';

/**
* Minimally run all the commands.
*/
public function testCommands() {
// Run the commands with --help to ensure there are no fundamental errors.
foreach ([
'dkan:datastore:apply-dictionary',
'dkan:datastore:drop',
'dkan:datastore:drop-all',
'dkan:datastore:import',
'dkan:datastore:list',
'dkan:datastore:localize',
'dkan:datastore:prepare-localized',
'dkan:datastore:purge',
'dkan:datastore:purge-all',
'dkan:datastore:reimport',
] as $command) {
$this->drush($command, ['--help']);
$this->assertErrorOutputEquals('');
}

// Run the commands with no arguments, assert the result.
foreach ([
'dkan:datastore:apply-dictionary' => DrushCommand::FAILURE,
'dkan:datastore:drop' => DrushCommand::FAILURE,
'dkan:datastore:drop-all' => DrushCommand::SUCCESS,
'dkan:datastore:import' => DrushCommand::FAILURE,
'dkan:datastore:list' => DrushCommand::SUCCESS,
'dkan:datastore:localize' => DrushCommand::FAILURE,
'dkan:datastore:prepare-localized' => DrushCommand::FAILURE,
'dkan:datastore:purge' => DrushCommand::FAILURE,
'dkan:datastore:purge-all' => DrushCommand::SUCCESS,
'dkan:datastore:reimport' => DrushCommand::FAILURE,
] as $command => $expected_return) {
$this->drush($command, [], [], NULL, NULL, $expected_return);
// Exceptions will tell you which PHP file.
$this->assertStringNotContainsString(
'.php',
$this->getSimplifiedErrorOutput()
);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testCommands() {
$this->assertErrorOutputEquals('');
}

// Run the commands with no arguments, assert the response code.
// Run the commands with no arguments, assert the result.
foreach ([
'dkan:harvest:list' => 0,
'dkan:harvest:register' => 0,
Expand Down