Skip to content
Snippets Groups Projects
Unverified Commit 76fa7e95 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3027598 by amateescu, pmelab, alexpott, larowlan: Omit workspaces...

Issue #3027598 by amateescu, pmelab, alexpott, larowlan: Omit workspaces entity presave and predelete hooks for internal entities
parent 155c2d43
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
......@@ -4,6 +4,7 @@
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\RevisionableInterface;
use Drupal\Core\Form\FormStateInterface;
......@@ -116,9 +117,10 @@ public function entityPreload(array $ids, $entity_type_id) {
public function entityPresave(EntityInterface $entity) {
$entity_type = $entity->getEntityType();
// Only run if this is not an entity type provided by the Workspaces module
// and we are in a non-default workspace
if ($entity_type->getProvider() === 'workspaces' || $this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
// Only run if we are not dealing with an entity type provided by the
// Workspaces module, an internal entity type or if we are in a non-default
// workspace.
if ($this->shouldSkipPreOperations($entity_type)) {
return;
}
......@@ -224,9 +226,10 @@ public function entityUpdate(EntityInterface $entity) {
public function entityPredelete(EntityInterface $entity) {
$entity_type = $entity->getEntityType();
// Only run if this is not an entity type provided by the Workspaces module
// and we are in a non-default workspace
if ($entity_type->getProvider() === 'workspaces' || $this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace()) {
// Only run if we are not dealing with an entity type provided by the
// Workspaces module, an internal entity type or if we are in a non-default
// workspace.
if ($this->shouldSkipPreOperations($entity_type)) {
return;
}
......@@ -340,4 +343,24 @@ public static function entityFormEntityBuild($entity_type_id, RevisionableInterf
$entity->isDefaultRevision(FALSE);
}
/**
* Determines whether we need to react on pre-save or pre-delete operations.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type to check.
*
* @return bool
* Returns TRUE if the pre-save or pre-delete entity operations should not
* be altered in the current request, FALSE otherwise.
*/
protected function shouldSkipPreOperations(EntityTypeInterface $entity_type) {
// We should not react on pre-save and pre-delete entity operations if one
// of the following conditions are met:
// - the entity type is provided by the Workspaces module;
// - the entity type is internal, which means that it should not affect
// anything in the default (Live) workspace;
// - we are in the default workspace.
return $entity_type->getProvider() === 'workspaces' || $entity_type->isInternal() || $this->workspaceManager->getActiveWorkspace()->isDefaultWorkspace();
}
}
......@@ -4,7 +4,6 @@
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormState;
use Drupal\entity_test\Entity\EntityTestMulRev;
use Drupal\entity_test\Entity\EntityTestMulRevPub;
use Drupal\KernelTests\KernelTestBase;
use Drupal\system\Form\SiteInformationForm;
......@@ -75,6 +74,7 @@ protected function setUp() {
$this->installEntitySchema('entity_test_mulrev');
$this->installEntitySchema('entity_test_mulrevpub');
$this->installEntitySchema('entity_test_no_label');
$this->installEntitySchema('node');
$this->installEntitySchema('user');
......@@ -457,41 +457,109 @@ public function testEntityQueryRelationship() {
}
/**
* Tests CRUD operations for unsupported entity types.
* Tests CREATE operations for unsupported entity types.
*
* @dataProvider providerTestAllowedEntityCrudInNonDefaultWorkspace
*/
public function testDisallowedEntityCreateInNonDefaultWorkspace($entity_type_id, $allowed) {
$this->initializeWorkspacesModule();
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($entity_type_id);
// Switch to a non-default workspace and check whether creating a new entity
// is allowed.
$this->switchToWorkspace('stage');
$entity = $storage->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
if (!$allowed) {
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be saved in the default workspace.');
}
$entity->save();
}
/**
* Tests UPDATE operations for unsupported entity types.
*
* @dataProvider providerTestAllowedEntityCrudInNonDefaultWorkspace
*/
public function testDisallowedEntityCRUDInNonDefaultWorkspace() {
public function testDisallowedEntityUpdateInNonDefaultWorkspace($entity_type_id, $allowed) {
$this->initializeWorkspacesModule();
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($entity_type_id);
// Create an unsupported entity type in the default workspace.
// Create the entity in the default workspace.
$this->switchToWorkspace('live');
$entity_test = EntityTestMulRev::create([
'name' => 'live entity_test_mulrev',
]);
$entity_test->save();
$entity = $storage->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
$entity->save();
// Switch to a non-default workspace and check that any entity type CRUD are
// not allowed.
// Switch to a non-default workspace and check whether updating the entity
// is allowed.
$this->switchToWorkspace('stage');
// Check updating an existing entity.
$entity_test->name->value = 'stage entity_test_mulrev';
$entity_test->setNewRevision(TRUE);
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be saved in the default workspace.');
$entity_test->save();
$entity->created->value = 1;
// Check saving a new entity.
$new_entity_test = EntityTestMulRev::create([
'name' => 'stage entity_test_mulrev',
]);
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be saved in the default workspace.');
$new_entity_test->save();
// Check deleting an existing entity.
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be deleted in the default workspace.');
$entity_test->delete();
if (!$allowed) {
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be saved in the default workspace.');
}
$entity->save();
}
/**
* Tests DELETE operations for unsupported entity types.
*
* @dataProvider providerTestAllowedEntityCrudInNonDefaultWorkspace
*/
public function testDisallowedEntityDeleteInNonDefaultWorkspace($entity_type_id, $allowed) {
$this->initializeWorkspacesModule();
/** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
$storage = $this->entityTypeManager->getStorage($entity_type_id);
// Create the entity in the default workspace.
$this->switchToWorkspace('live');
$entity = $storage->createWithSampleValues($entity_type_id);
if ($entity_type_id === 'workspace') {
$entity->id = 'test';
}
$entity->save();
// Switch to a non-default workspace and check whether deleting the entity
// is allowed.
$this->switchToWorkspace('stage');
if (!$allowed) {
$this->expectException(EntityStorageException::class);
$this->expectExceptionMessage('This entity can only be deleted in the default workspace.');
}
$entity->delete();
}
/**
* Data provider for allowed entity CRUD operations.
*/
public function providerTestAllowedEntityCrudInNonDefaultWorkspace() {
return [
'workspace-provided non-internal entity type' => [
'entity_type_id' => 'workspace',
'allowed' => TRUE,
],
'internal entity type' => [
'entity_type_id' => 'entity_test_no_label',
'allowed' => TRUE,
],
'non-internal entity type' => [
'entity_type_id' => 'entity_test_mulrev',
'allowed' => FALSE,
],
];
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment