public void MapToProperty_GetUrlInItemLanguage_ReturnsUrlWithFr()
        {
            //Assign
            var    type     = SitecoreInfoType.Url;
            string expected = "/fr-FR/sitecore/content/TestItem.aspx";

            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();

            config.Type       = type;
            config.UrlOptions = SitecoreInfoUrlOptions.UseItemLanguage;
            mapper.Setup(new DataMapperResolverArgs(null, config));

            Sitecore.Context.Site = null;
            var templateId = ID.NewID;
            var itemId     = new ID("031501A9C7F24596BD659276DA3A627A");
            var options    = new GetItemOptionsParams();

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem", itemId)
                {
                    Fields =
                    {
                        new DbField("title")
                        {
                            { "fr-fr", 1, "test" }
                        }
                    },
                }
            })
            {
                var item = database.GetItem("/sitecore/content/TestItem", "fr-fr");
                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null, options);

                //Act
                var value = mapper.MapToProperty(dataContext);

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
        public void MapToProperty_SitecoreInfoTypeBaseTemplateIds_ReturnsBaseTemplateIds()
        {
            //Assign
            ID templateId1 = ID.NewID;
            ID templateId2 = ID.NewID;

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbTemplate("TestTemplate1", templateId1),
                new Sitecore.FakeDb.DbTemplate("TestTemplate2", templateId2)
                {
                    new FakeDbField(new ID("{12C33F3F-86C5-43A5-AEB4-5598CEC45116}"), templateId1.ToString())
                },
                new Sitecore.FakeDb.DbItem("TestItem", ID.NewID, templateId2)
            })
            {
                var type = SitecoreInfoType.BaseTemplateIds;

                var mapper  = new SitecoreInfoMapper();
                var config  = new SitecoreInfoConfiguration();
                var options = new GetItemOptionsParams();
                config.Type         = type;
                config.PropertyInfo = typeof(Stub).GetProperty("BaseTemplateIds");

                mapper.Setup(new DataMapperResolverArgs(null, config));

                var item = database.GetItem("/sitecore/Content/TestItem");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null, options);

                //Act
                IEnumerable <ID> results;
                using (new SecurityDisabler())
                {
                    results = mapper.MapToProperty(dataContext) as IEnumerable <ID>;
                }

                //Assert
                Assert.Greater(results.Count(), 1);
                Assert.IsTrue(results.All(x => x != item.TemplateID));
            }
        }
Пример #3
0
        public void MapToProperty_AbsoluteQuery_ReturnsResults()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                    new Sitecore.FakeDb.DbItem("Child1"),
                    new Sitecore.FakeDb.DbItem("Child2")
                }
            })
            {
                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");

                config.Query      = "/sitecore/content/Target/*";
                config.IsRelative = false;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(null);
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source  = database.GetItem("/sitecore/content/Target");
                var service = new SitecoreService(database.Database, context);

                var result1 = database.GetItem("/sitecore/content/Target/Child1");
                var result2 = database.GetItem("/sitecore/content/Target/Child2");
                var options = new GetItemOptionsParams();

                //Act
                var results =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service, options)) as
                    IEnumerable <StubMapped>;

                //Assert
                Assert.AreEqual(2, results.Count());
                Assert.IsTrue(results.Any(x => x.Id == result1.ID.Guid));
                Assert.IsTrue(results.Any(x => x.Id == result2.ID.Guid));
            }
        }
Пример #4
0
        public void MapToProperty_GetItemByIdDifferentLanguage_ReturnsItem()
        {
            //Assign
            var config   = new SitecoreNodeConfiguration();
            var context  = Context.Create(FakeDb.Utilities.CreateStandardResolver());
            var mapper   = new SitecoreNodeMapper();
            var language = LanguageManager.GetLanguage("af-ZA");

            context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

            mapper.Setup(new DataMapperResolverArgs(context, config));

            var obj = new Stub();

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Source"),
                new Sitecore.FakeDb.DbItem("Target")
            })
            {
                var source   = database.Database.GetItem("/sitecore/content/Source", language);
                var target   = database.Database.GetItem("/sitecore/content/Target", language);
                var service  = Substitute.For <ISitecoreService>();
                var expected = new StubMapped();
                var options  = new GetItemOptionsParams();

                config.PropertyInfo = typeof(Stub).GetProperty("StubMapped");
                config.Id           = target.ID.Guid.ToString();

                service.GetItem(Arg.Is <GetItemByItemOptions>(x => x.Item.Uri == target.Uri)).Returns(expected);



                var mappingContext = new SitecoreDataMappingContext(obj, source, service, options);

                //Act
                var result = mapper.MapToProperty(mappingContext);

                //Assert
                Assert.AreEqual(expected, result);
            }
        }
Пример #5
0
        public void MapToCms_SavingName_UpdatesTheItemName()
        {
            //Assign

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
            })
            {
                var type     = SitecoreInfoType.Name;
                var expected = "new  name";

                var mapper  = new SitecoreInfoMapper();
                var config  = new SitecoreInfoConfiguration();
                var options = new GetItemOptionsParams();
                config.Type = type;
                mapper.Setup(new DataMapperResolverArgs(null, config));

                var item = database.GetItem("/sitecore/Content/TestItem");

                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");


                var dataContext = new SitecoreDataMappingContext(null, item, null, options);
                dataContext.PropertyValue = expected;

                string actual = string.Empty;

                //Act
                using (new SecurityDisabler())
                {
                    item.Editing.BeginEdit();
                    mapper.MapToCms(dataContext);
                    actual = item.Name;
                    item.Editing.CancelEdit();
                }

                //Assert
                Assert.AreEqual(expected, actual);
            }
        }
Пример #6
0
        public void MapToProperty_GetItemByPathIsLazyIntegrationTest_ReturnsItem()
        {
            //Assign
            var config   = new SitecoreNodeConfiguration();
            var context  = Context.Create(FakeDb.Utilities.CreateStandardResolver());
            var mapper   = new SitecoreNodeMapper();
            var language = LanguageManager.GetLanguage("en");

            context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

            mapper.Setup(new DataMapperResolverArgs(context, config));

            var obj = new Stub();

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Source"),
                new Sitecore.FakeDb.DbItem("Target")
            })
            {
                var source   = database.Database.GetItem("/sitecore/content/Source", language);
                var target   = database.Database.GetItem("/sitecore/content/Target", language);
                var service  = new SitecoreService(database.Database);
                var expected = new StubMapped();
                var options  = new GetItemOptionsParams
                {
                    Lazy = LazyLoading.Enabled
                };

                config.PropertyInfo = typeof(Stub).GetProperty("StubMapped");
                config.Path         = "/sitecore/content/Target";

                var mappingContext = new SitecoreDataMappingContext(obj, source, service, options);

                //Act
                var result = mapper.MapToProperty(mappingContext);

                //Assert
                Assert.True(result is StubMapped);
            }
        }
Пример #7
0
        public void MapToProperty_EnforceTemplate_ReturnsParentItem()
        {
            //Assign

            ID templateId = ID.NewID;
            ID parentID   = ID.NewID;

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Parent", parentID, templateId)
                {
                    new Sitecore.FakeDb.DbItem("TestItem")
                }
            })
            {
                var item    = database.GetItem("/sitecore/content/Parent/TestItem");
                var context = Context.Create(Utilities.CreateStandardResolver());
                var service = new SitecoreService(database.Database, context);
                var options = new GetItemOptionsParams();
                options.Lazy = LazyLoading.Enabled;
                var scContext = new SitecoreDataMappingContext(null, item, service, options);

                var config = new SitecoreParentConfiguration();
                config.PropertyInfo    = typeof(Stub).GetProperty("Property");
                config.TemplateId      = templateId;
                config.EnforceTemplate = SitecoreEnforceTemplate.TemplateAndBase;

                var mapper = new SitecoreParentMapper();
                mapper.Setup(new DataMapperResolverArgs(null, config));

                //Act
                var result = mapper.MapToProperty(scContext) as Stub;

                //Assert
                Assert.NotNull(result);
                Assert.AreEqual(parentID, result.Id);
            }
        }
Пример #8
0
        public void MapToProperty_RelativeQuerySelf_ReturnsSelf()
        {
            //Assign
            //Assign

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                    new Sitecore.FakeDb.DbItem("Child1"),
                    new Sitecore.FakeDb.DbItem("Child2")
                }
            })
            {
                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMapped");

                config.Query      = "ancestor-or-self::*";
                config.IsRelative = true;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(null);
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source  = database.GetItem("/sitecore/content/Target");
                var service = new SitecoreService(database.Database, context);
                var options = new GetItemOptionsParams();

                //Act
                var result =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service, options)) as StubMapped;

                //Assert
                Assert.AreEqual(source.ID.Guid, result.Id);
            }
        }
Пример #9
0
        public void SetFieldValue_NullPassed_ReturnsNull()
        {
            //Assign
            using (Db database = new Db
            {
                new DbItem("Target")
            })
            {
                var item      = database.GetItem("/sitecore/content/Target");
                var mapper    = new SitecoreFieldItemMapper();
                var config    = new SitecoreFieldConfiguration();
                var context   = Context.Create(Utilities.CreateStandardResolver());
                var service   = new SitecoreService(database.Database, context);
                var options   = new GetItemOptionsParams();
                var scContext = new SitecoreDataMappingContext(null, item, service, options);

                //Act
                var result = mapper.SetFieldValue(null, config, scContext);

                //Assert
                Assert.Null(result);
            }
        }
Пример #10
0
        public void MapToProperty_MapsItemToProperty()
        {
            //Arrange

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
            })
            {
                var item    = database.GetItem("/sitecore/content/TestItem");
                var mapper  = new SitecoreItemMapper();
                var obj     = new StubClass();
                var options = new GetItemOptionsParams();

                var mappingContext = new SitecoreDataMappingContext(obj, item, null, options);

                //Act
                var result = mapper.MapToProperty(mappingContext);

                //Assign
                Assert.AreEqual(item, result);
            }
        }
Пример #11
0
        public void MapToProperty_ItemHasNoChildren_NoObjectsCreated()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem")
                {
                }
            })
            {
                var item    = database.GetItem("/sitecore/content/TestItem");
                var mapper  = new SitecoreChildrenMapper();
                var options = new GetItemOptionsParams();

                var config = new SitecoreChildrenConfiguration();
                config.InferType    = false;
                config.PropertyInfo = typeof(Stub).GetProperty("Children");

                var scContext = Context.Create(Utilities.CreateStandardResolver());

                var service = new SitecoreService(database.Database, scContext);

                service.Config = new Config();

                var context = new SitecoreDataMappingContext(null, item, service, options);

                mapper.Setup(new DataMapperResolverArgs(null, config));

                //Act
                var result = mapper.MapToProperty(context) as IEnumerable <StubChild>;

                //Assert

                Assert.AreEqual(0, result.Count());
            }
        }
