You are here

public function ContentEntitySourceUnitTest::testEntityTest in Translation Management Tool 8

Create an english test entity.

File

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

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
    ->assertEqual($data['name']['#label'], 'Name');
  $this
    ->assertFalse(isset($data['name'][0]['#label']));
  $this
    ->assertFalse(isset($data['name'][0]['value']['#label']));
  $this
    ->assertEqual($data['name'][0]['value']['#text'], $entity_test->name->value);
  $this
    ->assertEqual($data['name'][0]['value']['#translate'], TRUE);

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

  // Test the image field.
  $image_item = $data['image_test'][0];
  $this
    ->assertEqual($data['image_test']['#label'], $this->image_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
    ->assertEqual($image_item['alt']['#label'], t('Alternative text'));
  $this
    ->assertEqual($image_item['alt']['#text'], $entity_test->image_test->alt);
  $this
    ->assertTrue($image_item['title']['#translate']);
  $this
    ->assertEqual($image_item['title']['#label'], t('Title'));
  $this
    ->assertEqual($image_item['title']['#text'], $entity_test->image_test->title);

  // 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
    ->assertEqual($translation->name->value, $data['name'][0]['value']['#translation']['#text']);
  $this
    ->assertEqual($translation->field_test_text[0]->value, $data['field_test_text'][0]['value']['#translation']['#text']);
  $this
    ->assertEqual($translation->field_test_text[1]->value, $data['field_test_text'][1]['value']['#translation']['#text']);
  $this
    ->assertEqual($translation->field_test_text[2]->value, $data['field_test_text'][2]['value']['#text']);
  $this
    ->assertEqual($translation->field_test_text[3]->value, $data['field_test_text'][3]['value']['#translation']['#text']);

  // 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
    ->assertEqual($data['later_addition']['#label'], $this->later_addition_label);
  $this
    ->assertFalse(isset($data['later_addition'][0]['value']['#label']));
  $this
    ->assertEqual($data['later_addition'][0]['value']['#text'], $entity_test->later_addition->value);
  $this
    ->assertEqual($data['later_addition'][0]['value']['#translate'], TRUE);
  $this
    ->assertFalse(isset($data['later_addition'][0]['format']['#label']));
  $this
    ->assertEqual($data['later_addition'][0]['value']['#format'], 'text_plain');
  $this
    ->assertEqual($data['later_addition'][0]['format']['#text'], $entity_test->later_addition->format);
  $this
    ->assertEqual($data['later_addition'][0]['format']['#translate'], FALSE);
  $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
    ->assertEqual($translation->later_addition[0]->value, $data['later_addition'][0]['value']['#translation']['#text']);
}