From 9c1e103520e6a92d690fa625d068c257c025c1d0 Mon Sep 17 00:00:00 2001
From: catch <catch@35733.no-reply.drupal.org>
Date: Thu, 7 Mar 2024 19:12:43 +0000
Subject: [PATCH] Issue #3426309 by Wim Leers, smustgrave: Add config
 validation for weights (blocks, filters, etc. all use weights)

---
 core/config/schema/core.data_types.schema.yml     | 15 +++++++++++++--
 core/config/schema/core.entity.schema.yml         |  4 ++--
 core/config/schema/core.menu.schema.yml           |  2 +-
 core/modules/block/config/schema/block.schema.yml |  2 +-
 .../config/schema/config_schema_test.schema.yml   |  2 +-
 .../config/schema/config_test.schema.yml          |  2 +-
 .../contact/config/schema/contact.schema.yml      |  2 +-
 .../filter/config/schema/filter.schema.yml        |  2 +-
 core/modules/image/config/schema/image.schema.yml |  2 +-
 .../language/config/schema/language.schema.yml    |  2 +-
 .../config/schema/layout_builder.schema.yml       |  2 +-
 .../search/config/schema/search.schema.yml        |  2 +-
 .../taxonomy/config/schema/taxonomy.schema.yml    |  5 +----
 core/modules/tour/config/schema/tour.schema.yml   |  2 +-
 core/modules/user/config/schema/user.schema.yml   |  2 +-
 .../views/config/schema/views.display.schema.yml  |  4 ++--
 .../workflows/config/schema/workflows.schema.yml  |  4 ++--
 .../KernelTests/Core/Config/ConfigSchemaTest.php  |  4 ++--
 .../Core/Entity/ConfigEntityAdapterTest.php       |  5 ++++-
 19 files changed, 38 insertions(+), 27 deletions(-)

diff --git a/core/config/schema/core.data_types.schema.yml b/core/config/schema/core.data_types.schema.yml
index ca95ffc2e858..e5dc2728338c 100644
--- a/core/config/schema/core.data_types.schema.yml
+++ b/core/config/schema/core.data_types.schema.yml
@@ -168,6 +168,17 @@ bytes:
   constraints:
     Callback: ['\Drupal\Component\Utility\Bytes', 'validateConstraint']
 
+weight:
+  type: integer
+  label: 'Weight'
+  constraints:
+    Range:
+      # @see PHP_INT_MIN (32-bit)
+      min: -2147483648
+      # @see PHP_INT_MAX (32-bit)
+      max: 2147483647
+    FullyValidatable: ~
+
 # Complex extended data types:
 
 # Root of a configuration object.
@@ -229,7 +240,7 @@ filter:
       type: boolean
       label: 'Status'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     settings:
       type: filter_settings.[%parent.id]
@@ -483,7 +494,7 @@ display_variant.plugin:
       type: required_label
       label: 'Label'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     uuid:
       type: uuid
diff --git a/core/config/schema/core.entity.schema.yml b/core/config/schema/core.entity.schema.yml
index 4b1cfb701d6a..2934ca7fee0c 100644
--- a/core/config/schema/core.entity.schema.yml
+++ b/core/config/schema/core.entity.schema.yml
@@ -101,7 +101,7 @@ field_formatter.entity_view_display:
   type: field_formatter
   mapping:
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     region:
       type: string
@@ -144,7 +144,7 @@ core.entity_form_display.*.*.*:
                 manager: plugin.manager.field.widget
                 interface: '\Drupal\Core\Field\WidgetInterface'
           weight:
-            type: integer
+            type: weight
             label: 'Weight'
           region:
             type: string
diff --git a/core/config/schema/core.menu.schema.yml b/core/config/schema/core.menu.schema.yml
index 1a6c268e1044..bf2bb195274e 100644
--- a/core/config/schema/core.menu.schema.yml
+++ b/core/config/schema/core.menu.schema.yml
@@ -16,7 +16,7 @@ core.menu.static_menu_link_overrides:
             type: string
             label: 'Parent'
           weight:
-            type: integer
+            type: weight
             label: 'Weight'
           expanded:
             type: boolean
diff --git a/core/modules/block/config/schema/block.schema.yml b/core/modules/block/config/schema/block.schema.yml
index f82c157ebc86..02c83722caf7 100644
--- a/core/modules/block/config/schema/block.schema.yml
+++ b/core/modules/block/config/schema/block.schema.yml
@@ -21,7 +21,7 @@ block.block.*:
       type: string
       label: 'Region'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     provider:
       type: string
