You are here

protected function ContentEntitySuggestionsTest::prepareTranslationSuggestions in Translation Management Tool 8

Prepare a node to get suggestions from.

Creates a node with two file fields. The first one is not translatable, the second one is. Both fields got two files attached, where one has translatable content (title and atl-text) and the other one not.

Return value

object The node which is prepared with all needed fields for the suggestions.

1 call to ContentEntitySuggestionsTest::prepareTranslationSuggestions()
ContentEntitySuggestionsTest::testSuggestions in sources/content/tests/src/Kernel/ContentEntitySuggestionsTest.php
Test suggested entities from a translation job.

File

sources/content/tests/src/Kernel/ContentEntitySuggestionsTest.php, line 46

Class

ContentEntitySuggestionsTest
Basic Source-Suggestions tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

protected function prepareTranslationSuggestions() {

  // Create an untranslatable node type.
  $untranslatable_type = NodeType::create([
    'type' => $this
      ->randomMachineName(),
  ]);
  $untranslatable_type
    ->save();

  // Create a translatable content type with fields.
  // Only the first field is a translatable reference.
  $type = NodeType::create([
    'type' => $this
      ->randomMachineName(),
  ]);
  $type
    ->save();
  $content_translation_manager = \Drupal::service('content_translation.manager');
  $content_translation_manager
    ->setEnabled('node', $type
    ->id(), TRUE);
  $field1 = FieldStorageConfig::create(array(
    'field_name' => 'field1',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $field1
    ->save();
  $field2 = FieldStorageConfig::create(array(
    'field_name' => 'field2',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $field2
    ->save();
  $embedded_field = FieldStorageConfig::create(array(
    'field_name' => 'embedded_field',
    'entity_type' => 'node',
    'type' => 'entity_reference',
    'cardinality' => -1,
    'settings' => array(
      'target_type' => 'node',
    ),
  ));
  $embedded_field
    ->save();
  $this
    ->config('tmgmt_content.settings')
    ->set('embedded_fields.node.embedded_field', TRUE)
    ->save();

  // Create field instances on the content type.
  FieldConfig::create(array(
    'field_storage' => $field1,
    'bundle' => $type
      ->id(),
    'label' => 'Field 1',
    'translatable' => FALSE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $field2,
    'bundle' => $type
      ->id(),
    'label' => 'Field 2',
    'translatable' => TRUE,
    'settings' => array(),
  ))
    ->save();
  FieldConfig::create(array(
    'field_storage' => $embedded_field,
    'bundle' => $type
      ->id(),
    'label' => 'Field 2',
    'translatable' => TRUE,
    'settings' => array(),
  ))
    ->save();

  // Create a translatable body field.
  node_add_body_field($type);
  $field = FieldConfig::loadByName('node', $type
    ->id(), 'body');
  $field
    ->setTranslatable(TRUE);
  $field
    ->save();

  // Create 4 translatable nodes to be referenced.
  $references = array();
  for ($i = 0; $i < 4; $i++) {
    $references[$i] = Node::create(array(
      'title' => $this
        ->randomMachineName(),
      'body' => $this
        ->randomMachineName(),
      'type' => $type
        ->id(),
    ));
    $references[$i]
      ->save();
  }

  // Create one untranslatable node.
  $untranslatable_node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => $untranslatable_type
      ->id(),
  ]);
  $untranslatable_node
    ->save();

  // Create one node in a different language.
  $different_language_node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => $type
      ->id(),
    'langcode' => 'de',
  ]);
  $different_language_node
    ->save();

  // Create a node with two translatable and two untranslatable references.
  $node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'type' => $type
      ->id(),
    'language' => 'en',
    'body' => $this
      ->randomMachineName(),
    $field1
      ->getName() => [
      [
        'target_id' => $references[0]
          ->id(),
      ],
      [
        'target_id' => $references[1]
          ->id(),
      ],
    ],
    $field2
      ->getName() => [
      [
        'target_id' => $references[2]
          ->id(),
      ],
      [
        'target_id' => $untranslatable_node
          ->id(),
      ],
      [
        'target_id' => $different_language_node
          ->id(),
      ],
    ],
    $embedded_field
      ->getName() => [
      [
        'target_id' => $references[3]
          ->id(),
      ],
    ],
  ]);
  $node
    ->save();
  $link = MenuLinkContent::create([
    'link' => [
      [
        'uri' => 'entity:node/' . $node
          ->id(),
      ],
    ],
    'title' => 'Node menu link',
    'menu_name' => 'main',
  ]);
  $link
    ->save();
  $node->link = $link;

  // Create a second menu link that is in a different language.
  $second_link = MenuLinkContent::create([
    'link' => [
      [
        'uri' => 'entity:node/' . $node
          ->id(),
      ],
    ],
    'title' => 'German Node menu link',
    'menu_name' => 'main',
    'langcode' => 'de',
  ]);
  $second_link
    ->save();
  return $node;
}