Skip to content
Snippets Groups Projects
Commit 6a7909ce authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3341881 by alexpott: Add a Drush command to send a single entity to VG Wort

parent d74eddfb
No related branches found
No related tags found
1 merge request!18Resolve #3341881 "Add a drush command to send a single item from the queue"
services:
vgwort.commands:
class: \Drupal\vgwort\Commands\VgwortCommands
arguments: ['@entity_type.manager', '@vgwort.entity_queuer', '@datetime.time', '@entity.memory_cache']
arguments: ['@entity_type.manager', '@vgwort.entity_queuer', '@datetime.time', '@entity.memory_cache', '@plugin.manager.advancedqueue_job_type']
tags:
- { name: drush.command }
......@@ -4,6 +4,8 @@ namespace Drupal\vgwort\Commands;
use Consolidation\AnnotatedCommand\CommandData;
use Consolidation\AnnotatedCommand\CommandError;
use Drupal\advancedqueue\Job;
use Drupal\advancedqueue\JobTypeManager;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Batch\BatchBuilder;
use Drupal\Core\Cache\MemoryCache\MemoryCacheInterface;
......@@ -11,6 +13,7 @@ use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Entity\EntityChangedInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\vgwort\EntityQueuer;
use Drupal\vgwort\Plugin\AdvancedQueue\JobType\RegistrationNotification;
use Drush\Commands\DrushCommands;
/**
......@@ -39,6 +42,11 @@ class VgwortCommands extends DrushCommands {
*/
protected MemoryCacheInterface $entityMemoryCache;
/**
* @var \Drupal\advancedqueue\JobTypeManager
*/
protected JobTypeManager $jobTypeManager;
/**
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
......@@ -48,12 +56,45 @@ class VgwortCommands extends DrushCommands {
* The time service.
* @param \Drupal\Core\Cache\MemoryCache\MemoryCacheInterface $entityMemoryCache
* The entity memory cache.
* @param \Drupal\advancedqueue\JobTypeManager $jobTypeManager
* The advanced queue job type manager.
*/
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityQueuer $entityQueuer, TimeInterface $time, MemoryCacheInterface $entityMemoryCache) {
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityQueuer $entityQueuer, TimeInterface $time, MemoryCacheInterface $entityMemoryCache, JobTypeManager $jobTypeManager) {
$this->entityTypeManager = $entityTypeManager;
$this->entityQueuer = $entityQueuer;
$this->time = $time;
$this->entityMemoryCache = $entityMemoryCache;
$this->jobTypeManager = $jobTypeManager;
}
/**
* Sends a single entity to VG Wort.
*
* @param string $entityType
* The entity type, for example, 'node'.
* @param string $entityId
* The entity ID, for example, '1'.
*
* @usage vgwort:send node 3
* Sends node 3 to VG Wort.
*
* @command vgwort:send
* @aliases vgws
* @validate-vgwort-entity-type
* @validate-vgwort-entity-id
*/
public function sendToVGWort(string $entityType, string $entityId) {
$entity = $this->entityTypeManager->getStorage($entityType)->load($entityId);
$job = RegistrationNotification::createJob($entity);
/** @var \Drupal\vgwort\Plugin\AdvancedQueue\JobType\RegistrationNotification $job_type */
$job_type = $this->jobTypeManager->createInstance($job->getType());
$job_result = $job_type->process($job);
if ($job_result->getState() !== Job::STATE_SUCCESS) {
$this->io()->error(\dt('Sending failed: @message.', ['@message' => $job_result->getMessage()]));
return self::EXIT_FAILURE;
}
$this->io()->success(\dt('Sending complete.'));
return self::EXIT_SUCCESS;
}
/**
......@@ -67,7 +108,7 @@ class VgwortCommands extends DrushCommands {
* @option batch-size
* How many entities to process in each batch invocation. Defaults to 50.
*
* @usage vgwort-addExistingContentToQueue node
* @usage vgwort:queue node
* Adds nodes to queue.
*
* @command vgwort:queue
......@@ -84,7 +125,7 @@ class VgwortCommands extends DrushCommands {
batch_set($batch->toArray());
$result = drush_backend_batch_process();
$success = FALSE;
$success = self::EXIT_FAILURE;
if (!is_array($result)) {
$this->logger()->error(dt('Batch process did not return a result array. Returned: !type', ['!type' => gettype($result)]));
}
......@@ -97,7 +138,7 @@ class VgwortCommands extends DrushCommands {
]));
}
else {
$success = TRUE;
$success = self::EXIT_SUCCESS;
}
return $success;
......@@ -196,6 +237,26 @@ class VgwortCommands extends DrushCommands {
return NULL;
}
/**
* Validates an entity ID exists.
*
* @hook validate @validate-vgwort-entity-id
*
* @param \Consolidation\AnnotatedCommand\CommandData $commandData
* The command data to validate.
*
* @return \Consolidation\AnnotatedCommand\CommandError|null
*/
public function validateEntityID(CommandData $commandData): ?CommandError {
$entityType = $commandData->input()->getArgument('entityType');
$entityId = $commandData->input()->getArgument('entityId');
$entity = $this->entityTypeManager->getStorage($entityType)->load($entityId);
if ($entity === NULL) {
return new CommandError(dt('The entity @entityType:@entityId does not exist.', ['@entityType' => $entityType, '@entityId' => $entityId]));
}
return NULL;
}
/**
* Validates the batch size is a positive integer.
*
......
......@@ -47,6 +47,33 @@ class DrushTest extends BrowserTestBase {
->save();
}
public function testSendInvalidEntityId() {
$this->config('vgwort.settings')->set('entity_types', ['node' => []])->save();
$this->expectException(\Exception::class);
$this->expectErrorMessageMatches('/The entity node:1 does not exist\./');
$this->drush('vgws', ['node', '1']);
}
public function testSendCommand() {
$this->config('vgwort.settings')
->set('entity_types', ['node' => []])
// This test should not send anything, but just in case send to the test
// API.
->set('test_mode', TRUE)
->save();
$node = Node::create([
'type' => 'page',
'title' => 'Page 1',
]);
$node->save();
\Drupal::state()->set('vgwort_test_vgwort_enable_for_entity', ['1']);
// This test ensures that RegistrationNotification::process() is called with
// the correct entity. Other test coverage ensure that that function works
// correctly if the entity is actually valid.
$this->drush('vgws', ['node', '1'], [], NULL, NULL, 1);
$this->assertSame('[ERROR] Sending failed: The entity node:1 does not have a VG Wort counter ID.', $this->getOutput());
}
public function testInvalidEntityType() {
$this->expectException(\Exception::class);
$this->expectErrorMessageMatches('/Entity type "node" is not configured for VG Wort/');
......@@ -60,7 +87,7 @@ class DrushTest extends BrowserTestBase {
$this->drush('vgwq', ['node'], ['batch-size' => -1]);
}
public function testDrushCommand() {
public function testQueueCommand() {
$this->createContentType(['type' => 'page']);
// Create 80 nodes.
for ($i = 1; $i <= 80; $i++) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment