Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions tests/specs/integration/BaseEntity/AutomaticTimestampsSpec.cfc
Original file line number Diff line number Diff line change
@@ -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() {
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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 };
}
}

}
Loading