public void Should_Delete_ServerControlWidget_Successfully()
        {
            var serverControlWidget = TestDataProvider.CreateNewServerControlWidget();

            RunActionInTransaction(session =>
                {
                    session.SaveOrUpdate(serverControlWidget);
                    session.Flush();
                    session.Clear();

                    var uow = new DefaultUnitOfWork(session);
                    var repository = new DefaultRepository(uow);
                    var optionService = new Mock<IOptionService>().Object;
                    var contentService = new Mock<IContentService>().Object;
                    var childContentService = new Mock<IChildContentService>().Object;
                    var categoryService = new Mock<ICategoryService>();
                    var cmsConfiguration = new Mock<ICmsConfiguration>().Object;
                    var widgetService = new DefaultWidgetService(repository, uow, optionService, contentService, childContentService, categoryService.Object, cmsConfiguration);

                    var command = new DeleteWidgetCommand(widgetService);

                    bool success = command.Execute(new DeleteWidgetRequest
                                                       {
                                                           WidgetId = serverControlWidget.Id,
                                                           Version = serverControlWidget.Version
                                                       });
                    Assert.IsTrue(success);
                });
        }
        public void Should_ThrowValidationException_AboutNonDeletableOption()
        {
            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;
                option1.IsDeletable = false;
                layout.LayoutOptions.Add(option1);

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

                optionService.SetOptions<LayoutOption, Layout>(layout, new List<IOptionEntity>());
                unitOfWork.Commit();
            });
        }
        public void Should_Delete_Page_Content()
        {
            RunActionInTransaction(session =>
            {
                // Create content
                var content = TestDataProvider.CreateNewHtmlContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);

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

                // Delete page content
                var request = new DeletePageContentCommandRequest
                                  {
                                      PageContentId = pageContent.Id,
                                      PageContentVersion = pageContent.Version,
                                      ContentVersion = content.Version
                                  };
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);
                var securityService = new Mock<ISecurityService>().Object;
                var optionService = new Mock<IOptionService>().Object;
                var childContentService = new Mock<IChildContentService>().Object;

                var contentService = new DefaultContentService(securityService, repository, optionService, childContentService);
                var command = new DeletePageContentCommand(contentService);
                command.UnitOfWork = unitOfWork;
                command.Repository = repository;

                var result = command.Execute(request);
                Assert.IsTrue(result);
            });
        }
        public void Should_Return_Widgets_List_Successfully()
        {
            RunActionInTransaction(
                session =>
                    {
                        ServerControlWidget control1 = TestDataProvider.CreateNewServerControlWidget();
                        HtmlContentWidget control2 = TestDataProvider.CreateNewHtmlContentWidget();

                        control1.Id = Guid.NewGuid();
                        control1.Name = Guid.NewGuid().ToString().Replace("-", string.Empty);

                        session.SaveOrUpdate(control1);
                        session.SaveOrUpdate(control2);
                        session.Flush();

                        var unitOfWork = new DefaultUnitOfWork(session);
                        var repository = new DefaultRepository(unitOfWork);
                        var categoryService = new Mock<ICategoryService>();
                        var cmsConfiguration = new Mock<ICmsConfiguration>().Object;
                        var widgetService = new DefaultWidgetService(repository, unitOfWork, null, null, null, categoryService.Object, cmsConfiguration);
                        var command = new GetSiteSettingsWidgetsCommand(widgetService);

                        var response = command.Execute(new WidgetsFilter { SearchQuery = control1.Name.Substring(1, control1.Name.Length - 1) });

                        Assert.IsNotNull(response);
                        Assert.IsNotNull(response.Items);
                        Assert.GreaterOrEqual(response.Items.Count(), 1);

                        var widget = response.Items.FirstOrDefault(w => control1.Id == w.Id);
                        Assert.IsNotNull(widget);
                        Assert.AreEqual(control1.Name, widget.WidgetName);
                    });
        }
 public void Should_Create_UoW_With_SessionFactoryprovider_Successfully()
 {
     using (var unitOfWork = new DefaultUnitOfWork(Container.Resolve<ISessionFactoryProvider>()))
     {
         Assert.IsNotNull(unitOfWork.Session);
     }
 }
 public void Should_Create_Transaction_Successfuly()
 {
     using (var unitOfWork = new DefaultUnitOfWork(Container.Resolve<ISessionFactoryProvider>()))
     {
         Assert.IsFalse(unitOfWork.IsActiveTransaction);
         unitOfWork.BeginTransaction();
         Assert.IsTrue(unitOfWork.IsActiveTransaction);
     }
 }
