From e434d61d4cc1fec047a8950dd9d470b811f5db83 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Mon, 13 Feb 2023 14:29:07 +0000
Subject: [PATCH] Issue #3081646 by Spokje, mallezie, richardbporter,
 VladimirAus, RoSk0: Follow up for "Adding Assertions to Drupal - Test Tools"

---
 .../Ajax/AjaxResponseAttachmentsProcessor.php    |  5 +----
 .../lib/Drupal/Core/Entity/ContentEntityBase.php | 10 +++-------
 .../Plugin/Context/LazyContextRepository.php     | 16 ++++------------
 core/lib/Drupal/Core/Render/Renderer.php         |  5 +----
 .../modules/entity_test/src/EntityTestForm.php   |  2 +-
 .../src/Functional/Entity/EntityFormTest.php     |  2 +-
 .../Core/Entity/ContentEntityBaseUnitTest.php    |  2 +-
 .../Plugin/Context/LazyContextRepositoryTest.php |  2 +-
 8 files changed, 13 insertions(+), 31 deletions(-)

diff --git a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
index b1b04bfe92d0..3936ce8fe0db 100644
--- a/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
+++ b/core/lib/Drupal/Core/Ajax/AjaxResponseAttachmentsProcessor.php
@@ -109,10 +109,7 @@ public function __construct(AssetResolverInterface $asset_resolver, ConfigFactor
    * {@inheritdoc}
    */
   public function processAttachments(AttachmentsInterface $response) {
-    // @todo Convert to assertion once https://www.drupal.org/node/2408013 lands
-    if (!$response instanceof AjaxResponse) {
-      throw new \InvalidArgumentException('\Drupal\Core\Ajax\AjaxResponse instance expected.');
-    }
+    assert($response instanceof AjaxResponse, '\Drupal\Core\Ajax\AjaxResponse instance expected.');
 
     $request = $this->requestStack->getCurrentRequest();
 
diff --git a/core/lib/Drupal/Core/Entity/ContentEntityBase.php b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
index 75dd42564b2b..7521601cd58a 100644
--- a/core/lib/Drupal/Core/Entity/ContentEntityBase.php
+++ b/core/lib/Drupal/Core/Entity/ContentEntityBase.php
@@ -439,13 +439,9 @@ public function isTranslatable() {
   public function preSave(EntityStorageInterface $storage) {
     // An entity requiring validation should not be saved if it has not been
     // actually validated.
-    if ($this->validationRequired && !$this->validated) {
-      // @todo Make this an assertion in https://www.drupal.org/node/2408013.
-      throw new \LogicException('Entity validation was skipped.');
-    }
-    else {
-      $this->validated = FALSE;
-    }
+    assert(!$this->validationRequired || $this->validated, 'Entity validation was skipped.');
+
+    $this->validated = FALSE;
 
     parent::preSave($storage);
   }
diff --git a/core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php b/core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php
index 68c7d6ffaa9e..21d3adc6e610 100644
--- a/core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php
+++ b/core/lib/Drupal/Core/Plugin/Context/LazyContextRepository.php
@@ -58,18 +58,10 @@ public function getRuntimeContexts(array $context_ids) {
         $contexts[$id] = $this->contexts[$id];
         continue;
       }
-      // The IDs have been passed in @{service_id}:{unqualified_context_id}
-      // format.
-      // @todo Convert to an assert once https://www.drupal.org/node/2408013 is
-      //   in.
-      if ($id[0] === '@' && strpos($id, ':') !== FALSE) {
-        [$service_id, $unqualified_context_id] = explode(':', $id, 2);
-        // Remove the leading '@'.
-        $service_id = substr($service_id, 1);
-      }
-      else {
-        throw new \InvalidArgumentException('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
-      }
+      assert($id[0] === '@' && strpos($id, ':'), 'You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
+      list($service_id, $unqualified_context_id) = explode(':', $id, 2);
+      // Remove the leading '@'.
+      $service_id = substr($service_id, 1);
       $context_ids_by_service[$service_id][] = $unqualified_context_id;
     }
 
diff --git a/core/lib/Drupal/Core/Render/Renderer.php b/core/lib/Drupal/Core/Render/Renderer.php
index b6ff24a41e06..019a2c133fdb 100644
--- a/core/lib/Drupal/Core/Render/Renderer.php
+++ b/core/lib/Drupal/Core/Render/Renderer.php
@@ -580,10 +580,7 @@ public function executeInRenderContext(RenderContext $context, callable $callabl
     // Set the provided context and call the callable, it will use that context.
     $this->setCurrentRenderContext($context);
     $result = $callable();
-    // @todo Convert to an assertion in https://www.drupal.org/node/2408013
-    if ($context->count() > 1) {
-      throw new \LogicException('Bubbling failed.');
-    }
+    assert($context->count() <= 1, 'Bubbling failed.');
 
     // Restore the original render context.
     $this->setCurrentRenderContext($previous_context);
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 3ea354d54993..4ec687aacd96 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 (\Exception $e) {
+    catch (\AssertionError $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 7b1b0995ed5f..5cdb38fc18a9 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('Drupal\\Core\\Entity\\EntityStorageException: Entity validation was skipped.', $state->get('entity_test.form.save.exception'), 'Button-level validation handlers behave correctly.');
+    $this->assertEquals('AssertionError: 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 f3060f6c38da..6ac67ace217a 100644
--- a/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php
+++ b/core/tests/Drupal/Tests/Core/Entity/ContentEntityBaseUnitTest.php
@@ -487,7 +487,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(\LogicException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('Entity validation was skipped.');
     $this->entity->save();
   }
diff --git a/core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php b/core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php
index 82bb5e192154..a1b9c8012b12 100644
--- a/core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php
+++ b/core/tests/Drupal/Tests/Core/Plugin/Context/LazyContextRepositoryTest.php
@@ -69,7 +69,7 @@ public function testGetRuntimeMultipleContextProviders() {
    */
   public function testInvalidContextId() {
     $lazy_context_repository = new LazyContextRepository($this->container, ['test_provider']);
-    $this->expectException(\InvalidArgumentException::class);
+    $this->expectException(\AssertionError::class);
     $this->expectExceptionMessage('You must provide the context IDs in the @{service_id}:{unqualified_context_id} format.');
     $lazy_context_repository->getRuntimeContexts(['test_context', '@test_provider:test_context1']);
   }
-- 
GitLab