public void Should_Save_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);

                var cmsConfiguration = new Mock <ICmsConfiguration>();
                var optionService    = new DefaultOptionService(repository, new HttpRuntimeCacheService(), cmsConfiguration.Object);

                // Create layout with options
                var layout           = TestDataProvider.CreateNewLayout();
                layout.LayoutOptions = new List <LayoutOption>();

                var option1  = TestDataProvider.CreateNewLayoutOption(layout);
                option1.Type = OptionType.Text;
                layout.LayoutOptions.Add(option1);

                var option2  = TestDataProvider.CreateNewLayoutOption(layout);
                option2.Type = OptionType.Text;
                layout.LayoutOptions.Add(option2);

                var option3  = TestDataProvider.CreateNewLayoutOption(layout);
                option3.Type = OptionType.Text;
                layout.LayoutOptions.Add(option3);

                session.SaveOrUpdate(layout);
                session.Flush();
                session.Clear();

                // Create fake options:
                // 1 should be kept
                // 2 should be updated
                // 3 should be inserted
                // option2 should be deleted
                var newOption1 = new LayoutOption {
                    Key = option1.Key, Type = option1.Type, DefaultValue = option1.DefaultValue
                };
                var newOption2 = new LayoutOption {
                    Key = option2.Key, Type = option2.Type, DefaultValue = TestDataProvider.ProvideRandomString(100)
                };
                var newOption3 = new LayoutOption {
                    Key = TestDataProvider.ProvideRandomString(100), Type = OptionType.Text, DefaultValue = TestDataProvider.ProvideRandomString(100)
                };
                var newOptions = new List <IOptionEntity> {
                    newOption1, newOption2, newOption3
                };

                optionService.SetOptions <LayoutOption, Layout>(layout, newOptions);
                unitOfWork.Commit();

                // Load all options
                var options = repository.AsQueryable <LayoutOption>(lo => lo.Layout == layout).ToList();

                Assert.AreEqual(options.Count, 3);
                Assert.IsTrue(options.Any(o => o.Key == option1.Key && o.DefaultValue == option1.DefaultValue && o.Type == option1.Type));
                Assert.IsTrue(options.Any(o => o.Key == option2.Key && o.DefaultValue == newOption2.DefaultValue && o.Type == option2.Type));
                Assert.IsTrue(options.Any(o => o.Key == newOption3.Key && o.DefaultValue == newOption3.DefaultValue && o.Type == newOption3.Type));
            });
        }
Пример #2
0
        public void Should_Clone_Page_With_Tags_Options_Contents_AccessRules()
        {
            RunActionInTransaction(session =>
            {
                const string url = "/test-link";
                var uow          = new DefaultUnitOfWork(session);
                var repository   = new DefaultRepository(uow);


                var pageToClone = TestDataProvider.CreateNewPageWithTagsContentsOptionsAndAccessRules(session, 2, 2, 2, 2);

                session.SaveOrUpdate(pageToClone);
                session.Flush();
                session.Clear();

                var pageService = new Mock <IPageService>();
                pageService.Setup(f => f.ValidatePageUrl(It.IsAny <string>(), It.IsAny <Guid?>()));
                pageService.Setup(f => f.CreatePagePermalink(It.IsAny <string>(), It.IsAny <string>())).Returns(url);

                var urlService = new Mock <IUrlService>();
                urlService.Setup(f => f.FixUrl(It.IsAny <string>())).Returns(url);

                var rules = new List <IAccessRule>();
                var rule1 = TestDataProvider.CreateNewAccessRule();
                rules.Add(rule1);
                var rule2 = TestDataProvider.CreateNewAccessRule();
                rules.Add(rule2);

                var command                  = new ClonePageCommand();
                command.Repository           = repository;
                command.UnitOfWork           = uow;
                command.PageService          = pageService.Object;
                command.UrlService           = urlService.Object;
                command.AccessControlService = new Mock <IAccessControlService>().Object;
                command.Context              = new Mock <ICommandContext>().Object;

                var result = command.Execute(new ClonePageViewModel
                {
                    PageId         = pageToClone.Id,
                    PageTitle      = "new cloned page",
                    PageUrl        = url,
                    UserAccessList = pageToClone.AccessRules.Select(u => new UserAccessViewModel(u)).ToList()
                });

                Assert.IsNotNull(result);
                session.Clear();

                var actual = repository.AsQueryable <PageProperties>().Where(f => f.Id == result.PageId).ToList().FirstOrDefault();

                Assert.IsNotNull(actual);
                Assert.AreEqual(2, actual.AccessRules.Count(), "AccessRules");
                Assert.AreEqual(2, actual.PageTags.Count(), "Tags");
                Assert.AreEqual(2, actual.PageContents.Count(), "Contents");
                Assert.AreEqual(2, actual.Options.Count(), "Options");
            });
        }
