TranslatableFieldTest.php in Drupal 10
File
core/modules/layout_builder/tests/src/Kernel/TranslatableFieldTest.php
View source
<?php
namespace Drupal\Tests\layout_builder\Kernel;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\layout_builder\Entity\LayoutBuilderEntityViewDisplay;
use Drupal\layout_builder\Plugin\SectionStorage\OverridesSectionStorage;
use Drupal\layout_builder\Section;
use Drupal\layout_builder\SectionComponent;
class TranslatableFieldTest extends KernelTestBase {
protected static $modules = [
'layout_discovery',
'layout_builder',
'entity_test',
'field',
'system',
'user',
'language',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
ConfigurableLanguage::createFromLangcode('es')
->save();
LayoutBuilderEntityViewDisplay::create([
'targetEntityType' => 'entity_test',
'bundle' => 'entity_test',
'mode' => 'default',
'status' => TRUE,
])
->enableLayoutBuilder()
->setOverridable()
->save();
FieldStorageConfig::loadByName('entity_test', OverridesSectionStorage::FIELD_NAME)
->setTranslatable(TRUE)
->save();
FieldConfig::loadByName('entity_test', 'entity_test', OverridesSectionStorage::FIELD_NAME)
->setTranslatable(TRUE)
->save();
}
public function testSectionsClearedOnCreateTranslation() {
$section_data = [
new Section('layout_onecol', [], [
'first-uuid' => new SectionComponent('first-uuid', 'content', [
'id' => 'foo',
]),
]),
];
$entity = EntityTest::create([
OverridesSectionStorage::FIELD_NAME => $section_data,
]);
$entity
->save();
$this
->assertFalse($entity
->get(OverridesSectionStorage::FIELD_NAME)
->isEmpty());
$entity = EntityTest::load($entity
->id());
$translation = $entity
->addTranslation('es', $entity
->toArray());
$this
->assertTrue($translation
->get(OverridesSectionStorage::FIELD_NAME)
->isEmpty());
}
}