From 878297e9b691371f6263a47ad1d0931a5dc62b0d Mon Sep 17 00:00:00 2001 From: nagyad Date: Mon, 27 May 2019 09:55:05 +0200 Subject: [PATCH] OPENEUROPA-1905: Complete kernel test. --- .../Field/FieldType/EntityVersionItem.php | 10 ++-- tests/Kernel/EntityVersionItemTest.php | 56 ++++++++++++++++--- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/Plugin/Field/FieldType/EntityVersionItem.php b/src/Plugin/Field/FieldType/EntityVersionItem.php index 2775a23..0a4d388 100644 --- a/src/Plugin/Field/FieldType/EntityVersionItem.php +++ b/src/Plugin/Field/FieldType/EntityVersionItem.php @@ -15,7 +15,7 @@ * id = "entity_version", * label = @Translation("Entity version"), * module = "entity_versions", - * description = @Translation("Stores the versions of the entity.") + * description = @Translation("Stores the version of the entity.") * ) */ class EntityVersionItem extends FieldItemBase { @@ -70,7 +70,7 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel * * @param string $category */ - public function increaseValue(string $category): void { + public function increase(string $category): void { $value = $this->get($category)->getValue(); $this->set($category, ($value + 1)); } @@ -80,9 +80,9 @@ public function increaseValue(string $category): void { * * @param string $category */ - public function decreaseValue(string $category): void { + public function decrease(string $category): void { $value = $this->get($category)->getValue(); - $this->set($category, ($value + 1)); + $this->set($category, ($value - 1)); } /** @@ -90,7 +90,7 @@ public function decreaseValue(string $category): void { * * @param string $category */ - public function resetValue(string $category): void { + public function reset(string $category): void { $this->set($category, 0); } diff --git a/tests/Kernel/EntityVersionItemTest.php b/tests/Kernel/EntityVersionItemTest.php index 626b771..a8b8eb1 100755 --- a/tests/Kernel/EntityVersionItemTest.php +++ b/tests/Kernel/EntityVersionItemTest.php @@ -24,13 +24,13 @@ protected function setUp() { // Create a generic field for validation. FieldStorageConfig::create([ - 'field_name' => 'field_test', + 'field_name' => 'version', 'entity_type' => 'entity_test', 'type' => 'entity_version', ])->save(); FieldConfig::create([ 'entity_type' => 'entity_test', - 'field_name' => 'field_test', + 'field_name' => 'version', 'bundle' => 'entity_test', ])->save(); } @@ -41,18 +41,56 @@ protected function setUp() { public function testEntityVersionItem() { // Create entity. $entity = EntityTest::create(); - $entity->field_test->title = 'Test'; - $entity->field_test->major = 1; - $entity->field_test->minor = 1; - $entity->field_test->patch = 1; + $entity->version->major = 1; + $entity->version->minor = 1; + $entity->version->patch = 1; $entity->save(); // Verify that the field value is changed. $id = $entity->id(); $entity = EntityTest::load($id); - $this->assertEqual($entity->field_test->major, 1); - $this->assertEqual($entity->field_test->minor, 1); - $this->assertEqual($entity->field_test->patch, 1); + $this->assertEqual($entity->version->major, 1); + $this->assertEqual($entity->version->minor, 1); + $this->assertEqual($entity->version->patch, 1); + + // 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, 2); + $this->assertEqual($entity->version->minor, 2); + $this->assertEqual($entity->version->patch, 2); + + // Use the field type method to decrease the values. + $entity->version->first()->decrease('major'); + $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, 1); + $this->assertEqual($entity->version->minor, 1); + $this->assertEqual($entity->version->patch, 1); + + // Use the field type method to reset the 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 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); } }