Skip to content

Commit

Permalink
VACMS-13852: Creates EntityEvent Subscriber. (department-of-veterans-…
Browse files Browse the repository at this point in the history
…affairs#14337)

* VACMS-13852: Creates EntityEvent Subscriber.

* Tweaks to prevent test failures.

* Yeah, no clue what I'm going to do here.

* Test all event types.

* Adds unit test coverage for LocalFilesystemBuildFileTest.

* Test coverage.

* Fix trait.

* IsFacilityTraitTest.

* Ignore coverage on plugin managers and plugin base classes.

* Unit tests for the OnDemand entity event strategy plugin.

* Make internal logic a little more consistent.

* Disable current Entity Event Subscriber 👀

* Adds VerboseFalse entity event strategy plugin.

* Oops, my plugins didn't have a logger 😧

* Adds mocks for OnDemand test.

* Tests for VerboseFalse.

* ContentReleaseTriggerTrait::hasTriggeringChanges() did the Bad Thing™

* D'oh.

* Fixes test.

* Fix tests.

* VerboseFalse and tests.

* Specify an exception occurred when getting a node detail threw.

* Further tweaks.

* Ugh, I'm an idiot.

* Updates to VerboseFalse and VerboseFalseTest to support better debugging messages.

* Removes bypass from test.

* 'exception occurred' => 'Not Applicable'
  • Loading branch information
ndouglas authored and tjheffner committed Jul 28, 2023
1 parent 6f033b0 commit e1415cb
Show file tree
Hide file tree
Showing 59 changed files with 2,829 additions and 49 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ cypress_accessibility_violations.json
*.log
docroot/cypress/videos/
docroot/cypress/screenshots/
docroot/phpunit_coverage/
runner-results/

# PHPLOC command output.
Expand Down
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,11 @@
"! ./scripts/should-run-directly.sh || bin/phpunit --group unit --exclude-group disabled tests/phpunit --",
"./scripts/should-run-directly.sh || ddev composer va:test:phpunit-unit --"
],
"va:test:phpunit-coverage": [
"# Run PHPUnit (unit) tests.",
"! ./scripts/should-run-directly.sh || ./tests/scripts/phpunit-coverage.sh",
"./scripts/should-run-directly.sh || ddev composer va:test:phpunit-coverage --"
],
"va:test:status-error": [
"# Check Drupal status for errors.",
"! ./scripts/should-run-directly.sh || ./tests/scripts/status-error.sh",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ services:
logger.channel.va_gov_build_trigger:
parent: logger.channel_base
arguments: ['va_gov_build_trigger']
va_gov_build_trigger.entity_event_subscriber:
class: Drupal\va_gov_build_trigger\EventSubscriber\EntityEventSubscriber
arguments: ['@va_gov_build_trigger.build_requester', '@va_gov.build_trigger.environment_discovery']
tags:
- { name: event_subscriber }
va_gov.build_trigger.environment_discovery:
class: Drupal\va_gov_build_trigger\Environment\EnvironmentDiscovery
arguments: [ '@plugin.manager.va_gov.environment' ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Drupal\va_gov_content_release\EntityEvent\Strategy\Plugin;

use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\PluginBase;
use Drupal\Core\StringTranslation\TranslationInterface;
Expand All @@ -19,21 +20,34 @@ abstract class StrategyPluginBase extends PluginBase implements StrategyPluginIn

use StringTranslationTrait;

/**
* The logger service.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;

/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
TranslationInterface $stringTranslation
TranslationInterface $stringTranslation,
LoggerChannelFactoryInterface $loggerFactory
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->stringTranslation = $stringTranslation;
$this->logger = $loggerFactory->get('va_gov_content_release');
}

/**
* {@inheritDoc}
*
* @codeCoverageIgnore
*/
public static function create(
ContainerInterface $container,
Expand All @@ -45,7 +59,8 @@ public static function create(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('string_translation')
$container->get('string_translation'),
$container->get('logger.factory')
);
}

Expand All @@ -54,4 +69,9 @@ public static function create(
*/
abstract public function shouldTriggerContentRelease(VaNodeInterface $node) : bool;

/**
* {@inheritDoc}
*/
abstract public function getReasonMessage(VaNodeInterface $node) : string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,15 @@ interface StrategyPluginInterface extends PluginInspectionInterface {
*/
public function shouldTriggerContentRelease(VaNodeInterface $node) : bool;

/**
* Populate a reason message for triggering a content release.
*
* @param \Drupal\va_gov_content_types\Entity\VaNodeInterface $node
* The node that triggered the event.
*
* @return string
* The reason message.
*/
public function getReasonMessage(VaNodeInterface $node) : string;

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class StrategyPluginManager extends DefaultPluginManager implements StrategyPlug

/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public function __construct(
\Traversable $namespaces,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public function getStrategyId() : string {
$environment = $this->environmentDiscovery->getEnvironment();
return match (TRUE) {
$environment->isBrd() => static::STRATEGY_ON_DEMAND,
$environment->isTugboat() => static::STRATEGY_NEVER,
$environment->isLocalDev() => static::STRATEGY_NEVER,
$environment->isTugboat() => static::STRATEGY_VERBOSE_NEVER,
$environment->isLocalDev() => static::STRATEGY_VERBOSE_NEVER,
default => throw new CouldNotDetermineStrategyException('Could not determine a valid content release strategy for environment: ' . $environment->getRawValue()),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ interface ResolverInterface {
// Respond to node updates immediately.
const STRATEGY_ON_DEMAND = 'on_demand';
// Ignore node updates.
const STRATEGY_NEVER = 'test_false';
const STRATEGY_VERBOSE_NEVER = 'verbose_false';

/**
* Get the strategy plugin ID.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

namespace Drupal\va_gov_content_release\EventSubscriber;

use Drupal\core_event_dispatcher\EntityHookEvents;
use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent;
use Drupal\va_gov_content_release\EntityEvent\Strategy\Plugin\StrategyPluginManagerInterface;
use Drupal\va_gov_content_release\EntityEvent\Strategy\Resolver\ResolverInterface;
use Drupal\va_gov_content_release\Request\RequestInterface;
use Drupal\va_gov_content_types\Entity\VaNodeInterface;

/**
* Listens and responds to entity change events by (maybe) releasing content.
*/
class EntityEventSubscriber implements EntityEventSubscriberInterface {

/**
* The strategy plugin manager.
*
* @var \Drupal\va_gov_content_release\EntityEvent\Strategy\Plugin\StrategyPluginManagerInterface
*/
protected $strategyPluginManager;

/**
* The strategy resolver.
*
* @var \Drupal\va_gov_content_release\EntityEvent\Strategy\Resolver\ResolverInterface
*/
protected $strategyResolver;

/**
* The content release request service.
*
* @var \Drupal\va_gov_content_release\Request\RequestInterface
*/
protected $request;

/**
* Constructor.
*
* @param \Drupal\va_gov_content_release\EntityEvent\Strategy\Plugin\StrategyPluginManagerInterface $strategyPluginManager
* The strategy plugin manager.
* @param \Drupal\va_gov_content_release\EntityEvent\Strategy\Resolver\ResolverInterface $strategyResolver
* The strategy resolver.
* @param \Drupal\va_gov_content_release\Request\RequestInterface $request
* The content release request service.
*/
public function __construct(
StrategyPluginManagerInterface $strategyPluginManager,
ResolverInterface $strategyResolver,
RequestInterface $request
) {
$this->strategyPluginManager = $strategyPluginManager;
$this->strategyResolver = $strategyResolver;
$this->request = $request;
}

/**
* {@inheritdoc}
*/
public static function getSubscribedEvents(): array {
return [
EntityHookEvents::ENTITY_INSERT => 'onInsert',
EntityHookEvents::ENTITY_UPDATE => 'onUpdate',
EntityHookEvents::ENTITY_DELETE => 'onDelete',
];
}

/**
* {@inheritDoc}
*/
public function onInsert(AbstractEntityEvent $event) : void {
$entity = $event->getEntity();
if ($entity instanceof VaNodeInterface) {
$this->handleNodeUpdate($entity);
}
}

/**
* {@inheritDoc}
*/
public function onUpdate(AbstractEntityEvent $event) : void {
$entity = $event->getEntity();
if ($entity instanceof VaNodeInterface) {
$this->handleNodeUpdate($entity);
}
}

/**
* {@inheritDoc}
*/
public function onDelete(AbstractEntityEvent $event) : void {
$entity = $event->getEntity();
if ($entity instanceof VaNodeInterface) {
$this->handleNodeUpdate($entity);
}
}

/**
* {@inheritDoc}
*/
public function handleNodeUpdate(VaNodeInterface $node) : void {
$strategyId = $this->strategyResolver->getStrategyId();
$strategy = $this->strategyPluginManager->getStrategy($strategyId);
if ($strategy->shouldTriggerContentRelease($node)) {
$reason = $strategy->getReasonMessage($node);
$this->request->submitRequest($reason);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Drupal\va_gov_content_release\EventSubscriber;

use Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent;
use Drupal\va_gov_content_types\Entity\VaNodeInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
* Listens and responds to entity change events by (maybe) releasing content.
*/
interface EntityEventSubscriberInterface extends EventSubscriberInterface {

/**
* Handle entity insert events.
*
* @param \Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent $event
* The event object passed by the event dispatcher.
*/
public function onInsert(AbstractEntityEvent $event) : void;

/**
* Handle entity update events.
*
* @param \Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent $event
* The event object passed by the event dispatcher.
*/
public function onUpdate(AbstractEntityEvent $event) : void;

/**
* Handle entity delete events.
*
* @param \Drupal\core_event_dispatcher\Event\Entity\AbstractEntityEvent $event
* The event object passed by the event dispatcher.
*/
public function onDelete(AbstractEntityEvent $event) : void;

/**
* Handle entity updates for a specific node.
*
* @param \Drupal\va_gov_content_types\Entity\VaNodeInterface $node
* The node to check for updates.
*/
public function handleNodeUpdate(VaNodeInterface $node) : void;

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@ public function shouldTriggerContentRelease(VaNodeInterface $node) : bool {
throw new StrategyErrorException('This is a test exception.');
}

/**
* {@inheritDoc}
*/
public function getReasonMessage(VaNodeInterface $node) : string {
return $this->t('This should never be reached because shouldTriggerContentRelease() throws an exception.');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function shouldTriggerContentRelease(VaNodeInterface $node) : bool {
return FALSE;
}

/**
* {@inheritDoc}
*/
public function getReasonMessage(VaNodeInterface $node) : string {
return $this->t('This should never be reached because the strategy always indicates that no content release should be triggered.');
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,16 @@ public function shouldTriggerContentRelease(VaNodeInterface $node) : bool {
return $node->shouldTriggerContentRelease();
}

/**
* {@inheritDoc}
*/
public function getReasonMessage(VaNodeInterface $node) : string {
$variables = [
'%link_to_node' => $node->toLink(NULL, 'canonical', ['absolute' => TRUE])->toString(),
'%nid' => $node->id(),
'%type' => $node->getType(),
];
return $this->t('A content release was triggered by a change to %type: %link_to_node (node%nid).', $variables);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ public function shouldTriggerContentRelease(VaNodeInterface $node) : bool {
return TRUE;
}

/**
* {@inheritDoc}
*/
public function getReasonMessage(VaNodeInterface $node) : string {
return $this->t('This event strategy _always_ returns TRUE.');
}

}
Loading

0 comments on commit e1415cb

Please sign in to comment.