Skip to content
31 changes: 31 additions & 0 deletions lib/Db/MagicMapper/MagicBulkHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ private function prepareObjectsForDynamicTable(array $objects, Register $registe
// 3. Empty strings are semantically equivalent to NULL for most use cases.
$value = match ($value) { '' => null, default => $value };

// Date/date-time normalization, matching the non-bulk path — see normalizeDateProperty().
$value = $this->normalizeDateProperty(value: $value, propertyFormat: ($schema->getProperties()[$propertyName]['format'] ?? null));

// Convert complex values for database storage.
$preparedObject[$columnName] = match (true) { is_array($value), is_object($value) => json_encode($value), default => $value };
}//end foreach
Expand Down Expand Up @@ -907,6 +910,34 @@ private function formatDateTimeForDatabase(mixed $value, ?string $default): ?str
return $formatted ?? $default;
}//end formatDateTimeForDatabase()

/**
* Normalise a date/date-time property, matching the non-bulk path
* (MagicMapper::prepareObjectDataForTable()). Shares one `datetime` column
* for both formats; see DateTimeNormalizer::formatForDatabase()/
* formatDateForDatabase() for the offset-handling difference. Anything else
* passes through unchanged.
*
* @param mixed $value The raw property value.
* @param mixed $propertyFormat The schema property's `format`, if any; any
* non-string (malformed schema) passes through.
*
* @return mixed The normalised value, or $value unchanged when not applicable.
*/
private function normalizeDateProperty(mixed $value, mixed $propertyFormat): mixed {
if (in_array($propertyFormat, ['date-time', 'date'], true) === false || $value === null) {
return $value;
}

if ($value instanceof \DateTimeInterface === false && is_string($value) === false) {
return $value;
}

return match ($propertyFormat) {
'date' => $this->dateTimeNormalizer->formatDateForDatabase($value),
default => $this->dateTimeNormalizer->formatForDatabase($value),
};
}//end normalizeDateProperty()

/**
* Hard-delete every row of a register+schema table whose `_expires` has passed.
*
Expand Down
1 change: 1 addition & 0 deletions phpmd.baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@
<violation rule="PHPMD\Rule\Design\TooManyFields" file="lib/Db/MagicMapper.php"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="lib/Db/MagicMapper.php"/>
<violation rule="PHPMD\Rule\CleanCode\StaticAccess" file="lib/Db/MagicMapper.php"/>
<violation rule="PHPMD\Rule\Design\LongClass" file="lib/Db/MagicMapper/MagicBulkHandler.php"/>
<violation rule="PHPMD\Rule\Design\LongMethod" file="lib/Db/MagicMapper/MagicRbacHandler.php" method="hasPermission"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="lib/Db/MagicMapper/MagicRbacHandler.php"/>
<violation rule="PHPMD\Rule\CleanCode\ElseExpression" file="lib/Db/MagicMapper/MagicSearchHandler.php"/>
Expand Down
92 changes: 92 additions & 0 deletions tests/Service/BulkAndHandlersIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ private function createTestRegisterAndSchema(): void {
'items' => ['type' => 'file'],
],
'website' => ['type' => 'string', 'title' => 'Website'],
'occurredAt' => ['type' => 'string', 'format' => 'date-time', 'title' => 'Occurred at'],
'validUntil' => ['type' => 'string', 'format' => 'date', 'title' => 'Valid until'],
]);
$this->testSchema = $this->schemaMapper->insert($schema);
}
Expand Down Expand Up @@ -279,6 +281,96 @@ public function testBulkSaveMixedInsertAndUpdate(): void {
$this->assertSame('Updated First Object', $updated->getObject()['naam']);
}//end testBulkSaveMixedInsertAndUpdate()

/**
* Integration coverage for #4773 against a REAL database: a bulk write
* carrying `date`/`date-time` properties as ISO-8601-with-offset strings
* (the shape every `ObjectService::saveObjects()` caller uses) must
* persist without the SQLSTATE[22007] MySQL used to reject it with, and
* must store the SAME representation the non-bulk insert path
* (`MagicMapper::insert()` → `prepareObjectDataForTable()`) does for the
* identical input — the unit-level reflection tests in
* `MagicBulkHandlerDateNormalizationTest` pin the row shape in isolation;
* this pins that the two real write paths agree once actual persistence
* is involved.
*
* @return void
*/
public function testBulkDateTimeWriteMatchesNonBulkWriteAndPersistsWithoutSqlError(): void {
$bulkUuid = Uuid::v4()->toRfc4122();
$nonBulkUuid = Uuid::v4()->toRfc4122();
$this->createdUuids[] = $bulkUuid;
$this->createdUuids[] = $nonBulkUuid;

$occurredAt = '2026-10-13T15:50:03+02:00';
$validUntil = '2026-10-20T23:30:00-05:00';

// Reaching the assertions below at all is the regression check: this
// exact ISO-8601-with-offset shape used to fail outright with
// SQLSTATE[22007] on the bulk path before #4773.
$this->objectMapper->saveObjectsToRegisterSchemaTable(
[
[
'uuid' => $bulkUuid,
'naam' => 'Bulk date test',
'occurredAt' => $occurredAt,
'validUntil' => $validUntil,
],
],
$this->testRegister,
$this->testSchema
);

$nonBulkEntity = new ObjectEntity();
$nonBulkEntity->setUuid($nonBulkUuid);
$nonBulkEntity->setRegister((string)$this->testRegister->getId());
$nonBulkEntity->setSchema((string)$this->testSchema->getId());
$nonBulkEntity->setObject(
[
'naam' => 'Non-bulk date test',
'occurredAt' => $occurredAt,
'validUntil' => $validUntil,
]
);
$this->objectMapper->insert($nonBulkEntity, $this->testRegister, $this->testSchema);

$bulkStored = $this->objectMapper->findInRegisterSchemaTable(
identifier: $bulkUuid,
register: $this->testRegister,
schema: $this->testSchema,
_rbac: false,
_multitenancy: false
);
$nonBulkStored = $this->objectMapper->findInRegisterSchemaTable(
identifier: $nonBulkUuid,
register: $this->testRegister,
schema: $this->testSchema,
_rbac: false,
_multitenancy: false
);

$bulkData = $bulkStored->getObject();
$nonBulkData = $nonBulkStored->getObject();

// Concrete expected values, not just bulk-vs-non-bulk equality: a
// regression shared by both paths (e.g. in DateTimeNormalizer itself)
// would pass an equality-only check. +02:00 converts to UTC for the
// date-time; the date keeps its given wall-clock day, read back bare
// (no time component) at this API boundary.
$this->assertSame('2026-10-13T13:50:03+00:00', $bulkData['occurredAt']);
$this->assertSame('2026-10-20', $bulkData['validUntil']);

$this->assertSame(
$nonBulkData['occurredAt'],
$bulkData['occurredAt'],
'bulk and non-bulk writes must read back an identical date-time representation'
);
$this->assertSame(
$nonBulkData['validUntil'],
$bulkData['validUntil'],
'bulk and non-bulk writes must read back an identical date representation'
);
}//end testBulkDateTimeWriteMatchesNonBulkWriteAndPersistsWithoutSqlError()

// -----------------------------------------------------------------------
// MetadataHydrationHandler tests
// -----------------------------------------------------------------------
Expand Down
236 changes: 236 additions & 0 deletions tests/Unit/Db/MagicBulkHandlerDateNormalizationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,236 @@
<?php

/**
* Unit tests for schema-aware date/date-time normalization in the bulk
* upsert path of MagicBulkHandler.
*
* Regression coverage for the gap where `prepareObjectsForDynamicTable()`
* normalized only the three fixed metadata columns (`_created`/`_updated`/
* `_expires`) and passed every other schema property through unformatted —
* so a `date-time` property given as an ISO-8601-with-offset string (the
* convention every caller of `ObjectService::saveObjects()` uses, e.g.
* `DateTime::ATOM`) reached MySQL's DATETIME column unconverted and failed
* outright (SQLSTATE[22007]). The non-bulk path
* (`MagicMapper::prepareObjectDataForTable()`) already normalized
* schema-declared `date`/`date-time` properties; this pins the bulk path to
* the same behaviour.
*
* SPDX-License-Identifier: EUPL-1.2
* SPDX-FileCopyrightText: 2026 Conduction B.V.
*
* @category Tests
* @package Unit\Db
*
* @author Conduction Development Team <info@conduction.nl>
* @copyright 2026 Conduction B.V.
* @license EUPL-1.2 https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
*
* @version GIT: <git_id>
*
* @link https://www.OpenRegister.app
*/

declare(strict_types=1);

namespace Unit\Db;

use DateTimeImmutable;
use DateTimeZone;
use OCA\OpenRegister\Db\MagicMapper\MagicBulkHandler;
use OCA\OpenRegister\Db\MagicMapper\MagicTableSql;
use OCA\OpenRegister\Db\Register;
use OCA\OpenRegister\Db\Schema;
use OCA\OpenRegister\Service\DateTimeNormalizer;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\IConfig;
use OCP\IDBConnection;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Log\LoggerInterface;
use ReflectionMethod;

class MagicBulkHandlerDateNormalizationTest extends TestCase {

private IDBConnection&MockObject $db;

private MagicBulkHandler $handler;

private Register $register;

protected function setUp(): void {
parent::setUp();

$this->db = $this->createMock(IDBConnection::class);
$logger = $this->createMock(LoggerInterface::class);

// A REAL normaliser: the point of this test is the stored format.
$this->handler = new MagicBulkHandler(
sql: new MagicTableSql(db: $this->db, config: $this->createMock(IConfig::class), logger: $logger),
eventDispatcher: $this->createMock(IEventDispatcher::class),
dateTimeNormalizer: new DateTimeNormalizer($logger)
);

$this->register = new Register();
$this->register->setId(11);
}

/**
* Run the private row preparation on one object, against a schema
* declaring the given properties.
*/
private function prepare(array $object, array $properties): array {
$schema = new Schema();
$schema->setId(22);
$schema->setProperties($properties);

$method = new ReflectionMethod(MagicBulkHandler::class, 'prepareObjectsForDynamicTable');
$method->setAccessible(true);

$prepared = $method->invoke($this->handler, [$object], $this->register, $schema);

return $prepared[0];
}

public function testADateTimePropertyGivenAsAnIsoOffsetStringIsNormalizedToMysqlFormat(): void {
// Exactly the shape every ObjectService::saveObjects() caller uses
// (DateTime::ATOM) — the string that previously reached MySQL's
// DATETIME column unconverted and failed with SQLSTATE[22007].
$row = $this->prepare(
['uuid' => 'u-1', 'sourceLastChecked' => '2026-10-13T14:50:03+00:00'],
['sourceLastChecked' => ['type' => 'string', 'format' => 'date-time']]
);

$this->assertSame('2026-10-13 14:50:03', $row['source_last_checked']);
}

public function testADateTimePropertyIsConvertedToUtcBeforeStorage(): void {
// A non-UTC offset must be APPLIED before storing: a date-time names
// an instant, so 15:50 at +02:00 is 13:50 UTC.
$row = $this->prepare(
['uuid' => 'u-2', 'sourceLastChecked' => '2026-10-13T15:50:03+02:00'],
['sourceLastChecked' => ['type' => 'string', 'format' => 'date-time']]
);

$this->assertSame('2026-10-13 13:50:03', $row['source_last_checked']);
}

public function testADatePropertyIsNotShiftedByTimezoneConversion(): void {
// A `date` names a calendar DAY with no instant. Converting it through
// a timezone is a category error that can move it to a different day
// — exactly the regression a date-time fix must not introduce. The
// offset is dropped, not applied: the wall-clock date survives as
// given rather than shifting to 2026-10-21 (what a date-time's
// timezone conversion would do to this same value).
//
// The "Y-m-d H:i:s" shape (not bare "Y-m-d") is intentional, not a
// leftover time component to strip: `format: date` and `format:
// date-time` map to the SAME `datetime` column
// (MagicMapper::mapStringProperty()), and DateTimeNormalizer::
// formatDateForDatabase()'s own docblock documents this exact
// representation for a calendar day. Confirmed identical to the
// non-bulk path (MagicMapper::prepareObjectDataForTable()), which
// stores a `date` property through the same normalizer method.
$row = $this->prepare(
['uuid' => 'u-3', 'dueDate' => '2026-10-20T23:30:00-05:00'],
['dueDate' => ['type' => 'string', 'format' => 'date']]
);

$this->assertSame('2026-10-20 23:30:00', $row['due_date']);
}

/**
* Direct parity check against DateTimeNormalizer itself: the bulk path
* delegates to it rather than reimplementing any part of its contract
* (review feedback on #4773), so a bulk-prepared date/date-time value
* must be byte-for-byte what calling the normalizer directly produces —
* for both a string and a DateTimeInterface input.
*/
public function testBulkOutputMatchesDateTimeNormalizerCalledDirectly(): void {
$normalizer = new DateTimeNormalizer($this->createMock(LoggerInterface::class));

$dateTimeString = '2026-10-13T15:50:03+02:00';
$dateString = '2026-10-20T23:30:00-05:00';
$dateTimeObject = new DateTimeImmutable('2026-10-13 14:50:03', new DateTimeZone('UTC'));

$row = $this->prepare(
[
'uuid' => 'u-7',
'sourceLastChecked' => $dateTimeString,
'dueDate' => $dateString,
'reopenedAt' => $dateTimeObject,
],
[
'sourceLastChecked' => ['type' => 'string', 'format' => 'date-time'],
'dueDate' => ['type' => 'string', 'format' => 'date'],
'reopenedAt' => ['type' => 'string', 'format' => 'date-time'],
]
);

$this->assertSame($normalizer->formatForDatabase($dateTimeString), $row['source_last_checked']);
$this->assertSame($normalizer->formatDateForDatabase($dateString), $row['due_date']);
$this->assertSame($normalizer->formatForDatabase($dateTimeObject), $row['reopened_at']);
}

/**
* A value that is neither a string nor a DateTimeInterface (a schema
* violation — a date/date-time property given, say, an integer) is left
* untouched rather than coerced or dropped, same as before this method's
* DateTimeInterface branch was folded into one delegation to the
* normalizer (review feedback on #4773).
*/
public function testANonStringNonDateTimeValueOnADatePropertyIsLeftUntouched(): void {
$row = $this->prepare(
['uuid' => 'u-8', 'sourceLastChecked' => 42],
['sourceLastChecked' => ['type' => 'string', 'format' => 'date-time']]
);

$this->assertSame(42, $row['source_last_checked']);
}

/**
* A malformed schema whose `format` is not a string (here an integer) must
* not throw a TypeError under strict_types: the property is simply not a
* date property, so its value passes through untouched.
*
* @return void
*/
public function testANonStringSchemaFormatPassesTheValueThroughUntouched(): void {
$row = $this->prepare(
object: ['uuid' => 'u-9', 'sourceLastChecked' => '2026-10-13T14:50:03+00:00'],
properties: ['sourceLastChecked' => ['type' => 'string', 'format' => 123]]
);

$this->assertSame(expected: '2026-10-13T14:50:03+00:00', actual: $row['source_last_checked']);
}

public function testADateTimeInterfaceValueIsAlsoNormalized(): void {
$row = $this->prepare(
['uuid' => 'u-4', 'sourceLastChecked' => new DateTimeImmutable('2026-10-13 14:50:03', new DateTimeZone('UTC'))],
['sourceLastChecked' => ['type' => 'string', 'format' => 'date-time']]
);

$this->assertSame('2026-10-13 14:50:03', $row['source_last_checked']);
}

public function testAPlainStringPropertyWithNoDateFormatIsLeftUntouched(): void {
// Only properties the SCHEMA declares as date/date-time are touched —
// an ordinary string that happens to look date-like must not be
// reformatted or rejected.
$row = $this->prepare(
['uuid' => 'u-5', 'reference' => '2026-10-13T14:50:03+00:00'],
['reference' => ['type' => 'string']]
);

$this->assertSame('2026-10-13T14:50:03+00:00', $row['reference']);
}

public function testANullDatePropertyStaysNull(): void {
$row = $this->prepare(
['uuid' => 'u-6', 'sourceLastChecked' => null],
['sourceLastChecked' => ['type' => 'string', 'format' => 'date-time']]
);

$this->assertArrayHasKey('source_last_checked', $row);
$this->assertNull($row['source_last_checked']);
}
}