Esempio n. 7
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<IEnumerable<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, Container.Resolve<ICmsConfiguration>());
                    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_Return_Page_Content_Options_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var content = TestDataProvider.CreateNewContent();
                var pageContent = TestDataProvider.CreateNewPageContent(content);

                FillContentWithOptions(content);
                FillPageContentWithOptions(content, pageContent);

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

                // Create command
                var unitOfWork = new DefaultUnitOfWork(session);
                var command = new GetPageContentOptionsCommand();
                var repository = new DefaultRepository(unitOfWork);
                command.UnitOfWork = unitOfWork;
                command.Repository = repository;
                command.CmsConfiguration = Container.Resolve<ICmsConfiguration>();
                command.OptionService = new DefaultOptionService(repository, new HttpRuntimeCacheService(), command.CmsConfiguration);

                // Execute command
                var result = command.Execute(pageContent.Id);

                // Should return 4 options: 2 with assigned values, 1 without parent option and 1 unassigned
                Assert.IsNotNull(result);
                Assert.IsNotNull(result.OptionValues);
                Assert.AreEqual(result.OptionValues.Count, 5);
                Assert.IsNotNull(result.OptionValues.FirstOrDefault(o => o.OptionKey == content.ContentOptions[0].Key
                    && o.OptionValue == pageContent.Options[0].Value
                    && o.OptionDefaultValue == content.ContentOptions[0].DefaultValue
                    && !o.UseDefaultValue
                    && !o.CanEditOption));
                Assert.IsNotNull(result.OptionValues.FirstOrDefault(o => o.OptionKey == content.ContentOptions[1].Key
                    && o.OptionValue == pageContent.Options[1].Value
                    && o.OptionDefaultValue == content.ContentOptions[1].DefaultValue
                    && !o.UseDefaultValue
                    && !o.CanEditOption));
                Assert.IsNotNull(result.OptionValues.FirstOrDefault(o => o.OptionKey == content.ContentOptions[2].Key
                    && o.OptionValue == content.ContentOptions[2].DefaultValue
                    && o.OptionDefaultValue == content.ContentOptions[2].DefaultValue
                    && o.UseDefaultValue
                    && !o.CanEditOption));
                Assert.IsNotNull(result.OptionValues.FirstOrDefault(o => o.OptionKey == pageContent.Options[2].Key
                    && o.OptionValue == pageContent.Options[2].Value
                    && o.OptionDefaultValue == null
                    && o.CanEditOption));
                Assert.IsNotNull(result.OptionValues.FirstOrDefault(o => o.OptionKey == pageContent.Options[3].Key
                    && o.OptionValue == pageContent.Options[3].Value
                    && o.OptionDefaultValue == null
                    && o.CanEditOption));
            });
        }
        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));
            });
        }
        public void Should_Return_Empty_List()
        {
            RunActionInTransaction(
                session =>
                    {
                        var unitOfWork = new DefaultUnitOfWork(session);
                        var repository = new DefaultRepository(unitOfWork);
                        var categoryService = new Mock<ICategoryService>();
                        var cmsConfiguration = new Mock<ICmsConfiguration>().Object;
                        var widgetService = new DefaultWidgetService(repository, unitOfWork, null, null, null, categoryService.Object, cmsConfiguration);
                        var command = new GetSiteSettingsWidgetsCommand(widgetService);

                        var response = command.Execute(new WidgetsFilter { SearchQuery = Guid.NewGuid().ToString() });

                        Assert.IsNotNull(response);
                        Assert.IsNotNull(response.Items);
                        Assert.IsEmpty(response.Items);
                    });
        }
        public void ShouldRetrieveFirstChildPageOptionsSuccessfully()
        {
            RunActionInTransaction(session =>
            {
                var unitOfWork = new DefaultUnitOfWork(session);
                var repository = new DefaultRepository(unitOfWork);

                var pages = CreateNestedOptions(session, 2);
                var cmsConfiguration = new Mock<ICmsConfiguration>();
                var optionService = new DefaultOptionService(repository, new HttpRuntimeCacheService(), cmsConfiguration.Object);
                var optionValues = optionService.GetMergedMasterPagesOptionValues(pages[1].Id, pages[1].MasterPage.Id, null);

                Assert.AreEqual(optionValues.Count, 5);
                Assert.IsTrue(optionValues.Any(o => o.OptionKey == "l1" && o.OptionValue == "l1"));
                Assert.IsTrue(optionValues.Any(o => o.OptionKey == "l2" && o.OptionValue == "l2p2"));
                Assert.IsTrue(optionValues.Any(o => o.OptionKey == "l3" && o.OptionValue == "l3p2"));
                Assert.IsTrue(optionValues.Any(o => o.OptionKey == "p1" && o.OptionValue == "p1"));
                Assert.IsTrue(optionValues.Any(o => o.OptionKey == "p2" && o.OptionValue == "p2"));
            });
        }
        public void Should_Retrieve_Widgets_From_Database_Paged_And_Sorted_By_WidgetName()
        {
            RunActionInTransaction(session =>
                {
                    var widgets = new Widget[]
                                      {
                                         TestDataProvider.CreateNewServerControlWidget(),
                                         TestDataProvider.CreateNewServerControlWidget(),
                                         TestDataProvider.CreateNewHtmlContentWidget()
                                      };
                    int i = 0;
                    foreach (var widget in widgets)
                    {
                        widget.Name = "test name " + i++;
                        session.SaveOrUpdate(widget);
                    }
                    session.Flush();
                    session.Clear();

                    var unitOfWork = new DefaultUnitOfWork(session);
                    var repository = new DefaultRepository(unitOfWork);
                    var categoryService = new Mock<ICategoryService>();
                    var cmsConfiguration = new Mock<ICmsConfiguration>().Object;
                    var widgetService = new DefaultWidgetService(repository, unitOfWork, null, null, null, categoryService.Object, cmsConfiguration);
                    var command = new GetSiteSettingsWidgetsCommand(widgetService);

                    var result = command.Execute(new WidgetsFilter
                                                     {
                                                         PageSize = 20,
                                                         Column = "WidgetName",
                                                         Direction = SortDirection.Ascending,
                                                         PageNumber = 1,
                                                         SearchQuery = "test name 2"
                                                     });

                    Assert.IsNotNull(result);
                    Assert.AreEqual(1, result.Items.TotalItems);
                    Assert.AreEqual(widgets[2].Name, result.Items.First().WidgetName);
                });
        }
