function PathWorkspacesTest::testPathAliasesUserSwitch

Same name in other branches
  1. 9 core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php \Drupal\Tests\workspaces\Functional\PathWorkspacesTest::testPathAliasesUserSwitch()
  2. 10 core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php \Drupal\Tests\workspaces\Functional\PathWorkspacesTest::testPathAliasesUserSwitch()
  3. 11.x core/modules/workspaces/tests/src/Functional/PathWorkspacesTest.php \Drupal\Tests\workspaces\Functional\PathWorkspacesTest::testPathAliasesUserSwitch()

Tests path aliases with workspaces and user switching.

File

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

Class

PathWorkspacesTest
Tests path aliases with workspaces.

Namespace

Drupal\Tests\workspaces\Functional

Code

public function testPathAliasesUserSwitch() {
    // Create a published node in Live, without an alias.
    $node = $this->drupalCreateNode([
        'type' => 'article',
        'status' => TRUE,
    ]);
    // Switch to Stage and create an alias for the node.
    $stage = Workspace::load('stage');
    $this->switchToWorkspace($stage);
    $edit = [
        'path[0][alias]' => '/' . $this->randomMachineName(),
    ];
    $this->drupalPostForm('node/' . $node->id() . '/edit', $edit, 'Save');
    // Check that the node can be accessed in Stage with the given alias.
    $path = $edit['path[0][alias]'];
    $this->assertAccessiblePaths([
        $path,
    ]);
    // Check that the 'preload-paths' cache includes the active workspace ID in
    // the cache key.
    $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:stage:/node/1'));
    $this->assertFalse(\Drupal::cache('data')->get('preload-paths:/node/1'));
    // Check that the alias can not be accessed in Live, by logging out without
    // an explicit switch.
    $this->drupalLogout();
    $this->assertNotAccessiblePaths([
        $path,
    ]);
    $this->assertFalse(\Drupal::cache('data')->get('preload-paths:/node/1'));
    // Publish the workspace and check that the alias can be accessed in Live.
    $this->drupalLogin($this->rootUser);
    $stage->publish();
    $this->drupalLogout();
    $this->assertAccessiblePaths([
        $path,
    ]);
    $this->assertNotEmpty(\Drupal::cache('data')->get('preload-paths:/node/1'));
}

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