From beb31dfe3f02d85adcfaca2a5bace97a8279c9d8 Mon Sep 17 00:00:00 2001 From: Danny Lamb Date: Fri, 9 Dec 2016 20:01:07 +0000 Subject: [PATCH] Rules actions for generating AS2 event messages. Test refactoring. --- .../rules.reaction.broadcast_create_event.yml | 56 +++++++++++++++ .../rules.reaction.broadcast_delete_event.yml | 56 +++++++++++++++ .../rules.reaction.broadcast_update_event.yml | 56 +++++++++++++++ .../EventGeneratorActionBase.php | 45 ++++++++++++ .../RulesAction/CreateEventGenerator.php | 42 +++++++++++ .../RulesAction/DeleteEventGenerator.php | 42 +++++++++++ .../Plugin/RulesAction/MessageBroadcaster.php | 71 +++++++++++++++++++ .../RulesAction/UpdateEventGenerator.php | 42 +++++++++++ .../src/Kernel/CreateEventGeneratorTest.php | 36 ++++++++++ .../src/Kernel/DeleteEventGeneratorTest.php | 36 ++++++++++ .../Kernel/EventGeneratorActionTestBase.php | 63 ++++++++++++++++ .../tests/src/Kernel/EventGeneratorTest.php | 67 ++++++----------- .../src/Kernel/EventGeneratorTestBase.php | 52 ++++++++++++++ .../src/Kernel/IslandoraKernelTestBase.php | 3 +- .../src/Kernel/UpdateEventGeneratorTest.php | 36 ++++++++++ .../tests/src/Kernel/VectorClockTest.php | 7 +- 16 files changed, 660 insertions(+), 50 deletions(-) create mode 100644 islandora/config/install/rules.reaction.broadcast_create_event.yml create mode 100644 islandora/config/install/rules.reaction.broadcast_delete_event.yml create mode 100644 islandora/config/install/rules.reaction.broadcast_update_event.yml create mode 100644 islandora/src/EventGenerator/EventGeneratorActionBase.php create mode 100644 islandora/src/Plugin/RulesAction/CreateEventGenerator.php create mode 100644 islandora/src/Plugin/RulesAction/DeleteEventGenerator.php create mode 100644 islandora/src/Plugin/RulesAction/MessageBroadcaster.php create mode 100644 islandora/src/Plugin/RulesAction/UpdateEventGenerator.php create mode 100644 islandora/tests/src/Kernel/CreateEventGeneratorTest.php create mode 100644 islandora/tests/src/Kernel/DeleteEventGeneratorTest.php create mode 100644 islandora/tests/src/Kernel/EventGeneratorActionTestBase.php create mode 100644 islandora/tests/src/Kernel/EventGeneratorTestBase.php create mode 100644 islandora/tests/src/Kernel/UpdateEventGeneratorTest.php diff --git a/islandora/config/install/rules.reaction.broadcast_create_event.yml b/islandora/config/install/rules.reaction.broadcast_create_event.yml new file mode 100644 index 00000000..e740c0b5 --- /dev/null +++ b/islandora/config/install/rules.reaction.broadcast_create_event.yml @@ -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 + diff --git a/islandora/config/install/rules.reaction.broadcast_delete_event.yml b/islandora/config/install/rules.reaction.broadcast_delete_event.yml new file mode 100644 index 00000000..96c6d411 --- /dev/null +++ b/islandora/config/install/rules.reaction.broadcast_delete_event.yml @@ -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 + diff --git a/islandora/config/install/rules.reaction.broadcast_update_event.yml b/islandora/config/install/rules.reaction.broadcast_update_event.yml new file mode 100644 index 00000000..f0529981 --- /dev/null +++ b/islandora/config/install/rules.reaction.broadcast_update_event.yml @@ -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 + diff --git a/islandora/src/EventGenerator/EventGeneratorActionBase.php b/islandora/src/EventGenerator/EventGeneratorActionBase.php new file mode 100644 index 00000000..9aca707b --- /dev/null +++ b/islandora/src/EventGenerator/EventGeneratorActionBase.php @@ -0,0 +1,45 @@ +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') + ); + } + +} diff --git a/islandora/src/Plugin/RulesAction/CreateEventGenerator.php b/islandora/src/Plugin/RulesAction/CreateEventGenerator.php new file mode 100644 index 00000000..e60b3586 --- /dev/null +++ b/islandora/src/Plugin/RulesAction/CreateEventGenerator.php @@ -0,0 +1,42 @@ +setProvidedValue('event_message', $this->eventGenerator->generateCreateEvent($entity, $user)); + } + +} diff --git a/islandora/src/Plugin/RulesAction/DeleteEventGenerator.php b/islandora/src/Plugin/RulesAction/DeleteEventGenerator.php new file mode 100644 index 00000000..4f2c3b17 --- /dev/null +++ b/islandora/src/Plugin/RulesAction/DeleteEventGenerator.php @@ -0,0 +1,42 @@ +setProvidedValue('event_message', $this->eventGenerator->generateDeleteEvent($entity, $user)); + } + +} diff --git a/islandora/src/Plugin/RulesAction/MessageBroadcaster.php b/islandora/src/Plugin/RulesAction/MessageBroadcaster.php new file mode 100644 index 00000000..9e956ab6 --- /dev/null +++ b/islandora/src/Plugin/RulesAction/MessageBroadcaster.php @@ -0,0 +1,71 @@ +setProvidedValue('event_message', $this->eventGenerator->generateUpdateEvent($entity, $user)); + } + +} diff --git a/islandora/tests/src/Kernel/CreateEventGeneratorTest.php b/islandora/tests/src/Kernel/CreateEventGeneratorTest.php new file mode 100644 index 00000000..5353b075 --- /dev/null +++ b/islandora/tests/src/Kernel/CreateEventGeneratorTest.php @@ -0,0 +1,36 @@ +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'."); + } + +} diff --git a/islandora/tests/src/Kernel/DeleteEventGeneratorTest.php b/islandora/tests/src/Kernel/DeleteEventGeneratorTest.php new file mode 100644 index 00000000..63c72997 --- /dev/null +++ b/islandora/tests/src/Kernel/DeleteEventGeneratorTest.php @@ -0,0 +1,36 @@ +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'."); + } + +} diff --git a/islandora/tests/src/Kernel/EventGeneratorActionTestBase.php b/islandora/tests/src/Kernel/EventGeneratorActionTestBase.php new file mode 100644 index 00000000..e5b777ba --- /dev/null +++ b/islandora/tests/src/Kernel/EventGeneratorActionTestBase.php @@ -0,0 +1,63 @@ +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; + } + +} diff --git a/islandora/tests/src/Kernel/EventGeneratorTest.php b/islandora/tests/src/Kernel/EventGeneratorTest.php index e1b63559..52906f0e 100644 --- a/islandora/tests/src/Kernel/EventGeneratorTest.php +++ b/islandora/tests/src/Kernel/EventGeneratorTest.php @@ -2,34 +2,15 @@ namespace Drupal\Tests\islandora\Kernel; -use Drupal\islandora\Entity\FedoraResource; use Drupal\islandora\EventGenerator\EventGenerator; -use Drupal\simpletest\UserCreationTrait; /** * Tests the EventGenerator default implementation. * * @group islandora + * @coversDefaultClass \Drupal\islandora\EventGenerator\EventGenerator */ -class EventGeneratorTest extends IslandoraKernelTestBase { - - use UserCreationTrait { - createUser as drupalCreateUser; - } - - /** - * User entity. - * - * @var \Drupal\user\UserInterface - */ - protected $user; - - /** - * Fedora resource entity. - * - * @var \Drupal\islandora\FedoraResourceInterface - */ - protected $entity; +class EventGeneratorTest extends EventGeneratorTestBase { /** * The EventGenerator to test. @@ -44,55 +25,47 @@ class EventGeneratorTest extends IslandoraKernelTestBase { 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(); - // Create the event generator so we can test it. $this->eventGenerator = new EventGenerator(); } /** * Tests the generateCreateEvent() method. + * + * @covers \Drupal\islandora\EventGenerator\EventGenerator::generateCreateEvent */ public function testGenerateCreateEvent() { $json = $this->eventGenerator->generateCreateEvent($this->entity, $this->user); $msg = json_decode($json, TRUE); $this->assertBasicStructure($msg); - $this->assertTrue($msg["type"] == "Create", "Event type is 'Create'."); + $this->assertTrue($msg["type"] == "Create", "Event must be of type 'Create'."); } /** * Tests the generateUpdateEvent() method. + * + * @covers \Drupal\islandora\EventGenerator\EventGenerator::generateUpdateEvent */ public function testGenerateUpdateEvent() { $json = $this->eventGenerator->generateUpdateEvent($this->entity, $this->user); $msg = json_decode($json, TRUE); $this->assertBasicStructure($msg); - $this->assertTrue($msg["type"] == "Update", "Event type is 'Update'."); + $this->assertTrue($msg["type"] == "Update", "Event must be of type 'Update'."); } /** * Tests the generateDeleteEvent() method. + * + * @covers \Drupal\islandora\EventGenerator\EventGenerator::generateDeleteEvent */ public function testGenerateDeleteEvent() { $json = $this->eventGenerator->generateDeleteEvent($this->entity, $this->user); $msg = json_decode($json, TRUE); $this->assertBasicStructure($msg); - $this->assertTrue($msg["type"] == "Delete", "Event type is 'Delete'."); - + $this->assertTrue($msg["type"] == "Delete", "Event must be of type 'Delete'."); } /** @@ -104,21 +77,21 @@ class EventGeneratorTest extends IslandoraKernelTestBase { protected function assertBasicStructure(array $msg) { // Looking for @context. $this->assertTrue(array_key_exists('@context', $msg), "Context key exists"); - $this->assertTrue($msg["@context"] == "https://www.w3.org/ns/activitystreams", "Context is activity stream."); + $this->assertTrue($msg["@context"] == "https://www.w3.org/ns/activitystreams", "Context must be activity stream."); // Make sure it has a type. - $this->assertTrue(array_key_exists('type', $msg), "Type key exists"); + $this->assertTrue(array_key_exists('type', $msg), "Message must have 'type' key."); // Make sure the actor exists, is a person, and has a uri. - $this->assertTrue(array_key_exists('actor', $msg), "Actor key exists"); - $this->assertTrue(array_key_exists("type", $msg["actor"]), "Type key exists for actor."); - $this->assertTrue($msg["actor"]["type"] == "Person", "Actor is a person."); - $this->assertTrue(array_key_exists("id", $msg["actor"]), "Id key exists for actor."); - $this->assertTrue($msg["actor"]["id"] == $this->user->toUrl()->setAbsolute()->toString(), "Id is user's uri"); + $this->assertTrue(array_key_exists('actor', $msg), "Message must have 'actor' key."); + $this->assertTrue(array_key_exists("type", $msg["actor"]), "Actor must have 'type' key."); + $this->assertTrue($msg["actor"]["type"] == "Person", "Actor must be a 'Person'."); + $this->assertTrue(array_key_exists("id", $msg["actor"]), "Actor must have 'id' key."); + $this->assertTrue($msg["actor"]["id"] == $this->user->toUrl()->setAbsolute()->toString(), "Id must be a user's uri"); // Make sure the object exists and is a uri. - $this->assertTrue(array_key_exists('object', $msg), "Object key exists"); - $this->assertTrue($msg["object"] == $this->entity->toUrl()->setAbsolute()->toString(), "Object is entity uri."); + $this->assertTrue(array_key_exists('object', $msg), "Message must have 'object' key."); + $this->assertTrue($msg["object"] == $this->entity->toUrl()->setAbsolute()->toString(), "Object must be an entity uri."); } } diff --git a/islandora/tests/src/Kernel/EventGeneratorTestBase.php b/islandora/tests/src/Kernel/EventGeneratorTestBase.php new file mode 100644 index 00000000..eb63bb35 --- /dev/null +++ b/islandora/tests/src/Kernel/EventGeneratorTestBase.php @@ -0,0 +1,52 @@ +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(); + } + +} diff --git a/islandora/tests/src/Kernel/IslandoraKernelTestBase.php b/islandora/tests/src/Kernel/IslandoraKernelTestBase.php index fb5932ab..7dc3a3d5 100644 --- a/islandora/tests/src/Kernel/IslandoraKernelTestBase.php +++ b/islandora/tests/src/Kernel/IslandoraKernelTestBase.php @@ -6,7 +6,6 @@ use Drupal\KernelTests\KernelTestBase; /** * Abstract base class for Islandora kernel tests. - * */ abstract class IslandoraKernelTestBase extends KernelTestBase { @@ -27,6 +26,8 @@ abstract class IslandoraKernelTestBase extends KernelTestBase { 'serialization', 'rest', 'rdf', + 'typed_data', + 'rules', 'jsonld', 'islandora' ]; diff --git a/islandora/tests/src/Kernel/UpdateEventGeneratorTest.php b/islandora/tests/src/Kernel/UpdateEventGeneratorTest.php new file mode 100644 index 00000000..00653f52 --- /dev/null +++ b/islandora/tests/src/Kernel/UpdateEventGeneratorTest.php @@ -0,0 +1,36 @@ +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'."); + } + +} diff --git a/islandora/tests/src/Kernel/VectorClockTest.php b/islandora/tests/src/Kernel/VectorClockTest.php index c73df8ee..310ca76d 100644 --- a/islandora/tests/src/Kernel/VectorClockTest.php +++ b/islandora/tests/src/Kernel/VectorClockTest.php @@ -9,6 +9,7 @@ use Drupal\simpletest\UserCreationTrait; * Tests the basic behavior of a vector clock. * * @group islandora + * @coversDefaultClass \Drupal\islandora\Entity\FedoraResource */ class VectorClockTest extends IslandoraKernelTestBase { @@ -39,16 +40,18 @@ class VectorClockTest extends IslandoraKernelTestBase { /** * Tests the basic behavior of the vector clock. + * + * @covers \Drupal\islandora\Entity\FedoraResource::getVclock */ public function testVectorClock() { // Check the vclock is set to 0 when a new entity is created. - $this->assertTrue($this->entity->getVclock() == 0, "Vector clock initialized to zero."); + $this->assertTrue($this->entity->getVclock() == 0, "Vector clock must be initialized to zero."); // Edit the entity. $this->entity->setName("Edited Test Fixture")->save(); // Check the vclock is incremented when the entity is updated. - $this->assertTrue($this->entity->getVclock() == 1, "Vector clock incremented on update."); + $this->assertTrue($this->entity->getVclock() == 1, "Vector clock must be incremented on update."); } }