Converted UmbracoExamine UmbracoContentService to use new data APIs and added a unit test for one of the converted methods.
This commit is contained in:
@@ -28,7 +28,7 @@ namespace Umbraco.Core.Services
|
||||
/// <param name="fileUnitOfWorkProvider"></param>
|
||||
/// <param name="publishingStrategy"></param>
|
||||
internal ServiceContext(IDatabaseUnitOfWorkProvider dbUnitOfWorkProvider, IUnitOfWorkProvider fileUnitOfWorkProvider, IPublishingStrategy publishingStrategy)
|
||||
{
|
||||
{
|
||||
BuildServiceCache(dbUnitOfWorkProvider, fileUnitOfWorkProvider, publishingStrategy,
|
||||
//this needs to be lazy because when we create the service context it's generally before the
|
||||
//resolvers have been initialized!
|
||||
|
||||
@@ -279,6 +279,7 @@
|
||||
<Compile Include="TestHelpers\Entities\MockedContentTypes.cs" />
|
||||
<Compile Include="TestHelpers\Entities\MockedEntity.cs" />
|
||||
<Compile Include="TestHelpers\Entities\MockedMedia.cs" />
|
||||
<Compile Include="UmbracoExamine\ContentServiceTest.cs" />
|
||||
<Compile Include="UmbracoExamine\EventsTest.cs" />
|
||||
<Compile Include="UmbracoExamine\IndexInitializer.cs" />
|
||||
<Compile Include="UmbracoExamine\IndexTest.cs" />
|
||||
|
||||
26
src/Umbraco.Tests/UmbracoExamine/ContentServiceTest.cs
Normal file
26
src/Umbraco.Tests/UmbracoExamine/ContentServiceTest.cs
Normal file
@@ -0,0 +1,26 @@
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
using Umbraco.Tests.TestHelpers;
|
||||
using UmbracoExamine.DataServices;
|
||||
|
||||
namespace Umbraco.Tests.UmbracoExamine
|
||||
{
|
||||
[TestFixture]
|
||||
public class ContentServiceTest : BaseDatabaseFactoryTest
|
||||
{
|
||||
|
||||
[Test]
|
||||
public void Get_All_User_Property_Names()
|
||||
{
|
||||
var contentService = new UmbracoContentService(ApplicationContext);
|
||||
var db = DatabaseContext.Database;
|
||||
|
||||
var result = contentService.GetAllUserPropertyNames();
|
||||
|
||||
Assert.IsTrue(result.Select(x => x).ContainsAll(new[] { "contents", "umbracoBytes", "umbracoExtension", "umbracoFile", "umbracoHeight", "umbracoWidth" }));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,14 @@ using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.Repositories;
|
||||
using UmbracoExamine;
|
||||
using UmbracoExamine.DataServices;
|
||||
|
||||
namespace Umbraco.Tests.UmbracoExamine
|
||||
{
|
||||
/// <summary>
|
||||
/// <summary>
|
||||
/// A mock data service used to return content from the XML data file created with CWS
|
||||
/// </summary>
|
||||
public class TestContentService : IContentService
|
||||
|
||||
@@ -6,6 +6,8 @@ using System.Text;
|
||||
using Umbraco.Core;
|
||||
using Umbraco.Core.Logging;
|
||||
using Umbraco.Core.Models;
|
||||
using Umbraco.Core.Persistence;
|
||||
using Umbraco.Core.Persistence.UnitOfWork;
|
||||
using Umbraco.Core.Services;
|
||||
using umbraco;
|
||||
using System.Xml.Linq;
|
||||
@@ -25,17 +27,19 @@ namespace UmbracoExamine.DataServices
|
||||
public class UmbracoContentService : IContentService
|
||||
{
|
||||
|
||||
private readonly ServiceContext _services;
|
||||
private readonly ApplicationContext _applicationContext;
|
||||
|
||||
[SecuritySafeCritical]
|
||||
public UmbracoContentService()
|
||||
: this(ApplicationContext.Current.Services)
|
||||
: this(ApplicationContext.Current)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public UmbracoContentService(ServiceContext services)
|
||||
[SecuritySafeCritical]
|
||||
public UmbracoContentService(ApplicationContext applicationContext)
|
||||
{
|
||||
_services = services;
|
||||
_applicationContext = applicationContext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -74,7 +78,7 @@ namespace UmbracoExamine.DataServices
|
||||
public XDocument GetLatestContentByXPath(string xpath)
|
||||
{
|
||||
var xmlContent = XDocument.Parse("<content></content>");
|
||||
foreach (var c in _services.ContentService.GetRootContent())
|
||||
foreach (var c in _applicationContext.Services.ContentService.GetRootContent())
|
||||
{
|
||||
xmlContent.Root.Add(c.ToXml());
|
||||
}
|
||||
@@ -115,39 +119,18 @@ namespace UmbracoExamine.DataServices
|
||||
/// <returns></returns>
|
||||
[SecuritySafeCritical]
|
||||
public IEnumerable<string> GetAllUserPropertyNames()
|
||||
{
|
||||
//TODO: this is how umb codebase 4.0 does this... convert to new data layer
|
||||
|
||||
var aliases = new List<string>();
|
||||
var fieldSql = "select distinct alias from cmsPropertyType order by alias";
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var dr = Application.SqlHelper.ExecuteReader(fieldSql))
|
||||
{
|
||||
while (dr.Read())
|
||||
{
|
||||
aliases.Add(dr.GetString("alias"));
|
||||
}
|
||||
}
|
||||
var result = _applicationContext.DatabaseContext.Database.Fetch<dynamic>("select distinct alias from cmsPropertyType order by alias");
|
||||
return result.Select(r => r.Alias.ToString()).Cast<string>().ToList();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
if (ex is SqlHelperException || ex is SqlException)
|
||||
{
|
||||
//if this happens, it could be due to wrong connection string, or something else.
|
||||
//we don't want to crash the app because of this so we'll actually swallow this
|
||||
//exception... Unfortunately logging probably won't work in this situation either :(
|
||||
|
||||
LogHelper.Error<UmbracoContentService>("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return aliases;
|
||||
}
|
||||
LogHelper.Error<UmbracoContentService>("EXCEPTION OCCURRED reading GetAllUserPropertyNames", ex);
|
||||
return Enumerable.Empty<string>();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a list of all system field names in Umbraco
|
||||
|
||||
@@ -92,6 +92,7 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\packages\Lucene.Net.2.9.4.1\lib\net40\Lucene.Net.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="Microsoft.CSharp" />
|
||||
<Reference Include="System" />
|
||||
<Reference Include="System.configuration" />
|
||||
<Reference Include="System.Core">
|
||||
|
||||
Reference in New Issue
Block a user