From b48a95b797caa0815e03bad6c1572d6661c778f6 Mon Sep 17 00:00:00 2001
From: Dave Long <dave@longwaveconsulting.com>
Date: Wed, 26 Jul 2023 11:43:18 +0100
Subject: [PATCH] Revert "Issue #3357454 by Arantxio, daffie: Remove bugfix for
 PHP bug 48383"

This reverts commit b78ae9ef49a6db9b704b32e0d5f45062a63b4ba8.
---
 .../src/Driver/Database/pgsql/Connection.php  |  9 +++
 .../KernelTests/Core/Database/InsertTest.php  | 63 -------------------
 2 files changed, 9 insertions(+), 63 deletions(-)

diff --git a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
index 325ffe95d141..3ba9748bcdde 100644
--- a/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
+++ b/core/modules/pgsql/src/Driver/Database/pgsql/Connection.php
@@ -184,6 +184,15 @@ public static function open(array &$connection_options = []) {
   public function query($query, array $args = [], $options = []) {
     $options += $this->defaultOptions();
 
+    // The PDO PostgreSQL driver has a bug which doesn't type cast booleans
+    // correctly when parameters are bound using associative arrays.
+    // @see http://bugs.php.net/bug.php?id=48383
+    foreach ($args as &$value) {
+      if (is_bool($value)) {
+        $value = (int) $value;
+      }
+    }
+
     // We need to wrap queries with a savepoint if:
     // - Currently in a transaction.
     // - A 'mimic_implicit_commit' does not exist already.
diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php
index 0f398f0af121..c1bc2f8ee255 100644
--- a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php
+++ b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php
@@ -2,7 +2,6 @@
 
 namespace Drupal\KernelTests\Core\Database;
 
-use Drupal\Core\Database\DatabaseExceptionWrapper;
 use Drupal\Core\Database\IntegrityConstraintViolationException;
 
 /**
@@ -233,66 +232,4 @@ public function testInsertIntegrityViolation() {
       ->execute();
   }
 
-  /**
-   * Tests if inserting boolean to integer field works.
-   */
-  public function testInsertBooleanToIntegerField() {
-    // @todo Remove this when https://www.drupal.org/i/3360420 drops.
-    if ($this->connection->databaseType() == 'sqlite') {
-      $this->markTestSkipped('SQLite does not use strict tables.');
-    }
-    $table_specification = [
-      'fields' => [
-        'id'  => [
-          'type' => 'int',
-          'not null' => TRUE,
-        ],
-        'test_field_1'  => [
-          'type' => 'int',
-        ],
-      ],
-      'primary key' => ['id'],
-    ];
-
-    $this->connection->schema()->createTable('insert_bool', $table_specification);
-
-    $this->expectException(DatabaseExceptionWrapper::class);
-    $this->connection->insert('insert_bool')
-      ->fields(['id' => 1, 'test_field_1' => FALSE])
-      ->execute();
-
-    // We should not have any rows in this table.
-    $this->assertEquals(0, $this->connection->select('insert_bool')->countQuery()->execute()->fetchField());
-  }
-
-  /**
-   * Tests if inserting boolean to integer field works using a query.
-   */
-  public function testQueryInsertBooleanToIntegerField() {
-    // @todo Remove this when https://www.drupal.org/i/3360420 drops.
-    if ($this->connection->databaseType() == 'sqlite') {
-      $this->markTestSkipped('SQLite does not use strict tables.');
-    }
-    $table_specification = [
-      'fields' => [
-        'id' => [
-          'type' => 'int',
-          'not null' => TRUE,
-        ],
-        'test_field_1' => [
-          'type' => 'int',
-        ],
-      ],
-      'primary key' => ['id'],
-    ];
-
-    $this->connection->schema()->createTable('insert_bool', $table_specification);
-
-    $this->expectException(DatabaseExceptionWrapper::class);
-    $this->connection->query('INSERT INTO {insert_bool} (id,test_field_1) VALUES (:id, :test_field_1)', [':id' => 1, ':test_field_1' => FALSE]);
-
-    // We should not have any rows in this table.
-    $this->assertEquals(0, $this->connection->select('insert_bool')->countQuery()->execute()->fetchField());
-  }
-
 }
-- 
GitLab