function EntityStateChangeValidationTest::testInvalidStateMultilingual

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateMultilingual()
  2. 8.9.x core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateMultilingual()
  3. 11.x core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php \Drupal\Tests\content_moderation\Kernel\EntityStateChangeValidationTest::testInvalidStateMultilingual()

Tests state transition validation with multiple languages.

File

core/modules/content_moderation/tests/src/Kernel/EntityStateChangeValidationTest.php, line 205

Class

EntityStateChangeValidationTest
@coversDefaultClass \Drupal\content_moderation\Plugin\Validation\Constraint\ModerationStateConstraintValidator[[api-linebreak]] @group content_moderation

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testInvalidStateMultilingual() : void {
  $this->setCurrentUser($this->adminUser);
  ConfigurableLanguage::createFromLangcode('fr')->save();
  $node_type = NodeType::create([
    'type' => 'example',
    'name' => 'Example',
  ]);
  $node_type->save();
  $workflow = $this->createEditorialWorkflow();
  $workflow->getTypePlugin()
    ->addEntityTypeAndBundle('node', 'example');
  $workflow->save();
  $node = Node::create([
    'type' => 'example',
    'title' => 'English Published Node',
    'langcode' => 'en',
    'moderation_state' => 'published',
  ]);
  $node->save();
  $node_fr = $node->addTranslation('fr', $node->toArray());
  $node_fr->setTitle('French Published Node');
  $node_fr->save();
  $this->assertEquals('published', $node_fr->moderation_state->value);
  // Create a pending revision of the original node.
  $node->moderation_state = 'draft';
  $node->setNewRevision(TRUE);
  $node->isDefaultRevision(FALSE);
  $node->save();
  // For the pending english revision, there should be a violation from draft
  // to archived.
  $node->moderation_state = 'archived';
  $violations = $node->validate();
  $this->assertCount(1, $violations);
  $this->assertEquals('Invalid state transition from Draft to Archived', $violations->get(0)
    ->getMessage());
  // From the default french published revision, there should be none.
  $node_fr = Node::load($node->id())
    ->getTranslation('fr');
  $this->assertEquals('published', $node_fr->moderation_state->value);
  $node_fr->moderation_state = 'archived';
  $violations = $node_fr->validate();
  $this->assertCount(0, $violations);
  // From the latest french revision, there should also be no violation.
  $node_fr = Node::load($node->id())
    ->getTranslation('fr');
  $this->assertEquals('published', $node_fr->moderation_state->value);
  $node_fr->moderation_state = 'archived';
  $violations = $node_fr->validate();
  $this->assertCount(0, $violations);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.