diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php index 9802c3df8d..e7011d761a 100644 --- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php +++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php @@ -439,9 +439,13 @@ public function isTranslatable() { public function preSave(EntityStorageInterface $storage) { // An entity requiring validation should not be saved if it has not been // actually validated. - assert(!$this->validationRequired || $this->validated, 'Entity validation was skipped.'); - - $this->validated = FALSE; + if ($this->validationRequired && !$this->validated) { + // @todo Explain why an assertion is not working. + throw new \LogicException('Entity validation was skipped.'); + } + else { + $this->validated = FALSE; + } parent::preSave($storage); } diff --git a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php index 4ec687aacd..3ea354d549 100644 --- a/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php +++ b/core/modules/system/tests/modules/entity_test/src/EntityTestForm.php @@ -80,7 +80,7 @@ public function save(array $form, FormStateInterface $form_state) { $form_state->setRebuild(); } } - catch (\AssertionError $e) { + catch (\Exception $e) { \Drupal::state()->set('entity_test.form.save.exception', get_class($e) . ': ' . $e->getMessage()); } return $status ?? FALSE; diff --git a/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php b/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php index 5cdb38fc18..7b1b0995ed 100644 --- a/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php +++ b/core/modules/system/tests/src/Functional/Entity/EntityFormTest.php @@ -225,7 +225,7 @@ public function testValidationHandlers() { $state->set('entity_test.form.validate.test', 'button-level'); $this->drupalGet('entity_test/add'); $this->submitForm([], 'Save'); - $this->assertEquals('AssertionError: Entity validation was skipped.', $state->get('entity_test.form.save.exception'), 'Button-level validation handlers behave correctly.'); + $this->assertEquals('Drupal\\Core\\Entity\\EntityStorageException: Entity validation was skipped.', $state->get('entity_test.form.save.exception'), 'Button-level validation handlers behave correctly.'); } /** diff --git a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php index 5da25e326f..fc59310593 100644 --- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php @@ -489,7 +489,7 @@ public function testRequiredValidation() { // that trying to save a non-validated entity when validation is required // results in an exception. $this->assertTrue($this->entity->isValidationRequired()); - $this->expectException(\AssertionError::class); + $this->expectException(\LogicException::class); $this->expectExceptionMessage('Entity validation was skipped.'); $this->entity->save(); }