function DefaultRevisionStateTest::testMultilingual

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

Tests a translatable Node.

File

core/modules/content_moderation/tests/src/Kernel/DefaultRevisionStateTest.php, line 65

Class

DefaultRevisionStateTest
Tests the correct default revision is set.

Namespace

Drupal\Tests\content_moderation\Kernel

Code

public function testMultilingual() : void {
    // Enable French.
    ConfigurableLanguage::createFromLangcode('fr')->save();
    $node_type = NodeType::create([
        'type' => 'example',
        'name' => 'Example',
    ]);
    $node_type->save();
    $this->container
        ->get('content_translation.manager')
        ->setEnabled('node', 'example', TRUE);
    $workflow = $this->createEditorialWorkflow();
    $workflow->getTypePlugin()
        ->addEntityTypeAndBundle('node', 'example');
    $workflow->save();
    $english_node = Node::create([
        'type' => 'example',
        'title' => 'Test title',
    ]);
    // Revision 1 (en).
    $english_node->setUnpublished()
        ->save();
    $this->assertEquals('draft', $english_node->moderation_state->value);
    $this->assertFalse($english_node->isPublished());
    $this->assertTrue($english_node->isDefaultRevision());
    $this->assertModerationState($english_node->getRevisionId(), $english_node->language()
        ->getId(), 'draft');
    // Revision 2 (fr)
    $french_node = $english_node->addTranslation('fr', [
        'title' => 'French title',
    ]);
    $french_node->moderation_state->value = 'published';
    $french_node->save();
    $this->assertTrue($french_node->isPublished());
    $this->assertTrue($french_node->isDefaultRevision());
    $this->assertModerationState($french_node->getRevisionId(), $french_node->language()
        ->getId(), 'published');
    // Revision 3 (fr)
    $node = Node::load($english_node->id())
        ->getTranslation('fr');
    $node->moderation_state->value = 'draft';
    $node->save();
    $this->assertFalse($node->isPublished());
    $this->assertFalse($node->isDefaultRevision());
    $this->assertModerationState($node->getRevisionId(), $node->language()
        ->getId(), 'draft');
    // Revision 4 (en)
    $latest_revision = $this->entityTypeManager
        ->getStorage('node')
        ->loadRevision(3);
    $latest_revision->moderation_state->value = 'draft';
    $latest_revision->save();
    $this->assertFalse($latest_revision->isPublished());
    $this->assertFalse($latest_revision->isDefaultRevision());
    $this->assertModerationState($latest_revision->getRevisionId(), $latest_revision->language()
        ->getId(), 'draft');
}

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