Пример #3
0
        public void Should_Clone_Page_With_Tags_Options_Contents_AccessRules()
        {
            RunActionInTransaction(session =>
                {
                    const string url = "/test-link";
                    var uow = new DefaultUnitOfWork(session);
                    var repository = new DefaultRepository(uow);

                    var pageToClone = TestDataProvider.CreateNewPageWithTagsContentsOptionsAndAccessRules(session, 2, 2, 2, 2);
                    
                    session.SaveOrUpdate(pageToClone);
                    session.Flush();
                    session.Clear();
                    
                    var pageService = new Mock<IPageService>();
                    pageService.Setup(f => f.ValidatePageUrl(It.IsAny<string>(), It.IsAny<Guid?>()));
                    pageService.Setup(f => f.CreatePagePermalink(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<Guid>(), It.IsAny<Guid>(), It.IsAny<Guid>())).Returns(url);

                    var urlService = new Mock<IUrlService>();
                    urlService.Setup(f => f.FixUrl(It.IsAny<string>())).Returns(url);

                    var rules = new List<IAccessRule>();
                    var rule1 = TestDataProvider.CreateNewAccessRule();
                    rules.Add(rule1);
                    var rule2 = TestDataProvider.CreateNewAccessRule();
                    rules.Add(rule2);

                    var pageCloningService = new DefaultPageCloneService(pageService.Object, urlService.Object, 
                        new Mock<ISecurityService>().Object, new Mock<IAccessControlService>().Object, repository, uow, 
                        Container.Resolve<ICmsConfiguration>());

                    var command = new ClonePageCommand(pageCloningService);
                    command.Repository = repository;
                    command.UnitOfWork = uow;

                    var result = command.Execute(new ClonePageViewModel
                                        {
                                            PageId = pageToClone.Id,
                                            PageTitle = "new cloned page",
                                            PageUrl = url,
                                            UserAccessList = pageToClone.AccessRules.Select(u => new UserAccessViewModel(u)).ToList()
                                        });

                    Assert.IsNotNull(result);
                    session.Clear();

                    var actual = repository.AsQueryable<PageProperties>().Where(f => f.Id == result.PageId).ToList().FirstOrDefault();
                    
                    Assert.IsNotNull(actual);
                    Assert.AreEqual(2, actual.AccessRules.Count(), "AccessRules");
                    Assert.AreEqual(2, actual.PageTags.Count(), "Tags");
                    Assert.AreEqual(2, actual.PageContents.Count(), "Contents");
                    Assert.AreEqual(2, actual.Options.Count(), "Options");
                });
        }
        public void Should_Save_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);

                var cmsConfiguration = new Mock<ICmsConfiguration>();
                var optionService = new DefaultOptionService(repository, new HttpRuntimeCacheService(), cmsConfiguration.Object);

                // Create layout with options
                var layout = TestDataProvider.CreateNewLayout();
                layout.LayoutOptions = new List<LayoutOption>();

                var option1 = TestDataProvider.CreateNewLayoutOption(layout);
                option1.Type = OptionType.Text;
                layout.LayoutOptions.Add(option1);

                var option2 = TestDataProvider.CreateNewLayoutOption(layout);
                option2.Type = OptionType.Text;
                layout.LayoutOptions.Add(option2);

                var option3 = TestDataProvider.CreateNewLayoutOption(layout);
                option3.Type = OptionType.Text;
                layout.LayoutOptions.Add(option3);
                
                session.SaveOrUpdate(layout);
                session.Flush();
                session.Clear();
                
                // Create fake options:
                // 1 should be kept
                // 2 should be updated
                // 3 should be inserted
                // option2 should be deleted
                var newOption1 = new LayoutOption { Key = option1.Key, Type = option1.Type, DefaultValue = option1.DefaultValue };
                var newOption2 = new LayoutOption { Key = option2.Key, Type = option2.Type, DefaultValue = TestDataProvider.ProvideRandomString(100) };
                var newOption3 = new LayoutOption { Key = TestDataProvider.ProvideRandomString(100), Type = OptionType.Text, DefaultValue = TestDataProvider.ProvideRandomString(100) };
                var newOptions = new List<IOptionEntity> { newOption1, newOption2, newOption3 };

                optionService.SetOptions<LayoutOption, Layout>(layout, newOptions);
                unitOfWork.Commit();

                // Load all options
                var options = repository.AsQueryable<LayoutOption>(lo => lo.Layout == layout).ToList();

                Assert.AreEqual(options.Count, 3);
                Assert.IsTrue(options.Any(o => o.Key == option1.Key && o.DefaultValue == option1.DefaultValue && o.Type == option1.Type));
                Assert.IsTrue(options.Any(o => o.Key == option2.Key && o.DefaultValue == newOption2.DefaultValue && o.Type == option2.Type));
                Assert.IsTrue(options.Any(o => o.Key == newOption3.Key && o.DefaultValue == newOption3.DefaultValue && o.Type == newOption3.Type));
            });
        }
Пример #5
0
        public void Sould_Delete_Page_Successfully()
        {
            RunActionInTransaction(session =>
                {
                    const string url = "/test-link/";
                    var uow = new DefaultUnitOfWork(session);
                    var repository = new DefaultRepository(uow);
                    
                    var page = TestDataProvider.CreateNewPageWithTagsContentsOptionsAndAccessRules(session);
                    
                    session.SaveOrUpdate(page);
                    session.Flush();
                    session.Clear();
                    
                    var pageService = new Mock<IPageService>();
                    pageService.Setup(f => f.ValidatePageUrl(It.IsAny<string>(), It.IsAny<Guid?>()));
                    pageService.Setup(f => f.CreatePagePermalink(It.IsAny<string>(), It.IsAny<string>())).Returns(url);

                    var sitemapService = new Mock<ISitemapService>();
                    sitemapService
                        .Setup(service => service.GetNodesByPage(It.IsAny<PageProperties>()))
                        .Returns(() => new List<SitemapNode>());

                    var urlService = new Mock<IUrlService>();
                    urlService.Setup(f => f.FixUrl(It.IsAny<string>())).Returns((string a) => a);

                    var securityService = new Mock<ICmsSecurityConfiguration>();
                    securityService.Setup(f => f.AccessControlEnabled).Returns(false);

                    var configurationService = new Mock<ICmsConfiguration>();
                    configurationService.Setup(f => f.Security).Returns(securityService.Object);

                    var command = new DeletePageCommand(null, sitemapService.Object, urlService.Object, configurationService.Object);
                    command.Repository = repository;
                    command.UnitOfWork = uow;

                    var result = command.Execute(new DeletePageViewModel
                                        {
                                            PageId = page.Id,
                                            UpdateSitemap = false,
                                            RedirectUrl = null,
                                            SecurityWord = "DELETE",
                                            Version = page.Version
                                        });

                    Assert.IsTrue(result);
                    session.Clear();

                    var actual = repository.AsQueryable<PageProperties>().FirstOrDefault(f => f.Id == page.Id && !f.IsDeleted);                    
                    Assert.IsNull(actual);   
                });
        }
Пример #6
0
        public void Sould_Delete_Page_Successfully()
        {
            RunActionInTransaction(session =>
            {
                const string url = "/test-link/";
                var uow          = new DefaultUnitOfWork(session);
                var repository   = new DefaultRepository(uow);

                var page = TestDataProvider.CreateNewPageWithTagsContentsOptionsAndAccessRules(session);

                session.SaveOrUpdate(page);
                session.Flush();
                session.Clear();

                var pageService = new Mock <IPageService>();
                pageService.Setup(f => f.ValidatePageUrl(It.IsAny <string>(), It.IsAny <Guid?>()));
                pageService.Setup(f => f.CreatePagePermalink(It.IsAny <string>(), It.IsAny <string>())).Returns(url);

                var sitemapService = new Mock <ISitemapService>();
                sitemapService
                .Setup(service => service.GetNodesByPage(It.IsAny <PageProperties>()))
                .Returns(() => new List <SitemapNode>());

                var urlService = new Mock <IUrlService>();
                urlService.Setup(f => f.FixUrl(It.IsAny <string>())).Returns((string a) => a);

                var securityService = new Mock <ICmsSecurityConfiguration>();
                securityService.Setup(f => f.AccessControlEnabled).Returns(false);

                var configurationService = new Mock <ICmsConfiguration>();
                configurationService.Setup(f => f.Security).Returns(securityService.Object);

                var command        = new DeletePageCommand(null, sitemapService.Object, urlService.Object, configurationService.Object);
                command.Repository = repository;
                command.UnitOfWork = uow;

                var result = command.Execute(new DeletePageViewModel
                {
                    PageId        = page.Id,
                    UpdateSitemap = false,
                    RedirectUrl   = null,
                    SecurityWord  = "DELETE",
                    Version       = page.Version
                });

                Assert.IsTrue(result);
                session.Clear();

                var actual = repository.AsQueryable <PageProperties>().FirstOrDefault(f => f.Id == page.Id && !f.IsDeleted);
                Assert.IsNull(actual);
            });
        }
Пример #7
0
        public void Should_Call_AddUsersToRoles_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create fake roles and fake users with already assigned one role
                var fakeRoles = CreateFakeRoles(session, 4);
                var fakeUsers = CreateFakeUsers(session, new[] { fakeRoles[2] }, 4);

                // Add duplicated roles and usernames. Also pass role, which already is assigned to users
                var userNames = new[] { fakeUsers[0], fakeUsers[1], fakeUsers[1], fakeUsers[2] }.Select(u => u.UserName).ToArray();
                var roleNames = new[] { fakeRoles[0], fakeRoles[1], fakeRoles[1], fakeRoles[2] }.Select(u => u.Name).ToArray();

                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);

                var roleProvider = GetRoleProvider(session, repository, unitOfWork);
                roleProvider.AddUsersToRoles(userNames, roleNames);

                var user1Roles = repository
                    .AsQueryable<UserRole>(userRole => userRole.User.UserName == userNames[0])
                    .Select(userRole => userRole.Role.Name)
                    .ToArray();
                Assert.AreEqual(user1Roles.Length, 3);
                Assert.IsTrue(user1Roles.Contains(roleNames[0]));
                Assert.IsTrue(user1Roles.Contains(roleNames[1]));
                Assert.IsTrue(user1Roles.Contains(roleNames[2]));

                var user2Roles = repository
                    .AsQueryable<UserRole>(userRole => userRole.User.UserName == userNames[1])
                    .Select(userRole => userRole.Role.Name)
                    .ToArray();
                Assert.AreEqual(user2Roles.Length, 3);
                Assert.IsTrue(user2Roles.Contains(roleNames[0]));
                Assert.IsTrue(user2Roles.Contains(roleNames[1]));
                Assert.IsTrue(user2Roles.Contains(roleNames[2]));
            });
        }
Пример #8
0
        public void Should_Call_AddUsersToRoles_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create fake roles and fake users with already assigned one role
                var fakeRoles = CreateFakeRoles(session, 4);
                var fakeUsers = CreateFakeUsers(session, new[] { fakeRoles[2] }, 4);

                // Add duplicated roles and usernames. Also pass role, which already is assigned to users
                var userNames = new[] { fakeUsers[0], fakeUsers[1], fakeUsers[1], fakeUsers[2] }.Select(u => u.UserName).ToArray();
                var roleNames = new[] { fakeRoles[0], fakeRoles[1], fakeRoles[1], fakeRoles[2] }.Select(u => u.Name).ToArray();

                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);

                var roleProvider = GetRoleProvider(session, repository, unitOfWork);
                roleProvider.AddUsersToRoles(userNames, roleNames);

                var user1Roles = repository
                                 .AsQueryable <UserRole>(userRole => userRole.User.UserName == userNames[0])
                                 .Select(userRole => userRole.Role.Name)
                                 .ToArray();
                Assert.AreEqual(user1Roles.Length, 3);
                Assert.IsTrue(user1Roles.Contains(roleNames[0]));
                Assert.IsTrue(user1Roles.Contains(roleNames[1]));
                Assert.IsTrue(user1Roles.Contains(roleNames[2]));

                var user2Roles = repository
                                 .AsQueryable <UserRole>(userRole => userRole.User.UserName == userNames[1])
                                 .Select(userRole => userRole.Role.Name)
                                 .ToArray();
                Assert.AreEqual(user2Roles.Length, 3);
                Assert.IsTrue(user2Roles.Contains(roleNames[0]));
                Assert.IsTrue(user2Roles.Contains(roleNames[1]));
                Assert.IsTrue(user2Roles.Contains(roleNames[2]));
            });
        }
