public function ContentEntitySourceContentModerationTest::testModerationState

Tests the Content Moderation integration.

File

sources/content/tests/src/Kernel/ContentEntitySourceContentModerationTest.php, line 60

Class

ContentEntitySourceContentModerationTest
Content entity Source unit tests.

Namespace

Drupal\Tests\tmgmt_content\Kernel

Code

public function testModerationState() {
  $this
    ->config('tmgmt_content.settings')
    ->set('default_moderation_states', [
    $this->workflow
      ->id() => 'published',
  ])
    ->save();
  $values = array(
    'langcode' => 'en',
    'user_id' => 1,
    'moderation_state' => 'published',
    'name' => $this
      ->randomMachineName(),
  );
  $entity_test = EntityTestMulRev::create($values);
  $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(), [
    'tjid' => $job
      ->id(),
  ]);
  $job_item
    ->save();
  $job
    ->requestTranslation();
  $job
    ->acceptTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $item
    ->isAccepted();
  $entity_test = EntityTestMulRev::load($entity_test
    ->id());
  $this
    ->assertTrue($entity_test
    ->hasTranslation('de'));
  $translation = $entity_test
    ->getTranslation('de');
  $this
    ->assertEquals('published', $translation
    ->get('moderation_state')->value);
  $this
    ->config('tmgmt_content.settings')
    ->set('default_moderation_states', [
    $this->workflow
      ->id() => 'draft',
  ])
    ->save();
  $entity_test = EntityTestMulRev::load($entity_test
    ->id());
  $job = tmgmt_job_create('en', 'es');
  $job->translator = 'test_translator';
  $job
    ->save();
  $job_item = $job
    ->addItem('content', $this->entityTypeId, $entity_test
    ->id());
  $job
    ->requestTranslation();
  $job
    ->acceptTranslation();
  $items = $job
    ->getItems();
  $item = reset($items);
  $this
    ->assertTrue($item
    ->isAccepted());

  // The default revision does not yet have a spanish translation.
  $entity_test = EntityTestMulRev::load($entity_test
    ->id());
  $this
    ->assertFalse($entity_test
    ->hasTranslation('es'));

  // Fetch the latest revision affecting spanish and check the moderation
  // status.
  $entity_test_revision_id = \Drupal::entityTypeManager()
    ->getStorage($this->entityTypeId)
    ->getLatestTranslationAffectedRevisionId($entity_test
    ->id(), 'es');
  $entity_test_revision = \Drupal::entityTypeManager()
    ->getStorage($this->entityTypeId)
    ->loadRevision($entity_test_revision_id);
  $this
    ->assertTrue($entity_test_revision
    ->hasTranslation('es'));
  $translation = $entity_test_revision
    ->getTranslation('es');
  $this
    ->assertEquals('draft', $translation
    ->get('moderation_state')->value);
}