Esempio n. 13
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]));
            });
        }
        public void Should_Delete_Media_With_Access_Rules()
        {
            RunActionInTransaction(session =>
                {
                    var uow = new DefaultUnitOfWork(session);
                    var repository = new DefaultRepository(uow);
                    var accessControlService = new Mock<IAccessControlService>().Object;
                    var commandContext = new Mock<ICommandContext>().Object;
                    var cmsConfiguration = Container.Resolve<ICmsConfiguration>();
                    var httpContextAccessor = new Mock<IHttpContextAccessor>();
                    httpContextAccessor.SetReturnsDefault("http://wwww.bcms.com/uploads/trash");

                    var file = TestDataProvider.CreateNewMediaFileWithAccessRules(3);
                    session.SaveOrUpdate(file);
                    session.Flush();
                    session.Clear();

                    var mediaService = new DefaultMediaService(repository, uow, accessControlService, cmsConfiguration);
                    var command = new DeleteMediaCommand(mediaService);
                    command.Repository = repository;
                    command.UnitOfWork = uow;
                    command.Context = commandContext;

                    var result = command.Execute(new DeleteMediaCommandRequest
                                        {
                                            Id = file.Id,
                                            Version = file.Version
                                        });

                    Assert.IsTrue(result);

                    session.Clear();

                    var deletedFile = session.Query<MediaFile>().FirstOrDefault(f => f.Id == file.Id && !f.IsDeleted);
                    Assert.IsNull(deletedFile);
                });
        }
        private CmsRoleProvider GetRoleProvider(ISession session, IRepository repository = null, IUnitOfWork unitOfWork = null)
        {
            if (repository == null || unitOfWork == null)
            {
                unitOfWork = new DefaultUnitOfWork(session);
                repository = new DefaultRepository(unitOfWork);
            }
            var roleService = new DefaultRoleService(repository);

            var roleProvider = new CmsRoleProvider(repository, unitOfWork, roleService);

            return roleProvider;
        }
        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));
            });
        }
        public void Should_Call_CreateRole_WithException_ExistingRole()
        {
            RunActionInTransaction(session =>
            {
                // Create role
                var role = CreateFakeRoles(session, 1)[0];

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

                // Try create existing role
                roleProvider.CreateRole(role.Name);
            });
        }
        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);
            });
        }
Esempio n. 19
0
        public void Should_Create_New_Page_With_Access_Rules()
        {
            RunActionInTransaction(
                session =>
                    {
                        var tempLayout = TestDataProvider.CreateNewLayout();
                        session.SaveOrUpdate(tempLayout);
                        session.Flush();

                        var uow = new DefaultUnitOfWork(session);
                        var repository = new DefaultRepository(uow);
                        var configMock = new Mock<ICmsConfiguration>();
                        configMock.SetupAllProperties().Setup(f => f.Security.AccessControlEnabled).Returns(true);
                        configMock.Setup(f => f.Security.DefaultAccessRules).Returns(new AccessControlCollection
                                                                                         {
                                                                                             DefaultAccessLevel = "readwrite"
                                                                                         });
                        var config = configMock.Object;

                        var command = new CreatePageCommand(
                            new Mock<IPageService>().SetupAllProperties().Object,
                            new DefaultUrlService(uow, config),
                            config,
                            new Mock<IOptionService>().SetupAllProperties().Object,
                            new Mock<IMasterPageService>().SetupAllProperties().Object);

                        command.UnitOfWork = uow;
                        command.Repository = repository;
                        command.AccessControlService = new DefaultAccessControlService(Container.Resolve<ISecurityService>(), new HttpRuntimeCacheService(), config, repository);

                        var contextMock = new Mock<ICommandContext>();
                        contextMock.Setup(c => c.Principal).Returns(new GenericPrincipal(new GenericIdentity("John Doe"), new[] { RootModuleConstants.UserRoles.EditContent }));
                        command.Context = contextMock.Object;

                        var request = new AddNewPageViewModel();
                        request.AccessControlEnabled = true;
                        request.PageTitle = "TestCreatePageCommand";
                        request.PageUrl = "/test-CreatePageCommand/";
                        request.TemplateId = tempLayout.Id;

                        request.UserAccessList = new List<UserAccessViewModel>(
                            new[]
                                {
                                    new UserAccessViewModel
                                        {
                                            Identity = "test 1",
                                            AccessLevel = AccessLevel.ReadWrite
                                        },

                                    new UserAccessViewModel
                                        {
                                            Identity = "test 2",
                                            AccessLevel = AccessLevel.Deny
                                        }
                                });

                        var response = command.Execute(request);
                        session.Clear();

                        Assert.IsNotNull(response);

                        var page = session.Query<Page>().FirstOrDefault(f => f.Id == response.PageId);
                        Assert.IsNotNull(page);
                        Assert.IsNotNull(page.AccessRules);
                        Assert.AreEqual(2, page.AccessRules.Count());

                    });
        }
