Files
Umbraco-CMS/tests/Umbraco.Tests.UnitTests/Umbraco.Core/Models/ContentExtensionsTests.cs

209 lines
8.4 KiB
C#
Raw Normal View History

// Copyright (c) Umbraco.
// See LICENSE for more details.
2018-06-29 19:52:40 +02:00
using System.Linq;
2019-02-05 19:58:33 +01:00
using Moq;
2018-06-29 19:52:40 +02:00
using NUnit.Framework;
using Umbraco.Cms.Core.Models;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Tests.Common.Builders;
using Umbraco.Extensions;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Models;
[TestFixture]
public class ContentExtensionsTests
2018-06-29 19:52:40 +02:00
{
[Test]
public void DirtyProperty_Reset_Clears_SavedPublishedState()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
content.PublishedState = PublishedState.Publishing;
Assert.IsFalse(content.Published);
content.ResetDirtyProperties(false); // resets
Assert.AreEqual(PublishedState.Unpublished, content.PublishedState);
Assert.IsFalse(content.Published);
}
[Test]
public void DirtyProperty_OnlyIfActuallyChanged_Content()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
// if you assign a content property with its value it is not dirty
// if you assign it with another value then back, it is dirty
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsPropertyDirty("Published"));
content.Published = true;
Assert.IsTrue(content.IsPropertyDirty("Published"));
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsPropertyDirty("Published"));
content.Published = true;
Assert.IsFalse(content.IsPropertyDirty("Published"));
content.Published = false;
content.Published = true;
Assert.IsTrue(content.IsPropertyDirty("Published"));
}
[Test]
public void DirtyProperty_OnlyIfActuallyChanged_User()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
var prop = content.Properties.First();
// if you assign a user property with its value it is not dirty
// if you assign it with another value then back, it is dirty
prop.SetValue("A");
content.ResetDirtyProperties(false);
Assert.IsFalse(prop.IsDirty());
prop.SetValue("B");
Assert.IsTrue(prop.IsDirty());
content.ResetDirtyProperties(false);
Assert.IsFalse(prop.IsDirty());
prop.SetValue("B");
Assert.IsFalse(prop.IsDirty());
prop.SetValue("A");
prop.SetValue("B");
Assert.IsTrue(prop.IsDirty());
}
[Test]
public void DirtyProperty_UpdateDate()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
var prop = content.Properties.First();
content.ResetDirtyProperties(false);
var d = content.UpdateDate;
prop.SetValue("A");
Assert.IsTrue(content.IsAnyUserPropertyDirty());
Assert.IsFalse(content.IsEntityDirty());
Assert.AreEqual(d, content.UpdateDate);
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
content.UpdateDate = DateTime.UtcNow;
Assert.IsTrue(content.IsEntityDirty());
Assert.AreNotEqual(d, content.UpdateDate);
// so... changing UpdateDate would count as a content property being changed
// however in ContentRepository.PersistUpdatedItem, we change UpdateDate AFTER
// we've tested for RequiresSaving & RequiresNewVersion so it's OK
}
[Test]
public void DirtyProperty_WasDirty_ContentProperty()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.Published = false;
content.Published = true;
Assert.IsTrue(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.Published = false;
content.Published = true;
content.ResetDirtyProperties(true); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
Assert.IsTrue(content.WasDirty());
content.Published = false;
content.Published = true;
content.ResetDirtyProperties(); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
Assert.IsTrue(content.WasDirty());
}
[Test]
public void DirtyProperty_WasDirty_ContentSortOrder()
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.SortOrder = 0;
content.SortOrder = 1;
Assert.IsTrue(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.SortOrder = 0;
content.SortOrder = 1;
content.ResetDirtyProperties(true); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
Assert.IsTrue(content.WasDirty());
content.SortOrder = 0;
content.SortOrder = 1;
content.ResetDirtyProperties(); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
Assert.IsTrue(content.WasDirty());
}
[Test]
public void DirtyProperty_WasDirty_UserProperty()
2018-06-29 19:52:40 +02:00
{
var contentTypeService = Mock.Of<IContentTypeService>();
var contentType = ContentTypeBuilder.CreateTextPageContentType();
Mock.Get(contentTypeService).As<IContentTypeBaseService>().Setup(x => x.Get(It.IsAny<int>()))
.Returns(contentType);
var content = ContentBuilder.CreateTextpageContent(contentType, "Textpage", -1);
var prop = content.Properties.First();
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
prop.SetValue("a");
prop.SetValue("b");
Assert.IsTrue(content.IsDirty());
Assert.IsFalse(content.WasDirty());
content.ResetDirtyProperties(false);
Assert.IsFalse(content.IsDirty());
Assert.IsFalse(content.WasDirty());
prop.SetValue("a");
prop.SetValue("b");
content.ResetDirtyProperties(true); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
//// Assert.IsFalse(content.WasDirty()); // not impacted by user properties
Assert.IsTrue(content.WasDirty()); // now it is!
prop.SetValue("a");
prop.SetValue("b");
content.ResetDirtyProperties(); // what PersistUpdatedItem does
Assert.IsFalse(content.IsDirty());
//// Assert.IsFalse(content.WasDirty()); // not impacted by user properties
Assert.IsTrue(content.WasDirty()); // now it is!
2018-06-29 19:52:40 +02:00
}
}