function WorkspaceEntityDeleteTest::testEntityDeletion

Same name and namespace in other branches
  1. 11.x core/modules/workspaces/tests/src/Kernel/WorkspaceEntityDeleteTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceEntityDeleteTest::testEntityDeletion()

Test entity deletion in a workspace.

File

core/modules/workspaces/tests/src/Kernel/WorkspaceEntityDeleteTest.php, line 79

Class

WorkspaceEntityDeleteTest
Tests entity deletions with workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testEntityDeletion() : void {
  /** @var \Drupal\workspaces\WorkspaceAssociationInterface $workspace_association */
  $workspace_association = \Drupal::service('workspaces.association');
  $storage = $this->entityTypeManager
    ->getStorage('node');
  $published_live = $this->createNode([
    'title' => 'Test 1 published - live',
    'type' => 'page',
  ]);
  $unpublished_live = $this->createNode([
    'title' => 'Test 2 unpublished - live',
    'type' => 'page',
    'status' => FALSE,
  ]);
  // Create a published and an unpublished node in Stage.
  Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
  ])->save();
  $this->switchToWorkspace('stage');
  $published_stage = $this->createNode([
    'title' => 'Test 3 published - stage',
    'type' => 'page',
  ]);
  $unpublished_stage = $this->createNode([
    'title' => 'Test 4 unpublished - stage',
    'type' => 'page',
    'status' => FALSE,
  ]);
  $this->assertEquals([
    'node' => [
      4 => 3,
      5 => 4,
    ],
  ], $workspace_association->getTrackedEntities('stage', 'node'));
  $this->assertTrue($published_stage->access('delete'));
  $this->assertTrue($unpublished_stage->access('delete'));
  // While the Stage workspace is active, check that the nodes created in
  // Stage can be deleted, while the ones created in Live can not be deleted.
  $published_stage->delete();
  $this->assertEquals([
    'node' => [
      5 => 4,
    ],
  ], $workspace_association->getTrackedEntities('stage', 'node'));
  $unpublished_stage->delete();
  $this->assertEmpty($workspace_association->getTrackedEntities('stage', 'node'));
  $this->assertEmpty($storage->loadMultiple([
    $published_stage->id(),
    $unpublished_stage->id(),
  ]));
  $this->expectExceptionMessage('This content item can only be deleted in the Live workspace.');
  $this->assertFalse($published_live->access('delete'));
  $this->assertFalse($unpublished_live->access('delete'));
  $published_live->delete();
  $unpublished_live->delete();
  $this->assertNotEmpty($storage->loadMultiple([
    $published_live->id(),
    $unpublished_live->id(),
  ]));
}

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