Danny Lamb
8 years ago
15 changed files with 414 additions and 23 deletions
@ -0,0 +1,60 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\EventGenerator; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
use Drupal\user\UserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* The default EventGenerator implementation. |
||||||
|
* |
||||||
|
* Provides Activity Stream 2.0 serialized events. |
||||||
|
*/ |
||||||
|
class EventGenerator implements EventGeneratorInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function generateCreateEvent(EntityInterface $entity, UserInterface $user) { |
||||||
|
return json_encode([ |
||||||
|
"@context" => "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(), |
||||||
|
]); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,52 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\EventGenerator; |
||||||
|
|
||||||
|
use Drupal\Core\Entity\EntityInterface; |
||||||
|
use Drupal\user\UserInterface; |
||||||
|
|
||||||
|
/** |
||||||
|
* Inteface for a service that provides serialized event messages. |
||||||
|
*/ |
||||||
|
interface EventGeneratorInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates a serialized 'Create' event. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* The entity that was created. |
||||||
|
* @param \Drupal\user\UserInterface $user |
||||||
|
* The user who created the entity. |
||||||
|
* |
||||||
|
* @return string |
||||||
|
* Serialized event message |
||||||
|
*/ |
||||||
|
public function generateCreateEvent(EntityInterface $entity, UserInterface $user); |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates a serialized 'Create' event. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* The entity that was updated. |
||||||
|
* @param \Drupal\user\UserInterface $user |
||||||
|
* The user who updated the entity. |
||||||
|
* |
||||||
|
* @return string |
||||||
|
* Serialized event message |
||||||
|
*/ |
||||||
|
public function generateUpdateEvent(EntityInterface $entity, UserInterface $user); |
||||||
|
|
||||||
|
/** |
||||||
|
* Generates a serialized 'Create' event. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Entity\EntityInterface $entity |
||||||
|
* The entity that was deleted. |
||||||
|
* @param \Drupal\user\UserInterface $user |
||||||
|
* The user who deleted the entity. |
||||||
|
* |
||||||
|
* @return string |
||||||
|
* Serialized event message |
||||||
|
*/ |
||||||
|
public function generateDeleteEvent(EntityInterface $entity, UserInterface $user); |
||||||
|
|
||||||
|
} |
@ -0,0 +1,124 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
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 |
||||||
|
*/ |
||||||
|
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; |
||||||
|
|
||||||
|
/** |
||||||
|
* The EventGenerator to test. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\EventGenerator\EventGeneratorInterface |
||||||
|
*/ |
||||||
|
protected $eventGenerator; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@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(); |
||||||
|
|
||||||
|
// 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); |
||||||
|
|
||||||
|
$this->assertBasicStructure($msg); |
||||||
|
$this->assertTrue($msg["type"] == "Create", "Event type is 'Create'."); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the generateUpdateEvent() method. |
||||||
|
*/ |
||||||
|
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'."); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the generateDeleteEvent() method. |
||||||
|
*/ |
||||||
|
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'."); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Util function for repeated checks. |
||||||
|
* |
||||||
|
* @param array $msg |
||||||
|
* The message parsed as an array. |
||||||
|
*/ |
||||||
|
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."); |
||||||
|
|
||||||
|
// Make sure it has a type. |
||||||
|
$this->assertTrue(array_key_exists('type', $msg), "Type key exists"); |
||||||
|
|
||||||
|
// 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"); |
||||||
|
|
||||||
|
// 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."); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,48 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\islandora\Kernel; |
||||||
|
|
||||||
|
use Drupal\KernelTests\KernelTestBase; |
||||||
|
|
||||||
|
/** |
||||||
|
* Abstract base class for Islandora kernel tests. |
||||||
|
* |
||||||
|
*/ |
||||||
|
abstract class IslandoraKernelTestBase extends KernelTestBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public static $modules = [ |
||||||
|
'system', |
||||||
|
'user', |
||||||
|
'field', |
||||||
|
'filter', |
||||||
|
'block', |
||||||
|
'node', |
||||||
|
'path', |
||||||
|
'text', |
||||||
|
'options', |
||||||
|
'inline_entity_form', |
||||||
|
'serialization', |
||||||
|
'rest', |
||||||
|
'rdf', |
||||||
|
'jsonld', |
||||||
|
'islandora' |
||||||
|
]; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function setUp() { |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
// Bootstrap minimal Drupal environment to run the tests. |
||||||
|
$this->installSchema('system', 'sequences'); |
||||||
|
$this->installEntitySchema('user'); |
||||||
|
$this->installConfig('filter'); |
||||||
|
$this->installEntitySchema('fedora_resource'); |
||||||
|
} |
||||||
|
|
||||||
|
} |
||||||
|
|
@ -0,0 +1,54 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\Tests\islandora\Kernel; |
||||||
|
|
||||||
|
use Drupal\islandora\Entity\FedoraResource; |
||||||
|
use Drupal\simpletest\UserCreationTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the basic behavior of a vector clock. |
||||||
|
* |
||||||
|
* @group islandora |
||||||
|
*/ |
||||||
|
class VectorClockTest extends IslandoraKernelTestBase { |
||||||
|
|
||||||
|
/** |
||||||
|
* Fedora resource entity. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\FedoraResourceInterface |
||||||
|
*/ |
||||||
|
protected $entity; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function setUp() { |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
// Create a test entity. |
||||||
|
$this->entity = FedoraResource::create([ |
||||||
|
"type" => "rdf_source", |
||||||
|
"uid" => 1, |
||||||
|
"name" => "Test Fixture", |
||||||
|
"langcode" => "und", |
||||||
|
"status" => 1, |
||||||
|
]); |
||||||
|
$this->entity->save(); |
||||||
|
|
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* Tests the basic behavior of the vector clock. |
||||||
|
*/ |
||||||
|
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."); |
||||||
|
|
||||||
|
// 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."); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue