mirror of
https://github.com/OrchardCMS/Orchard.git
synced 2025-10-15 19:54:57 +08:00
Recipe parser and related unit tests.
Metadata for blog,cms and minimal recipes in Setup. --HG-- branch : recipe
This commit is contained in:
@@ -1 +1,46 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0"?>
|
||||
<Recipe>
|
||||
<Name>cms</Name>
|
||||
<Description>a sample Orchard recipe describing a cms</Description>
|
||||
<Author>orchard</Author>
|
||||
<WebSite>http://orchardproject.net</WebSite>
|
||||
<Tags>tag1, tag2</Tags>
|
||||
<Version>1.1</Version>
|
||||
|
||||
<!-- Steps -->
|
||||
<Module src="source directory" replace="false" />
|
||||
<Module name="module1" repository="somerepo" version="1.1" replace="true" />
|
||||
|
||||
<Feature disable="f1, f2" enable="f3,f4" />
|
||||
|
||||
<Metadata>
|
||||
<Types>
|
||||
<Blog creatable="true">
|
||||
<Body format="abodyformat"/>
|
||||
</Blog>
|
||||
</Types>
|
||||
<Parts>
|
||||
</Parts>
|
||||
<Fields>
|
||||
</Fields>
|
||||
</Metadata>
|
||||
|
||||
<Command>
|
||||
command1
|
||||
command2
|
||||
command3
|
||||
</Command>
|
||||
|
||||
<Settings>
|
||||
<SiteSettingsPart PageSize="30" />
|
||||
<CommentSettingsPart enableSpamProtection="true" />
|
||||
</Settings>
|
||||
|
||||
<Migration feature="f2"/>
|
||||
|
||||
<Theme src="source dir" enabled="true" current="true" />
|
||||
<Theme name="theme1" repository="somerepo" replace="true" />
|
||||
|
||||
<Custom1 attr2="value1" />
|
||||
<Custom2 attr2="value2" />
|
||||
</Recipe>
|
||||
|
@@ -1,5 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml;
|
||||
using Autofac;
|
||||
using NUnit.Framework;
|
||||
using Orchard.Caching;
|
||||
@@ -16,6 +17,7 @@ namespace Orchard.Tests.Modules.Recipes.Services {
|
||||
public class RecipeManagerTests {
|
||||
private IContainer _container;
|
||||
private IRecipeManager _recipeManager;
|
||||
private IRecipeParser _recipeParser;
|
||||
private IExtensionFolders _folders;
|
||||
|
||||
private const string DataPrefix = "Orchard.Tests.Modules.Recipes.Services.FoldersData.";
|
||||
@@ -67,6 +69,7 @@ namespace Orchard.Tests.Modules.Recipes.Services {
|
||||
|
||||
_container = builder.Build();
|
||||
_recipeManager = _container.Resolve<IRecipeManager>();
|
||||
_recipeParser = _container.Resolve<IRecipeParser>();
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
@@ -86,5 +89,35 @@ namespace Orchard.Tests.Modules.Recipes.Services {
|
||||
var recipes = (List<Recipe>)_recipeManager.DiscoverRecipes("Sample1");
|
||||
Assert.That(recipes.Count, Is.EqualTo(1));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseRecipeLoadsRecipeMetaDataIntoModel() {
|
||||
var recipes = (List<Recipe>) _recipeManager.DiscoverRecipes("Sample1");
|
||||
Assert.That(recipes.Count, Is.EqualTo(1));
|
||||
|
||||
var sampleRecipe = recipes[0];
|
||||
Assert.That(sampleRecipe.Name, Is.EqualTo("cms"));
|
||||
Assert.That(sampleRecipe.Description, Is.EqualTo("a sample Orchard recipe describing a cms"));
|
||||
Assert.That(sampleRecipe.Author, Is.EqualTo("orchard"));
|
||||
Assert.That(sampleRecipe.Version, Is.EqualTo("1.1"));
|
||||
Assert.That(sampleRecipe.WebSite, Is.EqualTo("http://orchardproject.net"));
|
||||
Assert.That(sampleRecipe.Tags, Is.EqualTo("tag1, tag2"));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseRecipeLoadsRecipeStepsIntoModel() {
|
||||
var recipes = (List<Recipe>)_recipeManager.DiscoverRecipes("Sample1");
|
||||
Assert.That(recipes.Count, Is.EqualTo(1));
|
||||
|
||||
var sampleRecipe = recipes[0];
|
||||
var recipeSteps = (List<RecipeStep>) sampleRecipe.RecipeSteps;
|
||||
|
||||
Assert.That(recipeSteps.Count, Is.EqualTo(11));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void ParseRecipeThrowsOnInvalidXml() {
|
||||
Assert.Throws(typeof(XmlException), () => _recipeParser.ParseRecipe("<reipe></recipe>"));
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,16 +1,64 @@
|
||||
using Orchard.Localization;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Xml.Linq;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Logging;
|
||||
using Orchard.Recipes.Models;
|
||||
|
||||
namespace Orchard.Recipes.Services {
|
||||
public class RecipeParser : IRecipeParser {
|
||||
public RecipeParser() {
|
||||
Logger = NullLogger.Instance;
|
||||
T = NullLocalizer.Instance;
|
||||
}
|
||||
|
||||
public Localizer T { get; set; }
|
||||
ILogger Logger { get; set; }
|
||||
|
||||
public Recipe ParseRecipe(string recipeText) {
|
||||
return new Recipe();
|
||||
var recipe = new Recipe();
|
||||
|
||||
try {
|
||||
var recipeSteps = new List<RecipeStep>();
|
||||
TextReader textReader = new StringReader(recipeText);
|
||||
var recipeTree = XElement.Load(textReader, LoadOptions.PreserveWhitespace);
|
||||
textReader.Close();
|
||||
|
||||
foreach (var element in recipeTree.Elements()) {
|
||||
switch(element.Name.ToString()) {
|
||||
case "Name":
|
||||
recipe.Name = element.Value;
|
||||
break;
|
||||
case "Description":
|
||||
recipe.Description = element.Value;
|
||||
break;
|
||||
case "Author":
|
||||
recipe.Author = element.Value;
|
||||
break;
|
||||
case "WebSite":
|
||||
recipe.WebSite = element.Value;
|
||||
break;
|
||||
case "Version":
|
||||
recipe.Version = element.Value;
|
||||
break;
|
||||
case "Tags":
|
||||
recipe.Tags = element.Value;
|
||||
break;
|
||||
default:
|
||||
var recipeStep = new RecipeStep {Name = element.Name.ToString(), Step = element};
|
||||
recipeSteps.Add(recipeStep);
|
||||
break;
|
||||
}
|
||||
}
|
||||
recipe.RecipeSteps = recipeSteps;
|
||||
}
|
||||
catch (Exception exception) {
|
||||
Logger.Error(exception, "Parsing recipe failed. Recipe text was: {0}.", recipeText);
|
||||
throw;
|
||||
}
|
||||
|
||||
return recipe;
|
||||
}
|
||||
}
|
||||
}
|
@@ -1 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0"?>
|
||||
<Recipe>
|
||||
<Name>blog</Name>
|
||||
<Description>Orchard Blog Recipe</Description>
|
||||
<Author>The Orchard Team</Author>
|
||||
<WebSite>http://orchardproject.net</WebSite>
|
||||
<Tags></Tags>
|
||||
<Version>1.0</Version>
|
||||
</Recipe>
|
||||
|
@@ -1 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0"?>
|
||||
<Recipe>
|
||||
<Name>cms</Name>
|
||||
<Description>Orchard CMS Recipe</Description>
|
||||
<Author>The Orchard Team</Author>
|
||||
<WebSite>http://orchardproject.net</WebSite>
|
||||
<Tags></Tags>
|
||||
<Version>1.0</Version>
|
||||
</Recipe>
|
||||
|
@@ -1 +1,9 @@
|
||||
<?xml version="1.0" encoding="utf-8" ?>
|
||||
<?xml version="1.0"?>
|
||||
<Recipe>
|
||||
<Name>minimal</Name>
|
||||
<Description>A minimal recipe for Orchard devs</Description>
|
||||
<Author>The Orchard Team</Author>
|
||||
<WebSite>http://orchardproject.net</WebSite>
|
||||
<Tags>developer</Tags>
|
||||
<Version>1.0</Version>
|
||||
</Recipe>
|
@@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.Cryptography;
|
||||
using System.Web;
|
||||
@@ -24,7 +23,6 @@ using Orchard.Environment.Descriptor.Models;
|
||||
using Orchard.Indexing;
|
||||
using Orchard.Localization;
|
||||
using Orchard.Localization.Services;
|
||||
using Orchard.Recipes.Models;
|
||||
using Orchard.Recipes.Services;
|
||||
using Orchard.Reports.Services;
|
||||
using Orchard.Security;
|
||||
|
@@ -2,8 +2,12 @@
|
||||
|
||||
namespace Orchard.Recipes.Models {
|
||||
public class Recipe {
|
||||
public string Title { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Description { get; set; }
|
||||
public string Author { get; set; }
|
||||
public string WebSite { get; set; }
|
||||
public string Version { get; set; }
|
||||
public string Tags { get; set; }
|
||||
public IEnumerable<RecipeStep> RecipeSteps { get; set; }
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user