Пример #12
0
        public void MapToProperty_GetItemByPath_ReturnsItem()
        {
            //Assign
            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Source"),
                new Sitecore.FakeDb.DbItem("Target")
            })
            {
                var config   = new SitecoreNodeConfiguration();
                var mapper   = new SitecoreNodeMapper();
                var language = LanguageManager.GetLanguage("en");

                mapper.Setup(new DataMapperResolverArgs(null, config));

                var obj      = new Stub();
                var source   = database.Database.GetItem("/sitecore/content/Source", language);
                var target   = database.Database.GetItem("/sitecore/content/Target", language);
                var service  = Substitute.For <ISitecoreService>();
                var expected = new StubMapped();
                var options  = new GetItemOptionsParams();

                config.PropertyInfo = typeof(Stub).GetProperty("StubMapped");
                config.Path         = "/sitecore/content/Target";

                service.GetItem(Arg.Is <GetItemByItemOptions>(x => x.Item.Uri == target.Uri)).Returns(expected);

                var mappingContext = new SitecoreDataMappingContext(obj, source, service, options);

                //Act
                var result = mapper.MapToProperty(mappingContext);

                //Assert
                Assert.AreEqual(expected, result);
            }
        }
Пример #13
0
        public void MapToProperty_MediaUrlWithFlag_ReturnsModifiedUrl(
            SitecoreInfoMediaUrlOptions option

            )
        {
            //Assign
            var mapper = new SitecoreInfoMapper();
            var config = new SitecoreInfoConfiguration();

            config.Type            = SitecoreInfoType.MediaUrl;
            config.MediaUrlOptions = option;
            mapper.Setup(new DataMapperResolverArgs(null, config));
            var    itemId   = new ID("031501A9C7F24596BD659276DA3A627A");
            string expected = "media url";

            Sitecore.Context.Site = null;

            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("TestItem", itemId)
                {
                    Fields =
                    {
                        new FakeDbField(new ID("{B5E02AD9-D56F-4C41-A065-A133DB87BDEB}"),
                                        "DataMappersEmptyItem DisplayName")
                    },
                }
            })
            {
                Func <MediaUrlOptions, bool> pred = x =>
                {
                    switch (option)
                    {
                    case SitecoreInfoMediaUrlOptions.Default:
                        return(true);

                    case SitecoreInfoMediaUrlOptions.RemoveExtension:
                        return(x.IncludeExtension == false);

                    case SitecoreInfoMediaUrlOptions.LowercaseUrls:
                        return(x.LowercaseUrls == true);

                    default:
                        return(false);
                    }
                };


#if SC90 || SC91 || SC92
                var mediaUrlProvider = Substitute.For <BaseMediaManager>();

                SitecoreVersionAbstractions.MediaManager = new LazyResetable <BaseMediaManager>(() => mediaUrlProvider);

                mediaUrlProvider
                .GetMediaUrl(
                    Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == itemId),
                    Arg.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);
#else
                Sitecore.Resources.Media.MediaProvider mediaProvider =
                    Substitute.For <Sitecore.Resources.Media.MediaProvider>();
                mediaProvider
                .GetMediaUrl(
                    Arg.Is <Sitecore.Data.Items.MediaItem>(i => i.ID == itemId),
                    Arg.Is <MediaUrlOptions>(x => pred(x))
                    )
                .Returns(expected);

                new Sitecore.FakeDb.Resources.Media.MediaProviderSwitcher(mediaProvider);
#endif

                var options = new GetItemOptionsParams();

                var item = database.GetItem("/sitecore/content/TestItem");
                Assert.IsNotNull(item, "Item is null, check in Sitecore that item exists");
                var dataContext = new SitecoreDataMappingContext(null, item, null, options);

                //Act
                var value = mapper.MapToProperty(dataContext);

                //Assert
                Assert.AreEqual(expected, value);
            }
        }
