From 6227621d38b72e733296d6d6a6dd3b228dbe14a4 Mon Sep 17 00:00:00 2001
From: Alex Pott <alex.a.pott@googlemail.com>
Date: Thu, 29 Feb 2024 02:08:38 +0000
Subject: [PATCH] Issue #3358581 by pfrenssen, _tarik_, a.dmitriiev,
 smustgrave, longwave, larowlan: Deprecated function: array_slice(): Passing
 null to parameter #2 ($offset) of type int is deprecated in
 Drupal\Core\Config\Entity\Query\Query->execute()

---
 .../Drupal/Core/Entity/Query/QueryBase.php    |  4 +--
 .../Core/Entity/ConfigEntityQueryTest.php     | 34 +++++++++++++++++++
 2 files changed, 36 insertions(+), 2 deletions(-)

diff --git a/core/lib/Drupal/Core/Entity/Query/QueryBase.php b/core/lib/Drupal/Core/Entity/Query/QueryBase.php
index cf7446cd0a06..585a8fdf9946 100644
--- a/core/lib/Drupal/Core/Entity/Query/QueryBase.php
+++ b/core/lib/Drupal/Core/Entity/Query/QueryBase.php
@@ -193,8 +193,8 @@ public function notExists($property, $langcode = NULL) {
    * {@inheritdoc}
    */
   public function range($start = NULL, $length = NULL) {
-    $this->range = [
-      'start' => $start,
+    $this->range = is_null($start) && is_null($length) ? [] : [
+      'start' => $start ?? 0,
       'length' => $length,
     ];
     return $this;
diff --git a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php
index 18ee774338a7..c0a0172a8f50 100644
--- a/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Entity/ConfigEntityQueryTest.php
@@ -499,6 +499,40 @@ public function testSortRange() {
       ->execute();
     $this->assertSame(['1', '2', '3'], array_values($this->queryResults));
 
+    // Omit optional parameters for the range and sort.
+    $this->queryResults = $this->entityStorage->getQuery()
+      ->range()
+      ->sort('id')
+      ->execute();
+    $this->assertSame(['1', '2', '3', '4', '5', '6', '7'], array_values($this->queryResults));
+
+    // Explicitly pass NULL for the range and sort.
+    $this->queryResults = $this->entityStorage->getQuery()
+      ->range(NULL, NULL)
+      ->sort('id')
+      ->execute();
+    $this->assertSame(['1', '2', '3', '4', '5', '6', '7'], array_values($this->queryResults));
+
+    // Omit the optional start parameter for the range.
+    $this->queryResults = $this->entityStorage->getQuery()
+      ->range(NULL, 1)
+      ->sort('id')
+      ->execute();
+    $this->assertSame(['1'], array_values($this->queryResults));
+
+    // Omit the optional length parameter for the range.
+    $this->queryResults = $this->entityStorage->getQuery()
+      ->range(4)
+      ->sort('id')
+      ->execute();
+    $this->assertSame(['5', '6', '7'], array_values($this->queryResults));
+
+    // Request an empty range.
+    $this->queryResults = $this->entityStorage->getQuery()
+      ->range(0, 0)
+      ->execute();
+    $this->assertEmpty($this->queryResults);
+
     // Apply a pager with limit 4.
     $this->queryResults = $this->entityStorage->getQuery()
       ->pager('4', 0)
-- 
GitLab