You are here

public function WorkspaceCRUDTest::testDeletingWorkspaceWithChildren 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::testDeletingWorkspaceWithChildren()
  2. 10 core/modules/workspaces/tests/src/Kernel/WorkspaceCRUDTest.php \Drupal\Tests\workspaces\Kernel\WorkspaceCRUDTest::testDeletingWorkspaceWithChildren()

Tests that a workspace with children can not be deleted.

File

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

Class

WorkspaceCRUDTest
Tests CRUD operations for workspaces.

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testDeletingWorkspaceWithChildren() {
  $stage = Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
  ]);
  $stage
    ->save();
  $dev = Workspace::create([
    'id' => 'dev',
    'label' => 'Dev',
    'parent' => 'stage',
  ]);
  $dev
    ->save();

  // Check that a workspace which has children can not be deleted.
  try {
    $stage
      ->delete();
    $this
      ->fail('The Stage workspace has children and should not be deletable.');
  } catch (EntityStorageException $e) {
    $this
      ->assertEquals('The Stage workspace can not be deleted because it has child workspaces.', $e
      ->getMessage());
    $this
      ->assertNotNull(Workspace::load('stage'));
  }

  // Check that if we delete its child first, the parent workspace can also be
  // deleted.
  $dev
    ->delete();
  $stage
    ->delete();
  $this
    ->assertNull(Workspace::load('dev'));
  $this
    ->assertNull(Workspace::load('stage'));
}