From 5a4c94d1724489fc8822cf25ff105fca0def88e0 Mon Sep 17 00:00:00 2001 From: Chris Nizzardini Date: Sun, 18 Feb 2024 10:07:37 -0500 Subject: [PATCH] port passing entity to events to latest version --- src/Event/BaseEvent.php | 5 +++- src/Event/BaseEventTrait.php | 17 +++++++++++ src/Event/SerializableEventTrait.php | 1 + src/EventFactory.php | 3 +- src/Model/Behavior/AuditLogBehavior.php | 2 +- tests/TestCase/Event/SerializeTest.php | 9 +++--- .../ElasticSearchPersisterIntegrationTest.php | 11 +++---- .../Persister/ElasticSearchPersisterTest.php | 3 +- .../TestCase/Persister/TablePersisterTest.php | 30 +++++++++---------- 9 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/Event/BaseEvent.php b/src/Event/BaseEvent.php index 8e1861c..0bd8763 100644 --- a/src/Event/BaseEvent.php +++ b/src/Event/BaseEvent.php @@ -4,6 +4,7 @@ namespace AuditStash\Event; use AuditStash\EventInterface; +use Cake\Datasource\EntityInterface; use DateTime; use ReturnTypeWillChange; @@ -38,13 +39,15 @@ abstract class BaseEvent implements EventInterface * @param string $source The name of the source (table) * @param array|null $changed The array of changes that got detected for the entity * @param array|null $original The original values the entity had before it got changed + * @param \Cake\Datasource\EntityInterface|null $entity The entity being changed */ public function __construct( string $transactionId, mixed $id, string $source, ?array $changed, - ?array $original + ?array $original, + ?EntityInterface $entity ) { $this->transactionId = $transactionId; $this->id = $id; diff --git a/src/Event/BaseEventTrait.php b/src/Event/BaseEventTrait.php index 88e303f..9b9e592 100644 --- a/src/Event/BaseEventTrait.php +++ b/src/Event/BaseEventTrait.php @@ -3,6 +3,8 @@ namespace AuditStash\Event; +use Cake\Datasource\EntityInterface; + /** * Implements most of the methods of the EventInterface. */ @@ -50,6 +52,13 @@ trait BaseEventTrait */ protected array $meta = []; + /** + * The entity being changed. + * + * @var \Cake\Datasource\EntityInterface|null + */ + protected ?EntityInterface $entity = null; + /** * Returns the global transaction id in which this event is contained. * @@ -135,4 +144,12 @@ public function setMetaInfo(array $meta): void { $this->meta = $meta; } + + /** + * @return \Cake\Datasource\EntityInterface|null + */ + public function getEntity(): ?EntityInterface + { + return $this->entity; + } } diff --git a/src/Event/SerializableEventTrait.php b/src/Event/SerializableEventTrait.php index d1b7835..ca8a247 100644 --- a/src/Event/SerializableEventTrait.php +++ b/src/Event/SerializableEventTrait.php @@ -71,6 +71,7 @@ protected function basicSerialize(): array 'parent_source' => $this->parentSource, '@timestamp' => $this->timestamp, 'meta' => $this->meta, + 'entity' => $this->entity, ]; } } diff --git a/src/EventFactory.php b/src/EventFactory.php index 7b291e2..bef3433 100644 --- a/src/EventFactory.php +++ b/src/EventFactory.php @@ -36,7 +36,8 @@ public function create(array $data): EventInterface $data['primary_key'], $data['source'], $data['changed'], - $data['original'] + $data['original'], + null ); } else { $event = new $map[$data['type']]( diff --git a/src/Model/Behavior/AuditLogBehavior.php b/src/Model/Behavior/AuditLogBehavior.php index e33d78a..568ceda 100644 --- a/src/Model/Behavior/AuditLogBehavior.php +++ b/src/Model/Behavior/AuditLogBehavior.php @@ -138,7 +138,7 @@ public function afterSave( $auditEvent = $entity->isNew() ? AuditCreateEvent::class : AuditUpdateEvent::class; $transaction = $options['_auditTransaction']; - $auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original); + $auditEvent = new $auditEvent($transaction, $primary, $this->_table->getTable(), $changed, $original, $entity); if (!empty($options['_sourceTable'])) { $auditEvent->setParentSourceName($options['_sourceTable']->getTable()); diff --git a/tests/TestCase/Event/SerializeTest.php b/tests/TestCase/Event/SerializeTest.php index 2b93285..8d9ea46 100644 --- a/tests/TestCase/Event/SerializeTest.php +++ b/tests/TestCase/Event/SerializeTest.php @@ -7,6 +7,7 @@ use AuditStash\Event\AuditDeleteEvent; use AuditStash\Event\AuditUpdateEvent; use AuditStash\EventFactory; +use Cake\ORM\Entity; use Cake\TestSuite\TestCase; class SerializeTest extends TestCase @@ -18,7 +19,7 @@ class SerializeTest extends TestCase */ public function testSerializeCreate() { - $event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']); + $event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity()); $event->setMetaInfo(['extra' => 'info']); $serialized = serialize($event); $this->assertEquals($event, unserialize($serialized)); @@ -31,7 +32,7 @@ public function testSerializeCreate() */ public function testSerializeUpdate() { - $event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']); + $event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity()); $event->setMetaInfo(['extra' => 'info']); $serialized = serialize($event); $this->assertEquals($event, unserialize($serialized)); @@ -58,7 +59,7 @@ public function testSerializeDelete() public function testJsonSerializeCreate() { $factory = new EventFactory(); - $event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']); + $event = new AuditCreateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity()); $event->setMetaInfo(['extra' => 'info']); $serialized = json_encode($event); $result = $factory->create(json_decode($serialized, true)); @@ -73,7 +74,7 @@ public function testJsonSerializeCreate() public function testJsonSerializeUpdate() { $factory = new EventFactory(); - $event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar']); + $event = new AuditUpdateEvent('123', 50, 'articles', ['title' => 'foo'], ['title' => 'bar'], new Entity()); $event->setMetaInfo(['extra' => 'info']); $serialized = json_encode($event); $result = $factory->create(json_decode($serialized, true)); diff --git a/tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php b/tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php index 5a1af56..25ff451 100644 --- a/tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php +++ b/tests/TestCase/Persister/ElasticSearchPersisterIntegrationTest.php @@ -10,6 +10,7 @@ use AuditStash\Persister\ElasticSearchPersister; use Cake\Datasource\ConnectionManager; use Cake\I18n\DateTime; +use Cake\ORM\Entity; use Cake\TestSuite\TestCase; class ElasticSearchPersisterIntegrationTest extends TestCase @@ -49,7 +50,7 @@ public function testLogSingleCreateEvent() 'published' => 'Y', ]; - $events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data); + $events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data, new Entity()); $persister->logEvents($events); $client->getIndex('article')->refresh(); @@ -108,7 +109,7 @@ public function testLogSingleUpdateEvent() 'published' => 'Y', ]; - $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original); + $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original, new Entity()); $events[0]->setParentSourceName('authors'); $persister->logEvents($events); $client->getIndex('article')->refresh(); @@ -194,7 +195,7 @@ public function testLogMultipleEvents() 'id' => 3, 'tag' => 'cakephp', ]; - $events[] = new AuditCreateEvent('1234', 4, 'tags', $data, $data); + $events[] = new AuditCreateEvent('1234', 4, 'tags', $data, $data, new Entity()); $original = [ 'title' => 'Old article title', @@ -204,7 +205,7 @@ public function testLogMultipleEvents() 'title' => 'A new article', 'published' => 'Y', ]; - $events[] = new AuditUpdateEvent('1234', 2, 'authors', $changed, $original); + $events[] = new AuditUpdateEvent('1234', 2, 'authors', $changed, $original, new Entity()); $events[] = new AuditDeleteEvent('1234', 50, 'articles'); $events[] = new AuditDeleteEvent('1234', 51, 'articles'); @@ -243,7 +244,7 @@ public function testPersistingTimeObjects() 'published_date' => new DateTime('2015-04-13 20:20:21'), ]; - $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original); + $events[] = new AuditUpdateEvent('1234', 50, 'articles', $changed, $original, new Entity()); $persister->logEvents($events); $client->getIndex('article')->refresh(); diff --git a/tests/TestCase/Persister/ElasticSearchPersisterTest.php b/tests/TestCase/Persister/ElasticSearchPersisterTest.php index eb28aa6..4d276f5 100644 --- a/tests/TestCase/Persister/ElasticSearchPersisterTest.php +++ b/tests/TestCase/Persister/ElasticSearchPersisterTest.php @@ -6,6 +6,7 @@ use AuditStash\Event\AuditCreateEvent; use AuditStash\Persister\ElasticSearchPersister; use Cake\ElasticSearch\Datasource\Connection; +use Cake\ORM\Entity; use Cake\TestSuite\TestCase; use Elastica\Bulk\ResponseSet; use Elastica\Client; @@ -41,7 +42,7 @@ public function testLogEvents() 'published' => 'Y', ]; - $events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data); + $events[] = new AuditCreateEvent('1234', 50, 'articles', $data, $data, new Entity()); $clientMock->expects($this->once())->method('addDocuments'); $persister->logEvents($events); } diff --git a/tests/TestCase/Persister/TablePersisterTest.php b/tests/TestCase/Persister/TablePersisterTest.php index 71ceb1c..5bcc2f9 100644 --- a/tests/TestCase/Persister/TablePersisterTest.php +++ b/tests/TestCase/Persister/TablePersisterTest.php @@ -96,7 +96,7 @@ public function testSetInvalidTable() */ public function testSerializeNull() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', null, null); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', null, null, new Entity()); $event->setMetaInfo([]); $entity = new Entity([ @@ -129,7 +129,7 @@ public function testSerializeNull() */ public function testExtractMetaFields() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $event->setMetaInfo([ 'foo' => 'bar', 'baz' => [ @@ -175,7 +175,7 @@ public function testExtractMetaFields() */ public function testExtractAllMetaFields() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $event->setMetaInfo([ 'foo' => 'bar', 'baz' => [ @@ -221,7 +221,7 @@ public function testExtractAllMetaFields() */ public function testExtractMetaFieldsDoNotUnset() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $event->setMetaInfo([ 'foo' => 'bar', ]); @@ -262,7 +262,7 @@ public function testExtractMetaFieldsDoNotUnset() */ public function testExtractAllMetaFieldsDoNotUnset() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $event->setMetaInfo([ 'foo' => 'bar', ]); @@ -301,7 +301,7 @@ public function testExtractAllMetaFieldsDoNotUnset() */ public function testErrorLogging() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); /** @var \AuditStash\Persister\TablePersister|\PHPUnit\Framework\MockObject\MockObject $TablePersister */ $TablePersister = $this @@ -366,7 +366,7 @@ function ($event, EntityInterface $entity) { } ); - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $TablePersister->logEvents([$event]); } @@ -375,7 +375,7 @@ function ($event, EntityInterface $entity) { */ public function testCompoundPrimaryKeyExtractDefault() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -408,7 +408,7 @@ public function testCompoundPrimaryKeyExtractDefault() */ public function testPrimaryKeyExtractRaw() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -442,7 +442,7 @@ public function testPrimaryKeyExtractRaw() */ public function testCompoundPrimaryKeyExtractRaw() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -478,7 +478,7 @@ public function testCompoundPrimaryKeyExtractRaw() */ public function testPrimaryKeyExtractProperties() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -512,7 +512,7 @@ public function testPrimaryKeyExtractProperties() */ public function testCompoundPrimaryKeyExtractProperties() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -548,7 +548,7 @@ public function testCompoundPrimaryKeyExtractProperties() */ public function testPrimaryKeyExtractSerialized() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 'pk', 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 'pk', 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -584,7 +584,7 @@ public function testPrimaryKeyExtractSerialized() */ public function testCompoundPrimaryKeyExtractSerialized() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', [1, 2, 3], 'source', [], [], new Entity()); $entity = new Entity([ 'transaction' => '62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', @@ -620,7 +620,7 @@ public function testCompoundPrimaryKeyExtractSerialized() */ public function testDoNotSerializeFields() { - $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], []); + $event = new AuditCreateEvent('62ba2e1e-1524-4d4e-bb34-9bf0e03b6a96', 1, 'source', [], [], new Entity()); $event->setMetaInfo([ 'foo' => 'bar', ]);