Skip to content

Commit

Permalink
OPENEUROPA-1905: Complete kernel test.
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyad committed May 27, 2019
1 parent 0608e91 commit 878297e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/Plugin/Field/FieldType/EntityVersionItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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));
}
Expand All @@ -80,17 +80,17 @@ 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));
}

/**
* Reset the given version number category to zero.
*
* @param string $category
*/
public function resetValue(string $category): void {
public function reset(string $category): void {
$this->set($category, 0);
}

Expand Down
56 changes: 47 additions & 9 deletions tests/Kernel/EntityVersionItemTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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);
}

}

0 comments on commit 878297e

Please sign in to comment.