From 644afe0b0ea6e31c4bdfe10d2d10c7d918321eb6 Mon Sep 17 00:00:00 2001 From: Danny Lamb Date: Tue, 6 Dec 2016 15:38:23 +0000 Subject: [PATCH] First pass at AS2 event generator --- islandora/islandora.services.yml | 2 + .../src/EventGenerator/EventGenerator.php | 60 +++++++ .../EventGeneratorInterface.php | 48 +++++ .../tests/src/Kernel/EventGeneratorTest.php | 164 ++++++++++++++++++ 4 files changed, 274 insertions(+) create mode 100644 islandora/src/EventGenerator/EventGenerator.php create mode 100644 islandora/src/EventGenerator/EventGeneratorInterface.php create mode 100644 islandora/tests/src/Kernel/EventGeneratorTest.php diff --git a/islandora/islandora.services.yml b/islandora/islandora.services.yml index 938de6ed..d1a94b40 100644 --- a/islandora/islandora.services.yml +++ b/islandora/islandora.services.yml @@ -6,3 +6,5 @@ services: tags: - { name: paramconverter } arguments: ['@entity.manager'] + islandora.eventgenerator: + class: Drupal\islandora\EventGenerator\EventGenerator diff --git a/islandora/src/EventGenerator/EventGenerator.php b/islandora/src/EventGenerator/EventGenerator.php new file mode 100644 index 00000000..6040bed6 --- /dev/null +++ b/islandora/src/EventGenerator/EventGenerator.php @@ -0,0 +1,60 @@ + "https://www.w3.org/ns/activitystreams", + "type" => "Create", + "actor" => [ + "type" => "Person", + "id" => $user->toUrl()->setAbsolute()->toString(), + ], + "object" => $entity->toUrl()->setAbsolute()->toString(), + ]); + } + + /** + * {@inheritdoc} + */ + public function generateUpdateEvent(EntityInterface $entity, UserInterface $user) { + return json_encode([ + "@context" => "https://www.w3.org/ns/activitystreams", + "type" => "Update", + "actor" => [ + "type" => "Person", + "id" => $user->toUrl()->setAbsolute()->toString(), + ], + "object" => $entity->toUrl()->setAbsolute()->toString(), + ]); + } + + /** + * {@inheritdoc} + */ + public function generateDeleteEvent(EntityInterface $entity, UserInterface $user) { + return json_encode([ + "@context" => "https://www.w3.org/ns/activitystreams", + "type" => "Delete", + "actor" => [ + "type" => "Person", + "id" => $user->toUrl()->setAbsolute()->toString(), + ], + "object" => $entity->toUrl()->setAbsolute()->toString(), + ]); + } + +} diff --git a/islandora/src/EventGenerator/EventGeneratorInterface.php b/islandora/src/EventGenerator/EventGeneratorInterface.php new file mode 100644 index 00000000..59578a8f --- /dev/null +++ b/islandora/src/EventGenerator/EventGeneratorInterface.php @@ -0,0 +1,48 @@ +installSchema('system', 'sequences'); + $this->installEntitySchema('user'); + $this->installConfig('filter'); + $this->installEntitySchema('fedora_resource'); + + // 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. + */ + public function testGenerateCreateEvent() { + $json = $this->eventGenerator->generateCreateEvent($this->entity, $this->user); + $msg = json_decode($json, TRUE); + + // Assert basic message structure. + $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(array_key_exists('type', $msg), "Type key exists"); + $this->assertTrue($msg["type"] == "Create", "Event type is 'Create'."); + + $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('object', $msg), "Object key exists"); + $this->assertTrue($msg["object"] == $this->entity->toUrl()->setAbsolute()->toString(), "Object is entity uri."); + } + + /** + * Tests the generateUpdateEvent() method. + */ + public function testGenerateUpdateEvent() { + $json = $this->eventGenerator->generateUpdateEvent($this->entity, $this->user); + $msg = json_decode($json, TRUE); + + // Assert basic message structure. + $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(array_key_exists('type', $msg), "Type key exists"); + $this->assertTrue($msg["type"] == "Update", "Event type is 'Update'."); + + $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('object', $msg), "Object key exists"); + $this->assertTrue($msg["object"] == $this->entity->toUrl()->setAbsolute()->toString(), "Object is entity uri."); + } + + /** + * Tests the generateDeleteEvent() method. + */ + public function testGenerateDeleteEvent() { + $json = $this->eventGenerator->generateDeleteEvent($this->entity, $this->user); + $msg = json_decode($json, TRUE); + + // Assert basic message structure. + $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(array_key_exists('type', $msg), "Type key exists"); + $this->assertTrue($msg["type"] == "Delete", "Event type is 'Delete'."); + + $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('object', $msg), "Object key exists"); + $this->assertTrue($msg["object"] == $this->entity->toUrl()->setAbsolute()->toString(), "Object is entity uri."); + } + +}