diff --git a/core/modules/config/tests/config_schema_test/config/schema/config_schema_test.schema.yml b/core/modules/config/tests/config_schema_test/config/schema/config_schema_test.schema.yml
index def6d8d02940..c85451a9d73a 100644
--- a/core/modules/config/tests/config_schema_test/config/schema/config_schema_test.schema.yml
+++ b/core/modules/config/tests/config_schema_test/config/schema/config_schema_test.schema.yml
@@ -143,7 +143,7 @@ config_schema_test.ignore:
       type: ignore
       label: 'Indescribable'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
 
 config_schema_test.plugin_types:
diff --git a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
index 98e6be0a69d7..b0118f682116 100644
--- a/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
+++ b/core/modules/config/tests/config_test/config/schema/config_test.schema.yml
@@ -10,7 +10,7 @@ config_test_dynamic:
       type: label
       label: 'Label'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     style:
       type: string
diff --git a/core/modules/contact/config/schema/contact.schema.yml b/core/modules/contact/config/schema/contact.schema.yml
index c3f6dc0aa39a..4357a426219f 100644
--- a/core/modules/contact/config/schema/contact.schema.yml
+++ b/core/modules/contact/config/schema/contact.schema.yml
@@ -25,7 +25,7 @@ contact.form.*:
       type: text
       label: 'Auto-reply'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     message:
       type: text
diff --git a/core/modules/filter/config/schema/filter.schema.yml b/core/modules/filter/config/schema/filter.schema.yml
index 1357c7ac6189..6f57fb413dc3 100644
--- a/core/modules/filter/config/schema/filter.schema.yml
+++ b/core/modules/filter/config/schema/filter.schema.yml
@@ -35,7 +35,7 @@ filter.format.*:
         Length:
           max: 255
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     roles:
       type: sequence
diff --git a/core/modules/image/config/schema/image.schema.yml b/core/modules/image/config/schema/image.schema.yml
index 7855f5367022..bd438deb8229 100644
--- a/core/modules/image/config/schema/image.schema.yml
+++ b/core/modules/image/config/schema/image.schema.yml
@@ -23,7 +23,7 @@ image.style.*:
                 manager: plugin.manager.image.effect
                 interface: 'Drupal\image\ImageEffectInterface'
           weight:
-            type: integer
+            type: weight
           data:
             type: image.effect.[%parent.id]
 
diff --git a/core/modules/language/config/schema/language.schema.yml b/core/modules/language/config/schema/language.schema.yml
index 06259081d8c3..ea319ae44dd5 100644
--- a/core/modules/language/config/schema/language.schema.yml
+++ b/core/modules/language/config/schema/language.schema.yml
@@ -98,7 +98,7 @@ language.entity.*:
       type: string
       label: 'Direction'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     locked:
       type: boolean
diff --git a/core/modules/layout_builder/config/schema/layout_builder.schema.yml b/core/modules/layout_builder/config/schema/layout_builder.schema.yml
index 46c12e0b0b14..11bfcd736126 100644
--- a/core/modules/layout_builder/config/schema/layout_builder.schema.yml
+++ b/core/modules/layout_builder/config/schema/layout_builder.schema.yml
@@ -51,7 +51,7 @@ layout_builder.component:
     configuration:
       type: block.settings.[id]
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     additional:
       type: ignore
diff --git a/core/modules/search/config/schema/search.schema.yml b/core/modules/search/config/schema/search.schema.yml
index f4284a0e12d5..1b0b199e55db 100644
--- a/core/modules/search/config/schema/search.schema.yml
+++ b/core/modules/search/config/schema/search.schema.yml
@@ -81,7 +81,7 @@ search.page.*:
       type: string
       label: 'Search page path'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     plugin:
       type: string
diff --git a/core/modules/taxonomy/config/schema/taxonomy.schema.yml b/core/modules/taxonomy/config/schema/taxonomy.schema.yml
index 4b4cbc0a22cd..4bc6c55e8036 100644
--- a/core/modules/taxonomy/config/schema/taxonomy.schema.yml
+++ b/core/modules/taxonomy/config/schema/taxonomy.schema.yml
@@ -44,11 +44,8 @@ taxonomy.vocabulary.*:
         NotBlank:
           allowNull: true
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
-      # A weight can be any integer, positive or negative.
-      constraints:
-        NotNull: []
     new_revision:
       type: boolean
       label: 'Whether a new revision should be created by default'
