Skip to content
Snippets Groups Projects
Commit 2d931339 authored by Pablo López's avatar Pablo López
Browse files

Issue #3526577: Add basic test coverage

parent 436f42da
No related branches found
No related tags found
1 merge request!23Issue #3526577: Add basic test coverage
Pipeline #518774 passed
<?php
declare(strict_types=1);
namespace Drupal\Tests\field_inheritance\Kernel;
use Drupal\entity_test\Entity\EntityTest;
/**
* Tests the field_inheritance field behavior.
*
* @group field_inheritance
*/
class FieldInheritanceFieldTest extends FieldInheritanceTestBase {
/**
* The parent entity.
*
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected EntityTest $parentNew;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
// Create parent entity with field values.
$this->parentNew = EntityTest::create([
'name' => 'Parent New entity',
'field_one' => [
['value' => 'Parent New note 1'],
['value' => 'Parent New note 2'],
],
'field_two' => [
['value' => 'Parent New note 3'],
],
]);
$this->parentNew->save();
}
/**
* Tests the field behavior.
*/
public function testInheritanceField(): void {
// By default the inheritance is set at entity level, so everything should
// be taken from entity 1.
$this->assertEquals($this->child->inheritance_one->getValue(), $this->parent->field_one->getValue());
$this->assertEquals($this->child->inheritance_two->getValue(), $this->parent->field_two->getValue());
// Override one of them at field level.
$this->child->field_inheritance = [
'enabled' => 1,
'entities' => [
'entity_test:entity_test' => ['entity' => $this->parent->id()],
],
'fields' => [
$this->inheritanceOne->idWithoutTypeAndBundle() => [
'entity' => $this->parentNew->id(),
],
],
];
$this->child->save();
$this->child = \Drupal::entityTypeManager()->getStorage('entity_test')->loadUnchanged($this->child->id());
$this->assertEquals($this->child->inheritance_one->getValue(), $this->parentNew->field_one->getValue());
$this->assertEquals($this->child->inheritance_two->getValue(), $this->parent->field_two->getValue());
// Skip one of them at field level.
$this->child->field_inheritance = [
'enabled' => 1,
'entities' => [
'entity_test:entity_test' => ['entity' => $this->parent->id()],
],
'fields' => [
$this->inheritanceOne->idWithoutTypeAndBundle() => [
'entity' => $this->parentNew->id(),
'skip' => 1,
],
],
];
$this->child->save();
$this->child = \Drupal::entityTypeManager()->getStorage('entity_test')->loadUnchanged($this->child->id());
$this->assertEquals($this->child->inheritance_one->getValue(), []);
$this->assertEquals($this->child->inheritance_two->getValue(), $this->parent->field_two->getValue());
// Use only field level definitions.
$this->child->field_inheritance = [
'enabled' => 1,
'entities' => [
'entity_test:entity_test' => ['entity' => $this->parent->id()],
],
'fields' => [
$this->inheritanceOne->idWithoutTypeAndBundle() => [
'entity' => $this->parentNew->id(),
],
$this->inheritanceTwo->idWithoutTypeAndBundle() => [
'entity' => $this->parent->id(),
],
],
];
$this->child->save();
$this->child = \Drupal::entityTypeManager()->getStorage('entity_test')->loadUnchanged($this->child->id());
$this->assertEquals($this->child->inheritance_one->getValue(), $this->parentNew->field_one->getValue());
$this->assertEquals($this->child->inheritance_two->getValue(), $this->parent->field_two->getValue());
// Disable field_inheritance globally.
$this->child->field_inheritance = [
'enabled' => 0,
'entities' => [
'entity_test:entity_test' => ['entity' => $this->parent->id()],
],
'fields' => [
$this->inheritanceOne->idWithoutTypeAndBundle() => [
'skip' => 1,
],
],
];
$this->child->save();
$this->child = \Drupal::entityTypeManager()->getStorage('entity_test')->loadUnchanged($this->child->id());
$this->assertEquals($this->child->inheritance_one->getValue(), []);
$this->assertEquals($this->child->inheritance_two->getValue(), []);
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\field_inheritance\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field_inheritance\Entity\FieldInheritance;
use Drupal\KernelTests\KernelTestBase;
/**
* Base class for Field Inheritance tests.
*/
abstract class FieldInheritanceTestBase extends KernelTestBase {
/**
* The 1st Field Inheritance.
*
* @var \Drupal\field_inheritance\Entity\FieldInheritance
*/
protected FieldInheritance $inheritanceOne;
/**
* The 2nd Field Inheritance.
*
* @var \Drupal\field_inheritance\Entity\FieldInheritance
*/
protected FieldInheritance $inheritanceTwo;
/**
* The parent entity.
*
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected EntityTest $parent;
/**
* The child entity.
*
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected EntityTest $child;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'user',
'field',
'text',
'field_inheritance',
'entity_test',
];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->installEntitySchema('entity_test');
$this->installSchema('user', ['users_data']);
\Drupal::configFactory()->getEditable('field_inheritance.config')
->set('included_entities', ['entity_test'])
->save();
// Create the field storage and config.
FieldStorageConfig::create([
'field_name' => 'field_one',
'entity_type' => 'entity_test',
'type' => 'string',
'cardinality' => -1,
])->save();
FieldConfig::create([
'field_name' => 'field_one',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Field One',
])->save();
// Create the field storage and config.
FieldStorageConfig::create([
'field_name' => 'field_two',
'entity_type' => 'entity_test',
'type' => 'string',
'cardinality' => 3,
])->save();
FieldConfig::create([
'field_name' => 'field_two',
'entity_type' => 'entity_test',
'bundle' => 'entity_test',
'label' => 'Field Two',
])->save();
$this->inheritanceOne = FieldInheritance::create([
'id' => 'entity_test_entity_test_inheritance_one',
]);
$this->inheritanceOne->setType('inherit');
$this->inheritanceOne->setSourceEntityType('entity_test');
$this->inheritanceOne->setSourceEntityBundle('entity_test');
$this->inheritanceOne->setSourceField('field_one');
$this->inheritanceOne->setDestinationEntityType('entity_test');
$this->inheritanceOne->setDestinationEntityBundle('entity_test');
$this->inheritanceOne->setDestinationField('field_one');
$this->inheritanceOne->setPlugin('default_inheritance');
$this->inheritanceOne->save();
$this->inheritanceTwo = FieldInheritance::create([
'id' => 'entity_test_entity_test_inheritance_two',
]);
$this->inheritanceTwo->setType('inherit');
$this->inheritanceTwo->setSourceEntityType('entity_test');
$this->inheritanceTwo->setSourceEntityBundle('entity_test');
$this->inheritanceTwo->setSourceField('field_two');
$this->inheritanceTwo->setDestinationEntityType('entity_test');
$this->inheritanceTwo->setDestinationEntityBundle('entity_test');
$this->inheritanceTwo->setDestinationField('field_two');
$this->inheritanceTwo->setPlugin('default_inheritance');
$this->inheritanceTwo->save();
// Create parent entity with field values.
$this->parent = EntityTest::create([
'name' => 'Parent entity',
'field_one' => [
['value' => 'Parent note 1'],
['value' => 'Parent note 2'],
],
'field_two' => [
['value' => 'Parent note 3'],
],
]);
$this->parent->save();
// Create child entity with field values.
$this->child = EntityTest::create([
'name' => 'Child entity',
'field_one' => [
['value' => 'Child note 1'],
['value' => 'Child note 2'],
],
'field_two' => [
['value' => 'Child note 3'],
],
'field_inheritance' => [
'enabled' => 1,
'entities' => [
'entity_test:entity_test' => ['entity' => $this->parent->id()],
],
],
]);
$this->child->save();
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\field_inheritance\Kernel;
use Drupal\entity_test\Entity\EntityTest;
/**
* Tests the different inheritance types behavior.
*
* @group field_inheritance
*/
class FieldInheritanceTypesTest extends FieldInheritanceTestBase {
/**
* Tests the 'inherit' inheritance type.
*/
public function testInheritInheritanceType(): void {
$this->assertEquals($this->child->inheritance_one->getValue(), $this->parent->field_one->getValue());
$this->assertEquals($this->child->inheritance_two->getValue(), $this->parent->field_two->getValue());
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('entity_test', 'entity_test');
$this->assertEquals(-1, $fields['inheritance_one']->getCardinality());
$this->assertEquals(3, $fields['inheritance_two']->getCardinality());
}
/**
* Tests the 'prepend' inheritance type.
*/
public function testPrependInheritanceType(): void {
$this->inheritanceOne->setType('prepend')->save();
$this->inheritanceTwo->setType('prepend')->save();
drupal_flush_all_caches();
$parent = EntityTest::load($this->parent->id());
$child = EntityTest::load($this->child->id());
$this->assertEquals($child->inheritance_one->getValue(), array_merge($child->field_one->getValue(), $parent->field_one->getValue()));
$this->assertEquals($child->inheritance_two->getValue(), array_merge($child->field_two->getValue(), $parent->field_two->getValue()));
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('entity_test', 'entity_test');
$this->assertEquals(-1, $fields['inheritance_one']->getCardinality());
$this->assertEquals(6, $fields['inheritance_two']->getCardinality());
}
/**
* Tests the 'append' inheritance type.
*/
public function testAppendInheritanceType(): void {
$this->inheritanceOne->setType('append')->save();
$this->inheritanceTwo->setType('append')->save();
drupal_flush_all_caches();
$parent = EntityTest::load($this->parent->id());
$child = EntityTest::load($this->child->id());
$this->assertEquals($child->inheritance_one->getValue(), array_merge($parent->field_one->getValue(), $child->field_one->getValue()));
$this->assertEquals($child->inheritance_two->getValue(), array_merge($parent->field_two->getValue(), $child->field_two->getValue()));
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('entity_test', 'entity_test');
$this->assertEquals(-1, $fields['inheritance_one']->getCardinality());
$this->assertEquals(6, $fields['inheritance_two']->getCardinality());
}
/**
* Tests the 'fallback' inheritance type.
*/
public function testFallbackInheritanceType(): void {
$this->inheritanceOne->setType('fallback')->save();
$this->inheritanceTwo->setType('fallback')->save();
drupal_flush_all_caches();
$parent = EntityTest::load($this->parent->id());
$child = EntityTest::load($this->child->id());
$this->assertEquals($child->inheritance_one->getValue(), $child->field_one->getValue());
$this->assertEquals($child->inheritance_two->getValue(), $child->field_two->getValue());
$fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('entity_test', 'entity_test');
$this->assertEquals(-1, $fields['inheritance_one']->getCardinality());
$this->assertEquals(3, $fields['inheritance_two']->getCardinality());
$child->field_one = NULL;
$child->field_two = NULL;
$child->save();
$child = \Drupal::entityTypeManager()->getStorage('entity_test')->loadUnchanged($this->child->id());
$this->assertEquals($child->inheritance_one->getValue(), $parent->field_one->getValue());
$this->assertEquals($child->inheritance_two->getValue(), $parent->field_two->getValue());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment