From b1bf4a9e2084a7c709b106f350c7beab38c04f18 Mon Sep 17 00:00:00 2001 From: Wouter Wolters Date: Wed, 25 Nov 2026 18:58:02 +0100 Subject: [PATCH] [BUGFIX] Preserve raw strings in database snapshots DatabaseAccessor exports raw database values. When a snapshot stores the Doctrine JSON type name, bulkInsert() encodes the raw JSON again during restore. Objects, arrays and scalars become JSON strings. Store the plain string type for string-bound columns so the import binds raw values directly. Keep Doctrine type names for other columns because DBAL 4 binding types are enums that cannot be JSON-encoded. Assert the exported types in a unit test and cover the JSON round trip with a functional test run from a TYPO3 Core checkout. Releases: main, 10 --- CONTRIBUTING.rst | 21 ++++- .../Snapshot/DatabaseAccessor.php | 16 ++-- Tests/Functional/DatabaseAccessorTest.php | 83 +++++++++++++++++++ .../Snapshot/DatabaseAccessorTest.php | 65 +++++++++++++++ 4 files changed, 173 insertions(+), 12 deletions(-) create mode 100644 Tests/Functional/DatabaseAccessorTest.php create mode 100644 Tests/Unit/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessorTest.php diff --git a/CONTRIBUTING.rst b/CONTRIBUTING.rst index 414f24da..a3f127a5 100644 --- a/CONTRIBUTING.rst +++ b/CONTRIBUTING.rst @@ -78,7 +78,26 @@ To provide a change to the testing framework, you have to ./Build/Scripts/runTests.sh -s functional - and confirming that the test runs have the same result as without the + * Testing framework functional tests. Composer's distribution archive + omits the ``Tests/`` directory. From the Core checkout, replace the + distribution copy with a clone of your branch if you have not already + done so: + + .. code-block:: bash + + mv vendor/typo3/testing-framework "$(mktemp -d)" && \ + git clone --branch "[BRANCH_OF_CHANGE]" \ + git@github.com:[YOUR_USERNAME]/testing-framework.git \ + vendor/typo3/testing-framework + + Then run the functional tests from the Core checkout: + + .. code-block:: bash + + ./Build/Scripts/runTests.sh -s functional -d sqlite -- \ + vendor/typo3/testing-framework/Tests/Functional/ + + and confirming that the Core test runs have the same result as without the changes, 4. push the change to your fork by diff --git a/Classes/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessor.php b/Classes/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessor.php index f6729e85..bada4e8f 100644 --- a/Classes/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessor.php +++ b/Classes/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessor.php @@ -109,18 +109,12 @@ private function prepareColumns(array $columnNames, Table $table): array { $columnTypes = array_map( function (string $columnName) use ($table) { - // Doctrine DBAL v4 converted the `*ParameterType` to an enum, and therefore returning this enum instead - // of the string value like before. As this is a non-baked enum, it cannot be serialized or json_encoded, - // and breaking the snapshot export badly. Due to the requirement to support Doctrine DBAL v3 and v4 it - // is necessary to detect the enum and return the doctrine type name instead. Connection transforms - // the provided types during import to the correct ParameterType again. - // @see https://github.com/doctrine/dbal/blob/4.0.x/UPGRADE.md#bc-break-converted-enum-like-classes-to-enums - // @todo Simplify this after Doctine DBAL v3 support can be dropped. + // Bind raw strings without reconverting them, which would double-encode JSON. + // Use type names because ParameterType enum cases cannot be JSON-encoded. $type = $table->getColumn($columnName)->getType(); - $bindingType = $type->getBindingType(); - return (enum_exists(ParameterType::class)) - ? Type::lookupName($type) - : $type->getBindingType(); + return $type->getBindingType() === ParameterType::STRING + ? Types::STRING + : Type::lookupName($type); }, $columnNames ); diff --git a/Tests/Functional/DatabaseAccessorTest.php b/Tests/Functional/DatabaseAccessorTest.php new file mode 100644 index 00000000..52934726 --- /dev/null +++ b/Tests/Functional/DatabaseAccessorTest.php @@ -0,0 +1,83 @@ +get(ConnectionPool::class)->getConnectionByName(ConnectionPool::DEFAULT_CONNECTION_NAME); + $schemaManager = $connection->createSchemaManager(); + $table = new Table('tx_testingframework_snapshot'); + $table->addColumn('uid', Types::INTEGER, ['autoincrement' => true]); + $table->addColumn('payload', Types::JSON, ['notnull' => false]); + $table->addColumn('created_at', Types::DATETIME_MUTABLE); + $table->addColumn('enabled', Types::BOOLEAN); + $table->setPrimaryKey(['uid']); + $schemaManager->createTable($table); + + try { + $jsonValues = [ + '{"name":"Alice","nested":{"items":[1,true,null]}}', + '[{"name":"Alice"},{"name":"Bob"}]', + '"quoted \\"text\\""', + '42', + '1.5', + 'true', + 'false', + 'null', + '{}', + '[]', + null, + ]; + foreach ($jsonValues as $index => $json) { + $connection->insert($table->getName(), [ + 'uid' => $index + 1, + 'payload' => $json, + 'created_at' => '2026-11-14 12:34:56', + 'enabled' => $index % 2 === 0, + ], [ + 'uid' => ParameterType::INTEGER, + 'payload' => $json === null ? ParameterType::NULL : ParameterType::STRING, + 'created_at' => ParameterType::STRING, + 'enabled' => ParameterType::BOOLEAN, + ]); + } + $expected = $connection->select(['*'], $table->getName(), [], [], ['uid' => 'ASC'])->fetchAllAssociative(); + self::assertCount(count($jsonValues), $expected); + $subject = new DatabaseAccessor($connection); + $snapshot = json_decode(json_encode($subject->export(), JSON_THROW_ON_ERROR), true, 512, JSON_THROW_ON_ERROR); + $connection->executeStatement('DELETE FROM ' . $connection->quoteIdentifier($table->getName())); + + $subject->import($snapshot); + + self::assertSame($expected, $connection->select(['*'], $table->getName(), [], [], ['uid' => 'ASC'])->fetchAllAssociative()); + } finally { + $schemaManager->dropTable($table->getName()); + } + } +} diff --git a/Tests/Unit/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessorTest.php b/Tests/Unit/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessorTest.php new file mode 100644 index 00000000..a4fec902 --- /dev/null +++ b/Tests/Unit/Core/Functional/Framework/DataHandling/Snapshot/DatabaseAccessorTest.php @@ -0,0 +1,65 @@ + [Types::JSON, '{"value":[1,true,null]}', Types::STRING]; + yield 'database datetime' => [Types::DATETIME_MUTABLE, '2026-11-14 12:34:56', Types::STRING]; + yield 'plain text' => [Types::TEXT, 'unchanged', Types::STRING]; + yield 'integer keeps its type' => [Types::INTEGER, '42', Types::INTEGER]; + } + + #[Test] + #[DataProvider('databaseValuesDataProvider')] + public function exportPreservesValuesAndNormalizesStringBoundTypes(string $type, string $value, string $expectedType): void + { + $table = new Table('tx_testingframework_snapshot'); + $table->addColumn('payload', $type); + $schemaManager = self::createStub(AbstractSchemaManager::class); + $schemaManager->method('listTables')->willReturn([$table]); + $result = self::createStub(Result::class); + $result->method('fetchAllAssociative')->willReturn([['payload' => $value]]); + $queryBuilder = self::createStub(QueryBuilder::class); + $queryBuilder->method('getRestrictions')->willReturn(self::createStub(QueryRestrictionContainerInterface::class)); + $queryBuilder->method('select')->willReturnSelf(); + $queryBuilder->method('from')->willReturnSelf(); + $queryBuilder->method('executeQuery')->willReturn($result); + $connection = self::createStub(Connection::class); + $connection->method('createSchemaManager')->willReturn($schemaManager); + $connection->method('createQueryBuilder')->willReturn($queryBuilder); + + $export = (new DatabaseAccessor($connection))->export(); + self::assertSame(['payload' => $expectedType], $export[0]['columns']); + self::assertSame([[$value]], $export[0]['items']); + } +}