Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vgwort
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
vgwort
Commits
6a7909ce
Commit
6a7909ce
authored
1 year ago
by
Alex Pott
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3341881
by alexpott: Add a Drush command to send a single entity to VG Wort
parent
d74eddfb
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!18
Resolve #3341881 "Add a drush command to send a single item from the queue"
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
drush.services.yml
+1
-1
1 addition, 1 deletion
drush.services.yml
src/Commands/VgwortCommands.php
+65
-4
65 additions, 4 deletions
src/Commands/VgwortCommands.php
tests/src/Functional/DrushTest.php
+28
-1
28 additions, 1 deletion
tests/src/Functional/DrushTest.php
with
94 additions
and
6 deletions
drush.services.yml
+
1
−
1
View file @
6a7909ce
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
}
This diff is collapsed.
Click to expand it.
src/Commands/VgwortCommands.php
+
65
−
4
View file @
6a7909ce
...
...
@@ -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
-addExistingContentToQ
ueue node
* @usage vgwort
:q
ueue 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
=
FALS
E
;
$success
=
self
::
EXIT_FAILUR
E
;
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.
*
...
...
This diff is collapsed.
Click to expand it.
tests/src/Functional/DrushTest.php
+
28
−
1
View file @
6a7909ce
...
...
@@ -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
test
Drush
Command
()
{
public
function
test
Queue
Command
()
{
$this
->
createContentType
([
'type'
=>
'page'
]);
// Create 80 nodes.
for
(
$i
=
1
;
$i
<=
80
;
$i
++
)
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment