Пример #1
0
        public void CreateForSection_UrlNameIsAlreadyUsedByAnotherPageNodeWithSameParentSectionNode_ThrowsException()
        {
            //arrange
            const string urlName      = "Foobar";
            var          pageTemplate = new PageTemplate {
                Id = 1
            };
            var sectionNode = new SectionNode
            {
                Id        = 1,
                PageNodes = new Collection <PageNode>
                {
                    new PageNode
                    {
                        UrlName = CmsUrlHelper.FormatUrlName(urlName)
                    }
                }
            };

            _dbContextMock.Setup(x => x.GetDbSet <SectionNode>()).Returns(new InMemoryDbSet <SectionNode>
            {
                sectionNode
            });

            //act + assert
            Assert.That(() => _pageNodeService.CreateForSection(sectionNode.Id, pageTemplate.Id, "Foobar", urlName),
                        Throws.Exception.TypeOf <ArgumentException>()
                        .With.Message.EqualTo(string.Format(Messages.PageNodeUrlNameMustBeUniqueWithinSection, CmsUrlHelper.FormatUrlName(urlName), sectionNode.Id)));
        }
Пример #2
0
        public void CreateForPage_UrlNameGetsFormatted()
        {
            //arrange
            var pageNodeForSection = new PageNode
            {
                Id           = 1,
                PageTemplate = new PageTemplate
                {
                    PageTemplates = new Collection <PageTemplate>
                    {
                        new PageTemplate {
                            Id = 44
                        }
                    }
                }
            };

            _dbContextMock.Setup(x => x.GetDbSet <PageNode>()).Returns(new InMemoryDbSet <PageNode>
            {
                pageNodeForSection
            });
            const string urlName = "Foo bar";

            //act
            var pageNode = _pageNodeService.CreateForPage(pageNodeForSection.Id, pageNodeForSection.PageTemplate.PageTemplates.First().Id, "Foobar", urlName);

            //assert
            pageNode.UrlName.Should().Be.EqualTo(CmsUrlHelper.FormatUrlName(urlName));
        }
Пример #3
0
        public PageNode CreateForSection(int sectionId, int pageTemplateId, string displayName, string urlName, bool commit = true)
        {
            Ensure.ArgumentIsNotNullOrEmpty(displayName, "displayName");
            Ensure.ArgumentIsNotNullOrEmpty(urlName, "urlName");

            var pageNodeDbSet = _unitOfWork.Context.GetDbSet <PageNode>();

            var parentSectionNode = _unitOfWork.Context.GetDbSet <SectionNode>()
                                    .Include(x => x.SectionTemplate.PageTemplates.Select(y => y.PropertyTemplates))
                                    .Include(x => x.PageNodes)
                                    .FirstOrDefault(x => x.Id == sectionId); //TODO: should use service instead of UnitOfWork directly, but issues with Ninject circular dependencies

            if (parentSectionNode == null)
            {
                throw new ArgumentException(string.Format(Messages.SectionNodeNotFound, sectionId));
            }

            var formattedUrlName = CmsUrlHelper.FormatUrlName(!string.IsNullOrEmpty(urlName) ? urlName : displayName);

            if (parentSectionNode.PageNodes != null && parentSectionNode.PageNodes.Any(s => s.UrlName == formattedUrlName))
            {
                throw new ArgumentException(string.Format(Messages.PageNodeUrlNameMustBeUniqueWithinSection, formattedUrlName, sectionId));
            }

            var pageTemplate = parentSectionNode.SectionTemplate.PageTemplates.FirstOrDefault(x => x.Id == pageTemplateId);         //TODO: should use service instead of UnitOfWork directly, but issues with Ninject circular dependencies

            if (pageTemplate == null)
            {
                throw new ArgumentException(string.Format(Messages.PageTemplateNotFoundForSectionTemplate, pageTemplateId, parentSectionNode.SectionTemplateId));
            }

            var pageNode = pageNodeDbSet.Create();

            pageNode.ParentSectionNode = parentSectionNode;
            pageNode.PageTemplate      = pageTemplate;
            pageNode.DisplayName       = displayName;
            pageNode.UrlName           = formattedUrlName;
            pageNode.Order             = parentSectionNode.PageNodes == null || !parentSectionNode.PageNodes.Any()
                                ? CmsConstants.FirstOrderNumber
                                : parentSectionNode.PageNodes.Count();
            pageNode.IsPublished = false;
            pageNode.CreatedOn   = DateTime.UtcNow;
            pageNode.ModifiedOn  = pageNode.CreatedOn;

            pageNodeDbSet.Add(pageNode);

            AddPropertiesForPageTemplatesPropertyTemplates(pageTemplate, pageNode);

            if (commit)
            {
                _unitOfWork.Commit();
            }

            return(pageNode);
        }
Пример #4
0
        public void FormatUrlName_ConvertsToLowercase()
        {
            //arrange
            const string urlName = "FooBar";

            //act
            var formattedUrlName = CmsUrlHelper.FormatUrlName(urlName);

            //assert
            formattedUrlName.Should().Be.EqualTo(urlName.ToLower());
        }
Пример #5
0
        public void FormatUrlName_RemovesAllSpaces()
        {
            //arrange
            const string urlName = "Foo Bar";

            //act
            var formattedUrlName = CmsUrlHelper.FormatUrlName(urlName);

            //assert
            formattedUrlName.Should().Be.EqualTo(urlName.Replace(" ", "").ToLower());
        }
Пример #6
0
        public PageNode CreateForPage(int pageId, int pageTemplateId, string displayName, string urlName)
        {
            Ensure.ArgumentIsNotNullOrEmpty(displayName, "displayName");
            Ensure.ArgumentIsNotNullOrEmpty(urlName, "urlName");

            var pageNodeDbSet = _unitOfWork.Context.GetDbSet <PageNode>();
            var pageNodes     = pageNodeDbSet
                                .Include(x => x.PageNodes)
                                .Include(x => x.PageTemplate.PageTemplates);

            var parentPage = pageNodes.FirstOrDefault(x => x.Id == pageId);

            if (parentPage == null)
            {
                throw new ArgumentException(string.Format(Messages.PageNodeNotFound, pageId));
            }

            var pageTemplate = parentPage.PageTemplate.PageTemplates.FirstOrDefault(x => x.Id == pageTemplateId);

            if (pageTemplate == null)
            {
                throw new ArgumentException(string.Format(Messages.PageTemplateNotFoundForPageTemplate, pageTemplateId, parentPage.PageTemplateId));
            }

            var formattedUrlName = CmsUrlHelper.FormatUrlName(!string.IsNullOrEmpty(urlName) ? urlName : displayName);

            if (parentPage.PageNodes != null && parentPage.PageNodes.Any(s => s.UrlName == formattedUrlName))
            {
                throw new ArgumentException(string.Format(Messages.PageNodeUrlNameMustBeUniqueWithinPage, formattedUrlName, pageId));
            }

            var pageNode = pageNodeDbSet.Create();

            pageNode.ParentPageNode = parentPage;
            pageNode.PageTemplate   = pageTemplate;
            pageNode.DisplayName    = displayName;
            pageNode.UrlName        = formattedUrlName;
            pageNode.Order          = parentPage.PageNodes == null || !parentPage.PageNodes.Any()
                                ? CmsConstants.FirstOrderNumber
                                : parentPage.PageNodes.Count();
            pageNode.IsPublished = false;
            pageNode.CreatedOn   = DateTime.UtcNow;
            pageNode.ModifiedOn  = pageNode.CreatedOn;

            pageNodeDbSet.Add(pageNode);

            AddPropertiesForPageTemplatesPropertyTemplates(pageTemplate, pageNode);

            _unitOfWork.Commit();
            return(pageNode);
        }
Пример #7
0
        public void Create_UrlNameGetsFormatted()
        {
            //arrange
            const string urlName          = "Foobar";
            var          formattedUrlName = CmsUrlHelper.FormatUrlName(urlName);
            var          sectionTemplate  = new SectionTemplate {
                Id = 1
            };

            _dbContextMock.Setup(x => x.GetDbSet <SectionNode>()).Returns(new InMemoryDbSet <SectionNode>());
            _sectionTemplateServiceMock.Setup(x => x.GetById(It.IsAny <int>())).Returns(sectionTemplate);

            //act
            var sectionNode = _sectionNodeService.CreateSectionNode(sectionTemplate.Id, "Foobar", urlName);

            //assert
            sectionNode.UrlName.Should().Be.EqualTo(formattedUrlName);
        }
Пример #8
0
        public SectionNode CreateSectionNode(int sectionTemplateId, string displayName, string urlName, bool commit = true)
        {
            Ensure.ArgumentIsNotNullOrEmpty(displayName, "displayName");
            Ensure.ArgumentIsNotNullOrEmpty(urlName, "urlName");

            var sectionNodeDbSet    = _unitOfWork.Context.GetDbSet <SectionNode>();
            var orderedSectionNodes = sectionNodeDbSet.OrderBy(x => x.Order);

            var formattedUrlName = CmsUrlHelper.FormatUrlName(!string.IsNullOrEmpty(urlName) ? urlName : displayName);

            if (sectionNodeDbSet.Any(s => s.UrlName == formattedUrlName))
            {
                throw new ArgumentException(string.Format(Messages.SectionNodeUrlNameMustBeUnique, formattedUrlName));
            }

            var sectionTemplate = _sectionTemplateService.GetById(sectionTemplateId);

            if (sectionTemplate == null)
            {
                throw new ArgumentException(string.Format(Messages.SectionTemplateNotFound, sectionTemplateId));
            }

            var section = sectionNodeDbSet.Create();

            section.SectionTemplateId = sectionTemplate.Id;
            section.DisplayName       = displayName;
            section.UrlName           = formattedUrlName;
            section.Order             = !orderedSectionNodes.Any() ? CmsConstants.FirstOrderNumber : orderedSectionNodes.Count();
            section.IsPublished       = false;
            section.CreatedOn         = DateTime.UtcNow;
            section.ModifiedOn        = section.CreatedOn;
            section.InTrash           = false;

            sectionNodeDbSet.Add(section);

            if (commit)
            {
                _unitOfWork.Commit();
            }

            return(section);
        }
Пример #9
0
        public void Create_UrlNameIsAlreadyUsedByAnotherSectionNode_ThrowsException()
        {
            //arrange
            var sectionTemplate = new SectionTemplate {
                Id = 1
            };
            const string urlName          = "Foobar";
            var          formattedUrlName = CmsUrlHelper.FormatUrlName(urlName);

            _dbContextMock.Setup(x => x.GetDbSet <SectionNode>()).Returns(new InMemoryDbSet <SectionNode>
            {
                new SectionNode
                {
                    UrlName = formattedUrlName
                }
            });
            _sectionTemplateServiceMock.Setup(x => x.GetById(It.IsAny <int>())).Returns(sectionTemplate);


            //act + assert
            Assert.That(() => _sectionNodeService.CreateSectionNode(sectionTemplate.Id, "Foobar", urlName),
                        Throws.Exception.TypeOf <ArgumentException>()
                        .With.Message.EqualTo(string.Format(Messages.SectionNodeUrlNameMustBeUnique, formattedUrlName)));
        }