diff --git a/core/modules/block_content/block_content.install b/core/modules/block_content/block_content.install index 3a9fb83b7b5686f06cf94662af231fb69f6794d7..b75d59b336025885ccfecb1dc8993ca64b3bb0ed 100644 --- a/core/modules/block_content/block_content.install +++ b/core/modules/block_content/block_content.install @@ -35,3 +35,18 @@ function block_content_update_10100(&$sandbox = NULL): TranslatableMarkup { $entityDefinitionUpdateManager->updateEntityType($definition); return \t('Added revision routes to Custom block entity type.'); } + +/** + * Remove the unique values constraint from block content info fields. + */ +function block_content_update_10200() { + $constraint = 'UniqueField'; + $definition_update_manager = \Drupal::entityDefinitionUpdateManager(); + $field_storage_definition = $definition_update_manager->getFieldStorageDefinition('info', 'block_content'); + $constraints = $field_storage_definition->getConstraints(); + if (isset($constraints[$constraint])) { + unset($constraints[$constraint]); + $field_storage_definition->setConstraints($constraints); + $definition_update_manager->updateFieldStorageDefinition($field_storage_definition); + } +} diff --git a/core/modules/block_content/src/Entity/BlockContent.php b/core/modules/block_content/src/Entity/BlockContent.php index dcbe2005b3110f03cb7a0b6032c40b1372cf7568..2ac44964769a6f9a0549006e9100cb238df1095a 100644 --- a/core/modules/block_content/src/Entity/BlockContent.php +++ b/core/modules/block_content/src/Entity/BlockContent.php @@ -215,8 +215,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { 'type' => 'string_textfield', 'weight' => -5, ]) - ->setDisplayConfigurable('form', TRUE) - ->addConstraint('UniqueField', []); + ->setDisplayConfigurable('form', TRUE); $fields['changed'] = BaseFieldDefinition::create('changed') ->setLabel(t('Changed')) diff --git a/core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php b/core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php index ba3f7335b888d33f2836a6b0877901b2ad33a74b..e795dd6b2a4234b8b3ab6ce509ebb967b8602ab2 100644 --- a/core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php +++ b/core/modules/block_content/tests/src/Functional/BlockContentCreationTest.php @@ -70,15 +70,6 @@ public function testBlockContentCreation() { ->loadByProperties(['info' => $edit['info[0][value]']]); $block = reset($blocks); $this->assertNotEmpty($block, 'Custom Block found in database.'); - - // Check that attempting to create another block with the same value for - // 'info' returns an error. - $this->drupalGet('block/add/basic'); - $this->submitForm($edit, 'Save'); - - // Check that the Basic block has been created. - $this->assertSession()->pageTextContains('A custom block with block description ' . $edit['info[0][value]'] . ' already exists.'); - $this->assertSession()->statusCodeEquals(200); } /** @@ -148,15 +139,6 @@ public function testBlockContentCreationMultipleViewModes() { ->loadByProperties(['info' => $edit['info[0][value]']]); $block = reset($blocks); $this->assertNotEmpty($block, 'Custom Block found in database.'); - - // Check that attempting to create another block with the same value for - // 'info' returns an error. - $this->drupalGet('block/add/basic'); - $this->submitForm($edit, 'Save'); - - // Check that the Basic block has been created. - $this->assertSession()->pageTextContains('A custom block with block description ' . $edit['info[0][value]'] . ' already exists.'); - $this->assertSession()->statusCodeEquals(200); } /** diff --git a/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php b/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php deleted file mode 100644 index 9c39686074741d88c01fe3e28e925aedadf264ed..0000000000000000000000000000000000000000 --- a/core/modules/block_content/tests/src/Functional/BlockContentValidationTest.php +++ /dev/null @@ -1,45 +0,0 @@ -<?php - -namespace Drupal\Tests\block_content\Functional; - -use Drupal\Component\Render\FormattableMarkup; - -/** - * Tests block content validation constraints. - * - * @group block_content - */ -class BlockContentValidationTest extends BlockContentTestBase { - - /** - * {@inheritdoc} - */ - protected $defaultTheme = 'stark'; - - /** - * Tests the block content validation constraints. - */ - public function testValidation() { - // Add a block. - $description = $this->randomMachineName(); - $block = $this->createBlockContent($description, 'basic'); - // Validate the block. - $violations = $block->validate(); - // Make sure we have no violations. - $this->assertCount(0, $violations); - // Save the block. - $block->save(); - - // Add another block with the same description. - $block = $this->createBlockContent($description, 'basic'); - // Validate this block. - $violations = $block->validate(); - // Make sure we have 1 violation. - $this->assertCount(1, $violations); - // Make sure the violation is on the info property - $this->assertEquals('info', $violations[0]->getPropertyPath()); - // Make sure the message is correct. - $this->assertEquals(new FormattableMarkup('A custom block with Block description %value already exists.', ['%value' => $block->label()]), $violations[0]->getMessage()); - } - -} diff --git a/core/modules/block_content/tests/src/Functional/Update/BlockContentRemoveConstraint.php b/core/modules/block_content/tests/src/Functional/Update/BlockContentRemoveConstraint.php new file mode 100644 index 0000000000000000000000000000000000000000..fd1d7f031d7c348a5dd95388861562da97be89fe --- /dev/null +++ b/core/modules/block_content/tests/src/Functional/Update/BlockContentRemoveConstraint.php @@ -0,0 +1,69 @@ +<?php + +namespace Drupal\Tests\block_content\Functional\Update; + +use Drupal\FunctionalTests\Update\UpdatePathTestBase; + +/** + * Tests the upgrade path for removing unique constraint on blocks. + * + * @group block_content + */ +class BlockContentRemoveConstraint extends UpdatePathTestBase { + + /** + * Entity definition update manager. + * + * @var \Drupal\Core\Entity\EntityDefinitionUpdateManagerInterface + */ + protected $entityDefinitionUpdateManager; + + /** + * {@inheritdoc} + */ + protected function setUp(): void { + parent::setUp(); + $this->entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager(); + } + + /** + * {@inheritdoc} + */ + protected function setDatabaseDumpFiles() { + $this->databaseDumpFiles = [ + __DIR__ . '/../../../../../system/tests/fixtures/update/drupal-9.4.0.filled.standard.php.gz', + ]; + } + + /** + * Tests the upgrade path for moderation state reindexing. + */ + public function testRunUpdates() { + $constraint = 'UniqueField'; + $constraints = $this->getFieldInfoConstraints(); + if (!isset($constraints[$constraint])) { + $constraints[$constraint] = []; + $field_storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('info', 'block_content'); + $field_storage_definition->setConstraints($constraints); + $this->entityDefinitionUpdateManager->updateFieldStorageDefinition($field_storage_definition); + } + + $this->assertCount(2, $this->getFieldInfoConstraints()); + + $this->runUpdates(); + + $this->assertCount(1, $this->getFieldInfoConstraints()); + } + + /** + * Get constraints for info field. + * + * @return array[] + * List of constraints. + */ + protected function getFieldInfoConstraints() { + $field_storage_definition = $this->entityDefinitionUpdateManager->getFieldStorageDefinition('info', 'block_content'); + return $field_storage_definition->getConstraints(); + } + +}