Пример #14
0
        public void MapToProperty_AbsoluteQueryMultiLanguages_ReturnsResults()
        {
            //Assign

            var templateId = ID.NewID;


            using (Db database = new Db
            {
                new Sitecore.FakeDb.DbItem("Target")
                {
                    new DbTemplate(templateId)
                    {
                        { "title", "" }
                    },
                    new Sitecore.FakeDb.DbItem("Child1", new ID(Guid.NewGuid()), templateId)
                    {
                        Fields =
                        {
                            new DbField("title")
                            {
                                { "en", "test en" },
                                { "da", "test"    }
                            }
                        }
                    },
                    new Sitecore.FakeDb.DbItem("Child2", new ID(Guid.NewGuid()), templateId)
                    {
                        Fields =
                        {
                            new DbField("title")
                            {
                                { "en", "test en" },
                                { "da", "test1"   }
                            }
                        },
                    }
                }
            })
            {
                var config = new SitecoreQueryConfiguration();
                config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");

                config.Query      = "/sitecore/content/Target/Child2";
                config.IsRelative = false;

                var context = Context.Create(Utilities.CreateStandardResolver());
                context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

                var mapper = new SitecoreQueryMapper(null);
                mapper.Setup(new DataMapperResolverArgs(context, config));

                var source  = database.GetItem("/sitecore/content/Target/child1", "da");
                var service = new SitecoreService(database.Database, context);

                var options = new GetItemOptionsParams();

                //Act
                var results =
                    mapper.MapToProperty(new SitecoreDataMappingContext(null, source, service, options)) as
                    IEnumerable <StubMapped>;

                //Assert
                Assert.AreEqual(1, results.Count());
                Assert.AreEqual("test1", results.First().Title);
            }
        }
Пример #15
0
 protected AbstractGetParamsBuilder(GetItemOptionsParams options)
     : base(options)
 {
     _options = options;
 }
Пример #16
0
        public void MapToProperty_GetAll_ReferrersListReturned()
        {
            //Assign

            var templateId = ID.NewID;
            var fieldId    = ID.NewID;
            var targetId   = ID.NewID;
            var language   = LanguageManager.GetLanguage("af-ZA");

            using (Db database = new Db
            {
                new DbTemplate(templateId),
                new Sitecore.FakeDb.DbItem("Target", targetId, templateId),
                new Sitecore.FakeDb.DbItem("Source", ID.NewID, templateId)
                {
                    new DbField("Field", fieldId)
                    {
                        { language.Name, 1, targetId.ToString() }
                    }
                }
            })
            {
                var target   = database.GetItem("/sitecore/content/Target");
                var source   = database.Database.GetItem("/sitecore/content/Source", language);
                var template = database.Database.GetTemplate(templateId);

                var behavior = Substitute.For <Sitecore.Links.LinkDatabase>();

                behavior.GetReferences(target).Returns(new[]
                {
                    new Sitecore.Links.ItemLink(target, fieldId, template, template.InnerItem.Paths.FullPath),
                });
                behavior.GetReferrers(target).Returns(new[]
                {
                    new Sitecore.Links.ItemLink(source, fieldId, target, target.Paths.FullPath),
                });


                using (new Sitecore.FakeDb.Links.LinkDatabaseSwitcher(behavior))
                {
                    //ME - when getting templates you have to disable the role manager
                    using (new SecurityDisabler())
                    {
                        var config = new SitecoreLinkedConfiguration();
                        config.PropertyInfo = typeof(StubClass).GetProperty("StubMappeds");
                        config.Option       = SitecoreLinkedOptions.All;

                        var context = Context.Create(Utilities.CreateStandardResolver());
                        context.Load(new OnDemandLoader <SitecoreTypeConfiguration>(typeof(StubMapped)));

                        var mapper = new SitecoreLinkedMapper();
                        mapper.Setup(new DataMapperResolverArgs(context, config));

                        var service = new SitecoreService(database.Database, context);
                        var options = new GetItemOptionsParams();

                        //Act
                        var raw =
                            mapper.MapToProperty(new SitecoreDataMappingContext(null, target, service, options));
                        var result = raw as IEnumerable <StubMapped>;

                        //Assert
                        Assert.AreEqual(2, result.Count());
                        Assert.AreEqual(template.ID.Guid, result.First().Id);
                        Assert.AreEqual(source.ID.Guid, result.Skip(1).First().Id);
                        Assert.AreEqual(source.Language, result.Skip(1).First().Language);
                    }
                }
            }
        }