diff --git a/core/modules/tour/config/schema/tour.schema.yml b/core/modules/tour/config/schema/tour.schema.yml
index 27cbcf7925a9..a51874b28848 100644
--- a/core/modules/tour/config/schema/tour.schema.yml
+++ b/core/modules/tour/config/schema/tour.schema.yml
@@ -49,7 +49,7 @@ tour.tip:
       type: required_label
       label: 'Label'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
     position:
       type: string
diff --git a/core/modules/user/config/schema/user.schema.yml b/core/modules/user/config/schema/user.schema.yml
index 222fd5b6c343..ed7e3875eda9 100644
--- a/core/modules/user/config/schema/user.schema.yml
+++ b/core/modules/user/config/schema/user.schema.yml
@@ -114,7 +114,7 @@ user.role.*:
       type: required_label
       label: 'Label'
     weight:
-      type: integer
+      type: weight
       label: 'User role weight'
     is_admin:
       type: boolean
diff --git a/core/modules/views/config/schema/views.display.schema.yml b/core/modules/views/config/schema/views.display.schema.yml
index a685e33a9901..d4af22527010 100644
--- a/core/modules/views/config/schema/views.display.schema.yml
+++ b/core/modules/views/config/schema/views.display.schema.yml
@@ -32,7 +32,7 @@ views.display.page:
           type: text
           label: 'Description'
         weight:
-          type: integer
+          type: weight
           label: 'Weight'
         enabled:
           type: boolean
@@ -63,7 +63,7 @@ views.display.page:
           type: text
           label: 'Description'
         weight:
-          type: integer
+          type: weight
           label: 'Weight'
         menu_name:
           type: string
diff --git a/core/modules/workflows/config/schema/workflows.schema.yml b/core/modules/workflows/config/schema/workflows.schema.yml
index 1d102d0ac442..fb0197ff8f20 100644
--- a/core/modules/workflows/config/schema/workflows.schema.yml
+++ b/core/modules/workflows/config/schema/workflows.schema.yml
@@ -26,7 +26,7 @@ workflows.state:
       label: 'Label'
       translation context: 'Workflow state label'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
 
 workflows.transition:
@@ -46,5 +46,5 @@ workflows.transition:
       type: string
       label: 'To state ID'
     weight:
-      type: integer
+      type: weight
       label: 'Weight'
diff --git a/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
index 5243cc4c5efe..688da95f0d49 100644
--- a/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Config/ConfigSchemaTest.php
@@ -157,7 +157,7 @@ public function testSchemaMapping() {
     ];
     $expected['mapping']['weight'] = [
       'label' => 'Weight',
-      'type' => 'integer',
+      'type' => 'weight',
     ];
     $expected['type'] = 'config_schema_test.ignore';
     $expected['unwrap_for_canonical_representation'] = TRUE;
@@ -206,7 +206,7 @@ public function testSchemaMapping() {
       ],
     ];
     $expected['mapping']['effects']['sequence']['mapping']['data']['type'] = 'image.effect.[%parent.id]';
-    $expected['mapping']['effects']['sequence']['mapping']['weight']['type'] = 'integer';
+    $expected['mapping']['effects']['sequence']['mapping']['weight']['type'] = 'weight';
     $expected['mapping']['effects']['sequence']['mapping']['uuid']['type'] = 'uuid';
     $expected['mapping']['third_party_settings']['type'] = 'sequence';
     $expected['mapping']['third_party_settings']['label'] = 'Third party settings';
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php
index 277a1165fff8..1f490bdb8e1e 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityAdapterTest.php
@@ -71,8 +71,11 @@ public function testValidate() {
     ]);
     $adapter = ConfigEntityAdapter::createFromEntity($this->entity);
     $violations = $adapter->validate();
-    $this->assertCount(1, $violations);
+    $this->assertCount(2, $violations);
     $violation = $violations->get(0);
+    $this->assertEquals('This value should be a valid number.', $violation->getMessage());
+    $this->assertEquals('weight', $violation->getPropertyPath());
+    $violation = $violations->get(1);
     $this->assertEquals('This value should be of the correct primitive type.', $violation->getMessage());
     $this->assertEquals('weight', $violation->getPropertyPath());
   }
-- 
GitLab