From 33df2f63f6ec8f6f033b4355b5c0c3a85bf14042 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 21 Oct 2024 10:51:57 -0700 Subject: [PATCH 1/3] adds dkan:datastore:apply-dictionary --- modules/datastore/drush.services.yml | 8 ++ .../src/Commands/DictionaryCommands.php | 76 +++++++++++++++++++ .../ResourceDoesNotHaveDictionary.php | 2 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 modules/datastore/src/Commands/DictionaryCommands.php diff --git a/modules/datastore/drush.services.yml b/modules/datastore/drush.services.yml index 75f0e0422d..0498c7b8b3 100755 --- a/modules/datastore/drush.services.yml +++ b/modules/datastore/drush.services.yml @@ -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 } diff --git a/modules/datastore/src/Commands/DictionaryCommands.php b/modules/datastore/src/Commands/DictionaryCommands.php new file mode 100644 index 0000000000..73b9b53643 --- /dev/null +++ b/modules/datastore/src/Commands/DictionaryCommands.php @@ -0,0 +1,76 @@ +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; + } + +} diff --git a/modules/datastore/src/Service/ResourceProcessor/ResourceDoesNotHaveDictionary.php b/modules/datastore/src/Service/ResourceProcessor/ResourceDoesNotHaveDictionary.php index bb5669b4fc..f8e050a787 100644 --- a/modules/datastore/src/Service/ResourceProcessor/ResourceDoesNotHaveDictionary.php +++ b/modules/datastore/src/Service/ResourceProcessor/ResourceDoesNotHaveDictionary.php @@ -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 { From 478f5330f1d0e5245ebabfc75d585e4bbae47555 Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Mon, 21 Oct 2024 16:11:13 -0700 Subject: [PATCH 2/3] adds generic drush commands test for datastore --- .../src/Commands/DictionaryCommands.php | 5 ++ .../Commands/DatastoreCommandsTest.php | 72 +++++++++++++++++++ .../Commands/HarvestCommandsTest.php | 2 +- 3 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 modules/datastore/tests/src/Functional/Commands/DatastoreCommandsTest.php diff --git a/modules/datastore/src/Commands/DictionaryCommands.php b/modules/datastore/src/Commands/DictionaryCommands.php index 73b9b53643..bc25e4cbe2 100644 --- a/modules/datastore/src/Commands/DictionaryCommands.php +++ b/modules/datastore/src/Commands/DictionaryCommands.php @@ -9,6 +9,11 @@ use Drush\Commands\DrushCommands; use Drush\Psysh\DrushCommand; +/** + * Dictionary Drush commands. + * + * @codeCoverageIgnore + */ class DictionaryCommands extends DrushCommands { /** diff --git a/modules/datastore/tests/src/Functional/Commands/DatastoreCommandsTest.php b/modules/datastore/tests/src/Functional/Commands/DatastoreCommandsTest.php new file mode 100644 index 0000000000..42fcf38364 --- /dev/null +++ b/modules/datastore/tests/src/Functional/Commands/DatastoreCommandsTest.php @@ -0,0 +1,72 @@ +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() + ); + } + } + +} diff --git a/modules/harvest/tests/src/Functional/Commands/HarvestCommandsTest.php b/modules/harvest/tests/src/Functional/Commands/HarvestCommandsTest.php index 0dcf695655..5e2beff9f5 100644 --- a/modules/harvest/tests/src/Functional/Commands/HarvestCommandsTest.php +++ b/modules/harvest/tests/src/Functional/Commands/HarvestCommandsTest.php @@ -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, From b5fea465041287f8a3ae3a95accf334dcb7601df Mon Sep 17 00:00:00 2001 From: Paul Mitchum Date: Tue, 10 Dec 2024 10:55:34 -0600 Subject: [PATCH 3/3] added docs --- docs/source/drush_commands.rst | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/docs/source/drush_commands.rst b/docs/source/drush_commands.rst index 0726dda420..8faaae0bf1 100644 --- a/docs/source/drush_commands.rst +++ b/docs/source/drush_commands.rst @@ -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 -----------------------