mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-12-03 12:03:51 +08:00
Selective import/Export of metadata from the import/export module.
Update unit tests. --HG-- branch : dev
This commit is contained in:
@@ -1,8 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Xml.Linq;
|
||||
using Autofac;
|
||||
using Moq;
|
||||
using NHibernate;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Caching;
|
||||
using Orchard.ContentManagement;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.ContentManagement.MetaData.Models;
|
||||
using Orchard.ContentManagement.MetaData.Services;
|
||||
using Orchard.ContentManagement.Records;
|
||||
using Orchard.Core.Settings.Metadata;
|
||||
using Orchard.Data;
|
||||
using Orchard.Environment.Descriptor;
|
||||
using Orchard.Environment.Descriptor.Models;
|
||||
using Orchard.Environment.Extensions;
|
||||
@@ -12,14 +22,29 @@ using Orchard.FileSystems.WebSite;
|
||||
using Orchard.ImportExport.Services;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Services;
|
||||
using Orchard.Tests.ContentManagement;
|
||||
using Orchard.Tests.Environment.Extensions;
|
||||
using Orchard.Tests.Modules.Recipes.Services;
|
||||
using Orchard.Tests.Stubs;
|
||||
using Orchard.Tests.UI.Navigation;
|
||||
|
||||
namespace Orchard.Tests.Modules.ImportExport.Services {
|
||||
[TestFixture]
|
||||
public class ImportExportManagerTests {
|
||||
private IContainer _container;
|
||||
private IImportExportService _importExportService;
|
||||
private ISessionFactory _sessionFactory;
|
||||
private ISession _session;
|
||||
|
||||
[TestFixtureSetUp]
|
||||
public void InitFixture() {
|
||||
var databaseFileName = System.IO.Path.GetTempFileName();
|
||||
_sessionFactory = DataUtility.CreateSessionFactory(
|
||||
databaseFileName,
|
||||
typeof(ContentTypeRecord),
|
||||
typeof(ContentItemRecord),
|
||||
typeof(ContentItemVersionRecord));
|
||||
}
|
||||
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
@@ -36,10 +61,21 @@ namespace Orchard.Tests.Modules.ImportExport.Services {
|
||||
builder.RegisterType<StubAppDataFolder>().As<IAppDataFolder>();
|
||||
builder.RegisterType<StubClock>().As<IClock>();
|
||||
builder.RegisterType<StubCacheManager>().As<ICacheManager>();
|
||||
builder.RegisterType<Environment.Extensions.ExtensionManagerTests.StubLoaders>().As<IExtensionLoader>();
|
||||
builder.RegisterType<ExtensionManagerTests.StubLoaders>().As<IExtensionLoader>();
|
||||
builder.RegisterType<RecipeParser>().As<IRecipeParser>();
|
||||
builder.RegisterType<StubWebSiteFolder>().As<IWebSiteFolder>();
|
||||
builder.RegisterType<CustomRecipeHandler>().As<IRecipeHandler>();
|
||||
builder.RegisterType<DefaultContentManager>().As<IContentManager>();
|
||||
builder.RegisterType<ContentDefinitionManager>().As<IContentDefinitionManager>();
|
||||
builder.RegisterType<ContentDefinitionWriter>().As<IContentDefinitionWriter>();
|
||||
builder.RegisterType<StubOrchardServices>().As<IOrchardServices>();
|
||||
builder.RegisterType<StubAppDataFolder>().As<IAppDataFolder>();
|
||||
builder.RegisterType<Signals>().As<ISignals>();
|
||||
builder.RegisterGeneric(typeof(Repository<>)).As(typeof(IRepository<>));
|
||||
builder.RegisterInstance(new Mock<IMapper<SettingsDictionary, XElement>>().Object);
|
||||
builder.RegisterInstance(new Mock<IMapper<XElement, SettingsDictionary>>().Object);
|
||||
_session = _sessionFactory.OpenSession();
|
||||
builder.RegisterInstance(new DefaultContentManagerTests.TestSessionLocator(_session)).As<ISessionLocator>();
|
||||
|
||||
_container = builder.Build();
|
||||
_importExportService = _container.Resolve<IImportExportService>();
|
||||
|
||||
@@ -74,8 +74,8 @@ namespace Orchard.ImportExport.Controllers {
|
||||
exportOptions.ExportData = true;
|
||||
exportOptions.VersionHistoryOptions = (VersionHistoryOptions)Enum.Parse(typeof(VersionHistoryOptions), viewModel.DataImportChoice, true);
|
||||
}
|
||||
var exportFile = _importExportService.Export(contentTypesToExport, exportOptions);
|
||||
Services.Notifier.Information(T("Your export file has been created at <a href=\"{0}\" />", exportFile));
|
||||
var exportFilePath = _importExportService.Export(contentTypesToExport, exportOptions);
|
||||
Services.Notifier.Information(T("Your export file has been created at {0}", exportFilePath));
|
||||
|
||||
return RedirectToAction("Export");
|
||||
}
|
||||
|
||||
@@ -42,6 +42,8 @@
|
||||
<SpecificVersion>False</SpecificVersion>
|
||||
<HintPath>..\..\..\..\lib\aspnetmvc\System.Web.Mvc.dll</HintPath>
|
||||
</Reference>
|
||||
<Reference Include="System.Xml" />
|
||||
<Reference Include="System.Xml.Linq" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="AdminMenu.cs" />
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using JetBrains.Annotations;
|
||||
using Orchard.ContentManagement.MetaData;
|
||||
using Orchard.Environment.Descriptor;
|
||||
using Orchard.FileSystems.AppData;
|
||||
using Orchard.ImportExport.Models;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
@@ -11,11 +15,27 @@ using Orchard.Recipes.Services;
|
||||
namespace Orchard.ImportExport.Services {
|
||||
[UsedImplicitly]
|
||||
public class ImportExportService : IImportExportService {
|
||||
private readonly IOrchardServices _orchardServices;
|
||||
private readonly IContentDefinitionManager _contentDefinitionManager;
|
||||
private readonly IContentDefinitionWriter _contentDefinitionWriter;
|
||||
private readonly IAppDataFolder _appDataFolder;
|
||||
private readonly IRecipeParser _recipeParser;
|
||||
private readonly IRecipeManager _recipeManager;
|
||||
private readonly IShellDescriptorManager _shellDescriptorManager;
|
||||
private const string ExportsDirectory = "Exports";
|
||||
|
||||
public ImportExportService(IRecipeParser recipeParser, IRecipeManager recipeManager, IShellDescriptorManager shellDescriptorManager) {
|
||||
public ImportExportService(
|
||||
IOrchardServices orchardServices,
|
||||
IContentDefinitionManager contentDefinitionManager,
|
||||
IContentDefinitionWriter contentDefinitionWriter,
|
||||
IAppDataFolder appDataFolder,
|
||||
IRecipeParser recipeParser,
|
||||
IRecipeManager recipeManager,
|
||||
IShellDescriptorManager shellDescriptorManager) {
|
||||
_orchardServices = orchardServices;
|
||||
_contentDefinitionManager = contentDefinitionManager;
|
||||
_contentDefinitionWriter = contentDefinitionWriter;
|
||||
_appDataFolder = appDataFolder;
|
||||
_recipeParser = recipeParser;
|
||||
_recipeManager = recipeManager;
|
||||
_shellDescriptorManager = shellDescriptorManager;
|
||||
@@ -34,7 +54,77 @@ namespace Orchard.ImportExport.Services {
|
||||
}
|
||||
|
||||
public string Export(IEnumerable<string> contentTypes, ExportOptions exportOptions) {
|
||||
return String.Empty;
|
||||
var exportDocument = CreateExportRoot();
|
||||
|
||||
if (exportOptions.ExportMetadata) {
|
||||
exportDocument.Element("Orchard").Add(ExportMetadata(contentTypes));
|
||||
}
|
||||
|
||||
if (exportOptions.ExportSiteSettings) {
|
||||
exportDocument.Element("Orchard").Add(ExportSiteSettings(contentTypes));
|
||||
}
|
||||
|
||||
if (exportOptions.ExportData) {
|
||||
exportDocument.Element("Orchard").Add(ExportData(contentTypes, exportOptions.VersionHistoryOptions));
|
||||
}
|
||||
|
||||
return WriteExportFile(exportDocument.ToString());
|
||||
}
|
||||
|
||||
private XDocument CreateExportRoot() {
|
||||
var exportRoot = new XDocument(
|
||||
new XDeclaration("1.0", "", "yes"),
|
||||
new XComment("Exported from Orchard"),
|
||||
new XElement("Orchard",
|
||||
new XElement("Recipe",
|
||||
new XElement("Name", "Generated by Orchard.ImportExport"),
|
||||
new XElement("Author", _orchardServices.WorkContext.CurrentUser.UserName)
|
||||
)
|
||||
)
|
||||
);
|
||||
return exportRoot;
|
||||
}
|
||||
|
||||
private XElement ExportMetadata(IEnumerable<string> contentTypes) {
|
||||
var typesElement = new XElement("Types");
|
||||
var partsElement = new XElement("Parts");
|
||||
var typesToExport = _contentDefinitionManager.ListTypeDefinitions()
|
||||
.Where(typeDefinition => contentTypes.Contains(typeDefinition.Name))
|
||||
.ToList();
|
||||
var partsToExport = new List<string>();
|
||||
|
||||
foreach (var contentTypeDefinition in typesToExport) {
|
||||
foreach (var contentPartDefinition in contentTypeDefinition.Parts) {
|
||||
if (partsToExport.Contains(contentPartDefinition.PartDefinition.Name)) {
|
||||
continue;
|
||||
}
|
||||
partsToExport.Add(contentPartDefinition.PartDefinition.Name);
|
||||
partsElement.Add(_contentDefinitionWriter.Export(contentPartDefinition.PartDefinition));
|
||||
}
|
||||
typesElement.Add(_contentDefinitionWriter.Export(contentTypeDefinition));
|
||||
}
|
||||
|
||||
return new XElement("Metadata", typesElement, partsElement);
|
||||
}
|
||||
|
||||
private XElement ExportSiteSettings(IEnumerable<string> contentTypes) {
|
||||
return new XElement("Settings");
|
||||
}
|
||||
|
||||
private XElement ExportData(IEnumerable<string> contentTypes, VersionHistoryOptions versionHistoryOptions) {
|
||||
return new XElement("Data");
|
||||
}
|
||||
|
||||
private string WriteExportFile(string exportDocument) {
|
||||
var exportFile = string.Format("Export-{0}-{1}.xml", _orchardServices.WorkContext.CurrentUser.UserName, DateTime.UtcNow.Ticks);
|
||||
if (!_appDataFolder.DirectoryExists(ExportsDirectory)) {
|
||||
_appDataFolder.CreateDirectory(ExportsDirectory);
|
||||
}
|
||||
|
||||
var path = _appDataFolder.Combine(ExportsDirectory, exportFile);
|
||||
_appDataFolder.CreateFile(path, exportDocument);
|
||||
|
||||
return _appDataFolder.MapPath(path);
|
||||
}
|
||||
|
||||
private void CheckRecipeSteps(Recipe recipe) {
|
||||
|
||||
Reference in New Issue
Block a user