From ea1d4c48f26f6eb9063ee84380fa6116de2543c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Tue, 4 Jun 2024 22:31:50 +0200 Subject: [PATCH] Add history support to StoredObject entity This commit adds a history saving feature to the StoredObject entity, which allows saving versions of the object's changes over time. This is achieved by implementing a saveHistory method that captures data attributes like filename, IV, key information, and type. The corresponding Automated tests were also created. Furthermore, adjustments were made to the StoredObject test to align with the new feature. --- .../Entity/StoredObject.php | 15 ++++++ .../DataMapper/StoredObjectDataMapper.php | 4 +- .../Tests/Entity/StoredObjectTest.php | 53 +++++++++++++++++++ .../Tests/Form/StoredObjectTypeTest.php | 4 +- 4 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectTest.php diff --git a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php index aae003e09..e2cb4fff2 100644 --- a/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php +++ b/src/Bundle/ChillDocStoreBundle/Entity/StoredObject.php @@ -354,4 +354,19 @@ class StoredObject implements AsyncFileInterface, Document, TrackCreationInterfa return $this; } + + public function saveHistory(): void + { + if ('' === $this->getFilename()) { + return; + } + + $this->datas['history'][] = [ + 'filename' => $this->getFilename(), + 'iv' => $this->getIv(), + 'key_infos' => $this->getKeyInfos(), + 'type' => $this->getType(), + 'before' => (new \DateTimeImmutable('now'))->getTimestamp(), + ]; + } } diff --git a/src/Bundle/ChillDocStoreBundle/Form/DataMapper/StoredObjectDataMapper.php b/src/Bundle/ChillDocStoreBundle/Form/DataMapper/StoredObjectDataMapper.php index 3ec52134f..4e1aa36d6 100644 --- a/src/Bundle/ChillDocStoreBundle/Form/DataMapper/StoredObjectDataMapper.php +++ b/src/Bundle/ChillDocStoreBundle/Form/DataMapper/StoredObjectDataMapper.php @@ -59,8 +59,8 @@ class StoredObjectDataMapper implements DataMapperInterface /** @var StoredObject $viewData */ if ($viewData->getFilename() !== $forms['stored_object']->getData()['filename']) { - // we do not want to erase the previous object - $viewData = new StoredObject(); + // we want to keep the previous history + $viewData->saveHistory(); } $viewData->setFilename($forms['stored_object']->getData()['filename']); diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectTest.php new file mode 100644 index 000000000..33394114f --- /dev/null +++ b/src/Bundle/ChillDocStoreBundle/Tests/Entity/StoredObjectTest.php @@ -0,0 +1,53 @@ +setFilename('test_0') + ->setIv([2, 4, 6, 8]) + ->setKeyInfos(['key' => ['data0' => 'data0']]) + ->setType('text/html'); + + $storedObject->saveHistory(); + + $storedObject + ->setFilename('test_1') + ->setIv([8, 10, 12]) + ->setKeyInfos(['key' => ['data1' => 'data1']]) + ->setType('text/text'); + + $storedObject->saveHistory(); + + self::assertEquals('test_0', $storedObject->getDatas()['history'][0]['filename']); + self::assertEquals([2, 4, 6, 8], $storedObject->getDatas()['history'][0]['iv']); + self::assertEquals(['key' => ['data0' => 'data0']], $storedObject->getDatas()['history'][0]['key_infos']); + self::assertEquals('text/html', $storedObject->getDatas()['history'][0]['type']); + + self::assertEquals('test_1', $storedObject->getDatas()['history'][1]['filename']); + self::assertEquals([8, 10, 12], $storedObject->getDatas()['history'][1]['iv']); + self::assertEquals(['key' => ['data1' => 'data1']], $storedObject->getDatas()['history'][1]['key_infos']); + self::assertEquals('text/text', $storedObject->getDatas()['history'][1]['type']); + } +} diff --git a/src/Bundle/ChillDocStoreBundle/Tests/Form/StoredObjectTypeTest.php b/src/Bundle/ChillDocStoreBundle/Tests/Form/StoredObjectTypeTest.php index 863433af8..2fc17787a 100644 --- a/src/Bundle/ChillDocStoreBundle/Tests/Form/StoredObjectTypeTest.php +++ b/src/Bundle/ChillDocStoreBundle/Tests/Form/StoredObjectTypeTest.php @@ -56,14 +56,14 @@ class StoredObjectTypeTest extends TypeTestCase {"filename":"abcdef","iv":[10, 15, 20, 30],"keyInfos":[],"type":"text/html","status":"object_store_created"} JSON]; $model = new StoredObject(); - $originalObjectId = spl_object_id($model); + $originalObjectId = spl_object_hash($model); $form = $this->factory->create(StoredObjectType::class, $model, ['has_title' => true]); $form->submit($formData); $this->assertTrue($form->isSynchronized()); $model = $form->getData(); - $this->assertNotEquals($originalObjectId, spl_object_hash($model)); + $this->assertEquals($originalObjectId, spl_object_hash($model)); $this->assertEquals('abcdef', $model->getFilename()); $this->assertEquals([10, 15, 20, 30], $model->getIv()); $this->assertEquals('text/html', $model->getType());