Skip to content

Commit

Permalink
OPENEUROPA-1905: Make sure that zero is a value.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyad committed May 27, 2019
1 parent 878297e commit 77eb527
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
18 changes: 6 additions & 12 deletions src/Plugin/Field/FieldType/EntityVersionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* description = @Translation("Stores the version of the entity.")
* )
*/
class EntityVersionItem extends FieldItemBase {
class EntityVersionItem extends FieldItemBase implements EntityVersionItemInterface {

/**
* {@inheritdoc}
Expand Down Expand Up @@ -48,7 +48,7 @@ public function isEmpty() {
$minor = $this->get('minor')->getValue();
$patch = $this->get('patch')->getValue();

return empty($major) || empty($minor) || empty($patch);
return $major === NULL || $major === '' || $minor === NULL || $minor === '' || $patch === NULL || $patch === '';
}

/**
Expand All @@ -66,29 +66,23 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
}

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

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

/**
* Reset the given version number category to zero.
*
* @param string $category
* {@inheritdoc}
*/
public function reset(string $category): void {
$this->set($category, 0);
Expand Down
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_versions\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);

}
41 changes: 39 additions & 2 deletions tests/Kernel/EntityVersionItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class EntityVersionItemTest extends FieldKernelTestBase {
*/
public static $modules = ['entity_versions'];

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

Expand Down Expand Up @@ -66,8 +69,18 @@ public function testEntityVersionItem() {
$this->assertEqual($entity->version->minor, 2);
$this->assertEqual($entity->version->patch, 2);

// Use the field type method to decrease the values.
// 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, 1);
$this->assertEqual($entity->version->minor, 2);
$this->assertEqual($entity->version->patch, 2);

// Use the field type method to decrease values.
$entity->version->first()->decrease('minor');
$entity->version->first()->decrease('patch');
$entity->save();
Expand All @@ -79,7 +92,7 @@ public function testEntityVersionItem() {
$this->assertEqual($entity->version->minor, 1);
$this->assertEqual($entity->version->patch, 1);

// Use the field type method to reset the values to zero.
// 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');
Expand All @@ -91,6 +104,30 @@ public function testEntityVersionItem() {
$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 77eb527

Please sign in to comment.