function EntityWorkspaceConflictConstraintValidatorTest::testNewEntitiesAllowedInDefaultWorkspace

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

@covers ::validate

File

core/modules/workspaces/tests/src/Kernel/EntityWorkspaceConflictConstraintValidatorTest.php, line 57

Class

EntityWorkspaceConflictConstraintValidatorTest
@coversDefaultClass \Drupal\workspaces\Plugin\Validation\Constraint\EntityWorkspaceConflictConstraintValidator[[api-linebreak]] @group workspaces

Namespace

Drupal\Tests\workspaces\Kernel

Code

public function testNewEntitiesAllowedInDefaultWorkspace() : void {
  // Create two top-level workspaces and a second-level one.
  $stage = Workspace::create([
    'id' => 'stage',
    'label' => 'Stage',
  ]);
  $stage->save();
  $dev = Workspace::create([
    'id' => 'dev',
    'label' => 'Dev',
    'parent' => 'stage',
  ]);
  $dev->save();
  $other = Workspace::create([
    'id' => 'other',
    'label' => 'Other',
  ]);
  $other->save();
  // Create an entity in Live, and check that the validation is skipped.
  $entity = EntityTestMulRevPub::create();
  $this->assertCount(0, $entity->validate());
  $entity->save();
  $entity = $this->reloadEntity($entity);
  $this->assertCount(0, $entity->validate());
  // Edit the entity in Stage.
  $this->switchToWorkspace('stage');
  $entity->save();
  $entity = $this->reloadEntity($entity);
  $this->assertCount(0, $entity->validate());
  $expected_message = 'The content is being edited in the Stage workspace. As a result, your changes cannot be saved.';
  // Check that the entity can no longer be edited in Live.
  $this->switchToLive();
  $entity = $this->reloadEntity($entity);
  $violations = $entity->validate();
  $this->assertCount(1, $violations);
  $this->assertSame($expected_message, (string) $violations->get(0)
    ->getMessage());
  // Check that the entity can no longer be edited in another top-level
  // workspace.
  $this->switchToWorkspace('other');
  $entity = $this->reloadEntity($entity);
  $violations = $entity->validate();
  $this->assertCount(1, $violations);
  $this->assertSame($expected_message, (string) $violations->get(0)
    ->getMessage());
  // Check that the entity can still be edited in a sub-workspace of Stage.
  $this->switchToWorkspace('dev');
  $entity = $this->reloadEntity($entity);
  $this->assertCount(0, $entity->validate());
  // Edit the entity in Dev.
  $this->switchToWorkspace('dev');
  $entity->save();
  $entity = $this->reloadEntity($entity);
  $this->assertCount(0, $entity->validate());
  $expected_message = 'The content is being edited in the Dev workspace. As a result, your changes cannot be saved.';
  // Check that the entity can no longer be edited in Live.
  $this->switchToLive();
  $entity = $this->reloadEntity($entity);
  $violations = $entity->validate();
  $this->assertCount(1, $violations);
  $this->assertSame($expected_message, (string) $violations->get(0)
    ->getMessage());
  // Check that the entity can no longer be edited in the parent workspace.
  $this->switchToWorkspace('stage');
  $entity = $this->reloadEntity($entity);
  $violations = $entity->validate();
  $this->assertCount(1, $violations);
  $this->assertSame($expected_message, (string) $violations->get(0)
    ->getMessage());
  // Check that the entity can no longer be edited in another top-level
  // workspace.
  $this->switchToWorkspace('other');
  $entity = $this->reloadEntity($entity);
  $violations = $entity->validate();
  $this->assertCount(1, $violations);
  $this->assertSame($expected_message, (string) $violations->get(0)
    ->getMessage());
}

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