Пример #9
0
        public void Should_Call_CreateRole_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var unitOfWork   = new DefaultUnitOfWork(session);
                var repository   = new DefaultRepository(unitOfWork);
                var roleProvider = GetRoleProvider(session, repository, unitOfWork);

                var roleName = TestDataProvider.ProvideRandomString(MaxLength.Name);
                roleProvider.CreateRole(roleName);

                var roles = repository.AsQueryable <Role>(role => role.Name == roleName).ToList();

                Assert.NotNull(roles);
                Assert.AreEqual(roles.Count, 1);
                Assert.AreEqual(roles[0].Name, roleName);
            });
        }
Пример #10
0
        public void Should_Call_RemoveUsersFromRoles_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var fakeRoles = CreateFakeRoles(session, 3);
                var fakeUsers = CreateFakeUsers(session, fakeRoles, 3);

                var unitOfWork   = new DefaultUnitOfWork(session);
                var repository   = new DefaultRepository(unitOfWork);
                var roleProvider = GetRoleProvider(session, repository, unitOfWork);

                // Pass filtered user names and filtered roleNames
                var userNames = new[] { fakeUsers[0].UserName, fakeUsers[1].UserName };
                var roleNames = new[] { fakeRoles[0].Name, fakeRoles[1].Name };
                roleProvider.RemoveUsersFromRoles(userNames, roleNames);

                roleNames = fakeRoles.Select(role => role.Name).ToArray();
                userNames = fakeUsers.Select(user => user.UserName).ToArray();

                var userRoles = repository
                                .AsQueryable <UserRole>(userRole => userNames.Contains(userRole.User.UserName) || roleNames.Contains(userRole.Role.Name))
                                .Select(userRole => new
                {
                    UserName = userRole.User.UserName,
                    RoleName = userRole.Role.Name
                })
                                .ToList();

                Assert.NotNull(userRoles);

                // Should be left pairs:
                // 0 user -> 2 role
                // 1 user -> 2 role
                // 2 user -> 0 role
                // 2 user -> 1 role
                // 2 user -> 2 role
                Assert.AreEqual(userRoles.Count, 5);
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[0].UserName && ur.RoleName == fakeRoles[2].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[1].UserName && ur.RoleName == fakeRoles[2].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[0].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[1].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[2].Name));
            });
        }
Пример #11
0
        public void Should_Retrieve_AllField_If_Image_Is_Null()
        {
            RunActionInTransaction(
                session =>
                    {
                        var testAuthor = TestDataProvider.CreateNewAuthor();
                        testAuthor.Image = TestDataProvider.CreateNewMediaImage();
                        testAuthor.Image.IsDeleted = true;                        
                        session.SaveOrUpdate(testAuthor);
                        session.Flush();
                        session.Clear();

                        var repository = new DefaultRepository(new DefaultUnitOfWork(session));

                        var model = repository.AsQueryable<Author>()
                            .Where(f => !f.IsDeleted)
                            .Where(f => f.Id == testAuthor.Id)
                            .Select(author => new AuthorModel
                                          {
                                              Id = author.Id,
                                              Version = author.Version,
                                              CreatedBy = author.CreatedByUser,
                                              CreatedOn = author.CreatedOn,
                                              LastModifiedBy = author.ModifiedByUser,
                                              LastModifiedOn = author.ModifiedOn,

                                              Name = author.Name,

                                              ImageId = author.Image != null && !author.Image.IsDeleted ? author.Image.Id : (Guid?)null,
                                              ImageUrl = author.Image != null && !author.Image.IsDeleted ? author.Image.PublicUrl : (string)null,
                                              ImageThumbnailUrl = author.Image != null && !author.Image.IsDeleted ? author.Image.PublicThumbnailUrl : (string)null,
                                              ImageCaption = author.Image != null && !author.Image.IsDeleted ? author.Image.Caption : (string)null

                                          }).FirstOne();

                        Assert.IsNotNull(model);
                        Assert.AreEqual(testAuthor.Id, model.Id);
                        Assert.IsNull(model.ImageId);
                    });
        }
