Skip to content

Commit

Permalink
Merge pull request #2 from openeuropa/OPENEUROPA-1905
Browse files Browse the repository at this point in the history
OPENEUROPA-1905: Create field type plugin for entity revisions.
  • Loading branch information
hernani authored Jun 3, 2019
2 parents b75a186 + f3ffde6 commit 17b1eb1
Show file tree
Hide file tree
Showing 9 changed files with 297 additions and 14 deletions.
4 changes: 2 additions & 2 deletions .drone.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
workspace:
base: /test
path: entity_versions
path: entity_version

services:
web:
image: fpfis/httpd-php-ci:7.1
environment:
- DOCUMENT_ROOT=/test/entity_versions
- DOCUMENT_ROOT=/test/entity_version
mysql:
image: percona/percona-server:5.6
environment:
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Entity versions
# Entity version

Entity versions allows to attach version number to content revisions that help content editors to understand the evolution of the content item. Versions are composed of major, minor and patch numbers that can be changed manually or by third party modules.
Entity version allows to attach version number to content revisions that help content editors to understand the evolution of the content item.
Versions are composed of major, minor and patch numbers that can be changed manually or by third party modules.

## Development setup

Expand Down
9 changes: 4 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "openeuropa/entity_versions",
"description": "Entity Versions.",
"name": "openeuropa/entity_version",
"description": "Entity Version.",
"type": "drupal-module",
"license": "EUPL-1.2",
"minimum-stability": "dev",
Expand All @@ -13,7 +13,6 @@
"composer/installers": "~1.5",
"drupal-composer/drupal-scaffold": "~2.2",
"drupal/config_devel": "~1.2",
"drupal/console": "^1.6",
"drupal/drupal-extension": "~4.0",
"drush/drush": "~9.0@stable",
"openeuropa/code-review": "~1.0.0-beta2",
Expand All @@ -34,12 +33,12 @@
],
"autoload": {
"psr-4": {
"Drupal\\entity_versions\\": "./src/"
"Drupal\\entity_version\\": "./src/"
}
},
"autoload-dev": {
"psr-4": {
"Drupal\\Tests\\entity_versions\\": "./tests/"
"Drupal\\Tests\\entity_version\\": "./tests/"
}
},
"extra": {
Expand Down
4 changes: 2 additions & 2 deletions entity_versions.info.yml → entity_version.info.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: Entity versions
description: Entity versions allows to attach version number to content revisions that help content editors to
name: Entity version
description: Entity version allows to attach version number to content revisions that help content editors to
understand the evolution of the content item. Versions are composed of major, minor and patch numbers that can be
changed manually or by third party modules.

Expand Down
2 changes: 1 addition & 1 deletion entity_versions.module → entity_version.module
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/**
* @file
* Entity versions module.
* Entity version module.
*/

declare(strict_types = 1);
4 changes: 2 additions & 2 deletions runner.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ drupal:
- "./vendor/bin/drush config-set node.settings use_admin_theme 1 -y"
# Enable the modules.
- "./vendor/bin/drush en config_devel -y"
- "./vendor/bin/drush en entity_versions -y"
- "./vendor/bin/drush en entity_version -y"
- "./vendor/bin/drush cr"
settings:
settings:
Expand All @@ -32,7 +32,7 @@ drupal:

commands:
drupal:site-setup:
- { task: "symlink", from: "../../..", to: "${drupal.root}/modules/custom/entity_versions" }
- { task: "symlink", from: "../../..", to: "${drupal.root}/modules/custom/entity_version" }
- { task: "run", command: "drupal:drush-setup" }
- { task: "run", command: "drupal:settings-setup" }
- { task: "run", command: "setup:phpunit" }
Expand Down
109 changes: 109 additions & 0 deletions src/Plugin/Field/FieldType/EntityVersionItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

declare(strict_types = 1);

namespace Drupal\entity_version\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\TypedData\DataDefinition;

/**
* Plugin implementation of the 'entity_version' field type.
*
* @FieldType(
* id = "entity_version",
* label = @Translation("Entity version"),
* module = "entity_version",
* description = @Translation("Stores the version of the entity.")
* )
*/
class EntityVersionItem extends FieldItemBase implements EntityVersionItemInterface {

/**
* {@inheritdoc}
*/
public static function schema(FieldStorageDefinitionInterface $field_definition) {
return [
'columns' => [
'major' => [
'type' => 'int',
],
'minor' => [
'type' => 'int',
],
'patch' => [
'type' => 'int',
],
],
];
}

/**
* {@inheritdoc}
*/
public function isEmpty() {
// We consider the field empty if either of these properties left empty.
$major = $this->get('major')->getValue();
$minor = $this->get('minor')->getValue();
$patch = $this->get('patch')->getValue();

return $major === NULL || $major === '' || $minor === NULL || $minor === '' || $patch === NULL || $patch === '';
}

/**
* {@inheritdoc}
*/
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
$properties['major'] = DataDefinition::create('integer')
->setLabel(t('Major number'));
$properties['minor'] = DataDefinition::create('integer')
->setLabel(t('Minor number'));
$properties['patch'] = DataDefinition::create('integer')
->setLabel(t('Patch number'));

return $properties;
}

/**
* {@inheritdoc}
*/
public function applyDefaultValue($notify = TRUE) {
parent::applyDefaultValue($notify);
// Created fields default to zero.
$this->setValue(
[
'major' => 0,
'minor' => 0,
'patch' => 0,
],
$notify
);

return $this;
}

/**
* {@inheritdoc}
*/
public function increase(string $category): void {
$value = $this->get($category)->getValue();
$this->set($category, ($value + 1));
}

/**
* {@inheritdoc}
*/
public function decrease(string $category): void {
$value = $this->get($category)->getValue();
$this->set($category, empty($value) ? 0 : ($value - 1));
}

/**
* {@inheritdoc}
*/
public function reset(string $category): void {
$this->set($category, 0);
}

}
38 changes: 38 additions & 0 deletions src/Plugin/Field/FieldType/EntityVersionItemInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types = 1);

