You are here

public function ScheduledTransitionTest::testTranslationTransition in Scheduled Transitions 8

Same name and namespace in other branches
  1. 2.x tests/src/Kernel/ScheduledTransitionTest.php \Drupal\Tests\scheduled_transitions\Kernel\ScheduledTransitionTest::testTranslationTransition()

Tests the moderation state for a specific translation is changed.

Other translations remain unaffected.

File

tests/src/Kernel/ScheduledTransitionTest.php, line 557

Class

ScheduledTransitionTest
Tests basic functionality of scheduled_transitions fields.

Namespace

Drupal\Tests\scheduled_transitions\Kernel

Code

public function testTranslationTransition() : void {
  ConfigurableLanguage::createFromLangcode('de')
    ->save();
  ConfigurableLanguage::createFromLangcode('fr')
    ->save();
  $workflow = $this
    ->createEditorialWorkflow();
  $workflow
    ->getTypePlugin()
    ->addEntityTypeAndBundle('st_entity_test', 'st_entity_test');
  $workflow
    ->save();
  $entity = TestEntity::create([
    'type' => 'st_entity_test',
  ]);
  $de = $entity
    ->addTranslation('de');
  $fr = $entity
    ->addTranslation('fr');
  $de->name = 'deName';
  $fr->name = 'frName';
  $de->moderation_state = 'draft';
  $fr->moderation_state = 'draft';
  $entity
    ->save();
  $originalRevisionId = $entity
    ->getRevisionId();
  $originalDeRevisionId = $de
    ->getRevisionId();
  $originalFrRevisionId = $fr
    ->getRevisionId();
  $this
    ->assertEquals(1, $entity
    ->id());
  $this
    ->assertEquals(1, $entity
    ->getRevisionId());
  $this
    ->assertEquals(1, $originalDeRevisionId);
  $this
    ->assertEquals(1, $originalFrRevisionId);
  $author = User::create([
    'uid' => 2,
    'name' => $this
      ->randomMachineName(),
  ]);
  $author
    ->save();
  $scheduledTransition = ScheduledTransition::create([
    'entity' => $entity,
    'entity_revision_id' => 1,
    // Transition 'de'.
    'entity_revision_langcode' => 'de',
    'author' => $author,
    'workflow' => $workflow
      ->id(),
    'moderation_state' => 'published',
    'transition_on' => (new \DateTime('2 Feb 2018 11am'))
      ->getTimestamp(),
  ]);
  $scheduledTransition
    ->save();
  $this
    ->runTransition($scheduledTransition);

  // Reload entity.
  $entity = TestEntity::load($entity
    ->id());

  // Revision ID increments for all translations.
  $this
    ->assertEquals($originalRevisionId + 1, $entity
    ->getRevisionId());
  $this
    ->assertEquals($originalFrRevisionId + 1, $entity
    ->getTranslation('fr')
    ->getRevisionId());
  $this
    ->assertEquals($originalDeRevisionId + 1, $entity
    ->getTranslation('de')
    ->getRevisionId());
  $this
    ->assertEquals('draft', $entity->moderation_state->value);
  $this
    ->assertEquals('draft', $entity
    ->getTranslation('fr')->moderation_state->value);

  // Only 'de' is published.
  $this
    ->assertEquals('published', $entity
    ->getTranslation('de')->moderation_state->value);
}