Пример #12
0
        public void Should_Retrieve_AllField_If_Image_Is_Null()
        {
            RunActionInTransaction(
                session =>
            {
                var testAuthor             = TestDataProvider.CreateNewAuthor();
                testAuthor.Image           = TestDataProvider.CreateNewMediaImage();
                testAuthor.Image.IsDeleted = true;
                session.SaveOrUpdate(testAuthor);
                session.Flush();
                session.Clear();

                var repository = new DefaultRepository(new DefaultUnitOfWork(session));

                var model = repository.AsQueryable <Author>()
                            .Where(f => !f.IsDeleted)
                            .Where(f => f.Id == testAuthor.Id)
                            .Select(author => new AuthorModel
                {
                    Id             = author.Id,
                    Version        = author.Version,
                    CreatedBy      = author.CreatedByUser,
                    CreatedOn      = author.CreatedOn,
                    LastModifiedBy = author.ModifiedByUser,
                    LastModifiedOn = author.ModifiedOn,

                    Name = author.Name,

                    ImageId           = author.Image != null && !author.Image.IsDeleted ? author.Image.Id : (Guid?)null,
                    ImageUrl          = author.Image != null && !author.Image.IsDeleted ? author.Image.PublicUrl : (string)null,
                    ImageThumbnailUrl = author.Image != null && !author.Image.IsDeleted ? author.Image.PublicThumbnailUrl : (string)null,
                    ImageCaption      = author.Image != null && !author.Image.IsDeleted ? author.Image.Caption : (string)null
                }).FirstOne();

                Assert.IsNotNull(model);
                Assert.AreEqual(testAuthor.Id, model.Id);
                Assert.IsNull(model.ImageId);
            });
        }
Пример #13
0
        public void Should_Save_Page_Content_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create page content with options
                var content     = TestDataProvider.CreateNewContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);
                FillContentWithOptions(content);
                FillPageContentWithOptions(content, pageContent);

                session.SaveOrUpdate(pageContent);
                session.Flush();
                session.Clear();

                // Random option value
                var randomOptionValue = TestDataProvider.CreateNewPageContentOption();

                // Create request
                var request = new PageContentOptionsViewModel
                {
                    WidgetOptions = new List <PageContentOptionViewModel>
                    {
                        new PageContentOptionViewModel
                        {
                            // Will be deleted because of default value
                            OptionValue        = content.ContentOptions[0].DefaultValue,
                            OptionKey          = pageContent.Options[0].Key,
                            OptionDefaultValue = content.ContentOptions[0].DefaultValue
                        },
                        new PageContentOptionViewModel
                        {
                            // Will be deleted because of null value
                            OptionValue = null,
                            OptionKey   = pageContent.Options[1].Key
                        },
                        new PageContentOptionViewModel
                        {
                            OptionValue = pageContent.Options[2].Value,
                            OptionKey   = pageContent.Options[2].Key
                        },
                        new PageContentOptionViewModel
                        {
                            // Random value
                            OptionValue = randomOptionValue.Value,
                            OptionKey   = randomOptionValue.Key
                        }
                    },
                    PageContentId = pageContent.Id
                };

                // Create command
                var unitOfWork     = new DefaultUnitOfWork(session);
                var repository     = new DefaultRepository(unitOfWork);
                var command        = new SavePageContentOptionsCommand();
                command.UnitOfWork = unitOfWork;
                command.Repository = repository;
                var result         = command.Execute(request);

                Assert.IsTrue(result);

                // Check results: one of page content values must be deleted after save
                var results = repository
                              .AsQueryable <PageContentOption>(pco => pco.PageContent.Id == pageContent.Id &&
                                                               !pco.IsDeleted &&
                                                               !pco.PageContent.IsDeleted)
                              .ToList();
                Assert.AreEqual(results.Count, 2);
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == pageContent.Options[2].Key &&
                                                        pco.Value == pageContent.Options[2].Value));
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == randomOptionValue.Key &&
                                                        pco.Value == randomOptionValue.Value));
            });
        }
        public void Should_Save_Page_Content_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create page content with options
                var content = TestDataProvider.CreateNewContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);
                FillContentWithOptions(content);
                FillPageContentWithOptions(content, pageContent);

                session.SaveOrUpdate(pageContent);
                session.Flush();
                session.Clear();

                // Random option value
                var randomOptionValue = TestDataProvider.CreateNewPageContentOption();

                // Create request
                var request = new PageContentOptionsViewModel
                    {
                        WidgetOptions = new List<PageContentOptionViewModel>
                            {
                                  new PageContentOptionViewModel
                                      {
                                          // Will be deleted because of default value
                                          OptionValue = content.ContentOptions[0].DefaultValue,
                                          OptionKey = pageContent.Options[0].Key,
                                          OptionDefaultValue = content.ContentOptions[0].DefaultValue
                                      },
                                  new PageContentOptionViewModel
                                      {
                                          // Will be deleted because of null value
                                          OptionValue = null,
                                          OptionKey = pageContent.Options[1].Key
                                      },
                                  new PageContentOptionViewModel
                                      {
                                          OptionValue = pageContent.Options[2].Value,
                                          OptionKey = pageContent.Options[2].Key
                                      },
                                  new PageContentOptionViewModel
                                      {
                                          // Random value
                                          OptionValue = randomOptionValue.Value,
                                          OptionKey = randomOptionValue.Key
                                      }

                            },
                        PageContentId = pageContent.Id
                    };

                // Create command
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);
                var command = new SavePageContentOptionsCommand();
                command.UnitOfWork = unitOfWork;
                command.Repository = repository;
                var result = command.Execute(request);

                Assert.IsTrue(result);

                // Check results: one of page content values must be deleted after save
                var results = repository
                    .AsQueryable<PageContentOption>(pco => pco.PageContent.Id == pageContent.Id
                        && !pco.IsDeleted
                        && !pco.PageContent.IsDeleted)
                    .ToList();
                Assert.AreEqual(results.Count, 2);
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == pageContent.Options[2].Key
                    && pco.Value == pageContent.Options[2].Value));
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == randomOptionValue.Key
                    && pco.Value == randomOptionValue.Value));
            });
        }
Пример #15
0
        public void Should_Save_Page_Content_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create page content with options
                var content     = TestDataProvider.CreateNewContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);
                FillContentWithOptions(content);
                FillPageContentWithOptions(content, pageContent);

                session.SaveOrUpdate(pageContent);
                session.Flush();
                session.Clear();

                // Random option value
                var randomOptionValue = TestDataProvider.CreateNewPageContentOption();

                // Create request
                var request = new PageContentOptionsViewModel
                {
                    OptionValues = new List <OptionValueEditViewModel>
                    {
                        new OptionValueEditViewModel
                        {
                            // Must be updated
                            OptionKey       = pageContent.Options[0].Key,
                            OptionValue     = content.ContentOptions[0].DefaultValue,
                            UseDefaultValue = false
                        },
                        new OptionValueEditViewModel
                        {
                            // Must be deleted
                            OptionKey       = pageContent.Options[1].Key,
                            OptionValue     = null,
                            UseDefaultValue = true
                        },
                        new OptionValueEditViewModel
                        {
                            // Must be updated
                            OptionKey       = pageContent.Options[2].Key,
                            OptionValue     = null,
                            UseDefaultValue = false
                        },
                        new OptionValueEditViewModel
                        {
                            // Must be created
                            OptionValue = randomOptionValue.Value,
                            OptionKey   = randomOptionValue.Key,
                            Type        = OptionType.Text
                        }
                    },
                    PageContentId = pageContent.Id
                };

                // Create command
                var unitOfWork        = new DefaultUnitOfWork(session);
                var repository        = new DefaultRepository(unitOfWork);
                var command           = new SavePageContentOptionsCommand();
                command.UnitOfWork    = unitOfWork;
                command.Repository    = repository;
                command.OptionService = new DefaultOptionService(repository, new HttpRuntimeCacheService());
                var result            = command.Execute(request);

                Assert.IsTrue(result);

                // Check results: one of page content values must be deleted after save
                var results = repository
                              .AsQueryable <PageContentOption>(pco => pco.PageContent.Id == pageContent.Id &&
                                                               !pco.IsDeleted &&
                                                               !pco.PageContent.IsDeleted)
                              .ToList();
                Assert.AreEqual(results.Count, 3);
                Assert.IsNotNull(results.FirstOrDefault(pco => {
                    return(pco.Key == pageContent.Options[0].Key && pco.Value == content.ContentOptions[0].DefaultValue);
                }));
                Assert.IsNotNull(results.FirstOrDefault(pco => {
                    return(pco.Key == pageContent.Options[2].Key && pco.Value == null);
                }));
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == randomOptionValue.Key &&
                                                        pco.Value == randomOptionValue.Value));
            });
        }
