You are here

public function PathWorkspacesTest::testPathAliasesWithTranslation in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php \Drupal\Tests\workspaces\Functional\PathWorkspacesTest::testPathAliasesWithTranslation()

Tests path aliases with workspaces for translatable nodes.

File

core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php, line 158

Class

PathWorkspacesTest
Tests path aliases with workspaces.

Namespace

Drupal\Tests\workspaces\Functional

Code

public function testPathAliasesWithTranslation() {
  $stage = Workspace::load('stage');

  // Create one node with a random alias.
  $default_node = $this
    ->drupalCreateNode([
    'type' => 'article',
    'langcode' => 'en',
    'status' => TRUE,
    '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(),
    'status[value]' => TRUE,
    'path[0][alias]' => '/' . $this
      ->randomMachineName(),
  ];
  $this
    ->submitForm($edit_translation, 'Save (this translation)');

  // Confirm that the alias works.
  $this
    ->drupalGet('ro' . $edit_translation['path[0][alias]']);
  $this
    ->assertSession()
    ->pageTextContains($edit_translation['body[0][value]']);
  $default_path = $default_node->path->alias;
  $translation_path = 'ro' . $edit_translation['path[0][alias]'];
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->switchToWorkspace($stage);
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);

  // Create a workspace-specific revision for the translation with a new path
  // alias.
  $edit_new_translation_draft_with_alias = [
    'path[0][alias]' => '/' . $this
      ->randomMachineName(),
  ];
  $this
    ->drupalGet('ro/node/' . $default_node
    ->id() . '/edit');
  $this
    ->submitForm($edit_new_translation_draft_with_alias, 'Save (this translation)');
  $stage_translation_path = 'ro' . $edit_new_translation_draft_with_alias['path[0][alias]'];

  // The new alias of the translation should be available in Stage, but not
  // available in Live.
  $this
    ->assertAccessiblePaths([
    $default_path,
    $stage_translation_path,
  ]);

  // Check that the previous (Live) path alias no longer works.
  $this
    ->assertNotAccessiblePaths([
    $translation_path,
  ]);

  // Switch out of Stage and check that the initial path aliases still work.
  $this
    ->switchToLive();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);

  // Switch back to Stage.
  $this
    ->switchToWorkspace($stage);

  // Create new workspace-specific revision for translation without changing
  // the path alias.
  $edit_new_translation_draft = [
    'body[0][value]' => $this
      ->randomMachineName(),
  ];
  $this
    ->drupalGet('ro/node/' . $default_node
    ->id() . '/edit');
  $this
    ->submitForm($edit_new_translation_draft, 'Save (this translation)');

  // Confirm that the new draft revision was created.
  $this
    ->assertSession()
    ->pageTextContains($edit_new_translation_draft['body[0][value]']);

  // Switch out of Stage and check that the initial path aliases still work.
  $this
    ->switchToLive();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);

  // Switch back to Stage.
  $this
    ->switchToWorkspace($stage);
  $this
    ->assertAccessiblePaths([
    $default_path,
    $stage_translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $translation_path,
  ]);

  // Create a new workspace-specific revision for translation with path alias
  // from the original language's default revision.
  $edit_new_translation_draft_with_defaults_alias = [
    'path[0][alias]' => $default_node->path->alias,
  ];
  $this
    ->drupalGet('ro/node/' . $default_node
    ->id() . '/edit');
  $this
    ->submitForm($edit_new_translation_draft_with_defaults_alias, 'Save (this translation)');

  // Switch out of Stage and check that the initial path aliases still work.
  $this
    ->switchToLive();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);

  // Check that only one path alias (the original one) is available in Stage.
  $this
    ->switchToWorkspace($stage);
  $this
    ->assertAccessiblePaths([
    $default_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $translation_path,
    $stage_translation_path,
  ]);

  // Create new workspace-specific revision for translation with a deleted
  // (empty) path alias.
  $edit_new_translation_draft_empty_alias = [
    'body[0][value]' => $this
      ->randomMachineName(),
    'path[0][alias]' => '',
  ];
  $this
    ->drupalGet('ro/node/' . $default_node
    ->id() . '/edit');
  $this
    ->submitForm($edit_new_translation_draft_empty_alias, 'Save (this translation)');

  // Check that only one path alias (the original one) is available now.
  $this
    ->switchToLive();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);
  $this
    ->switchToWorkspace($stage);
  $this
    ->assertAccessiblePaths([
    $default_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $translation_path,
    $stage_translation_path,
  ]);

  // Create a new workspace-specific revision for the translation with a new
  // path alias.
  $edit_new_translation = [
    'body[0][value]' => $this
      ->randomMachineName(),
    'path[0][alias]' => '/' . $this
      ->randomMachineName(),
  ];
  $this
    ->drupalGet('ro/node/' . $default_node
    ->id() . '/edit');
  $this
    ->submitForm($edit_new_translation, 'Save (this translation)');

  // Confirm that the new revision was created.
  $this
    ->assertSession()
    ->pageTextContains($edit_new_translation['body[0][value]']);
  $this
    ->assertSession()
    ->addressEquals('ro' . $edit_new_translation['path[0][alias]']);

  // Check that only the new path alias of the translation can be accessed.
  $new_stage_translation_path = 'ro' . $edit_new_translation['path[0][alias]'];
  $this
    ->assertAccessiblePaths([
    $default_path,
    $new_stage_translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);

  // Switch out of Stage and check that none of the workspace-specific path
  // aliases can be accessed.
  $this
    ->switchToLive();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
    $new_stage_translation_path,
  ]);

  // Publish Stage and check that its path alias for the translation can be
  // accessed.
  $stage
    ->publish();
  $this
    ->assertAccessiblePaths([
    $default_path,
    $new_stage_translation_path,
  ]);
  $this
    ->assertNotAccessiblePaths([
    $stage_translation_path,
  ]);
}