public function ContentEntitySourceUnitTest::testEntityTest

Create an english test entity.

File

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

Class

ContentEntitySourceUnitTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testEntityTest() {
  $values = array(
    'langcode' => 'en',
    'user_id' => 1,
  );
  $entity_test = EntityTestMul::create($values);
  $entity_test->name->value = $this
    ->randomMachineName();
  $entity_test->field_test_text
    ->appendItem([
    'value' => $this
      ->randomMachineName(),
    'format' => 'text_plain',
  ]);
  $entity_test->field_test_text
    ->appendItem([
    'value' => $this
      ->randomMachineName(),
    'format' => 'text_plain',
  ]);
  $entity_test->field_test_text
    ->appendItem([
    'value' => $this
      ->randomMachineName(),
    'format' => 'unallowed_format',
  ]);

  // Add another item that will be removed again.
  $entity_test->field_test_text
    ->appendItem([
    'value' => $this
      ->randomMachineName(),
    'format' => 'text_plain',
  ]);
  $values = array(
    'target_id' => $this->image
      ->id(),
    'alt' => $this
      ->randomMachineName(),
    'title' => $this
      ->randomMachineName(),
  );
  $entity_test->image_test
    ->appendItem($values);
  $entity_test->ignored_field
    ->appendItem([
    'value' => 'This field should not be translated.',
    'format' => 'text_plain',
  ]);
  $entity_test
    ->save();
  $job = tmgmt_job_create('en', 'de');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', $this->entityTypeId, $entity_test
    ->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 name property.
  $this
    ->assertEquals('Name', $data['name']['#label']);
  $this
    ->assertFalse(isset($data['name'][0]['#label']));
  $this
    ->assertFalse(isset($data['name'][0]['value']['#label']));
  $this
    ->assertEquals($entity_test->name->value, $data['name'][0]['value']['#text']);
  $this
    ->assertTrue($data['name'][0]['value']['#translate']);

  // Test the test field.
  $this
    ->assertEquals('Test text-field', $data['field_test_text']['#label']);
  $this
    ->assertEquals('Delta #0', $data['field_test_text'][0]['#label']);
  $this
    ->assertFalse(isset($data['field_test_text'][0]['value']['#label']));
  $this
    ->assertEquals($entity_test->field_test_text->value, $data['field_test_text'][0]['value']['#text']);
  $this
    ->assertTrue($data['field_test_text'][0]['value']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][0]['format']['#label']));
  $this
    ->assertEquals('text_plain', $data['field_test_text'][0]['value']['#format']);
  $this
    ->assertEquals($entity_test->field_test_text->format, $data['field_test_text'][0]['format']['#text']);
  $this
    ->assertFalse($data['field_test_text'][0]['format']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][0]['processed']));
  $this
    ->assertEquals('Delta #1', $data['field_test_text'][1]['#label']);
  $this
    ->assertFalse(isset($data['field_test_text'][1]['value']['#label']));
  $this
    ->assertEquals($entity_test->field_test_text[1]->value, $data['field_test_text'][1]['value']['#text']);
  $this
    ->assertTrue($data['field_test_text'][1]['value']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][1]['format']['#label']));
  $this
    ->assertEquals('text_plain', $data['field_test_text'][1]['value']['#format']);
  $this
    ->assertEquals($entity_test->field_test_text[1]->format, $data['field_test_text'][1]['format']['#text']);
  $this
    ->assertFalse($data['field_test_text'][1]['format']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][1]['processed']));
  $this
    ->assertEquals('Delta #2', $data['field_test_text'][2]['#label']);
  $this
    ->assertFalse(isset($data['field_test_text'][2]['value']['#label']));
  $this
    ->assertEquals($entity_test->field_test_text[2]->value, $data['field_test_text'][2]['value']['#text']);
  $this
    ->assertFalse($data['field_test_text'][2]['value']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][2]['format']['#label']));
  $this
    ->assertEquals($entity_test->field_test_text[2]->format, $data['field_test_text'][2]['format']['#text']);
  $this
    ->assertFalse($data['field_test_text'][2]['format']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][2]['processed']));
  $this
    ->assertEquals('Delta #3', $data['field_test_text'][3]['#label']);
  $this
    ->assertFalse(isset($data['field_test_text'][3]['value']['#label']));
  $this
    ->assertEquals('text_plain', $data['field_test_text'][3]['value']['#format']);
  $this
    ->assertEquals($entity_test->field_test_text[3]->value, $data['field_test_text'][3]['value']['#text']);
  $this
    ->assertTrue($data['field_test_text'][3]['value']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][3]['format']['#label']));
  $this
    ->assertEquals($entity_test->field_test_text[3]->format, $data['field_test_text'][3]['format']['#text']);
  $this
    ->assertFalse($data['field_test_text'][3]['format']['#translate']);
  $this
    ->assertFalse(isset($data['field_test_text'][3]['processed']));

  // Test the image field.
  $image_item = $data['image_test'][0];
  $this
    ->assertEquals($this->image_label, $data['image_test']['#label']);
  $this
    ->assertFalse(isset($image_item['#label']));
  $this
    ->assertFalse($image_item['target_id']['#translate']);
  $this
    ->assertFalse($image_item['width']['#translate']);
  $this
    ->assertFalse($image_item['height']['#translate']);
  $this
    ->assertTrue($image_item['alt']['#translate']);
  $this
    ->assertEquals('Alternative text', $image_item['alt']['#label']);
  $this
    ->assertEquals($entity_test->image_test->alt, $image_item['alt']['#text']);
  $this
    ->assertTrue($image_item['title']['#translate']);
  $this
    ->assertEquals('Title', $image_item['title']['#label']);
  $this
    ->assertEquals($entity_test->image_test->title, $image_item['title']['#text']);

  // Test the ignored field.
  $this
    ->assertFalse(isset($data['ignored_field']));

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

  // Check that the translations were saved correctly.
  $entity_test = \Drupal::entityTypeManager()
    ->getStorage($this->entityTypeId)
    ->load($entity_test
    ->id());
  $translation = $entity_test
    ->getTranslation('de');
  $this
    ->assertEquals($data['name'][0]['value']['#translation']['#text'], $translation->name->value);
  $this
    ->assertEquals($data['field_test_text'][0]['value']['#translation']['#text'], $translation->field_test_text[0]->value);
  $this
    ->assertEquals($data['field_test_text'][1]['value']['#translation']['#text'], $translation->field_test_text[1]->value);
  $this
    ->assertEquals($data['field_test_text'][2]['value']['#text'], $translation->field_test_text[2]->value);
  $this
    ->assertEquals($data['field_test_text'][3]['value']['#translation']['#text'], $translation->field_test_text[3]->value);

  // Test adding data to the source and translating again.
  $source = $entity_test
    ->getTranslation('en');
  $source->later_addition
    ->appendItem([
    'value' => 'This field data is added after the translation exists.',
    'format' => 'text_plain',
  ]);
  $entity_test
    ->save();

  // Reset the job item.
  $job_item
    ->resetData();
  $job_item
    ->save();

  // Test that the job item was updated correctly.
  $source_plugin = $this->container
    ->get('plugin.manager.tmgmt.source')
    ->createInstance('content');
  $data = $source_plugin
    ->getData($job_item);

  // Test the later addition field.
  $this
    ->assertEquals($this->later_addition_label, $data['later_addition']['#label']);
  $this
    ->assertFalse(isset($data['later_addition'][0]['value']['#label']));
  $this
    ->assertEquals($entity_test->later_addition->value, $data['later_addition'][0]['value']['#text']);
  $this
    ->assertTrue($data['later_addition'][0]['value']['#translate']);
  $this
    ->assertFalse(isset($data['later_addition'][0]['format']['#label']));
  $this
    ->assertEquals('text_plain', $data['later_addition'][0]['value']['#format']);
  $this
    ->assertEquals($entity_test->later_addition->format, $data['later_addition'][0]['format']['#text']);
  $this
    ->assertFalse($data['later_addition'][0]['format']['#translate']);
  $this
    ->assertFalse(isset($data['later_addition'][0]['processed']));

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

  // Check that the new translation was saved correctly.
  $entity_test = \Drupal::entityTypeManager()
    ->getStorage($this->entityTypeId)
    ->load($entity_test
    ->id());
  $translation = $entity_test
    ->getTranslation('de');

  // Ensure no item was created for the removed delta.
  $this
    ->assertEquals($data['later_addition'][0]['value']['#translation']['#text'], $translation->later_addition[0]->value);
}