<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Form\FormState;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\paragraphs_test\Form\TestEmbeddedEntityForm;
class ParagraphsLangcodeChangeTest extends EntityKernelTestBase {
protected static $modules = [
'user',
'system',
'field',
'text',
'filter',
'entity_test',
'paragraphs',
'paragraphs_test',
'entity_reference_revisions',
'node',
'language',
'file',
];
protected $nodeType = 'page';
protected $nodeParagraphsFieldName = 'field_paragraphs';
protected $paragraphType = 'paragraph_type';
protected $node;
protected $paragraph;
protected $form;
protected $formState;
protected $formObject;
protected $formDisplay;
protected $formBuilder;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('file');
$this
->installEntitySchema('node');
$this
->installEntitySchema('paragraph');
$this
->installSchema('node', [
'node_access',
]);
$this
->installConfig(static::$modules);
$this->formBuilder = $this->container
->get('form_builder');
$this->entityTypeManager
->getStorage('configurable_language')
->create([
'id' => 'es',
])
->save();
$this->entityTypeManager
->getStorage('paragraphs_type')
->create([
'label' => 'Paragraph type',
'id' => $this->paragraphType,
'status' => TRUE,
])
->save();
$this->entityTypeManager
->getStorage('node_type')
->create([
'name' => 'Example page',
'type' => $this->nodeType,
'create_body' => FALSE,
])
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', $this->nodeType)
->setLanguageAlterable(TRUE)
->setDefaultLangcode('en')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('paragraph', $this->paragraphType)
->setLanguageAlterable(TRUE)
->setDefaultLangcode('en')
->save();
FieldStorageConfig::create([
'entity_type' => 'node',
'type' => 'entity_reference_revisions',
'field_name' => $this->nodeParagraphsFieldName,
'settings' => [
'target_type' => 'paragraph',
],
'cardinality' => -1,
'translatable' => TRUE,
])
->save();
FieldConfig::create([
'entity_type' => 'node',
'bundle' => $this->nodeType,
'field_name' => $this->nodeParagraphsFieldName,
'label' => $this
->randomString(),
'settings' => [
'handler' => 'default:paragraph',
'handler_settings' => [
'negate' => 1,
'target_bundles' => NULL,
'target_bundles_drag_drop' => [
$this->paragraphType => [
'weight' => 0,
'enabled' => FALSE,
],
],
],
],
])
->save();
EntityFormDisplay::create([
'targetEntityType' => 'node',
'bundle' => $this->nodeType,
'mode' => 'default',
'status' => TRUE,
])
->setComponent('langcode', [
'type' => 'language_select',
'region' => 'content',
'weight' => 10,
])
->setComponent('uid', [
'type' => 'options_select',
'region' => 'content',
'weight' => 100,
])
->save();
$this->formDisplay = EntityFormDisplay::load('node.' . $this->nodeType . '.default');
$this
->createUser([
'uid' => 1,
'name' => 'user1',
])
->save();
$this->paragraph = $this->entityTypeManager
->getStorage('paragraph')
->create([
'type' => $this->paragraphType,
]);
$this->node = $this->entityTypeManager
->getStorage('node')
->create([
'type' => $this->nodeType,
'title' => $this
->randomString(),
'status' => TRUE,
'uid' => 1,
'langcode' => 'es',
$this->nodeParagraphsFieldName => [
$this->paragraph,
],
]);
}
public function testChangeWithLegacyWidget() {
$this
->doTestLangcodeChange([
'type' => 'entity_reference_paragraphs',
'weight' => 5,
], FALSE);
}
public function testChangeWithStableWidget() {
$this
->doTestLangcodeChange([
'type' => 'paragraphs',
'weight' => 15,
], FALSE);
}
public function testChangeWithEmbeddedLegacyWidget() {
$this
->doTestLangcodeChange([
'type' => 'entity_reference_paragraphs',
'weight' => 5,
], TRUE);
}
public function testChangeWithEmbeddedStableWidget() {
$this
->doTestLangcodeChange([
'type' => 'paragraphs',
'weight' => 15,
], TRUE);
}
protected function doTestLangcodeChange(array $widget_options, $embedded = FALSE) {
$this->formDisplay
->setComponent($this->nodeParagraphsFieldName, $widget_options)
->save();
$this
->doTestChangeWithinNodeForm($embedded);
}
protected function doTestChangeWithinNodeForm($embedded = FALSE) {
$this
->assertEquals('es', $this->node
->language()
->getId(), "The node was created with langcode es.");
$this
->assertEquals('en', $this->paragraph
->language()
->getId(), "The paragraph was created with its default langcode en.");
$this
->buildNodeForm($embedded);
$this
->submitNodeForm();
$langcode = $this->node
->language()
->getId();
$this
->assertEquals('es', $langcode, "The node's langcode remains unchanged to value es (after submission).");
$this
->assertEquals($langcode, $this->paragraph
->language()
->getId(), "The paragraph's langcode was inherited from its parent (after submission).");
$this
->buildNodeForm($embedded);
if ($embedded) {
$this->formState
->setValue([
'embedded_entity_form',
'langcode',
], [
[
'value' => 'en',
],
]);
}
else {
$this->formState
->setValue('langcode', [
[
'value' => 'en',
],
]);
}
$this
->submitNodeForm();
$langcode = $this->node
->language()
->getId();
$this
->assertEquals('en', $langcode, "The node's langcode was updated to value en (after submission).");
$this
->assertEquals($langcode, $this->paragraph
->language()
->getId(), "The paragraph's updated langcode was inherited from its parent (after submission).");
$this
->buildNodeForm($embedded);
if ($embedded) {
$this->formState
->setValue([
'embedded_entity_form',
'langcode',
], [
[
'value' => 'es',
],
]);
}
else {
$this->formState
->setValue('langcode', [
[
'value' => 'es',
],
]);
}
$this
->submitNodeForm();
$langcode = $this->node
->language()
->getId();
$this
->assertEquals('es', $langcode, "The node's langcode was set to es (after rebuild and submission).");
$this
->assertEquals($langcode, $this->paragraph
->language()
->getId(), "The paragraph's langcode was inherited from its parent (after rebuild and submission).");
}
protected function buildNodeForm($embedded = FALSE) {
if ($embedded) {
$this->formObject = new TestEmbeddedEntityForm($this->node);
}
else {
$this->formObject = $this->entityTypeManager
->getFormObject('node', 'default');
$this->formObject
->setEntity($this->node);
}
$this->formState = (new FormState())
->disableRedirect()
->setFormObject($this->formObject);
$this->form = $this->formBuilder
->buildForm($this->formObject, $this->formState);
$this
->reassignEntities();
}
protected function submitNodeForm() {
$this->formState
->setValue('op', $this->formState
->getValue('submit'));
$this->formBuilder
->submitForm($this->formObject, $this->formState);
$this
->reassignEntities();
}
protected function reassignEntities() {
$this->node = $this->formObject
->getEntity();
$paragraphs = $this->node
->get($this->nodeParagraphsFieldName)
->referencedEntities();
$this->paragraph = reset($paragraphs);
}
}