public function ContentEntitySourceUnitTest::testNode

Test node field extraction.

File

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

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
    ->assertEquals('Title', $data['title']['#label']);
  $this
    ->assertFalse(isset($data['title'][0]['#label']));
  $this
    ->assertFalse(isset($data['title'][0]['value']['#label']));
  $this
    ->assertEquals($node
    ->getTitle(), $data['title'][0]['value']['#text']);
  $this
    ->assertTrue($data['title'][0]['value']['#translate']);

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

  // Ensure that the translation insert hook is invoked correctly.
  $tracked_translation_insertions = tmgmt_test_tracked_translation_inserts();
  $this
    ->assertTrue($tracked_translation_insertions['node'][$node
    ->id()]['de']);

  // 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();

  // Check that adding of language neutral to a translation job fails.
  $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(),
  ));
  $this
    ->expectException(EntityStorageException::class);
  $this
    ->expectExceptionMessage('Entity <em class="placeholder">und</em> could not be translated because the language <em class="placeholder">Not specified</em> is not applicable');
  $job_item
    ->save();
}