3 namespace Drupal\Tests\content_moderation\Kernel;
5 use Drupal\KernelTests\KernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9 use Drupal\workflows\Entity\Workflow;
12 * Tests the correct default revision is set.
14 * @group content_moderation
16 class DefaultRevisionStateTest extends KernelTestBase {
21 public static $modules = [
29 'content_translation',
35 * @var \Drupal\Core\Entity\EntityTypeManager
37 protected $entityTypeManager;
42 protected function setUp() {
45 $this->installSchema('node', 'node_access');
46 $this->installEntitySchema('node');
47 $this->installEntitySchema('user');
48 $this->installEntitySchema('entity_test_with_bundle');
49 $this->installEntitySchema('entity_test_rev');
50 $this->installEntitySchema('entity_test_mulrevpub');
51 $this->installEntitySchema('block_content');
52 $this->installEntitySchema('content_moderation_state');
53 $this->installConfig('content_moderation');
55 $this->entityTypeManager = $this->container->get('entity_type.manager');
59 * Tests a translatable Node.
61 public function testMultilingual() {
63 ConfigurableLanguage::createFromLangcode('fr')->save();
64 $node_type = NodeType::create([
69 $this->container->get('content_translation.manager')->setEnabled('node', 'example', TRUE);
71 $workflow = Workflow::load('editorial');
72 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
75 $english_node = Node::create([
77 'title' => 'Test title',
83 $this->assertEquals('draft', $english_node->moderation_state->value);
84 $this->assertFalse($english_node->isPublished());
85 $this->assertTrue($english_node->isDefaultRevision());
86 $this->assertModerationState($english_node->getRevisionId(), $english_node->language()->getId(), 'draft');
89 $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
90 $french_node->moderation_state->value = 'published';
92 $this->assertTrue($french_node->isPublished());
93 $this->assertTrue($french_node->isDefaultRevision());
94 $this->assertModerationState($french_node->getRevisionId(), $french_node->language()->getId(), 'published');
97 $node = Node::load($english_node->id())->getTranslation('fr');
98 $node->moderation_state->value = 'draft';
100 $this->assertFalse($node->isPublished());
101 $this->assertFalse($node->isDefaultRevision());
102 $this->assertModerationState($node->getRevisionId(), $node->language()->getId(), 'draft');
105 $latest_revision = $this->entityTypeManager->getStorage('node')->loadRevision(3);
106 $latest_revision->moderation_state->value = 'draft';
107 $latest_revision->save();
108 $this->assertFalse($latest_revision->isPublished());
109 $this->assertFalse($latest_revision->isDefaultRevision());
110 $this->assertModerationState($latest_revision->getRevisionId(), $latest_revision->language()->getId(), 'draft');
114 * Verifies the expected moderation state revision exists.
116 * @param int $revision_id
117 * The revision ID of the host entity.
118 * @param string $langcode
119 * The language code of the host entity to check.
120 * @param string $expected_state
121 * The state the content moderation state revision should be in.
122 * @param string $expected_workflow
123 * The workflow the content moderation state revision should be using.
125 protected function assertModerationState($revision_id, $langcode, $expected_state, $expected_workflow = 'editorial') {
126 $moderation_state_storage = $this->entityTypeManager->getStorage('content_moderation_state');
128 $query = $moderation_state_storage->getQuery();
129 $results = $query->allRevisions()
130 ->condition('content_entity_revision_id', $revision_id)
131 ->condition('langcode', $langcode)
133 $this->assertCount(1, $results);
135 $moderation_state = $moderation_state_storage
136 ->loadRevision(key($results))
137 ->getTranslation($langcode);
138 $this->assertEquals($expected_state, $moderation_state->get('moderation_state')->value);
139 $this->assertEquals($expected_workflow, $moderation_state->get('workflow')->target_id);