Browse Source

Merge pull request #23 from dannylamb/rules

Rules actions for event message
pull/756/head
Jared Whiklo 8 years ago committed by GitHub
parent
commit
53b73371aa
  1. 6
      .scripts/travis_setup.sh
  2. 56
      islandora/config/install/rules.reaction.broadcast_create_event.yml
  3. 56
      islandora/config/install/rules.reaction.broadcast_delete_event.yml
  4. 56
      islandora/config/install/rules.reaction.broadcast_update_event.yml
  5. 3
      islandora/islandora.info.yml
  6. 50
      islandora/src/EventGenerator/EventGeneratorActionBase.php
  7. 42
      islandora/src/Plugin/RulesAction/CreateEventGenerator.php
  8. 42
      islandora/src/Plugin/RulesAction/DeleteEventGenerator.php
  9. 42
      islandora/src/Plugin/RulesAction/UpdateEventGenerator.php
  10. 36
      islandora/tests/src/Kernel/CreateEventGeneratorTest.php
  11. 36
      islandora/tests/src/Kernel/DeleteEventGeneratorTest.php
  12. 63
      islandora/tests/src/Kernel/EventGeneratorActionTestBase.php
  13. 67
      islandora/tests/src/Kernel/EventGeneratorTest.php
  14. 52
      islandora/tests/src/Kernel/EventGeneratorTestBase.php
  15. 3
      islandora/tests/src/Kernel/IslandoraKernelTestBase.php
  16. 36
      islandora/tests/src/Kernel/UpdateEventGeneratorTest.php
  17. 7
      islandora/tests/src/Kernel/VectorClockTest.php

6
.scripts/travis_setup.sh

@ -68,6 +68,12 @@ drush dl search_api
drush -y pm-uninstall search
drush en -y search_api
drush dl typed_data
drush en -y typed_data
drush dl rules
drush en -y rules
cd $HOME/drupal/modules
git clone https://github.com/DiegoPino/claw-jsonld.git
drush en -y jsonld

56
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

56
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

56
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

3
islandora/islandora.info.yml

@ -13,4 +13,5 @@ dependencies:
- options
- inline_entity_form
- jsonld
version: '8.x-1.x-dev'
- rules
version: '8.x-1.x-dev'

50
islandora/src/EventGenerator/EventGeneratorActionBase.php

@ -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')
);
}
}

42
islandora/src/Plugin/RulesAction/CreateEventGenerator.php

@ -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));
}
}

42
islandora/src/Plugin/RulesAction/DeleteEventGenerator.php

@ -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));
}
}

42
islandora/src/Plugin/RulesAction/UpdateEventGenerator.php

@ -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));
}
}

36
islandora/tests/src/Kernel/CreateEventGeneratorTest.php

@ -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'.");
}
}

36
islandora/tests/src/Kernel/DeleteEventGeneratorTest.php

@ -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'.");
}
}

63
islandora/tests/src/Kernel/EventGeneratorActionTestBase.php

@ -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;
}
}

67
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.");
}
}

52
islandora/tests/src/Kernel/EventGeneratorTestBase.php

@ -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();
}
}

3
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'
];

36
islandora/tests/src/Kernel/UpdateEventGeneratorTest.php

@ -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'.");
}
}

7
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.");
}
}

Loading…
Cancel
Save