namespace Drupal\entity_version\Plugin\Field\FieldType;

use Drupal\Core\Field\FieldItemInterface;

/**
* Defines an interface for the entity_version field item.
*/
interface EntityVersionItemInterface extends FieldItemInterface {

/**
* Increase the given version number category.
*
* @param string $category
* The version number category.
*/
public function increase(string $category);

/**
* Decrease the given version number category.
*
* @param string $category
* The version number category.
*/
public function decrease(string $category);

/**
* Reset the given version number category to zero.
*
* @param string $category
* The version number category.
*/
public function reset(string $category);

}
136 changes: 136 additions & 0 deletions tests/Kernel/EntityVersionItemTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
<?php

namespace Drupal\Tests\entity_version\Kernel;

use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;

/**
* Tests the new entity API for the entity version field type.
*/
class EntityVersionItemTest extends FieldKernelTestBase {

/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['entity_version'];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

// Create a generic field for validation.
FieldStorageConfig::create([
'field_name' => 'version',
'entity_type' => 'entity_test',
'type' => 'entity_version',
])->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'version',
'bundle' => 'entity_test',
])->save();
}

/**
* Tests using entity fields of the entity version field type.
*/
public function testEntityVersionItem() {
// Create entity.
$entity = EntityTest::create();
$entity->save();

// Verify that the field default value is zero.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 0);
$this->assertEqual($entity->version->patch, 0);

// Use the field type method to increase the values.
$entity->version->first()->increase('major');
$entity->version->first()->increase('minor');
$entity->version->first()->increase('patch');
$entity->save();

// Verify that the field value is changed.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 1);
$this->assertEqual($entity->version->minor, 1);
$this->assertEqual($entity->version->patch, 1);

// Use the field type method to decrease the major number.
$entity->version->first()->decrease('major');
$entity->save();

// Verify that the field value is changed.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 1);
$this->assertEqual($entity->version->patch, 1);

// Use the field type method to decrease values.
$entity->version->first()->decrease('minor');
$entity->version->first()->decrease('patch');
$entity->save();

// Verify that the field value is changed.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 0);
$this->assertEqual($entity->version->patch, 0);

// Use the field type method to increase the values.
$entity->version->first()->increase('major');
$entity->version->first()->increase('minor');
$entity->version->first()->increase('patch');
$entity->save();

// Use the field type method to reset values to zero.
$entity->version->first()->reset('major');
$entity->version->first()->reset('minor');
$entity->version->first()->reset('patch');
$entity->save();

// Verify that the field value is reset.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 0);
$this->assertEqual($entity->version->patch, 0);

// Use the field type method to decrease values.
$entity->version->first()->decrease('major');
$entity->version->first()->decrease('minor');
$entity->version->first()->decrease('patch');
$entity->save();

// Verify that the field value has not changed.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 0);
$this->assertEqual($entity->version->patch, 0);

// Use the field type method to increase patch.
$entity->version->first()->increase('patch');
$entity->save();

// Verify that the field value is changed.
$id = $entity->id();
$entity = EntityTest::load($id);
$this->assertEqual($entity->version->major, 0);
$this->assertEqual($entity->version->minor, 0);
$this->assertEqual($entity->version->patch, 1);
}

}

0 comments on commit 17b1eb1

Please sign in to comment.