Browse Source
* Pulling version counting out into its own service. * Coding standards * Removing rdf mapping entry for vclockpull/756/head
dannylamb
8 years ago
committed by
Nick Ruest
20 changed files with 456 additions and 237 deletions
@ -0,0 +1,42 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* @file |
||||||
|
* Install/update hook implementations. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** |
||||||
|
* Implements hook_schema(). |
||||||
|
*/ |
||||||
|
function islandora_schema() { |
||||||
|
$schema = []; |
||||||
|
$schema['islandora_version_count'] = [ |
||||||
|
'description' => 'Keeps track of the number of changes to an entity', |
||||||
|
'fields' => [ |
||||||
|
'id' => [ |
||||||
|
'description' => 'Autoincrementing id for record', |
||||||
|
'type' => 'serial', |
||||||
|
'unsigned' => TRUE, |
||||||
|
'not null' => TRUE, |
||||||
|
], |
||||||
|
'uuid' => [ |
||||||
|
'description' => 'UUID for an entity', |
||||||
|
'type' => 'varchar', |
||||||
|
'length' => 128, |
||||||
|
'not null' => TRUE, |
||||||
|
'unique' => TRUE, |
||||||
|
], |
||||||
|
'count' => [ |
||||||
|
'description' => 'Number of times an entity has been updated.', |
||||||
|
'type' => 'int', |
||||||
|
'unsigned' => TRUE, |
||||||
|
'default' => 0, |
||||||
|
], |
||||||
|
], |
||||||
|
'primary key' => ['id'], |
||||||
|
'unique keys' => [ |
||||||
|
'uuid' => ['uuid'], |
||||||
|
], |
||||||
|
]; |
||||||
|
return $schema; |
||||||
|
} |
@ -0,0 +1,78 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\VersionCounter; |
||||||
|
|
||||||
|
use Drupal\Core\Database\Connection; |
||||||
|
|
||||||
|
/** |
||||||
|
* Default VersionCounterInterface implemenation. |
||||||
|
* |
||||||
|
* Uses the drupal database. |
||||||
|
*/ |
||||||
|
class VersionCounter implements VersionCounterInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Database connection. |
||||||
|
* |
||||||
|
* @var \Drupal\Core\Database\Connection |
||||||
|
*/ |
||||||
|
protected $database; |
||||||
|
|
||||||
|
/** |
||||||
|
* VersionCounter constructor. |
||||||
|
* |
||||||
|
* @param \Drupal\Core\Database\Connection $database |
||||||
|
* Database connection. |
||||||
|
*/ |
||||||
|
public function __construct(Connection $database) { |
||||||
|
$this->database = $database; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function create($uuid) { |
||||||
|
$this->database->insert('islandora_version_count') |
||||||
|
->fields([ |
||||||
|
'uuid' => $uuid, |
||||||
|
]) |
||||||
|
->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function get($uuid) { |
||||||
|
$query = $this->database->select('islandora_version_count', 'ivc') |
||||||
|
->condition('ivc.uuid', $uuid) |
||||||
|
->fields('ivc', ['count']); |
||||||
|
|
||||||
|
$results = $query->execute(); |
||||||
|
|
||||||
|
foreach ($results as $result) { |
||||||
|
return $result->count; |
||||||
|
} |
||||||
|
|
||||||
|
return -1; |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function increment($uuid) { |
||||||
|
return $this->database->update('islandora_version_count') |
||||||
|
->expression('count', 'count + 1') |
||||||
|
->condition('uuid', $uuid) |
||||||
|
->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function delete($uuid) { |
||||||
|
return $this->database->delete('islandora_version_count') |
||||||
|
->condition('uuid', $uuid) |
||||||
|
->execute(); |
||||||
|
} |
||||||
|
|
||||||
|
} |
@ -0,0 +1,59 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
namespace Drupal\islandora\VersionCounter; |
||||||
|
|
||||||
|
/** |
||||||
|
* Service for tracking the number of times an entity has been updated. |
||||||
|
*/ |
||||||
|
interface VersionCounterInterface { |
||||||
|
|
||||||
|
/** |
||||||
|
* Creates a version count record in the db for an entity. |
||||||
|
* |
||||||
|
* @param string $uuid |
||||||
|
* Entity UUID. |
||||||
|
* |
||||||
|
* @throws Drupal\Core\Database\IntegrityConstraintViolationException |
||||||
|
* |
||||||
|
* @return int |
||||||
|
* The id of the newly created db record. |
||||||
|
*/ |
||||||
|
public function create($uuid); |
||||||
|
|
||||||
|
/** |
||||||
|
* Returns the version count for an entity. |
||||||
|
* |
||||||
|
* @param string $uuid |
||||||
|
* Entity UUID. |
||||||
|
* |
||||||
|
* @return int |
||||||
|
* The version count of the entity. Returns -1 if there is no record for the |
||||||
|
* uuid in the database. |
||||||
|
*/ |
||||||
|
public function get($uuid); |
||||||
|
|
||||||
|
/** |
||||||
|
* Increments a version count for an entity. |
||||||
|
* |
||||||
|
* @param string $uuid |
||||||
|
* Entity UUID. |
||||||
|
* |
||||||
|
* @return int |
||||||
|
* Returns 1 on success. Returns 0 if no record exists for the uuid in the |
||||||
|
* database. |
||||||
|
*/ |
||||||
|
public function increment($uuid); |
||||||
|
|
||||||
|
/** |
||||||
|
* Deletes a version count record in the db for an entity. |
||||||
|
* |
||||||
|
* @param string $uuid |
||||||
|
* Entity UUID. |
||||||
|
* |
||||||
|
* @return int |
||||||
|
* Returns 1 on success. Returns 0 if no record exists for the uuid in the |
||||||
|
* database. |
||||||
|
*/ |
||||||
|
public function delete($uuid); |
||||||
|
|
||||||
|
} |
@ -1,69 +0,0 @@ |
|||||||
<?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 |
|
||||||
* @coversDefaultClass \Drupal\islandora\Entity\FedoraResource |
|
||||||
*/ |
|
||||||
class VectorClockTest extends IslandoraKernelTestBase { |
|
||||||
|
|
||||||
use UserCreationTrait; |
|
||||||
|
|
||||||
/** |
|
||||||
* Fedora resource entity. |
|
||||||
* |
|
||||||
* @var \Drupal\islandora\FedoraResourceInterface |
|
||||||
*/ |
|
||||||
protected $entity; |
|
||||||
|
|
||||||
/** |
|
||||||
* User entity. |
|
||||||
* |
|
||||||
* @var \Drupal\user\UserInterface |
|
||||||
*/ |
|
||||||
protected $user; |
|
||||||
|
|
||||||
/** |
|
||||||
* {@inheritdoc} |
|
||||||
*/ |
|
||||||
public function setUp() { |
|
||||||
parent::setUp(); |
|
||||||
|
|
||||||
// Create a test user. |
|
||||||
$this->user = $this->createUser(['add fedora resource entities', 'edit fedora resource entities']); |
|
||||||
|
|
||||||
// Create a test entity. |
|
||||||
$this->entity = FedoraResource::create([ |
|
||||||
"type" => "rdf_source", |
|
||||||
"uid" => $this->user->get('uid'), |
|
||||||
"name" => "Test Fixture", |
|
||||||
"langcode" => "und", |
|
||||||
"status" => 1, |
|
||||||
]); |
|
||||||
$this->entity->save(); |
|
||||||
|
|
||||||
} |
|
||||||
|
|
||||||
/** |
|
||||||
* 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 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 must be incremented on update."); |
|
||||||
} |
|
||||||
|
|
||||||
} |
|
@ -0,0 +1,102 @@ |
|||||||
|
<?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 |
||||||
|
* @coversDefaultClass \Drupal\islandora\Entity\FedoraResource |
||||||
|
*/ |
||||||
|
class VersionCounterTest extends IslandoraKernelTestBase { |
||||||
|
|
||||||
|
use UserCreationTrait; |
||||||
|
|
||||||
|
/** |
||||||
|
* Fedora resource entity. |
||||||
|
* |
||||||
|
* @var \Drupal\islandora\FedoraResourceInterface |
||||||
|
*/ |
||||||
|
protected $entity; |
||||||
|
|
||||||
|
/** |
||||||
|
* User entity. |
||||||
|
* |
||||||
|
* @var \Drupal\user\UserInterface |
||||||
|
*/ |
||||||
|
protected $user; |
||||||
|
|
||||||
|
/** |
||||||
|
* {@inheritdoc} |
||||||
|
*/ |
||||||
|
public function setUp() { |
||||||
|
parent::setUp(); |
||||||
|
|
||||||
|
// Create a test user. |
||||||
|
$this->user = $this->createUser(['add fedora resource entities', 'edit fedora resource entities']); |
||||||
|
|
||||||
|
// Create a test entity. |
||||||
|
$this->entity = FedoraResource::create([ |
||||||
|
"type" => "rdf_source", |
||||||
|
"uid" => $this->user->get('uid'), |
||||||
|
"name" => "Test Fixture", |
||||||
|
"langcode" => "und", |
||||||
|
"status" => 1, |
||||||
|
]); |
||||||
|
$this->entity->save(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::create |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::get |
||||||
|
*/ |
||||||
|
public function testInitializesRecord() { |
||||||
|
$versionCounter = $this->container->get('islandora.versioncounter'); |
||||||
|
|
||||||
|
$this->assertTrue($versionCounter->get($this->entity->uuid()) == 0, |
||||||
|
"Version counter db record must be initialized to 0." |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::create |
||||||
|
* @expectedException \Drupal\Core\Database\IntegrityConstraintViolationException |
||||||
|
*/ |
||||||
|
public function testCannotCreateDuplicateRecord() { |
||||||
|
$versionCounter = $this->container->get('islandora.versioncounter'); |
||||||
|
$versionCounter->create($this->entity->uuid()); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::increment |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::get |
||||||
|
*/ |
||||||
|
public function testRecordIncrementsOnUpdate() { |
||||||
|
$this->entity->setName("New Name"); |
||||||
|
$this->entity->save(); |
||||||
|
|
||||||
|
$versionCounter = $this->container->get('islandora.versioncounter'); |
||||||
|
|
||||||
|
$this->assertTrue($versionCounter->get($this->entity->uuid()) == 1, |
||||||
|
"Version counter db record must increment on entity update." |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::delete |
||||||
|
* @covers \Drupal\islandora\VersionCounter\VersionCounter::get |
||||||
|
*/ |
||||||
|
public function testRecordsGetCleanedUp() { |
||||||
|
$this->entity->delete(); |
||||||
|
|
||||||
|
$versionCounter = $this->container->get('islandora.versioncounter'); |
||||||
|
|
||||||
|
$this->assertTrue($versionCounter->get($this->entity->uuid()) == -1, |
||||||
|
"Version counter db record must be deleted on entity delete." |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
} |
Loading…
Reference in new issue