Skip to content

Commit

Permalink
Merge pull request #1767 from umbraco/temp-u4-9560
Browse files Browse the repository at this point in the history
U4-9560 - fix db issue
  • Loading branch information
nul800sebastiaan committed Feb 22, 2017
2 parents 76b696e + 16f8f60 commit 33c9e2b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 7 deletions.
92 changes: 92 additions & 0 deletions 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<dynamic>
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);
}
}
}
11 changes: 9 additions & 2 deletions src/Umbraco.Tests/Persistence/DatabaseContextTests.cs
Expand Up @@ -34,7 +34,7 @@ public void Setup()
{
DatabaseContext = _dbContext,
IsReady = true
};
};
}

[TearDown]
Expand All @@ -44,6 +44,13 @@ public void TearDown()
ApplicationContext.Current = null;
}

[Test]
public void Database_Connection()
{
var db = _dbContext.Database;
Assert.IsNull(db.Connection);
}

[Test]
public void Can_Verify_Single_Database_Instance()
{
Expand Down Expand Up @@ -99,7 +106,7 @@ public void Can_Assert_Created_Database()

var appCtx = new ApplicationContext(
new DatabaseContext(Mock.Of<IDatabaseFactory>(), Mock.Of<ILogger>(), Mock.Of<ISqlSyntaxProvider>(), "test"),
new ServiceContext(migrationEntryService: Mock.Of<IMigrationEntryService>()),
new ServiceContext(migrationEntryService: Mock.Of<IMigrationEntryService>()),
CacheHelper.CreateDisabledCacheHelper(),
new ProfilingLogger(Mock.Of<ILogger>(), Mock.Of<IProfiler>()));

Expand Down
1 change: 1 addition & 0 deletions src/Umbraco.Tests/Umbraco.Tests.csproj
Expand Up @@ -155,6 +155,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Dependencies\NuGet.cs" />
<Compile Include="Issues\U9560.cs" />
<Compile Include="Migrations\MigrationIssuesTests.cs" />
<Compile Include="Persistence\BulkDataReaderTests.cs" />
<Compile Include="Persistence\Migrations\MigrationStartupHandlerTests.cs" />
Expand Down
9 changes: 4 additions & 5 deletions src/umbraco.cms/businesslogic/propertytype/propertytype.cs
Expand Up @@ -56,7 +56,7 @@ public PropertyType(int id)
{
var found = ApplicationContext.Current.DatabaseContext.Database
.SingleOrDefault<dynamic>(
"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)
Expand All @@ -72,14 +72,13 @@ public PropertyType(int id)
_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
Expand Down

0 comments on commit 33c9e2b

Please sign in to comment.