protected function ParagraphsLangcodeChangeTest::setUp

File

paragraphs/tests/src/Kernel/ParagraphsLangcodeChangeTest.php, line 114

Class

ParagraphsLangcodeChangeTest
Tests the langcode change mechanics of paragraphs.

Namespace

Drupal\Tests\paragraphs\Kernel

Code

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');

  // Activate Spanish language, so there are two languages activated.
  ConfigurableLanguage::createFromLangcode('es')
    ->save();

  // Create a paragraph type.
  $this->entityTypeManager
    ->getStorage('paragraphs_type')
    ->create([
    'label' => 'Paragraph type',
    'id' => $this->paragraphType,
    'status' => TRUE,
  ])
    ->save();

  // Create a node type.
  $this->entityTypeManager
    ->getStorage('node_type')
    ->create([
    'name' => 'Example page',
    'type' => $this->nodeType,
    'create_body' => FALSE,
  ])
    ->save();

  // Enable translations on the node type and paragraph type.
  ContentLanguageSettings::loadByEntityTypeBundle('node', $this->nodeType)
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode('en')
    ->save();
  ContentLanguageSettings::loadByEntityTypeBundle('paragraph', $this->paragraphType)
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode('en')
    ->save();

  // Create a field with paragraphs for the node type.
  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();

  // Create the form display of the node type,
  // with the language switcher enabled.
  // The default autocomplete widget does not work properly
  // within a kernel test. Thus, use a simple select list widget instead.
  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,
    ],
  ]);
}