You are here

public function WorkspaceCRUDTest::testDeletingPublishedWorkspace in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceCRUDTest::testDeletingPublishedWorkspace()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceCRUDTest::testDeletingPublishedWorkspace()

Tests that deleting a workspace keeps its already published content.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php, line 227

Class

WorkspaceCRUDTest
Tests CRUD operations for workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testDeletingPublishedWorkspace() {
  $admin = $this
    ->createUser([
    'administer nodes',
    'create workspace',
    'view own workspace',
    'edit own workspace',
    'delete own workspace',
  ]);
  $this
    ->setCurrentUser($admin);
  $live_workspace = Workspace::create([
    'id' => 'live',
    'label' => 'Live',
  ]);
  $live_workspace
    ->save();
  $workspace = Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
  ]);
  $workspace
    ->save();
  $this->workspaceManager
    ->setActiveWorkspace($workspace);

  // Create a new node in the 'stage' workspace
  $node = $this
    ->createNode([
    'status' => TRUE,
  ]);

  // Create an additional workspace-specific revision for the node.
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();

  // The node should have 3 revisions now: a default and 2 pending ones.
  $revisions = $this->entityTypeManager
    ->getStorage('node')
    ->loadMultipleRevisions([
    1,
    2,
    3,
  ]);
  $this
    ->assertCount(3, $revisions);
  $this
    ->assertTrue($revisions[1]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[2]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[3]
    ->isDefaultRevision());

  // Publish the workspace, which should mark revision 3 as the default one
  // and keep revision 2 as a 'source' draft revision.
  $workspace
    ->publish();
  $revisions = $this->entityTypeManager
    ->getStorage('node')
    ->loadMultipleRevisions([
    1,
    2,
    3,
  ]);
  $this
    ->assertFalse($revisions[1]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[2]
    ->isDefaultRevision());
  $this
    ->assertTrue($revisions[3]
    ->isDefaultRevision());

  // Create two new workspace-revisions for the node.
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();

  // The node should now have 5 revisions.
  $revisions = $this->entityTypeManager
    ->getStorage('node')
    ->loadMultipleRevisions([
    1,
    2,
    3,
    4,
    5,
  ]);
  $this
    ->assertFalse($revisions[1]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[2]
    ->isDefaultRevision());
  $this
    ->assertTrue($revisions[3]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[4]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[5]
    ->isDefaultRevision());

  // Delete the workspace and check that only the two new pending revisions
  // were deleted by the workspace purging process.
  $workspace
    ->delete();
  $revisions = $this->entityTypeManager
    ->getStorage('node')
    ->loadMultipleRevisions([
    1,
    2,
    3,
    4,
    5,
  ]);
  $this
    ->assertCount(3, $revisions);
  $this
    ->assertFalse($revisions[1]
    ->isDefaultRevision());
  $this
    ->assertFalse($revisions[2]
    ->isDefaultRevision());
  $this
    ->assertTrue($revisions[3]
    ->isDefaultRevision());
  $this
    ->assertFalse(isset($revisions[4]));
  $this
    ->assertFalse(isset($revisions[5]));
}