Pull merge.
[yaffs-website] / web / core / modules / content_moderation / tests / src / Kernel / ContentModerationStateTest.php
index 8574ad6a171b4c60ce082132835b0b084090e862..4b7d75be4dba4ab2db6258bccb6912c1650fee8d 100644 (file)
@@ -296,7 +296,7 @@ class ContentModerationStateTest extends KernelTestBase {
     // Create a French translation.
     $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
     $french_node->setUnpublished();
-    // Revision 1 (fr).
+    // Revision 2 (fr).
     $french_node->save();
     $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
     $this->assertEquals('draft', $french_node->moderation_state->value);
@@ -305,7 +305,7 @@ class ContentModerationStateTest extends KernelTestBase {
     // Move English node to create another draft.
     $english_node = $this->reloadEntity($english_node);
     $english_node->moderation_state->value = 'draft';
-    // Revision 2 (en, fr).
+    // Revision 3 (en, fr).
     $english_node->save();
     $english_node = $this->reloadEntity($english_node);
     $this->assertEquals('draft', $english_node->moderation_state->value);
@@ -316,7 +316,7 @@ class ContentModerationStateTest extends KernelTestBase {
 
     // Publish the French node.
     $french_node->moderation_state->value = 'published';
-    // Revision 3 (en, fr).
+    // Revision 4 (en, fr).
     $french_node->save();
     $french_node = $this->reloadEntity($french_node)->getTranslation('fr');
     $this->assertTrue($french_node->isPublished());
@@ -327,7 +327,7 @@ class ContentModerationStateTest extends KernelTestBase {
 
     // Publish the English node.
     $english_node->moderation_state->value = 'published';
-    // Revision 4 (en, fr).
+    // Revision 5 (en, fr).
     $english_node->save();
     $english_node = $this->reloadEntity($english_node);
     $this->assertTrue($english_node->isPublished());
@@ -336,15 +336,15 @@ class ContentModerationStateTest extends KernelTestBase {
     $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
     $this->assertTrue($french_node->isPublished());
     $french_node->moderation_state->value = 'draft';
-    // Revision 5 (en, fr).
+    // Revision 6 (en, fr).
     $french_node->save();
-    $french_node = $this->reloadEntity($english_node, 5)->getTranslation('fr');
+    $french_node = $this->reloadEntity($english_node, 6)->getTranslation('fr');
     $this->assertFalse($french_node->isPublished());
     $this->assertTrue($french_node->getTranslation('en')->isPublished());
 
     // Republish the French node.
     $french_node->moderation_state->value = 'published';
-    // Revision 6 (en, fr).
+    // Revision 7 (en, fr).
     $french_node->save();
     $french_node = $this->reloadEntity($english_node)->getTranslation('fr');
     $this->assertTrue($french_node->isPublished());
@@ -353,7 +353,7 @@ class ContentModerationStateTest extends KernelTestBase {
     $content_moderation_state = ContentModerationState::load(1);
     $content_moderation_state->set('moderation_state', 'draft');
     $content_moderation_state->setNewRevision(TRUE);
-    // Revision 7 (en, fr).
+    // Revision 8 (en, fr).
     $content_moderation_state->save();
     $english_node = $this->reloadEntity($french_node, $french_node->getRevisionId() + 1);
 
@@ -366,12 +366,12 @@ class ContentModerationStateTest extends KernelTestBase {
     $content_moderation_state = $content_moderation_state->getTranslation('fr');
     $content_moderation_state->set('moderation_state', 'draft');
     $content_moderation_state->setNewRevision(TRUE);
-    // Revision 8 (en, fr).
+    // Revision 9 (en, fr).
     $content_moderation_state->save();
 
     $english_node = $this->reloadEntity($english_node, $english_node->getRevisionId());
     $this->assertEquals('draft', $english_node->moderation_state->value);
-    $french_node = $this->reloadEntity($english_node, '8')->getTranslation('fr');
+    $french_node = $this->reloadEntity($english_node, '9')->getTranslation('fr');
     $this->assertEquals('draft', $french_node->moderation_state->value);
     // Switching the moderation state to an unpublished state should update the
     // entity.
@@ -380,7 +380,7 @@ class ContentModerationStateTest extends KernelTestBase {
     // Get the default english node.
     $english_node = $this->reloadEntity($english_node);
     $this->assertTrue($english_node->isPublished());
-    $this->assertEquals(6, $english_node->getRevisionId());
+    $this->assertEquals(7, $english_node->getRevisionId());
   }
 
   /**
@@ -416,25 +416,83 @@ class ContentModerationStateTest extends KernelTestBase {
 
   /**
    * Tests that entities with special languages can be moderated.
+   *
+   * @dataProvider moderationWithSpecialLanguagesTestCases
    */
-  public function testModerationWithSpecialLanguages() {
+  public function testModerationWithSpecialLanguages($original_language, $updated_language) {
     $workflow = $this->createEditorialWorkflow();
     $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_rev', 'entity_test_rev');
     $workflow->save();
 
     // Create a test entity.
     $entity = EntityTestRev::create([
-      'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
+      'langcode' => $original_language,
     ]);
     $entity->save();
     $this->assertEquals('draft', $entity->moderation_state->value);
 
     $entity->moderation_state->value = 'published';
+    $entity->langcode = $updated_language;
     $entity->save();
 
     $this->assertEquals('published', EntityTestRev::load($entity->id())->moderation_state->value);
   }
 
+  /**
+   * Test cases for ::testModerationWithSpecialLanguages().
+   */
+  public function moderationWithSpecialLanguagesTestCases() {
+    return [
+      'Not specified to not specified' => [
+        LanguageInterface::LANGCODE_NOT_SPECIFIED,
+        LanguageInterface::LANGCODE_NOT_SPECIFIED,
+      ],
+      'English to not specified' => [
+        'en',
+        LanguageInterface::LANGCODE_NOT_SPECIFIED,
+      ],
+      'Not specified to english' => [
+        LanguageInterface::LANGCODE_NOT_SPECIFIED,
+        'en',
+      ],
+    ];
+  }
+
+  /**
+   * Test changing the language of content without adding a translation.
+   */
+  public function testChangingContentLangcode() {
+    ConfigurableLanguage::createFromLangcode('fr')->save();
+    NodeType::create([
+      'type' => 'test_type',
+    ])->save();
+    $workflow = $this->createEditorialWorkflow();
+    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'test_type');
+    $workflow->save();
+
+    $entity = Node::create([
+      'title' => 'Test node',
+      'langcode' => 'en',
+      'type' => 'test_type',
+    ]);
+    $entity->save();
+
+    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
+    $this->assertCount(1, $entity->getTranslationLanguages());
+    $this->assertCount(1, $content_moderation_state->getTranslationLanguages());
+    $this->assertEquals('en', $entity->langcode->value);
+    $this->assertEquals('en', $content_moderation_state->langcode->value);
+
+    $entity->langcode = 'fr';
+    $entity->save();
+
+    $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
+    $this->assertCount(1, $entity->getTranslationLanguages());
+    $this->assertCount(1, $content_moderation_state->getTranslationLanguages());
+    $this->assertEquals('fr', $entity->langcode->value);
+    $this->assertEquals('fr', $content_moderation_state->langcode->value);
+  }
+
   /**
    * Tests that a non-translatable entity type with a langcode can be moderated.
    */