public void Can_Save_ContentType_Structure_And_Create_Content_Based_On_It() { // Arrange var cs = ServiceContext.ContentService; var cts = ServiceContext.ContentTypeService; var dtdYesNo = ServiceContext.DataTypeService.GetDataTypeDefinitionById(-49); var ctBase = new ContentType(-1) { Name = "Base", Alias = "Base", Icon = "folder.gif", Thumbnail = "folder.png" }; ctBase.AddPropertyType(new PropertyType(dtdYesNo) { Name = "Hide From Navigation", Alias = "umbracoNaviHide" } /*,"Navigation"*/ ); cts.Save(ctBase); var ctHomePage = new ContentType(ctBase) { Name = "Home Page", Alias = "HomePage", Icon = "settingDomain.gif", Thumbnail = "folder.png", AllowedAsRoot = true }; bool addedContentType = ctHomePage.AddContentType(ctBase); ctHomePage.AddPropertyType(new PropertyType(dtdYesNo) { Name = "Some property", Alias = "someProperty" } /*,"Navigation"*/ ); cts.Save(ctHomePage); // Act var homeDoc = cs.CreateContent("Home Page", -1, "HomePage"); cs.SaveAndPublish(homeDoc); // Assert Assert.That(ctBase.HasIdentity, Is.True); Assert.That(ctHomePage.HasIdentity, Is.True); Assert.That(homeDoc.HasIdentity, Is.True); Assert.That(homeDoc.ContentTypeId, Is.EqualTo(ctHomePage.Id)); Assert.That(addedContentType, Is.True); }
public void General_RetrieveItemsAndFieldsFromUmbraco_ReturnPopulatedClass() { const string fieldValue = "test field value"; const string name = "Target"; const string contentTypeAlias = "TestType"; const string contentTypeName = "Test Type"; const string contentTypeProperty = "TestProperty"; var context = WindsorContainer.GetContext(); var loader = new UmbracoAttributeConfigurationLoader("Glass.Mapper.Umb.Integration"); context.Load(loader); IContentType contentType = new ContentType(-1); contentType.Name = contentTypeName; contentType.Alias = contentTypeAlias; contentType.Thumbnail = string.Empty; ContentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var definitions = DataTypeService.GetDataTypeDefinitionByControlId(new Guid("ec15c1e5-9d90-422a-aa52-4f7622c63bea")); var firstDefinition = definitions.FirstOrDefault(); DataTypeService.Save(firstDefinition); var propertyType = new PropertyType(firstDefinition) { Alias = contentTypeProperty }; contentType.AddPropertyType(propertyType); ContentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var content = new Content(name, -1, contentType); content.SetPropertyValue(contentTypeProperty, fieldValue); ContentService.Save(content); var umbracoService = new UmbracoService(ContentService, context); //Act var result = umbracoService.GetItem<AttributeStub>(content.Id); //Assert Assert.IsNotNull(result); Assert.AreEqual(fieldValue, result.TestProperty); Assert.AreEqual(content.Id, result.Id); Assert.AreEqual(content.Key, result.Key); Assert.AreEqual(name, result.Name); Assert.AreEqual(contentTypeName, result.ContentTypeName); Assert.AreEqual(content.ParentId + "," + content.Id, result.Path); Assert.AreEqual(contentTypeAlias, result.ContentTypeAlias); Assert.AreEqual(content.Version, result.Version); }
public void CreateStub() { string name = "Target"; string contentTypeAlias = "TestType"; string contentTypeName = "Test Type"; var unitOfWork = Global.CreateUnitOfWork(); var repoFactory = new RepositoryFactory(); _contentService = new ContentService(unitOfWork, repoFactory); var contentTypeService = new ContentTypeService(unitOfWork, repoFactory, new ContentService(unitOfWork), new MediaService(unitOfWork, repoFactory)); var dataTypeService = new DataTypeService(unitOfWork, repoFactory); var contentType = new ContentType(-1); contentType.Name = contentTypeName; contentType.Alias = contentTypeAlias; contentType.Thumbnail = string.Empty; contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var definitions = dataTypeService.GetDataTypeDefinitionByControlId(new Guid("ec15c1e5-9d90-422a-aa52-4f7622c63bea")); dataTypeService.Save(definitions.FirstOrDefault()); var propertyType = new PropertyType(definitions.FirstOrDefault()); propertyType.Alias = "Property"; contentType.AddPropertyType(propertyType); contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var content = new Content(name, -1, contentType); content.Key = new Guid("{2867D837-B258-4DF1-90F1-D5E849FCAF84}"); _contentService.Save(content); }
public ActionResult Install() { var dataTypeService = ApplicationContext.Current.Services.DataTypeService; var dataType = dataTypeService.GetDataTypeDefinitionById(Constants.System.DefaultContentListViewDataTypeId); var preVals = dataTypeService.GetPreValuesCollectionByDataTypeId(dataType.Id); var dict = preVals.FormatAsDictionary(); if (!dict.ContainsKey("pageSize")) dict["pageSize"] = new PreValue("10"); dict["pageSize"].Value = "200"; dataTypeService.SavePreValues(dataType, dict); var contentTypeService = ApplicationContext.Current.Services.ContentTypeService; var contentType = new ContentType(-1) { Alias = _contentAlias, Name = "LoadTest Content", Description = "Content for LoadTest", Icon = "icon-document" }; var def = ApplicationContext.Current.Services.DataTypeService.GetDataTypeDefinitionById(_textboxDefinitionId); contentType.AddPropertyType(new PropertyType(def) { Name = "Origin", Alias = "origin", Description = "The origin of the content.", }); contentTypeService.Save(contentType); var containerTemplate = ImportTemplate(ApplicationContext.Current.Services, "~/Views/LoadTestContainer.cshtml", "LoadTestContainer", "LoadTestContainer", _containerTemplateText); var containerType = new ContentType(-1) { Alias = _containerAlias, Name = "LoadTest Container", Description = "Container for LoadTest content", Icon = "icon-document", AllowedAsRoot = true, IsContainer = true }; containerType.AllowedContentTypes = containerType.AllowedContentTypes.Union(new[] { new ContentTypeSort(new Lazy<int>(() => contentType.Id), 0, contentType.Alias), }); containerType.AllowedTemplates = containerType.AllowedTemplates.Union(new[] { containerTemplate }); containerType.SetDefaultTemplate(containerTemplate); contentTypeService.Save(containerType); var contentService = ApplicationContext.Current.Services.ContentService; var content = contentService.CreateContent("LoadTestContainer", -1, _containerAlias); contentService.SaveAndPublishWithStatus(content); return ContentHtml("Installed."); }
public void CreateStub() { string fieldValue = "test field value"; string name = "Target"; string contentTypeAlias = "TestType"; string contentTypeName = "Test Type"; var unitOfWork = Global.CreateUnitOfWork(); var repoFactory = new RepositoryFactory(); _contentService = new ContentService(unitOfWork, repoFactory); var contentTypeService = new ContentTypeService(unitOfWork, repoFactory, new ContentService(unitOfWork), new MediaService(unitOfWork, repoFactory)); var dataTypeService = new DataTypeService(unitOfWork, repoFactory); var contentType = new ContentType(-1); contentType.Name = contentTypeName; contentType.Alias = contentTypeAlias; contentType.Thumbnail = string.Empty; contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var definitions = dataTypeService.GetDataTypeDefinitionByControlId(new Guid("ec15c1e5-9d90-422a-aa52-4f7622c63bea")); dataTypeService.Save(definitions.FirstOrDefault()); var propertyType = new PropertyType(definitions.FirstOrDefault()); propertyType.Alias = "TestProperty"; contentType.AddPropertyType(propertyType); contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var content = new Content(name, -1, contentType); content.Key = new Guid("{5F6D851E-46C0-40C7-A93A-EC3F6D7EBA3E}"); content.SetPropertyValue("TestProperty", fieldValue); _contentService.Save(content); }
/// <summary> /// Import /// </summary> /// <param name="content">content</param> /// <param name="umbApplication">umb Application</param> /// <param name="appContext">app Context</param> /// <returns></returns> public void Import(Site site, ApplicationContext appContext) { references.Clear(); var mediaService = appContext.Services.MediaService; var cts = appContext.Services.ContentTypeService; var folderTypeID = cts.GetMediaType(Constants.Conventions.MediaTypes.Folder).Id; var folders = mediaService.GetMediaOfMediaType(folderTypeID); foreach (var folder in folders) { if (folder.Name == site.Name) { foreach (var media in mediaService.GetChildren(folder.Id)) { mediaService.Delete(media); } mediaService.Delete(folder); break; } } var serializer = new SiteSerializer(); //Accents removal byte[] tempBytes; tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(site.Name.ToLowerInvariant()); string siteAlias = System.Text.Encoding.UTF8.GetString(tempBytes); siteAlias = siteAlias.Replace(" ", string.Empty); //site.Root.Name = site.Name; var fileService = appContext.Services.FileService; var scriptPath = HostingEnvironment.MapPath("/scripts") + Path.DirectorySeparatorChar; var cssPath = HostingEnvironment.MapPath("/css") + Path.DirectorySeparatorChar; var resourcePath = site.Name + '/'; var mediaParent = mediaService.CreateMediaWithIdentity(site.Name, -1, Constants.Conventions.MediaTypes.Folder); foreach (var resource in site.Resources) { var filePath = resource.TextData; if (resource.ResourceType == ResourceType.Javascript) { try { var scripts = fileService.GetScripts(resource.TextData.ToLowerInvariant()); if (scripts != null && scripts.Any()) { foreach (var item in scripts) { fileService.DeleteScript(item.Name); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); } var newPath = scriptPath + Path.GetFileName(filePath); if (!newPath.EndsWith(".js")) { newPath = newPath + ".js"; } var script = new Script(filePath); fileService.SaveScript(script); System.IO.File.Copy(filePath, newPath, true); AddToReferences(resource.TemplateReference, newPath, true); } else if (resource.ResourceType == ResourceType.Stylesheet) { try { var stylesheets = fileService.GetStylesheets(resource.TextData.ToLowerInvariant()); if (stylesheets != null && stylesheets.Any()) { foreach (var item in stylesheets) { fileService.DeleteStylesheet(item.Name); } } } catch (Exception e) { Console.WriteLine(e.StackTrace); } var newPath = cssPath + Path.GetFileName(filePath); if (!newPath.EndsWith(".css")) { newPath = newPath + ".css"; } var stylesheet = new Stylesheet(newPath); fileService.SaveStylesheet(stylesheet); System.IO.File.Copy(filePath, newPath, true); AddToReferences(resource.TemplateReference, newPath, true); } else if (resource.ResourceType == ResourceType.Image) { var media = mediaService.CreateMedia(resource.FileName, mediaParent.Id, Constants.Conventions.MediaTypes.Image); using (var fs = System.IO.File.OpenRead(filePath)) { media.SetValue(Constants.Conventions.Media.File, resource.FileName, fs); } mediaService.Save(media); AddToReferences(resource.TemplateReference, media.GetValue<string>(Constants.Conventions.Media.File)); } else if (resource.ResourceType == ResourceType.File) { var media = mediaService.CreateMedia(resource.FileName, mediaParent.Id, Constants.Conventions.MediaTypes.File); using (var fs = System.IO.File.OpenRead(filePath)) { media.SetValue(Constants.Conventions.Media.File, resource.FileName, fs); } mediaService.Save(media); AddToReferences(resource.TemplateReference, media.GetValue<string>(Constants.Conventions.Media.File)); } } var rootCT = new ContentType(-1); rootCT.Name = site.Name; rootCT.Alias = siteAlias; var oldCT = cts.GetContentType(rootCT.Alias); if (oldCT != null) { foreach (var child in cts.GetContentTypeChildren(oldCT.Id)) { cts.Delete(child); } cts.Delete(oldCT); } cts.Save(rootCT); var templatePathFormat = HostingEnvironment .MapPath("/Views") + Path.DirectorySeparatorChar + "{0}.cshtml"; var templates = new Dictionary<Guid, ITemplate>(); var templateTexts = new Dictionary<Guid, string>(); foreach (var template in site.Templates.WhereNotNull()) { var templateAlias = siteAlias + template.Name.ToLowerInvariant(); if (fileService.GetTemplate(templateAlias) != null) { fileService.DeleteTemplate(templateAlias); } var templatePath = string.Format(templatePathFormat, templateAlias); var templateModel = new Umbraco.Core.Models.Template(templatePath, template.Name, templateAlias); fileService.SaveTemplate(templateModel); templateTexts.Add(template.ID, template.Text); templates.Add(template.ID, templateModel); } var contentTypes = new Dictionary<Guid, ContentType>(); foreach (var pageType in site.PageTypes) { var contentType = new ContentType(rootCT); contentType.Name = pageType.Name; contentType.Alias = siteAlias + pageType.Name; contentType.Icon = "icon-umb-content"; cts.Save(contentType); contentType.AddPropertyGroup(site.Name); var propertyGroup = contentType.PropertyGroups[site.Name]; bool hasTemplate = pageType.TemplateID != Guid.Empty; string templateBody = null; ITemplate contentTypeTemplate = null; if (hasTemplate) { contentTypeTemplate = templates[pageType.TemplateID]; templateBody = templateTexts[pageType.TemplateID]; } foreach (var definition in pageType.Definitions) { var def = appContext.Services.DataTypeService .GetDataTypeDefinitionByPropertyEditorAlias(Constants.PropertyEditors.TinyMCEAlias) .First(); var pt = new PropertyType(def); pt.Alias = siteAlias + definition.Name; pt.Name = definition.Name; contentType.AddPropertyType(pt, site.Name); if (hasTemplate) { templateBody = templateBody.Replace(definition.TemplateReference, "@Umbraco.Field(\"" + pt.Alias + "\")"); } } if (hasTemplate) { contentTypeTemplate.Content = templateBody; contentType.AllowedTemplates = contentTypeTemplate.AsEnumerableOfOne(); contentType.SetDefaultTemplate(contentTypeTemplate); var templatePath = string.Format(templatePathFormat, contentTypeTemplate.Alias); System.IO.File.WriteAllText(templatePath, templateBody, Encoding.UTF8); } cts.Save(contentType); contentTypes.Add(pageType.ID, contentType); } IContent root = null; var cs = appContext.Services.ContentService; var pages = new Dictionary<Guid, IContent>(); var pageReferences = new Dictionary<string, IContent>(); foreach (var page in site.Root.GetDescendantsAndSelf()) { if(page.Name == null) { page.Name = "unknown"; } var parentID = page.ParentID == Guid.Empty ? -1 : pages[page.ParentID].Id; var cnt = cs.CreateContent(page.Name, parentID, contentTypes[page.PageTypeID].Alias); pages.Add(page.ID, cnt); foreach (var prop in page.Properties) { //Accents removal tempBytes = System.Text.Encoding.GetEncoding("ISO-8859-8").GetBytes(prop.Name); string propName = System.Text.Encoding.UTF8.GetString(tempBytes); propName = propName.Replace(" ", string.Empty); cnt.Properties[siteAlias + propName].Value = prop.Value; } cs.Save(cnt); if (page.ParentID == Guid.Empty) { root = cnt; } if (!string.IsNullOrWhiteSpace(page.TemplateReference)) { pageReferences.Add(page.TemplateReference, cnt); } } var pageRegex = new Regex(@"(@Raw\(Model\.Page\d+\))", RegexOptions.Compiled | RegexOptions.Multiline); var resourceRegex = new Regex(@"(@Raw\(Model\.Resource\d+\))", RegexOptions.Compiled | RegexOptions.Multiline); foreach (var page in root.Descendants().Where(c => c.Template != null)) { foreach (var property in page.Properties) { var value = property.Value != null ? property.Value.ToString() : ""; value = pageRegex.Replace(value, m => "/{localLink:" + pageReferences[m.Value].Id + "}"); property.Value = resourceRegex.Replace(value, m => references[m.Value]); } cs.Save(page); } cs.PublishWithChildrenWithStatus(root, includeUnpublished: true); foreach (var template in templates.Values) { var sb = new StringBuilder(); var templatePath = string.Format(templatePathFormat, template.Alias); var newContent = pageRegex.Replace(template.Content, m => string.Format("@Umbraco.NiceUrl({0})", pageReferences[m.Value].Id)); newContent = resourceRegex.Replace(newContent, m => references[m.Value]); System.IO.File.WriteAllText(templatePath, templatePrefix + newContent, Encoding.UTF8); } }
public void CreateStub() { bool fieldValue = false; string name = "Target"; string contentTypeAlias = "TestType"; string contentTypeName = "Test Type"; var unitOfWork = Global.CreateUnitOfWork(); var repoFactory = new RepositoryFactory(); _contentService = new ContentService(unitOfWork, repoFactory); var contentTypeService = new ContentTypeService(unitOfWork, repoFactory, new ContentService(unitOfWork), new MediaService(unitOfWork, repoFactory)); var dataTypeService = new DataTypeService(unitOfWork, repoFactory); var contentType = new ContentType(-1); contentType.Name = contentTypeName; contentType.Alias = contentTypeAlias; contentType.Thumbnail = string.Empty; contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var definitions = dataTypeService.GetDataTypeDefinitionByControlId(new Guid("38b352c1-e9f8-4fd8-9324-9a2eab06d97a")); dataTypeService.Save(definitions.FirstOrDefault()); var propertyType = new PropertyType(definitions.FirstOrDefault()); propertyType.Alias = ContentTypeProperty; contentType.AddPropertyType(propertyType); contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var content = new Content(name, -1, contentType); content.Key = new Guid("{5928EFBB-6DF2-4BB6-A026-BF4938D7ED7A}"); content.SetPropertyValue(ContentTypeProperty, fieldValue); _contentService.Save(content); }
private ContentType CreateBannerComponent(ContentType parent) { var banner = new ContentType(parent) { Alias = "banner", Name = "Banner Component", Description = "ContentType used for Banner Component", Icon = ".sprTreeDoc3", Thumbnail = "doc.png", SortOrder = 1, CreatorId = 0, Trashed = false }; var propertyType = new PropertyType("test", DataTypeDatabaseType.Ntext) { Alias = "bannerName", Name = "Banner Name", Description = "", HelpText = "", Mandatory = false, SortOrder = 2, DataTypeDefinitionId = -88 }; banner.AddPropertyType(propertyType, "Component"); return banner; }
public void General_RetrieveContentAndPropertiesFromUmbraco_ReturnPopulatedClass() { //Assign string fieldValue = "test field value"; string name = "Target"; string contentTypeAlias = "TestType"; string contentTypeName = "Test Type"; string contentTypeProperty = "TestProperty"; var unitOfWork = Global.CreateUnitOfWork(); var repoFactory = new RepositoryFactory(); var contentService = new ContentService(unitOfWork, repoFactory); var contentTypeService = new ContentTypeService(unitOfWork, repoFactory, new ContentService(unitOfWork), new MediaService(unitOfWork, repoFactory)); var dataTypeService = new DataTypeService(unitOfWork, repoFactory); var context = WindsorContainer.GetContext(); var loader = new UmbracoFluentConfigurationLoader(); var stubConfig = loader.Add<Stub>(); stubConfig.Configure(x => { x.Id(y => y.Id); x.Id(y => y.Key); x.Property(y => y.TestProperty); x.Info(y => y.Name).InfoType(UmbracoInfoType.Name); x.Info(y => y.ContentTypeName).InfoType(UmbracoInfoType.ContentTypeName); x.Info(y => y.ContentTypeAlias).InfoType(UmbracoInfoType.ContentTypeAlias); x.Info(y => y.Path).InfoType(UmbracoInfoType.Path); x.Info(y => y.Version).InfoType(UmbracoInfoType.Version); x.Info(y => y.CreateDate).InfoType(UmbracoInfoType.CreateDate); x.Info(y => y.UpdateDate).InfoType(UmbracoInfoType.UpdateDate); x.Info(y => y.Creator).InfoType(UmbracoInfoType.Creator); }); context.Load(loader); IContentType contentType = new ContentType(-1); contentType.Name = contentTypeName; contentType.Alias = contentTypeAlias; contentType.Thumbnail = string.Empty; contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var definitions = dataTypeService.GetDataTypeDefinitionByControlId(new Guid("ec15c1e5-9d90-422a-aa52-4f7622c63bea")); dataTypeService.Save(definitions.FirstOrDefault()); var propertyType = new PropertyType(definitions.FirstOrDefault()); propertyType.Alias = contentTypeProperty; contentType.AddPropertyType(propertyType); contentTypeService.Save(contentType); Assert.Greater(contentType.Id, 0); var content = new Content(name, -1, contentType); content.SetPropertyValue(contentTypeProperty, fieldValue); contentService.Save(content); var umbracoService = new UmbracoService(contentService, context); //Act var result = umbracoService.GetItem<Stub>(content.Id); //Assert Assert.IsNotNull(result); Assert.AreEqual(fieldValue, result.TestProperty); Assert.AreEqual(content.Id, result.Id); Assert.AreEqual(content.Key, result.Key); Assert.AreEqual(name, result.Name); Assert.AreEqual(contentTypeName, result.ContentTypeName); Assert.AreEqual(content.ParentId + "," + content.Id, result.Path); Assert.AreEqual(contentTypeAlias, result.ContentTypeAlias); Assert.AreEqual(content.Version, result.Version); Assert.AreEqual(content.CreateDate, result.CreateDate); Assert.AreEqual(content.UpdateDate, result.UpdateDate); Assert.AreEqual("admin", result.Creator); }