You are here

public function ContentEntitySourceContentModerationTest::testModerationWithUntranslatableCompositeEntityReference in Translation Management Tool 8

Tests content moderation translations with untranslatable composites.

File

sources/content/tests/src/Functional/ContentEntitySourceContentModerationTest.php, line 69

Class

ContentEntitySourceContentModerationTest
Tests content entity source integration with content moderation.

Namespace

Drupal\Tests\tmgmt_content\Functional

Code

public function testModerationWithUntranslatableCompositeEntityReference() {
  $this
    ->createNodeType('composite', 'composite', TRUE, FALSE);
  $this
    ->createEditorialWorkflow('composite');

  // Enable entity translations for entity_test_composite.
  \Drupal::service('content_translation.manager')
    ->setEnabled('entity_test_composite', 'entity_test_composite', TRUE);

  // Create the reference field to the composite entity test.
  $this
    ->createEntityReferenceField('node', 'composite', 'entity_test_composite', 'entity_test_composite', 'entity_test_composite');
  FieldConfig::loadByName('node', 'composite', 'entity_test_composite')
    ->setTranslatable(FALSE)
    ->save();

  /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $fd */
  \Drupal::service('entity_display.repository')
    ->getFormDisplay('node', 'composite')
    ->setComponent('entity_test_composite', [
    'type' => 'entity_reference_autocomplete',
  ])
    ->save();

  // Create the composite entity test.
  $composite = EntityTestComposite::create([
    'name' => 'composite name',
  ]);
  $composite
    ->save();

  // Allow auto-accept.
  $this->default_translator
    ->setAutoAccept(TRUE)
    ->save();
  $user = $this
    ->loginAsTranslator([
    'administer tmgmt',
    'translate any entity',
    'create content translations',
    'access content',
    'view own unpublished content',
    'edit own composite content',
    'access content overview',
    'view all revisions',
    'view latest version',
    'view test entity',
    'use ' . $this->workflow
      ->id() . ' transition create_new_draft',
    'use ' . $this->workflow
      ->id() . ' transition publish',
  ]);

  // Create a node with a reference to the composite entity test.
  $node = $this
    ->createNode([
    'title' => 'node title',
    'type' => 'composite',
    'entity_test_composite' => $composite,
    'moderation_state' => 'published',
    'uid' => $user
      ->id(),
  ]);

  // Create a draft.
  $edit = [
    'title[0][value]' => 'node title (draft)',
    'moderation_state[0][state]' => 'draft',
  ];
  $this
    ->drupalPostForm($node
    ->toUrl('edit-form')
    ->toString(), $edit, 'Save');

  // Create a translation in German.
  $this
    ->drupalPostForm('/node/' . $node
    ->id() . '/translations', [
    'languages[de]' => TRUE,
  ], 'Request translation');
  $this
    ->assertUniqueText('One job needs to be checked out.');
  $this
    ->drupalPostForm(NULL, [
    'target_language' => 'de',
  ], 'Submit to provider');
  $this
    ->assertText('Test translation created.');
  $this
    ->assertText('This translation cannot be accepted as there is a pending revision in the default translation. You must publish node title (draft) first before saving this translation.');

  // Assert the translation can not be accepted if there is a composite entity
  // reference field.
  $this
    ->clickLink('Review');
  $url = $this
    ->getUrl();
  $this
    ->drupalPostForm(NULL, [], 'Validate');
  $this
    ->assertUniqueText('Validation completed successfully.');
  $this
    ->drupalPostForm(NULL, [], 'Save as completed');
  $this
    ->assertText('This translation cannot be accepted as there is a pending revision in the default translation. You must publish node title (draft) first before saving this translation.');

  // Publish the source draft first and accept the translation.
  $this
    ->clickLink('node title (draft)');
  $this
    ->drupalPostForm(NULL, [
    'new_state' => 'published',
  ], 'Apply');
  $this
    ->assertUniqueText('The moderation state has been updated.');
  $this
    ->drupalGet($url);
  $this
    ->assertOptionSelected('edit-moderation-state-new-state', 'published');
  $this
    ->drupalPostForm(NULL, [], 'Save as completed');
  $this
    ->assertText('The translation for node title (draft) has been accepted as de(de-ch): node title (draft).');
  $this
    ->drupalGet($node
    ->toUrl('edit-form')
    ->toString());
  $this
    ->assertFieldByName('entity_test_composite[0][target_id]', 'composite name (' . $composite
    ->id() . ')');
  $composite = EntityTestComposite::load($composite
    ->id());
  $this
    ->assertEquals('de(de-ch): composite name', $composite
    ->getTranslation('de')
    ->getName());
  $referenced_values = [
    'langcode' => 'en',
    'name' => 'Referenced entity',
  ];
  $referenced_entity = EntityTestComposite::create($referenced_values);
  $referenced_entity
    ->save();

  // Create a main entity.
  $node = Node::create([
    'type' => 'composite',
    'title' => 'Example',
    'entity_test_composite' => $referenced_entity,
  ]);
  $node
    ->save();
  $job = tmgmt_job_create('en', 'es');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', 'node', $node
    ->id(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();

  // Now request a translation.
  $job
    ->requestTranslation();
  $job
    ->acceptTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->isAccepted();
  $node = Node::load($node
    ->id());
  $this
    ->assertTrue($node
    ->hasTranslation('es'));
  $job = tmgmt_job_create('en', 'de');
  $job
    ->acceptTranslation();
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = tmgmt_job_item_create('content', 'node', $node
    ->id(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();

  // Now request a translation.
  $job
    ->requestTranslation();
  $job
    ->acceptTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->isAccepted();
  $node = Node::load($node
    ->id());
  $this
    ->assertTrue($node
    ->hasTranslation('de'));
}