Jared Whiklo
8 years ago
committed by
GitHub
17 changed files with 602 additions and 51 deletions
@ -0,0 +1,56 @@
|
||||
uuid: 3ef878c1-3187-4141-aba1-2aac5acc9366 |
||||
langcode: en |
||||
status: true |
||||
dependencies: |
||||
module: |
||||
- islandora |
||||
- rules |
||||
enforced: |
||||
module: |
||||
- islandora |
||||
id: broadcast_create_event |
||||
label: 'Broadcast Create Event' |
||||
events: |
||||
- |
||||
event_name: 'rules_entity_insert:fedora_resource' |
||||
description: 'Broadcasts a ''Create'' event when a user creates a new FedoraResource.' |
||||
tags: |
||||
- '' |
||||
config_version: '3' |
||||
expression: |
||||
id: rules_rule |
||||
uuid: d25688b0-eef1-46b0-acf4-98a6d1ccd76a |
||||
conditions: |
||||
id: rules_and |
||||
uuid: 6648629a-0b52-4352-bb85-df2cbafb7db2 |
||||
conditions: { } |
||||
actions: |
||||
id: rules_action_set |
||||
uuid: 957db51c-d055-4350-8caa-4ad773172e87 |
||||
actions: |
||||
- |
||||
id: rules_action |
||||
uuid: d483f5bc-fd36-4d7c-baa4-66234b438d1a |
||||
context_values: { } |
||||
context_mapping: |
||||
entity: fedora_resource |
||||
user: '@user.current_user_context:current_user' |
||||
context_processors: { } |
||||
provides_mapping: { } |
||||
action_id: islandora_create_event_generator |
||||
- |
||||
id: rules_action |
||||
uuid: bbcd7317-2879-4961-a79d-5827fab4e194 |
||||
context_values: |
||||
type: info |
||||
repeat: false |
||||
context_mapping: |
||||
message: event_message |
||||
context_processors: |
||||
type: |
||||
rules_tokens: { } |
||||
repeat: |
||||
rules_tokens: { } |
||||
provides_mapping: { } |
||||
action_id: rules_system_message |
||||
|
@ -0,0 +1,56 @@
|
||||
uuid: 16c10c0c-68a7-4b75-ab8f-ab1778d7c2e2 |
||||
langcode: en |
||||
status: true |
||||
dependencies: |
||||
module: |
||||
- islandora |
||||
- rules |
||||
enforced: |
||||
module: |
||||
- islandora |
||||
id: broadcast_delete_event |
||||
label: 'Broadcast Delete Event' |
||||
events: |
||||
- |
||||
event_name: 'rules_entity_delete:fedora_resource' |
||||
description: 'Broadcasts a ''Delete'' event when a user deletes a FedoraResource.' |
||||
tags: |
||||
- '' |
||||
config_version: '3' |
||||
expression: |
||||
id: rules_rule |
||||
uuid: dd0ffd15-c6b4-4578-8b54-9c9debae0216 |
||||
conditions: |
||||
id: rules_and |
||||
uuid: 9c50dd1f-ec9a-4cd8-9d18-27c860815a1c |
||||
conditions: { } |
||||
actions: |
||||
id: rules_action_set |
||||
uuid: 7f1f1fc6-d5d2-450b-ba14-225e8e56448f |
||||
actions: |
||||
- |
||||
id: rules_action |
||||
uuid: 7ebcda8c-3626-4bff-a536-60caf9f46708 |
||||
context_values: { } |
||||
context_mapping: |
||||
entity: fedora_resource |
||||
user: '@user.current_user_context:current_user' |
||||
context_processors: { } |
||||
provides_mapping: { } |
||||
action_id: islandora_delete_event_generator |
||||
- |
||||
id: rules_action |
||||
uuid: bacf7f09-1d54-46f2-a4b9-e345992a604d |
||||
context_values: |
||||
type: info |
||||
repeat: false |
||||
context_mapping: |
||||
message: event_message |
||||
context_processors: |
||||
type: |
||||
rules_tokens: { } |
||||
repeat: |
||||
rules_tokens: { } |
||||
provides_mapping: { } |
||||
action_id: rules_system_message |
||||
|
@ -0,0 +1,56 @@
|
||||
uuid: 3bfa9b33-b0a7-437e-a949-e50178a702fe |
||||
langcode: en |
||||
status: true |
||||
dependencies: |
||||
module: |
||||
- islandora |
||||
- rules |
||||
enforced: |
||||
module: |
||||
- islandora |
||||
id: broadcast_update_event |
||||
label: 'Broadcast Update Event' |
||||
events: |
||||
- |
||||
event_name: 'rules_entity_update:fedora_resource' |
||||
description: 'Broadcasts a ''Update'' event when a user updates a FedoraResource.' |
||||
tags: |
||||
- '' |
||||
config_version: '3' |
||||
expression: |
||||
id: rules_rule |
||||
uuid: 55563628-5ea2-4459-989e-0ce573f50aaa |
||||
conditions: |
||||
id: rules_and |
||||
uuid: 8113ab9f-7af6-4315-8701-60812bb86190 |
||||
conditions: { } |
||||
actions: |
||||
id: rules_action_set |
||||
uuid: 78330180-141b-404e-8bf3-b7c9d8dcc748 |
||||
actions: |
||||
- |
||||
id: rules_action |
||||
uuid: 86a6f157-8d0c-4b3c-bd8e-d4c3ce4794a7 |
||||
context_values: { } |
||||
context_mapping: |
||||
entity: fedora_resource |
||||
user: '@user.current_user_context:current_user' |
||||
context_processors: { } |
||||
provides_mapping: { } |
||||
action_id: islandora_update_event_generator |
||||
- |
||||
id: rules_action |
||||
uuid: f77c64ef-c737-4143-89b4-e1b6599232b5 |
||||
context_values: |
||||
type: info |
||||
repeat: false |
||||
context_mapping: |
||||
message: event_message |
||||
context_processors: |
||||
type: |
||||
rules_tokens: { } |
||||
repeat: |
||||
rules_tokens: { } |
||||
provides_mapping: { } |
||||
action_id: rules_system_message |
||||
|
@ -0,0 +1,50 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora\EventGenerator; |
||||
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface; |
||||
use Drupal\rules\Core\RulesActionBase; |
||||
use Symfony\Component\DependencyInjection\ContainerInterface; |
||||
|
||||
/** |
||||
* Abstract base class for EventGenerator RulesActions. Sets up DI. |
||||
*/ |
||||
abstract class EventGeneratorActionBase extends RulesActionBase implements ContainerFactoryPluginInterface { |
||||
|
||||
/** |
||||
* The event generator that will serialize the events. |
||||
* |
||||
* @var \Drupal\islandora\EventGenerator\EventGeneratorInterface |
||||
*/ |
||||
protected $eventGenerator; |
||||
|
||||
/** |
||||
* Constructs a MessageBroadcaster action. |
||||
* |
||||
* @param array $configuration |
||||
* A configuration array containing information about the plugin instance. |
||||
* @param string $plugin_id |
||||
* The plugin ID for the plugin instance. |
||||
* @param mixed $plugin_definition |
||||
* The plugin implementation definition. |
||||
* @param \Drupal\islandora\EventGenerator\EventGeneratorInterface $event_generator |
||||
* The EventGenerator service. |
||||
*/ |
||||
public function __construct(array $configuration, $plugin_id, $plugin_definition, EventGeneratorInterface $event_generator) { |
||||
parent::__construct($configuration, $plugin_id, $plugin_definition); |
||||
$this->eventGenerator = $event_generator; |
||||
} |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { |
||||
return new static( |
||||
$configuration, |
||||
$plugin_id, |
||||
$plugin_definition, |
||||
$container->get('islandora.eventgenerator') |
||||
); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora\Plugin\RulesAction; |
||||
|
||||
use Drupal\Core\Entity\EntityInterface; |
||||
use Drupal\user\UserInterface; |
||||
use Drupal\islandora\EventGenerator\EventGeneratorActionBase; |
||||
|
||||
/** |
||||
* Provides an action to generate a serialized Update event. |
||||
* |
||||
* @RulesAction( |
||||
* id = "islandora_create_event_generator", |
||||
* label = @Translation("Generate Create Event"), |
||||
* category = @Translation("Islandora"), |
||||
* context = { |
||||
* "entity" = @ContextDefinition("entity", |
||||
* label = @Translation("Created entity"), |
||||
* description = @Translation("The entity that was created") |
||||
* ), |
||||
* "user" = @ContextDefinition("entity:user", |
||||
* label = @Translation("User"), |
||||
* description = @Translation("The user who created the entity") |
||||
* ), |
||||
* }, |
||||
* provides = { |
||||
* "event_message" = @ContextDefinition("string", |
||||
* label = @Translation("Serialized event message") |
||||
* ) |
||||
* } |
||||
* ) |
||||
*/ |
||||
class CreateEventGenerator extends EventGeneratorActionBase { |
||||
|
||||
/** |
||||
* Provides the serialized create event to downstream actions. |
||||
*/ |
||||
protected function doExecute(EntityInterface $entity, UserInterface $user) { |
||||
$this->setProvidedValue('event_message', $this->eventGenerator->generateCreateEvent($entity, $user)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora\Plugin\RulesAction; |
||||
|
||||
use Drupal\Core\Entity\EntityInterface; |
||||
use Drupal\user\UserInterface; |
||||
use Drupal\islandora\EventGenerator\EventGeneratorActionBase; |
||||
|
||||
/** |
||||
* Provides an action to generate a serialized Update event. |
||||
* |
||||
* @RulesAction( |
||||
* id = "islandora_delete_event_generator", |
||||
* label = @Translation("Generate Delete Event"), |
||||
* category = @Translation("Islandora"), |
||||
* context = { |
||||
* "entity" = @ContextDefinition("entity", |
||||
* label = @Translation("Deleted entity"), |
||||
* description = @Translation("The entity that was deleted") |
||||
* ), |
||||
* "user" = @ContextDefinition("entity:user", |
||||
* label = @Translation("User"), |
||||
* description = @Translation("The user who deleted the entity") |
||||
* ), |
||||
* }, |
||||
* provides = { |
||||
* "event_message" = @ContextDefinition("string", |
||||
* label = @Translation("Serialized event message") |
||||
* ) |
||||
* } |
||||
* ) |
||||
*/ |
||||
class DeleteEventGenerator extends EventGeneratorActionBase { |
||||
|
||||
/** |
||||
* Provides the serialized delete event to downstream actions. |
||||
*/ |
||||
protected function doExecute(EntityInterface $entity, UserInterface $user) { |
||||
$this->setProvidedValue('event_message', $this->eventGenerator->generateDeleteEvent($entity, $user)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,42 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\islandora\Plugin\RulesAction; |
||||
|
||||
use Drupal\Core\Entity\EntityInterface; |
||||
use Drupal\user\UserInterface; |
||||
use Drupal\islandora\EventGenerator\EventGeneratorActionBase; |
||||
|
||||
/** |
||||
* Provides an action to generate a serialized Update event. |
||||
* |
||||
* @RulesAction( |
||||
* id = "islandora_update_event_generator", |
||||
* label = @Translation("Generate Update Event"), |
||||
* category = @Translation("Islandora"), |
||||
* context = { |
||||
* "entity" = @ContextDefinition("entity", |
||||
* label = @Translation("Updated entity"), |
||||
* description = @Translation("The entity that was updated") |
||||
* ), |
||||
* "user" = @ContextDefinition("entity:user", |
||||
* label = @Translation("User"), |
||||
* description = @Translation("The user who updated the entity") |
||||
* ), |
||||
* }, |
||||
* provides = { |
||||
* "event_message" = @ContextDefinition("string", |
||||
* label = @Translation("Serialized event message") |
||||
* ) |
||||
* } |
||||
* ) |
||||
*/ |
||||
class UpdateEventGenerator extends EventGeneratorActionBase { |
||||
|
||||
/** |
||||
* Provides the serialized update event to downstream actions. |
||||
*/ |
||||
protected function doExecute(EntityInterface $entity, UserInterface $user) { |
||||
$this->setProvidedValue('event_message', $this->eventGenerator->generateUpdateEvent($entity, $user)); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\Tests\islandora\Kernel; |
||||
|
||||
/** |
||||
* Tests the basic behavior of the CreateEventGenerator rules action. |
||||
* |
||||
* @group islandora |
||||
* @coversDefaultClass \Drupal\islandora\Plugin\RulesAction\CreateEventGenerator |
||||
*/ |
||||
class CreateEventGeneratorTest extends EventGeneratorActionTestBase { |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setUp() { |
||||
parent::setUp(); |
||||
|
||||
// Get an instance of the action. |
||||
$this->action = $this->actionManager->createInstance('islandora_create_event_generator'); |
||||
} |
||||
|
||||
/** |
||||
* Tests the CreateEventGenerator action. |
||||
* |
||||
* @covers \Drupal\islandora\Plugin\RulesAction\CreateEventGenerator::execute |
||||
*/ |
||||
public function testExecute() { |
||||
// Execute the action and get the message it outputs. |
||||
$msg = $this->assertExecution(); |
||||
|
||||
// Assert it's outputs a 'Create' event. |
||||
$this->assertTrue($msg["type"] == "Create", "Event must be of type 'Create'."); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\Tests\islandora\Kernel; |
||||
|
||||
/** |
||||
* Tests the basic behavior of the DeleteEventGenerator rules action. |
||||
* |
||||
* @group islandora |
||||
* @coversDefaultClass \Drupal\islandora\Plugin\RulesAction\DeleteEventGenerator |
||||
*/ |
||||
class DeleteEventGeneratorTest extends EventGeneratorActionTestBase { |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setUp() { |
||||
parent::setUp(); |
||||
|
||||
// Get an instance of the action. |
||||
$this->action = $this->actionManager->createInstance('islandora_delete_event_generator'); |
||||
} |
||||
|
||||
/** |
||||
* Tests the DeleteEventGenerator action. |
||||
* |
||||
* @covers \Drupal\islandora\Plugin\RulesAction\DeleteEventGenerator::execute |
||||
*/ |
||||
public function testExecute() { |
||||
// Execute the action and get the message it outputs. |
||||
$msg = $this->assertExecution(); |
||||
|
||||
// Assert it's outputs a 'Delete' event. |
||||
$this->assertTrue($msg["type"] == "Delete", "Event must be of type 'Delete'."); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,63 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\Tests\islandora\Kernel; |
||||
|
||||
use Drupal\islandora\Entity\FedoraResource; |
||||
use Drupal\islandora\EventGenerator\EventGenerator; |
||||
use Drupal\simpletest\UserCreationTrait; |
||||
|
||||
/** |
||||
* Base class for testing EventGenerator functionality. |
||||
*/ |
||||
abstract class EventGeneratorActionTestBase extends EventGeneratorTestBase { |
||||
|
||||
/** |
||||
* Action plugin manager. |
||||
* |
||||
* @var \Drupal\rules\Core\RulesActionManagerInterface |
||||
*/ |
||||
protected $actionManager; |
||||
|
||||
/** |
||||
* Action to test. |
||||
* |
||||
* @var Drupal\rules\Core\RulesActionInterface |
||||
*/ |
||||
protected $action; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setUp() { |
||||
parent::setUp(); |
||||
|
||||
// Get the action manager |
||||
$this->actionManager = $this->container->get('plugin.manager.rules_action'); |
||||
} |
||||
|
||||
/** |
||||
* Utility function to bootstrap an action, run it, and do basic asserts. |
||||
* |
||||
* @return array |
||||
* The event message, as an array. |
||||
*/ |
||||
protected function assertExecution() { |
||||
|
||||
// Set the required contexts for the action to run. |
||||
$this->action->setContextValue('entity', $this->entity); |
||||
$this->action->setContextValue('user', $this->user); |
||||
|
||||
// Execute the action. |
||||
$this->action->execute(); |
||||
|
||||
// Assert some basics. |
||||
$message_str = $this->action->getProvidedContext('event_message')->getContextValue(); |
||||
$this->assertNotEmpty($message_str, "Event message must not be empty."); |
||||
$message = json_decode($message_str, TRUE); |
||||
$this->assertTrue(array_key_exists('type', $message), "Event has type key."); |
||||
|
||||
// Return the event message. |
||||
return $message; |
||||
} |
||||
|
||||
} |
@ -0,0 +1,52 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\Tests\islandora\Kernel; |
||||
|
||||
use Drupal\islandora\Entity\FedoraResource; |
||||
use Drupal\islandora\EventGenerator\EventGenerator; |
||||
use Drupal\simpletest\UserCreationTrait; |
||||
|
||||
/** |
||||
* Base class for testing EventGenerator functionality. |
||||
*/ |
||||
abstract class EventGeneratorTestBase extends IslandoraKernelTestBase { |
||||
|
||||
use UserCreationTrait { |
||||
createUser as drupalCreateUser; |
||||
} |
||||
|
||||
/** |
||||
* User entity. |
||||
* |
||||
* @var \Drupal\user\UserInterface |
||||
*/ |
||||
protected $user; |
||||
|
||||
/** |
||||
* Fedora resource entity. |
||||
* |
||||
* @var \Drupal\islandora\FedoraResourceInterface |
||||
*/ |
||||
protected $entity; |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setUp() { |
||||
parent::setUp(); |
||||
|
||||
// Create a test user. |
||||
$this->user = $this->drupalCreateUser(); |
||||
|
||||
// Create a test entity. |
||||
$this->entity = FedoraResource::create([ |
||||
"type" => "rdf_source", |
||||
"uid" => 1, |
||||
"name" => "Test Fixture", |
||||
"langcode" => "und", |
||||
"status" => 1, |
||||
]); |
||||
$this->entity->save(); |
||||
} |
||||
|
||||
} |
@ -0,0 +1,36 @@
|
||||
<?php |
||||
|
||||
namespace Drupal\Tests\islandora\Kernel; |
||||
|
||||
/** |
||||
* Tests the basic behavior of the UpdateEventGenerator rules action. |
||||
* |
||||
* @group islandora |
||||
* @coversDefaultClass \Drupal\islandora\Plugin\RulesAction\UpdateEventGenerator |
||||
*/ |
||||
class UpdateEventGeneratorTest extends EventGeneratorActionTestBase { |
||||
|
||||
/** |
||||
* {@inheritdoc} |
||||
*/ |
||||
public function setUp() { |
||||
parent::setUp(); |
||||
|
||||
// Get an instance of the action. |
||||
$this->action = $this->actionManager->createInstance('islandora_update_event_generator'); |
||||
} |
||||
|
||||
/** |
||||
* Tests the UpdateEventGenerator action. |
||||
* |
||||
* @covers \Drupal\islandora\Plugin\RulesAction\UpdateEventGenerator::execute |
||||
*/ |
||||
public function testExecute() { |
||||
// Execute the action and get the message it outputs. |
||||
$msg = $this->assertExecution(); |
||||
|
||||
// Assert it's outputs a 'Update' event. |
||||
$this->assertTrue($msg["type"] == "Update", "Event must be of type 'Update'."); |
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue