class EntityWorkspaceConflictConstraintValidatorTest
Same name and namespace in other branches
- 11.x core/modules/workspaces/tests/src/Kernel/EntityWorkspaceConflictConstraintValidatorTest.php \Drupal\Tests\workspaces\Kernel\EntityWorkspaceConflictConstraintValidatorTest
@coversDefaultClass \Drupal\workspaces\Plugin\Validation\Constraint\EntityWorkspaceConflictConstraintValidator
@group workspaces
Hierarchy
- class \Drupal\Tests\workspaces\Kernel\EntityWorkspaceConflictConstraintValidatorTest
Expanded class hierarchy of EntityWorkspaceConflictConstraintValidatorTest
File
-
core/
modules/ workspaces/ tests/ src/ Kernel/ EntityWorkspaceConflictConstraintValidatorTest.php, line 18
Namespace
Drupal\Tests\workspaces\KernelView source
class EntityWorkspaceConflictConstraintValidatorTest extends KernelTestBase {
use UserCreationTrait;
use WorkspaceTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'entity_test',
'system',
'user',
'workspaces',
];
/**
* The entity type manager.
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = \Drupal::entityTypeManager();
$this->installSchema('workspaces', [
'workspace_association',
]);
$this->installEntitySchema('entity_test_mulrevpub');
$this->installEntitySchema('workspace');
$this->installEntitySchema('user');
$this->createUser();
}
/**
* @covers ::validate
*/
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());
}
/**
* Reloads the given entity from the storage and returns it.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be reloaded.
*
* @return \Drupal\Core\Entity\EntityInterface
* The reloaded entity.
*/
protected function reloadEntity(EntityInterface $entity) : EntityInterface {
$storage = $this->entityTypeManager
->getStorage($entity->getEntityTypeId());
$storage->resetCache([
$entity->id(),
]);
return $storage->load($entity->id());
}
}
Members
Title Sort descending | Modifiers | Object type | Summary |
---|---|---|---|
EntityWorkspaceConflictConstraintValidatorTest::$entityTypeManager | protected | property | The entity type manager. |
EntityWorkspaceConflictConstraintValidatorTest::$modules | protected static | property | Modules to install. |
EntityWorkspaceConflictConstraintValidatorTest::reloadEntity | protected | function | Reloads the given entity from the storage and returns it. |
EntityWorkspaceConflictConstraintValidatorTest::setUp | protected | function | |
EntityWorkspaceConflictConstraintValidatorTest::testNewEntitiesAllowedInDefaultWorkspace | public | function | @covers ::validate[[api-linebreak]] |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.