function PathContentModerationTest::testTranslatedModeratedNodeAlias

Same name and namespace in other branches
  1. 8.9.x core/modules/path/tests/src/Functional/PathContentModerationTest.php \Drupal\Tests\path\Functional\PathContentModerationTest::testTranslatedModeratedNodeAlias()
  2. 10 core/modules/path/tests/src/Functional/PathContentModerationTest.php \Drupal\Tests\path\Functional\PathContentModerationTest::testTranslatedModeratedNodeAlias()
  3. 11.x core/modules/path/tests/src/Functional/PathContentModerationTest.php \Drupal\Tests\path\Functional\PathContentModerationTest::testTranslatedModeratedNodeAlias()

Tests that translated and moderated node can get new draft revision.

File

core/modules/path/tests/src/Functional/PathContentModerationTest.php, line 143

Class

PathContentModerationTest
Tests path aliases with Content Moderation.

Namespace

Drupal\Tests\path\Functional

Code

public function testTranslatedModeratedNodeAlias() {
    // Create one node with a random alias.
    $default_node = $this->drupalCreateNode([
        'type' => 'moderated',
        'langcode' => 'en',
        'moderation_state' => 'published',
        'path' => '/' . $this->randomMachineName(),
    ]);
    // Add published translation with another alias.
    $this->drupalGet('node/' . $default_node->id());
    $this->drupalGet('node/' . $default_node->id() . '/translations');
    $this->clickLink('Add');
    $edit_translation = [
        'body[0][value]' => $this->randomMachineName(),
        'moderation_state[0][state]' => 'published',
        'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->submitForm($edit_translation, 'Save (this translation)');
    // Confirm that the alias works.
    $this->drupalGet('fr' . $edit_translation['path[0][alias]']);
    $this->assertSession()
        ->pageTextContains($edit_translation['body[0][value]']);
    $default_path = $default_node->path->alias;
    $translation_path = 'fr' . $edit_translation['path[0][alias]'];
    $this->assertPathsAreAccessible([
        $default_path,
        $translation_path,
    ]);
    // Try to create new draft revision for translation with a new path alias.
    $edit_new_translation_draft_with_alias = [
        'moderation_state[0][state]' => 'draft',
        'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->drupalGet('fr/node/' . $default_node->id() . '/edit');
    $this->submitForm($edit_new_translation_draft_with_alias, 'Save (this translation)');
    // Confirm the expected error.
    $this->assertSession()
        ->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
    // Create new draft revision for translation without changing path alias.
    $edit_new_translation_draft = [
        'body[0][value]' => $this->randomMachineName(),
        'moderation_state[0][state]' => 'draft',
    ];
    $this->drupalGet('fr/node/' . $default_node->id() . '/edit');
    $this->submitForm($edit_new_translation_draft, 'Save (this translation)');
    // Confirm that the new draft revision was created.
    $this->assertSession()
        ->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
    $this->assertSession()
        ->pageTextContains($edit_new_translation_draft['body[0][value]']);
    $this->assertPathsAreAccessible([
        $default_path,
        $translation_path,
    ]);
    // Try to create a new draft revision for translation with path alias from
    // the original language's default revision.
    $edit_new_translation_draft_with_defaults_alias = [
        'moderation_state[0][state]' => 'draft',
        'path[0][alias]' => $default_node->path->alias,
    ];
    $this->drupalGet('fr/node/' . $default_node->id() . '/edit');
    $this->submitForm($edit_new_translation_draft_with_defaults_alias, 'Save (this translation)');
    // Verify the expected error.
    $this->assertSession()
        ->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
    // Try to create new draft revision for translation with deleted (empty)
    // path alias.
    $edit_new_translation_draft_empty_alias = [
        'body[0][value]' => $this->randomMachineName(),
        'moderation_state[0][state]' => 'draft',
        'path[0][alias]' => '',
    ];
    $this->drupalGet('fr/node/' . $default_node->id() . '/edit');
    $this->submitForm($edit_new_translation_draft_empty_alias, 'Save (this translation)');
    // Confirm the expected error.
    $this->assertSession()
        ->statusMessageContains('You can only change the URL alias for the published version of this content.', 'error');
    // Create new default (published) revision for translation with new path
    // alias.
    $edit_new_translation = [
        'body[0][value]' => $this->randomMachineName(),
        'moderation_state[0][state]' => 'published',
        'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->drupalGet('fr/node/' . $default_node->id() . '/edit');
    $this->submitForm($edit_new_translation, 'Save (this translation)');
    // Confirm that the new published revision was created.
    $this->assertSession()
        ->statusMessageNotContains('You can only change the URL alias for the published version of this content.');
    $this->assertSession()
        ->pageTextContains($edit_new_translation['body[0][value]']);
    $this->assertSession()
        ->addressEquals('fr' . $edit_new_translation['path[0][alias]']);
    $this->assertPathsAreAccessible([
        $default_path,
    ]);
}

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