Пример #16
0
        public void Should_Call_RemoveUsersFromRoles_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var fakeRoles = CreateFakeRoles(session, 3);
                var fakeUsers = CreateFakeUsers(session, fakeRoles, 3);

                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);
                var roleProvider = GetRoleProvider(session, repository, unitOfWork);

                // Pass filtered user names and filtered roleNames
                var userNames = new[] { fakeUsers[0].UserName, fakeUsers[1].UserName };
                var roleNames = new[] { fakeRoles[0].Name, fakeRoles[1].Name };
                roleProvider.RemoveUsersFromRoles(userNames, roleNames);

                roleNames = fakeRoles.Select(role => role.Name).ToArray();
                userNames = fakeUsers.Select(user => user.UserName).ToArray();

                var userRoles = repository
                    .AsQueryable<UserRole>(userRole => userNames.Contains(userRole.User.UserName) || roleNames.Contains(userRole.Role.Name))
                    .Select(userRole => new
                        {
                            UserName = userRole.User.UserName,
                            RoleName = userRole.Role.Name
                        })
                    .ToList();

                Assert.NotNull(userRoles);

                // Should be left pairs:
                // 0 user -> 2 role
                // 1 user -> 2 role
                // 2 user -> 0 role
                // 2 user -> 1 role
                // 2 user -> 2 role
                Assert.AreEqual(userRoles.Count, 5);
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[0].UserName && ur.RoleName == fakeRoles[2].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[1].UserName && ur.RoleName == fakeRoles[2].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[0].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[1].Name));
                Assert.IsTrue(userRoles.Any(ur => ur.UserName == fakeUsers[2].UserName && ur.RoleName == fakeRoles[2].Name));
            });
        }
Пример #17
0
        public void Should_Call_CreateRole_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);
                var roleProvider = GetRoleProvider(session, repository, unitOfWork);

                var roleName = TestDataProvider.ProvideRandomString(MaxLength.Name);
                roleProvider.CreateRole(roleName);

                var roles = repository.AsQueryable<Role>(role => role.Name == roleName).ToList();
                
                Assert.NotNull(roles);
                Assert.AreEqual(roles.Count, 1);
                Assert.AreEqual(roles[0].Name, roleName);
            });
        }
        public void Should_Save_Page_Content_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                // Create page content with options
                var content = TestDataProvider.CreateNewContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);
                FillContentWithOptions(content);
                FillPageContentWithOptions(content, pageContent);

                session.SaveOrUpdate(pageContent);
                session.Flush();
                session.Clear();

                // Random option value
                var randomOptionValue = TestDataProvider.CreateNewPageContentOption();

                // Create request
                var request = new ContentOptionValuesViewModel
                    {
                        OptionValues = new List<OptionValueEditViewModel>
                            {
                                  new OptionValueEditViewModel
                                      {
                                          // Must be updated
                                          OptionKey = pageContent.Options[0].Key,
                                          OptionValue = content.ContentOptions[0].DefaultValue,
                                          UseDefaultValue = false
                                      },
                                  new OptionValueEditViewModel
                                      {
                                          // Must be deleted
                                          OptionKey = pageContent.Options[1].Key,
                                          OptionValue = null,
                                          UseDefaultValue = true
                                      },
                                  new OptionValueEditViewModel
                                      {
                                          // Must be updated
                                          OptionKey = pageContent.Options[2].Key,
                                          OptionValue = null,
                                          UseDefaultValue = false
                                      },
                                  new OptionValueEditViewModel
                                      {
                                          // Must be created
                                          OptionValue = randomOptionValue.Value,
                                          OptionKey = randomOptionValue.Key,
                                          Type = OptionType.Text
                                      }

                            },
                        OptionValuesContainerId = pageContent.Id
                    };

                // Create command
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);
                var command = new SavePageContentOptionsCommand();
                command.UnitOfWork = unitOfWork;
                command.Repository = repository;
                command.OptionService = new DefaultOptionService(repository, new HttpRuntimeCacheService());
                var result = command.Execute(request);

                Assert.IsNotNull(result);
                Assert.Greater(result.PageContentVersion, pageContent.Version);

                // Check results: one of page content values must be deleted after save
                var results = repository
                    .AsQueryable<PageContentOption>(pco => pco.PageContent.Id == pageContent.Id
                        && !pco.IsDeleted
                        && !pco.PageContent.IsDeleted)
                    .ToList();
                Assert.AreEqual(results.Count, 3);
                Assert.IsNotNull(results.FirstOrDefault(pco => {
                    return pco.Key == pageContent.Options[0].Key && pco.Value == content.ContentOptions[0].DefaultValue;
                }));
                Assert.IsNotNull(results.FirstOrDefault(pco => {
                    return pco.Key == pageContent.Options[2].Key && pco.Value == null;
                }));
                Assert.IsNotNull(results.FirstOrDefault(pco => pco.Key == randomOptionValue.Key
                    && pco.Value == randomOptionValue.Value));
            });
        }