From 16f8f6036ca53d5e358a42e9c140e5ca99fddf43 Mon Sep 17 00:00:00 2001 From: Stephan Date: Wed, 22 Feb 2017 15:52:02 +0100 Subject: [PATCH] U4-9560 - fix db issue --- src/Umbraco.Tests/Issues/U9560.cs | 92 +++++++++++++++++++ .../Persistence/DatabaseContextTests.cs | 11 ++- src/Umbraco.Tests/Umbraco.Tests.csproj | 1 + .../propertytype/propertytype.cs | 9 +- 4 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 src/Umbraco.Tests/Issues/U9560.cs diff --git a/src/Umbraco.Tests/Issues/U9560.cs b/src/Umbraco.Tests/Issues/U9560.cs new file mode 100644 index 0000000000..f9174498ff --- /dev/null +++ b/src/Umbraco.Tests/Issues/U9560.cs @@ -0,0 +1,92 @@ +using System; +using NUnit.Framework; +using Umbraco.Core.Models; +using Umbraco.Tests.TestHelpers; + +namespace Umbraco.Tests.Issues +{ + [TestFixture] + [DatabaseTestBehavior(DatabaseBehavior.NewDbFileAndSchemaPerTest)] + public class U9560 : BaseDatabaseFactoryTest + { + [Test] + public void Test() + { + // create a content type and some properties + var contentType = new ContentType(-1); + contentType.Alias = "test"; + contentType.Name = "test"; + var propertyType = new PropertyType("test", DataTypeDatabaseType.Ntext, "prop") { Name = "Prop", Description = "", Mandatory = false, SortOrder = 1, DataTypeDefinitionId = -88 }; + contentType.PropertyTypeCollection.Add(propertyType); + ServiceContext.ContentTypeService.Save(contentType); + + var aliasName = string.Empty; + + // read fields, same as what we do with PetaPoco Fetch + var db = DatabaseContext.Database; + db.OpenSharedConnection(); + try + { + var conn = db.Connection; + var cmd = conn.CreateCommand(); + cmd.CommandText = "SELECT mandatory, dataTypeId, propertyTypeGroupId, contentTypeId, sortOrder, alias, name, validationRegExp, description from cmsPropertyType where id=" + propertyType.Id; + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + for (var i = 0; i < reader.FieldCount; i++) + Console.WriteLine(reader.GetName(i)); + aliasName = reader.GetName(5); + } + } + } + finally + { + db.CloseSharedConnection(); + } + + // note that although the query is for 'alias' the field is named 'Alias' + Assert.AreEqual("Alias", aliasName); + + // try differently + db.OpenSharedConnection(); + try + { + var conn = db.Connection; + var cmd = conn.CreateCommand(); + cmd.CommandText = "SELECT mandatory, dataTypeId, propertyTypeGroupId, contentTypeId, sortOrder, alias as alias, name, validationRegExp, description from cmsPropertyType where id=" + propertyType.Id; + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + for (var i = 0; i < reader.FieldCount; i++) + Console.WriteLine(reader.GetName(i)); + aliasName = reader.GetName(5); + } + } + } + finally + { + db.CloseSharedConnection(); + } + + // and now it is OK + Assert.AreEqual("alias", aliasName); + + // get the legacy content type + var legacyContentType = new umbraco.cms.businesslogic.ContentType(contentType.Id); + Assert.AreEqual("test", legacyContentType.Alias); + + // get the legacy properties + var legacyProperties = legacyContentType.PropertyTypes; + + // without the fix, due to some (swallowed) inner exception, we have no properties + //Assert.IsNull(legacyProperties); + + // thanks to the fix, it works + Assert.IsNotNull(legacyProperties); + Assert.AreEqual(1, legacyProperties.Count); + Assert.AreEqual("prop", legacyProperties[0].Alias); + } + } +} diff --git a/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs b/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs index 94f0010201..eea011bc20 100644 --- a/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs +++ b/src/Umbraco.Tests/Persistence/DatabaseContextTests.cs @@ -34,7 +34,7 @@ namespace Umbraco.Tests.Persistence { DatabaseContext = _dbContext, IsReady = true - }; + }; } [TearDown] @@ -44,6 +44,13 @@ namespace Umbraco.Tests.Persistence ApplicationContext.Current = null; } + [Test] + public void Database_Connection() + { + var db = _dbContext.Database; + Assert.IsNull(db.Connection); + } + [Test] public void Can_Verify_Single_Database_Instance() { @@ -99,7 +106,7 @@ namespace Umbraco.Tests.Persistence var appCtx = new ApplicationContext( new DatabaseContext(Mock.Of(), Mock.Of(), Mock.Of(), "test"), - new ServiceContext(migrationEntryService: Mock.Of()), + new ServiceContext(migrationEntryService: Mock.Of()), CacheHelper.CreateDisabledCacheHelper(), new ProfilingLogger(Mock.Of(), Mock.Of())); diff --git a/src/Umbraco.Tests/Umbraco.Tests.csproj b/src/Umbraco.Tests/Umbraco.Tests.csproj index 1334451890..d5f83d4cb2 100644 --- a/src/Umbraco.Tests/Umbraco.Tests.csproj +++ b/src/Umbraco.Tests/Umbraco.Tests.csproj @@ -155,6 +155,7 @@ + diff --git a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs index 8d5f538fd9..63a4d80e91 100644 --- a/src/umbraco.cms/businesslogic/propertytype/propertytype.cs +++ b/src/umbraco.cms/businesslogic/propertytype/propertytype.cs @@ -56,7 +56,7 @@ namespace umbraco.cms.businesslogic.propertytype { var found = ApplicationContext.Current.DatabaseContext.Database .SingleOrDefault( - "Select mandatory, DataTypeId, propertyTypeGroupId, contentTypeId, sortOrder, alias, name, validationRegExp, description from cmsPropertyType where id=@id", + "Select mandatory as mandatory, dataTypeId as dataTypeId, propertyTypeGroupId as propertyTypeGroupId, contentTypeId as contentTypeId, sortOrder as sortOrder, alias as alias, name as name, validationRegExp as validationRegExp, description as description from cmsPropertyType where id=@id", new {id = id}); if (found == null) @@ -72,14 +72,13 @@ namespace umbraco.cms.businesslogic.propertytype _tabId = _propertyTypeGroup; } - //Fixed issue U4-9493 Case issues _sortOrder = found.sortOrder; - _alias = found.Alias; - _name = found.Name; + _alias = found.alias; + _name = found.name; _validationRegExp = found.validationRegExp; _DataTypeId = found.dataTypeId; _contenttypeid = found.contentTypeId; - _description = found.Description; + _description = found.description; } #endregion