Esempio n. 20
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 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 redirectService = new Mock<IRedirectService>();

                    var accessControlService = new Mock<IAccessControlService>();

                    var pageService = new DefaultPageService(repository,
                        redirectService.Object,
                        urlService.Object,
                        accessControlService.Object,
                        configurationService.Object,
                        sitemapService.Object,
                        uow);

                    var command = new DeletePageCommand(pageService);
                    command.Repository = repository;
                    command.UnitOfWork = uow;
                    command.Context = new Mock<ICommandContext>().Object;

                    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);
                });
        }
        public void Should_Sort_Page_Content_Successfully()
        {
            RunActionInTransaction(session =>
            {
                var layout = TestDataProvider.CreateNewLayout();
                var region = TestDataProvider.CreateNewRegion();

                layout.LayoutRegions = new List<LayoutRegion>
                    {
                        TestDataProvider.CreateNewLayoutRegion(layout, region),
                    };

                var page = TestDataProvider.CreateNewPageProperties(layout);
                page.PageContents = new[]
                    {
                        TestDataProvider.CreateNewPageContent(TestDataProvider.CreateNewHtmlContent(), page, region),
                        TestDataProvider.CreateNewPageContent(TestDataProvider.CreateNewHtmlContent(), page, region),
                        TestDataProvider.CreateNewPageContent(TestDataProvider.CreateNewHtmlContent(), page, region)
                    };

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

                IUnitOfWork unitOfWork = new DefaultUnitOfWork(session);

                var configuration = Container.Resolve<ICmsConfiguration>();
                var command = new SortPageContentCommand(configuration);
                command.UnitOfWork = unitOfWork;
                command.Repository = new DefaultRepository(unitOfWork);
                command.Context = new Mock<ICommandContext>().Object;

                var accessControlService = new Mock<IAccessControlService>();
                accessControlService.Setup(s => s.DemandAccess(It.IsAny<IPrincipal>(), It.IsAny<string>()));
                command.AccessControlService = accessControlService.Object;

                var request = new PageContentSortViewModel
                    {
                        PageId = page.Id,
                        PageContents =
                            new List<ContentSortViewModel>
                                {
                                    new ContentSortViewModel { RegionId = region.Id, PageContentId = page.PageContents[2].Id, Version = page.PageContents[2].Version },
                                    new ContentSortViewModel { RegionId = region.Id, PageContentId = page.PageContents[1].Id, Version = page.PageContents[1].Version },
                                    new ContentSortViewModel { RegionId = region.Id, PageContentId = page.PageContents[0].Id, Version = page.PageContents[0].Version },
                                }
                    };
                var response = command.Execute(request);

                Assert.IsNotNull(response);
                Assert.IsNotNull(response.PageContents);
                Assert.AreEqual(response.PageContents.Count, request.PageContents.Count);

                session.Flush();
                session.Clear();

                var updatedPage = command.Repository.AsQueryable<PageContent>(f => f.Page.Id == page.Id).ToList();

                Assert.AreEqual(2, updatedPage.FirstOrDefault(f => f.Id == page.PageContents[0].Id).Order);
                Assert.AreEqual(1, updatedPage.FirstOrDefault(f => f.Id == page.PageContents[1].Id).Order);
                Assert.AreEqual(0, updatedPage.FirstOrDefault(f => f.Id == page.PageContents[2].Id).Order);
            });
        }
        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(), Container.Resolve<ICmsConfiguration>());
                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));
            });
        }
 public void Should_Throw_Exception_Creating_Multiple_Transactions()
 {
     using (var unitOfWork = new DefaultUnitOfWork(Container.Resolve<ISessionFactoryProvider>()))
     {
         unitOfWork.BeginTransaction();
         unitOfWork.BeginTransaction();
     }
 }