Files
Umbraco-CMS/tests/Umbraco.Tests.Common/Builders/AuditEntryBuilder.cs

176 lines
4.8 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
2020-07-01 17:42:39 +02:00
using System;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Tests.Common.Builders.Interfaces;
2020-07-01 17:42:39 +02:00
namespace Umbraco.Cms.Tests.Common.Builders;
public class AuditEntryBuilder : AuditEntryBuilder<object>
2020-07-01 17:42:39 +02:00
{
public AuditEntryBuilder()
: base(null)
2020-07-01 17:42:39 +02:00
{
}
}
public class AuditEntryBuilder<TParent>
: ChildBuilderBase<TParent, IAuditEntry>,
IWithIdBuilder,
IWithKeyBuilder,
IWithCreateDateBuilder,
IWithUpdateDateBuilder,
IWithDeleteDateBuilder
{
private string _affectedDetails;
private int? _affectedUserId;
private Guid? _affectedUserKey;
private DateTime? _createDate;
private DateTime? _deleteDate;
Use UTC for system dates in Umbraco (#19822) * Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
2025-08-22 11:59:23 +02:00
private DateTime? _eventDate;
private string _eventDetails;
private string _eventType;
private int? _id;
private Guid? _key;
private string _performingDetails;
private string _performingIp;
private Guid? _performingUserKey;
private int? _performingUserId;
private DateTime? _updateDate;
public AuditEntryBuilder(TParent parentBuilder)
: base(parentBuilder)
{
}
DateTime? IWithCreateDateBuilder.CreateDate
{
get => _createDate;
set => _createDate = value;
}
DateTime? IWithDeleteDateBuilder.DeleteDate
{
get => _deleteDate;
set => _deleteDate = value;
}
int? IWithIdBuilder.Id
{
get => _id;
set => _id = value;
}
Guid? IWithKeyBuilder.Key
{
get => _key;
set => _key = value;
}
DateTime? IWithUpdateDateBuilder.UpdateDate
{
get => _updateDate;
set => _updateDate = value;
}
public AuditEntryBuilder<TParent> WithAffectedDetails(string affectedDetails)
{
_affectedDetails = affectedDetails;
return this;
}
public AuditEntryBuilder<TParent> WithAffectedUserId(int affectedUserId)
{
_affectedUserId = affectedUserId;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithAffectedUserKey(Guid? affectedUserKey)
{
_affectedUserKey = affectedUserKey;
return this;
}
public AuditEntryBuilder<TParent> WithEventDetails(string eventDetails)
{
_eventDetails = eventDetails;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithEventType(string eventType)
{
_eventType = eventType;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithPerformingDetails(string performingDetails)
{
_performingDetails = performingDetails;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithPerformingIp(string performingIp)
{
_performingIp = performingIp;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithEventDate(DateTime eventDateUtc)
{
Use UTC for system dates in Umbraco (#19822) * Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
2025-08-22 11:59:23 +02:00
_eventDate = eventDateUtc;
return this;
}
2020-07-01 17:42:39 +02:00
public AuditEntryBuilder<TParent> WithPerformingUserId(int performingUserId)
{
_performingUserId = performingUserId;
return this;
}
public AuditEntryBuilder<TParent> WithPerformingUserKey(Guid? performingUserKey)
{
_performingUserKey = performingUserKey;
return this;
}
public override IAuditEntry Build()
{
var id = _id ?? 0;
var key = _key ?? Guid.NewGuid();
Use UTC for system dates in Umbraco (#19822) * Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
2025-08-22 11:59:23 +02:00
var createDate = _createDate ?? DateTime.UtcNow;
var updateDate = _updateDate ?? DateTime.UtcNow;
var deleteDate = _deleteDate;
var affectedDetails = _affectedDetails ?? Guid.NewGuid().ToString();
var affectedUserId = _affectedUserId ?? -1;
var affectedUserKey = _affectedUserKey ?? Constants.Security.SuperUserKey;
var eventDetails = _eventDetails ?? Guid.NewGuid().ToString();
var eventType = _eventType ?? "umbraco/user";
var performingDetails = _performingDetails ?? Guid.NewGuid().ToString();
var performingIp = _performingIp ?? "127.0.0.1";
Use UTC for system dates in Umbraco (#19822) * Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
2025-08-22 11:59:23 +02:00
var eventDate = _eventDate ?? DateTime.UtcNow;
var performingUserId = _performingUserId ?? -1;
var performingUserKey = _performingUserKey ?? Constants.Security.SuperUserKey;
return new AuditEntry
{
Id = id,
Key = key,
CreateDate = createDate,
UpdateDate = updateDate,
DeleteDate = deleteDate,
AffectedDetails = affectedDetails,
AffectedUserId = affectedUserId,
AffectedUserKey = affectedUserKey,
EventDetails = eventDetails,
EventType = eventType,
PerformingDetails = performingDetails,
PerformingIp = performingIp,
Use UTC for system dates in Umbraco (#19822) * Persist and expose Umbraco system dates as UTC (#19705) * Updated persistence DTOs defining default dates to use UTC. * Remove ForceToUtc = false from all persistence DTO attributes (default when not specified is true). * Removed use of SpecifyKind setting dates to local. * Removed unnecessary Utc suffixes on properties. * Persist current date time with UtcNow. * Removed further necessary Utc suffixes and fixed failing unit tests. * Added migration for SQL server to update database date default constraints. * Added comment justifying not providing a migration for SQLite default date constraints. * Ensure UTC for datetimes created from persistence DTOs. * Ensure UTC when creating dates for published content rendering in Razor and outputting in delivery API. * Fixed migration SQL syntax. * Introduced AuditItemFactory for creating entries for the backoffice document history, so we can control the UTC setting on the retrieved persisted dates. * Ensured UTC dates are retrieved for document versions. * Ensured UTC is returned for backoffice display of last edited and published for variant content. * Fixed SQLite syntax for default current datetime. * Apply suggestions from code review Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Further updates from code review. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com> * Migrate system dates from local server time to UTC (#19798) * Add settings for the migration. * Add migration and implement for SQL server. * Implement for SQLite. * Fixes from testing with SQL Server. * Fixes from testing with SQLite. * Code tidy. * Cleaned up usings. * Removed audit log date from conversion. * Removed webhook log date from conversion. * Updated update date initialization on saving dictionary items. * Updated filter on log queries. * Use timezone ID instead of system name to work cross-culture. --------- Co-authored-by: Laura Neto <12862535+lauraneto@users.noreply.github.com>
2025-08-22 11:59:23 +02:00
EventDate = eventDate,
PerformingUserId = performingUserId,
PerformingUserKey = performingUserKey,
};
2020-07-01 17:42:39 +02:00
}
}