Correcting ability to set guid Key, as it was overwritten when saving.

Adding unit tests to verify get/set Key.
Adding unit tests to verify GetById with guid Key.
Adding overload to GetById with guid Key to supplement the int Id.
This commit is contained in:
Morten Christensen
2012-11-13 19:07:43 -01:00
parent f7b7b90bed
commit 5540959e71
5 changed files with 124 additions and 4 deletions

View File

@@ -327,7 +327,9 @@ namespace Umbraco.Core.Models
internal override void AddingEntity()
{
base.AddingEntity();
Key = Guid.NewGuid();
if(Key == Guid.Empty)
Key = Guid.NewGuid();
}
/// <summary>

View File

@@ -77,6 +77,20 @@ namespace Umbraco.Core.Services
return repository.Get(id);
}
/// <summary>
/// Gets an <see cref="IContent"/> object by its 'UniqueId'
/// </summary>
/// <param name="key">Guid key of the Content to retrieve</param>
/// <returns><see cref="IContent"/></returns>
public IContent GetById(Guid key)
{
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(_unitOfWork);
var query = Query<IContent>.Builder.Where(x => x.Key == key);
var contents = repository.GetByQuery(query);
return contents.SingleOrDefault();
}
/// <summary>
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
/// </summary>
@@ -740,7 +754,7 @@ namespace Umbraco.Core.Services
//If a user id was passed in we use that
content.CreatorId = userId;
}
else if(_userService != null)
else if (UserServiceOrContext())
{
var profile = _httpContext == null
? _userService.GetCurrentBackOfficeUser()
@@ -766,7 +780,7 @@ namespace Umbraco.Core.Services
//If a user id was passed in we use that
content.WriterId = userId;
}
else if (_userService != null)
else if (UserServiceOrContext())
{
var profile = _httpContext == null
? _userService.GetCurrentBackOfficeUser()
@@ -780,6 +794,11 @@ namespace Umbraco.Core.Services
}
}
private bool UserServiceOrContext()
{
return _userService != null && (HttpContext.Current != null || _httpContext != null);
}
//TODO Add method to remove versions from Content
//TODO Add method to remove versions from all Content - parameters to select date-interval, ea. remove versions older then.
}

View File

@@ -31,6 +31,13 @@ namespace Umbraco.Core.Services
/// <returns><see cref="IContent"/></returns>
IContent GetById(int id);
/// <summary>
/// Gets an <see cref="IContent"/> object by its 'UniqueId'
/// </summary>
/// <param name="key">Guid key of the Content to retrieve</param>
/// <returns><see cref="IContent"/></returns>
IContent GetById(Guid key);
/// <summary>
/// Gets a collection of <see cref="IContent"/> objects by the Id of the <see cref="IContentType"/>
/// </summary>

View File

@@ -295,18 +295,56 @@ namespace Umbraco.Tests.Persistence.Repositories
Assert.That(result, Is.GreaterThanOrEqualTo(2));
}
[Test]
public void Can_Verify_Keys_Set()
{
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
// Act
var textpage = repository.Get(1046);
var subpage = repository.Get(1047);
// Assert
Assert.That(textpage.Key.ToString().ToUpper(), Is.EqualTo("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"));
Assert.That(subpage.Key.ToString().ToUpper(), Is.EqualTo("FF11402B-7E53-4654-81A7-462AC2108059"));
}
[Test]
public void Can_Get_Content_By_Guid_Key()
{
// Arrange
var provider = new PetaPocoUnitOfWorkProvider();
var unitOfWork = provider.GetUnitOfWork();
var repository = RepositoryResolver.ResolveByType<IContentRepository, IContent, int>(unitOfWork);
// Act
var query = Query<IContent>.Builder.Where(x => x.Key == new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"));
var content = repository.GetByQuery(query).SingleOrDefault();
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.Id, Is.EqualTo(1046));
}
public void CreateTestData()
{
//Create and Save ContentType "umbTextpage" -> 1045
ContentType contentType = MockedContentTypes.CreateSimpleContentType("umbTextpage", "Textpage");
contentType.Key = new Guid("1D3A8E6E-2EA9-4CC1-B229-1AEE19821522");
ServiceContext.ContentTypeService.Save(contentType);
//Create and Save Content "Homepage" based on "umbTextpage" -> 1046
Content textpage = MockedContent.CreateSimpleContent(contentType);
textpage.Key = new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0");
ServiceContext.ContentService.Save(textpage, 0);
//Create and Save Content "Text Page 1" based on "umbTextpage" -> 1047
Content subpage = MockedContent.CreateSimpleContent(contentType, "Text Page 1", textpage.Id);
subpage.Key = new Guid("FF11402B-7E53-4654-81A7-462AC2108059");
ServiceContext.ContentService.Save(subpage, 0);
//Create and Save Content "Text Page 1" based on "umbTextpage" -> 1048

View File

@@ -47,7 +47,7 @@ namespace Umbraco.Tests.Services
}
[Test]
public void Can_Create_Content_Using_HttpContext()
public void Can_Create_Content_Using_HttpContext_To_Set_User()
{
// Arrange
var userId =
@@ -87,6 +87,44 @@ namespace Umbraco.Tests.Services
Assert.That(content.CreatorId, Is.EqualTo(userId));
}
[Test]
public void Can_Create_Content_Without_HttpContext_To_Set_User()
{
// Arrange
var userId =
Convert.ToInt32(
DatabaseContext.Database.Insert(new UserDto
{
ContentStartId = -1,
DefaultPermissions = null,
DefaultToLiveEditing = false,
Disabled = false,
Email = "my@email.com",
Login = "editor",
MediaStartId = -1,
NoConsole = false,
Password = "1234",
Type = 3,
UserLanguage = "en",
UserName = "John Doe the Editor"
}));
DatabaseContext.Database.Insert(new UserLoginDto
{
UserId = userId,
ContextId = new Guid("FBA996E7-D6BE-489B-B199-2B0F3D2DD826"),
Timeout = 634596443995451258
});
// Act
var content = ServiceContext.ContentService.CreateContent(-1, "umbTextpage");
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.HasIdentity, Is.False);
Assert.That(content.CreatorId, Is.EqualTo(0));//Default to zero/administrator
}
[Test]
public void Cannot_Create_Content_With_Non_Existing_ContentType_Alias()
{
@@ -111,6 +149,20 @@ namespace Umbraco.Tests.Services
Assert.That(content.Id, Is.EqualTo(1046));
}
[Test]
public void Can_Get_Content_By_Guid_Key()
{
// Arrange
var contentService = ServiceContext.ContentService;
// Act
var content = contentService.GetById(new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0"));
// Assert
Assert.That(content, Is.Not.Null);
Assert.That(content.Id, Is.EqualTo(1046));
}
[Test]
public void Can_Get_Content_By_Level()
{
@@ -643,10 +695,12 @@ namespace Umbraco.Tests.Services
//Create and Save ContentType "umbTextpage" -> 1045
ContentType contentType = MockedContentTypes.CreateSimpleContentType("umbTextpage", "Textpage");
contentType.Key = new Guid("1D3A8E6E-2EA9-4CC1-B229-1AEE19821522");
ServiceContext.ContentTypeService.Save(contentType);
//Create and Save Content "Homepage" based on "umbTextpage" -> 1046
Content textpage = MockedContent.CreateSimpleContent(contentType);
textpage.Key = new Guid("B58B3AD4-62C2-4E27-B1BE-837BD7C533E0");
ServiceContext.ContentService.Save(textpage, 0);
//Create and Save Content "Text Page 1" based on "umbTextpage" -> 1047