From 041e4d754faaa19ea7b4e7d1d939ed3d3aaa6625 Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Sun, 30 Aug 2026 17:21:01 -0600 Subject: [PATCH] test: verify timestamps precede persistence events --- .../BaseEntity/AutomaticTimestampsSpec.cfc | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/specs/integration/BaseEntity/AutomaticTimestampsSpec.cfc b/tests/specs/integration/BaseEntity/AutomaticTimestampsSpec.cfc index 8db12fe4..db2c9b70 100644 --- a/tests/specs/integration/BaseEntity/AutomaticTimestampsSpec.cfc +++ b/tests/specs/integration/BaseEntity/AutomaticTimestampsSpec.cfc @@ -1,5 +1,17 @@ component extends="tests.resources.ModuleIntegrationSpec" { + function beforeAll() { + super.beforeAll(); + controller + .getInterceptorService() + .registerInterceptor( interceptorObject = this, interceptorName = "AutomaticTimestampsSpec" ); + } + + function afterAll() { + controller.getInterceptorService().unregister( "AutomaticTimestampsSpec" ); + super.afterAll(); + } + function run() { describe( "Automatic Timestamps", function() { it( "uses the module setting as the per-entity default", function() { @@ -46,6 +58,34 @@ component extends="tests.resources.ModuleIntegrationSpec" { expect( dateCompare( user.getModifiedDate(), previousModifiedDate ) ).toBe( 1 ); } ); + it( "sets conventional timestamps before insert and update events", function() { + structDelete( variables, "preInsertTimestamps" ); + structDelete( variables, "preUpdateTimestamps" ); + + var user = getInstance( "AutomaticTimestampUser" ).create( { + "username" : "automatic-timestamp-events", + "firstName" : "Automatic", + "lastName" : "Events", + "password" : "secret" + } ); + + expect( variables ).toHaveKey( "preInsertTimestamps" ); + expect( variables.preInsertTimestamps.createdDate ).toBeDate(); + expect( variables.preInsertTimestamps.modifiedDate ).toBeDate(); + + queryExecute( + "UPDATE users SET modified_date = DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 1 DAY) WHERE id = :id", + { "id" : user.getId() } + ); + user = getInstance( "AutomaticTimestampUser" ).findOrFail( user.getId() ); + var previousModifiedDate = user.getModifiedDate(); + user.update( { "firstName" : "Updated" } ); + + expect( variables ).toHaveKey( "preUpdateTimestamps" ); + expect( variables.preUpdateTimestamps.modifiedDate ).toBeDate(); + expect( dateCompare( variables.preUpdateTimestamps.modifiedDate, previousModifiedDate ) ).toBe( 1 ); + } ); + it( "preserves explicitly assigned timestamps", function() { var createdDate = dateAdd( "y", -1, now() ); var modifiedDate = dateAdd( "m", -1, now() ); @@ -116,4 +156,34 @@ component extends="tests.resources.ModuleIntegrationSpec" { } ); } + function quickPreInsert( + event, + interceptData, + buffer, + rc, + prc + ) { + if ( + arguments.interceptData.attributes.keyExists( "created_date" ) + && arguments.interceptData.attributes.keyExists( "modified_date" ) + ) { + variables.preInsertTimestamps = { + "createdDate" : arguments.interceptData.attributes.created_date, + "modifiedDate" : arguments.interceptData.attributes.modified_date + }; + } + } + + function quickPreUpdate( + event, + interceptData, + buffer, + rc, + prc + ) { + if ( arguments.interceptData.newAttributes.keyExists( "modified_date" ) ) { + variables.preUpdateTimestamps = { "modifiedDate" : arguments.interceptData.newAttributes.modified_date }; + } + } + }