public function ConfigSourceUnitTest::testNodeType

Tests the node type config entity.

File

sources/tmgmt_config/tests/src/Kernel/ConfigSourceUnitTest.php, line 46

Class

ConfigSourceUnitTest
Unit tests for exporting translatable data from config entities and saving it back.

Namespace

Drupal\Tests\tmgmt_config\Kernel

Code

public function testNodeType() {

  // Create an english test entity.
  $node_type = NodeType::create(array(
    'type' => 'test',
    'name' => 'Node type name',
    'description' => 'Node type description',
    'title_label' => 'Title label',
    'langcode' => 'en',
  ));
  $node_type
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('config', 'node_type', 'node.type.' . $node_type
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('config');
  $data = $source_plugin
    ->getData($job_item);

  // Test the name property.
  $this
    ->assertEquals('Name', $data['name']['#label']);
  $this
    ->assertEquals($node_type
    ->label(), $data['name']['#text']);
  $this
    ->assertTrue($data['name']['#translate']);
  $this
    ->assertEquals('Description', $data['description']['#label']);
  $this
    ->assertEquals($node_type
    ->getDescription(), $data['description']['#text']);
  $this
    ->assertTrue($data['description']['#translate']);

  // Test item types.
  $this
    ->assertEquals('Content type', $source_plugin
    ->getItemTypes()['node_type']);

  // Now request a translation and save it back.
  $job
    ->requestTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->acceptTranslation();
  $data = $item
    ->getData();

  // Check that the translations were saved correctly.
  $language_manager = \Drupal::languageManager();
  $language_manager
    ->setConfigOverrideLanguage($language_manager
    ->getLanguage('de'));
  $node_type = NodeType::load($node_type
    ->id());
  $this
    ->assertEquals($data['name']['#translation']['#text'], $node_type
    ->label());
  $this
    ->assertEquals($data['description']['#translation']['#text'], $node_type
    ->getDescription());
}