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 | Deprecated | 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]] | |
ExtensionListTestTrait::getModulePath | protected | function | Gets the path for the specified module. | |
ExtensionListTestTrait::getThemePath | protected | function | Gets the path for the specified theme. | |
RandomGeneratorTrait::getRandomGenerator | protected | function | Gets the random generator for the utility methods. | |
RandomGeneratorTrait::randomMachineName | protected | function | Generates a unique random string containing letters and numbers. | |
RandomGeneratorTrait::randomObject | public | function | Generates a random PHP object. | |
RandomGeneratorTrait::randomString | public | function | Generates a pseudo-random string of ASCII characters of codes 32 to 126. | |
RandomGeneratorTrait::randomStringValidate | Deprecated | public | function | Callback for random string validation. |
StorageCopyTrait::replaceStorageContents | protected static | function | Copy the configuration from one storage to another and remove stale items. | |
TestRequirementsTrait::checkModuleRequirements | Deprecated | private | function | Checks missing module requirements. |
TestRequirementsTrait::checkRequirements | Deprecated | protected | function | Check module requirements for the Drupal use case. |
TestRequirementsTrait::getDrupalRoot | protected static | function | Returns the Drupal root directory. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.