BaseFieldOverrideTest.php in Drupal 9
File
core/tests/Drupal/KernelTests/Core/Field/Entity/BaseFieldOverrideTest.php
View source
<?php
namespace Drupal\KernelTests\Core\Field\Entity;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\Entity\BaseFieldOverride;
use Drupal\Core\Field\FieldItemList;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\KernelTests\KernelTestBase;
class BaseFieldOverrideTest extends KernelTestBase {
protected static $modules = [
'system',
'user',
'entity_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('base_field_override');
}
public function testGetClass($field_type, $base_field_class, $expected_override_class) {
$base_field = BaseFieldDefinition::create($field_type)
->setName('Test Field')
->setTargetEntityTypeId('entity_test');
if ($base_field_class) {
$base_field
->setClass($base_field_class);
}
$override = BaseFieldOverride::createFromBaseFieldDefinition($base_field, 'test_bundle');
$this
->assertEquals($expected_override_class, ltrim($override
->getClass(), '\\'));
}
public function getClassTestCases() {
return [
'String (default class)' => [
'string',
FALSE,
FieldItemList::class,
],
'String (overridden class)' => [
'string',
static::class,
static::class,
],
];
}
public function testDefaultValueCallback() {
$base_field = BaseFieldDefinition::create('entity_reference')
->setName('Test Field')
->setTargetEntityTypeId('entity_test')
->setDefaultValueCallback(static::class . '::defaultValueCallbackPrimitive');
$base_field_override = BaseFieldOverride::createFromBaseFieldDefinition($base_field, 'test_bundle');
$entity = EntityTest::create([]);
$this
->assertEquals([
[
'target_id' => 99,
],
], $base_field
->getDefaultValue($entity));
$this
->assertEquals([
[
'target_id' => 99,
],
], $base_field_override
->getDefaultValue($entity));
}
public static function defaultValueCallbackPrimitive() {
return 99;
}
}