You are here

public function ContentEntitySourceUnitTest::testNode in Translation Management Tool 8

Test node field extraction.

File

sources/content/tests/src/Kernel/ContentEntitySourceUnitTest.php, line 319

Class

ContentEntitySourceUnitTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testNode() {

  // Create an english node.
  $account = $this
    ->createUser();
  $type = $this
    ->drupalCreateContentType();
  $field = FieldStorageConfig::loadByName('node', 'body');
  $field
    ->setTranslatable(TRUE);
  $field
    ->setCardinality(2);
  $field
    ->save();
  $node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'uid' => $account
      ->id(),
    'type' => $type
      ->id(),
    'langcode' => 'en',
  ]);
  $value = [
    'value' => $this
      ->randomMachineName(),
    'summary' => $this
      ->randomMachineName(),
    'format' => 'text_plain',
  ];
  $node->body
    ->appendItem($value);
  $node->body
    ->appendItem($value);
  $node
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', 'node', $node
    ->id(), array(
    'tjid' => $job
      ->id(),
  ));
  $job_item
    ->save();
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('content');
  $data = $source_plugin
    ->getData($job_item);

  // Test the title property.
  $this
    ->assertEqual($data['title']['#label'], 'Title');
  $this
    ->assertFalse(isset($data['title'][0]['#label']));
  $this
    ->assertFalse(isset($data['title'][0]['value']['#label']));
  $this
    ->assertEqual($data['title'][0]['value']['#text'], $node
    ->getTitle());
  $this
    ->assertEqual($data['title'][0]['value']['#translate'], TRUE);

  // Test the body field.
  // @todo: Fields need better labels, needs to be fixed in core.
  $this
    ->assertEqual($data['body']['#label'], 'Body');
  $this
    ->assertEqual($data['body'][0]['#label'], 'Delta #0');
  $this
    ->assertEqual((string) $data['body'][0]['value']['#label'], 'Text');
  $this
    ->assertEqual($data['body'][0]['value']['#text'], $node->body->value);
  $this
    ->assertEqual($data['body'][0]['value']['#translate'], TRUE);
  $this
    ->assertEqual($data['body'][0]['value']['#format'], 'text_plain');
  $this
    ->assertEqual((string) $data['body'][0]['summary']['#label'], 'Summary');
  $this
    ->assertEqual($data['body'][0]['summary']['#text'], $node->body->summary);
  $this
    ->assertEqual($data['body'][0]['summary']['#translate'], TRUE);
  $this
    ->assertEqual((string) $data['body'][0]['format']['#label'], 'Text format');
  $this
    ->assertEqual($data['body'][0]['format']['#text'], $node->body->format);
  $this
    ->assertEqual($data['body'][0]['format']['#translate'], FALSE);
  $this
    ->assertFalse(isset($data['body'][0]['processed']));
  $this
    ->assertEqual($data['body'][1]['#label'], 'Delta #1');
  $this
    ->assertEqual((string) $data['body'][1]['value']['#label'], 'Text');
  $this
    ->assertEqual($data['body'][1]['value']['#text'], $node->body[1]->value);
  $this
    ->assertEqual($data['body'][1]['value']['#translate'], TRUE);
  $this
    ->assertEqual((string) $data['body'][1]['summary']['#label'], 'Summary');
  $this
    ->assertEqual($data['body'][1]['summary']['#text'], $node->body[1]->summary);
  $this
    ->assertEqual($data['body'][1]['summary']['#translate'], TRUE);
  $this
    ->assertEqual($data['body'][0]['summary']['#format'], 'text_plain');
  $this
    ->assertEqual((string) $data['body'][1]['format']['#label'], 'Text format');
  $this
    ->assertEqual($data['body'][1]['format']['#text'], $node->body[1]->format);
  $this
    ->assertEqual($data['body'][1]['format']['#translate'], FALSE);
  $this
    ->assertFalse(isset($data['body'][1]['processed']));

  // Test if language neutral entities can't be added to a translation job.
  $node = Node::create([
    'title' => $this
      ->randomMachineName(),
    'uid' => $account
      ->id(),
    'type' => $type
      ->id(),
    'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
  ]);
  $node
    ->save();
  try {
    $job = tmgmt_job_create(LanguageInterface::LANGCODE_NOT_SPECIFIED, 'de');
    $job
      ->save();
    $job_item = tmgmt_job_item_create('content', 'node', $node
      ->id(), array(
      'tjid' => $job
        ->id(),
    ));
    $job_item
      ->save();
    $this
      ->fail("Adding of language neutral to a translation job did not fail.");
  } catch (\Exception $e) {
    $this
      ->pass("Adding of language neutral to a translation job did fail.");
  }
}