コード例 #1
0
            public void Execute_03_NoCartLines(
                IRuleValue <string> targetTag,
                Cart cart,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                CartItemTargetTagFreeGiftAction action,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.Clear();

                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);

                action.TargetTag = targetTag;

                context.Fact(Arg.Any <IFactIdentifier>()).Returns(commerceContext);

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #2
0
            public void Execute_13_NoMaximumApplications(
                CartAnyItemQuantityXForQuantityYAction action,
                IRuleValue <int> quantityX,
                IRuleValue <int> quantityY,
                //IRuleValue<int> maximumApplications,
                Cart cart,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.QuantityX           = quantityX;
                action.QuantityY           = quantityY;
                action.MaximumApplications = null;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().Throw <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #3
0
            public void Execute_08_NoAmount(
                BaseCartItemSubtotalAmountOffAction action,
                IBinaryOperator <decimal, decimal> subtotalOperator,
                IRuleValue <decimal> subtotal,
                Cart cart,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.MatchingLines(context).ReturnsForAnyArgs(cart.Lines);
                action.SubtotalOperator = subtotalOperator;
                action.Subtotal         = subtotal;
                action.AmountOff        = null;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #4
0
            public void Execute_ShouldRoundPriceCalc_True(
                BaseCartItemSubtotalAmountOffAction action,
                IRuleValue <decimal> subtotal,
                IRuleValue <decimal> amountOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>();

                globalPricingPolicy.ShouldRoundPriceCalc         = true;
                globalPricingPolicy.RoundDigits                  = 3;
                globalPricingPolicy.MidPointRoundUp              = true;
                cart.Lines.ForEach(l => l.Totals.SubTotal.Amount = 100);
                var cartline = cart.Lines[1];

                cartline.Totals.SubTotal.Amount = 175;
                var cartTotals = new CartTotals(cart);

                subtotal.Yield(context).ReturnsForAnyArgs(150);
                amountOff.Yield(context).ReturnsForAnyArgs(25.555555M);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.MatchingLines(context).ReturnsForAnyArgs(cart.Lines);
                action.SubtotalOperator = new DecimalGreaterThanEqualToOperator();
                action.Subtotal         = subtotal;
                action.AmountOff        = amountOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(1);
                cart.Adjustments.Should().BeEmpty();
                cartTotals.Lines[cartline.Id].SubTotal.Amount.Should().Be(149.444M);
                cartline.Adjustments.Should().NotBeEmpty();
                cartline.Adjustments.FirstOrDefault().Should().NotBeNull();
                cartline.Adjustments.FirstOrDefault().Should().BeOfType <CartLineLevelAwardedAdjustment>();
                cartline.Adjustments.FirstOrDefault()?.Name.Should().Be(commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount);
                cartline.Adjustments.FirstOrDefault()?.DisplayName.Should().Be(commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount);
                cartline.Adjustments.FirstOrDefault()?.AdjustmentType.Should().Be(commerceContext.GetPolicy <KnownCartAdjustmentTypesPolicy>().Discount);
                cartline.Adjustments.FirstOrDefault()?.AwardingBlock.Should().Contain(nameof(BaseCartItemSubtotalAmountOffAction));
                cartline.Adjustments.FirstOrDefault()?.IsTaxable.Should().BeFalse();
                cartline.Adjustments.FirstOrDefault()?.Adjustment.CurrencyCode.Should().Be(commerceContext.CurrentCurrency());
                cartline.Adjustments.FirstOrDefault()?.Adjustment.Amount.Should().Be(-25.556M);
                cartline.HasComponent <MessagesComponent>().Should().BeTrue();
            }
コード例 #5
0
            public void Execute_TimesQualified_Two_MaximumApplications_One(
                CartAnyItemQuantityXForQuantityYAction action,
                IRuleValue <int> quantityX,
                IRuleValue <int> quantityY,
                IRuleValue <int> maximumApplications,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>();

                globalPricingPolicy.ShouldRoundPriceCalc         = false;
                cart.Lines.ForEach(l => l.Totals.SubTotal.Amount = 100);
                while (cart.Lines.Count > 1)
                {
                    cart.Lines.RemoveAt(0);
                }
                var cartline = cart.Lines[0];

                cartline.Quantity = 8;
                cartline.Totals.SubTotal.Amount = 200;
                cartline.SetPolicy(new PurchaseOptionMoneyPolicy
                {
                    SellPrice = new Money(25)
                });
                var cartTotals = new CartTotals(cart);

                quantityX.Yield(context).ReturnsForAnyArgs(4);
                quantityY.Yield(context).ReturnsForAnyArgs(3);
                maximumApplications.Yield(context).ReturnsForAnyArgs(1);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.QuantityX           = quantityX;
                action.QuantityY           = quantityY;
                action.MaximumApplications = maximumApplications;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(1);
                cart.Adjustments.Should().BeEmpty();
                cartTotals.Lines[cartline.Id].SubTotal.Amount.Should().Be(175);
            }
コード例 #6
0
            public void Execute_PercentOff(
                CartItemQuantityRangePercentOffAction action,
                IRuleValue <string> targetItemId,
                IRuleValue <decimal> minQuantity,
                IRuleValue <decimal> maxQuantity,
                IRuleValue <decimal> percentOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>();

                globalPricingPolicy.ShouldRoundPriceCalc         = true;
                cart.Lines.ForEach(l => l.Totals.SubTotal.Amount = 100);
                targetItemId.Yield(context).Returns("Habitat_Master|12345|");
                var index = 12345;

                cart.Lines.ForEach(line =>
                {
                    line.ItemId   = $"Habitat_Master|{index++}|";
                    line.Quantity = 2;
                    line.Totals.SubTotal.Amount = 150;
                    line.SetPolicy(new PurchaseOptionMoneyPolicy());
                });
                var cartTotals = new CartTotals(cart);

                minQuantity.Yield(context).ReturnsForAnyArgs(2);
                maxQuantity.Yield(context).ReturnsForAnyArgs(3);
                percentOff.Yield(context).ReturnsForAnyArgs(10);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.TargetItemId = targetItemId;
                action.MinQuantity  = minQuantity;
                action.MaxQuantity  = maxQuantity;
                action.PercentOff   = percentOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(1);
                cart.Adjustments.Should().BeEmpty();
                cartTotals.Lines[cart.Lines[0].Id].SubTotal.Amount.Should().Be(135);
            }
            public void Execute_12_NoPurchaseOptionMoneyPolicy(
                CartItemQuantityXForQuantityYAction action,
                IRuleValue <string> targetItemId,
                IRuleValue <int> quantityX,
                IRuleValue <int> quantityY,
                IRuleValue <int> maximumApplications,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>();

                globalPricingPolicy.ShouldRoundPriceCalc         = false;
                cart.Lines.ForEach(l => l.Totals.SubTotal.Amount = 100);
                while (cart.Lines.Count > 1)
                {
                    cart.Lines.RemoveAt(0);
                }
                var cartline = cart.Lines[0];

                cartline.Quantity = 4;
                cartline.Totals.SubTotal.Amount = 100;
                cartline.ItemId = "Habitat_Master|12345|";
                var cartTotals = new CartTotals(cart);

                targetItemId.Yield(context).ReturnsForAnyArgs("Habitat_Master|12345|");
                quantityX.Yield(context).ReturnsForAnyArgs(4);
                quantityY.Yield(context).ReturnsForAnyArgs(3);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.TargetItemId        = targetItemId;
                action.QuantityX           = quantityX;
                action.QuantityY           = quantityY;
                action.MaximumApplications = maximumApplications;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(0);
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #8
0
        public async Task <IEnumerable <CatalogContextModel> > Process(CommerceContext commerceContext, IEnumerable <string> catalogNameList, bool useCache = true)
        {
            using (CommandActivity.Start(commerceContext, this))
            {
                if (catalogNameList == null || catalogNameList.Count().Equals(0))
                {
                    throw new Exception($"{nameof(GetCatalogContextCommand)} no catalogs provided");
                }

                if (useCache == false)
                {
                    commerceContext.RemoveObjects <CatalogContextModel>();
                }

                var allCatalogs = commerceContext.GetObject <IEnumerable <Sitecore.Commerce.Plugin.Catalog.Catalog> >();
                if (allCatalogs == null)
                {
                    allCatalogs = await Command <GetCatalogsCommand>().Process(commerceContext);

                    commerceContext.AddObject(allCatalogs);
                }

                var catalogCategoryModelList = new List <CatalogContextModel>();
                foreach (var catalogName in catalogNameList)
                {
                    var model = commerceContext.GetObject <CatalogContextModel>(m => m.CatalogName.Equals(catalogName));
                    if (model == null)
                    {
                        var catalog       = allCatalogs.FirstOrDefault(c => c.Name.Equals(catalogName));
                        var allCategories = (await Command <GetCategoriesCommand>().Process(commerceContext, catalogName)).ToList();
                        if (allCategories == null)
                        {
                            allCategories = new List <Category>();
                        }

                        allCategories = allCategories.Where(c => c.Id.CatalogNameFromCategoryId().Equals(catalogName)).ToList();

                        model = new CatalogContextModel
                        {
                            CatalogName            = catalog.Name,
                            Catalog                = catalog,
                            CategoriesByName       = allCategories.ToDictionary(c => c.Name),
                            CategoriesBySitecoreId = allCategories.ToDictionary(c => c.SitecoreId)
                        };

                        commerceContext.AddObject(model);
                    }

                    catalogCategoryModelList.Add(model);
                }

                return(catalogCategoryModelList);
            }
        }
コード例 #9
0
            public async void Execute_NoMatchingLinesAutoAddToCartEnabled_ShouldAddToCartAndApplyCartLineAdjustment(
                string targetItemId,
                bool autoAddToCart,
                bool autoRemove,
                Cart cart,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                ApplyFreeGiftDiscountCommand    discountCommand;
                ApplyFreeGiftEligibilityCommand eligibilityCommand;
                ApplyFreeGiftAutoRemoveCommand  autoRemoveCommand;
                AddCartLineCommand             addCartLineCommand;
                CartItemTargetIdFreeGiftAction action = BuildAction(out discountCommand, out eligibilityCommand, out autoRemoveCommand, out addCartLineCommand);

                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);

                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                action.TargetItemId = Substitute.For <IRuleValue <string> >();
                action.TargetItemId.Yield(context).ReturnsForAnyArgs(targetItemId);

                action.AutoAddToCart = Substitute.For <IRuleValue <bool> >();
                action.AutoAddToCart.Yield(context).ReturnsForAnyArgs(autoAddToCart);

                action.AutoRemove = Substitute.For <IRuleValue <bool> >();
                action.AutoRemove.Yield(context).ReturnsForAnyArgs(autoRemove);

                context.Fact(Arg.Any <IFactIdentifier>()).Returns(commerceContext);

                addCartLineCommand
                .WhenForAnyArgs(x => x.Process(Arg.Any <CommerceContext>(), cart, Arg.Any <CartLineComponent>()))
                .Do(x => cart.Lines[0].ItemId = targetItemId);

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                await addCartLineCommand.Received().Process(Arg.Any <CommerceContext>(), cart, Arg.Any <CartLineComponent>());

                eligibilityCommand.Received().Process(Arg.Any <CommerceContext>(), cart, Arg.Any <string>());
                discountCommand.Received().Process(Arg.Any <CommerceContext>(), Arg.Any <CartLineComponent>(), Arg.Any <string>());
                autoRemoveCommand.Received().Process(Arg.Any <CommerceContext>(), Arg.Any <CartLineComponent>(), autoRemove);
            }
コード例 #10
0
            public void Evaluate_03_NoCartLines(
                IRuleValue <string> fulfillmentOptionName,
                CommerceContext commerceContext,
                Cart cart,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Lines.Clear();
                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>());
                var condition = new CartHasFulfillmentOptionCondition(commander);

                condition.FulfillmentOptionName = fulfillmentOptionName;

                /**********************************************
                * Act
                **********************************************/
                var result = condition.Evaluate(context);

                /**********************************************
                * Assert
                **********************************************/
                result.Should().BeFalse();
            }
コード例 #11
0
            public void Execute_02_NoCart(
                CartItemQuantityRangePercentOffAction action,
                IRuleValue <string> targetItemId,
                IRuleValue <decimal> minQuantity,
                IRuleValue <decimal> maxQuantity,
                IRuleValue <decimal> percentOff,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                action.TargetItemId = targetItemId;
                action.MinQuantity  = minQuantity;
                action.MaxQuantity  = maxQuantity;
                action.PercentOff   = percentOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
            }
コード例 #12
0
            public void Evaluate_05_NoFulfillmentOptionName(
                CommerceContext commerceContext,
                Cart cart,
                IRuleExecutionContext context,
                FulfillmentComponent fulfillmentComponent)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.SetComponent(fulfillmentComponent);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>());
                var condition = new CartHasFulfillmentOptionCondition(commander);

                condition.FulfillmentOptionName = null;

                /**********************************************
                * Act
                **********************************************/
                Action evaluateCondition = () => condition.Evaluate(context);

                /**********************************************
                * Assert
                **********************************************/
                evaluateCondition.Should().Throw <Exception>();
            }
コード例 #13
0
            public void Execute_04_NoCartTotals(
                CartItemQuantityRangePercentOffAction action,
                IRuleValue <string> targetItemId,
                IRuleValue <decimal> minQuantity,
                IRuleValue <decimal> maxQuantity,
                IRuleValue <decimal> percentOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);
                action.TargetItemId = targetItemId;
                action.MinQuantity  = minQuantity;
                action.MaxQuantity  = maxQuantity;
                action.PercentOff   = percentOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
            public void Execute_12_NoFulfillmentFee(
                IRuleValue <string> fulfillmentOptionName,
                IRuleValue <decimal> amountOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context,
                string fulfillmentOptionNameValue,
                FulfillmentComponent fulfillmentComponent)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());
                cart.SetComponent(fulfillmentComponent);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue);
                amountOff.Yield(context).ReturnsForAnyArgs(MINIMUM_AMOUNT_OFF);

                var commander = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>());
                var command   = Substitute.For <GetFulfillmentMethodsCommand>(
                    Substitute.For <IFindEntityPipeline>(),
                    Substitute.For <IGetCartFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetFulfillmentMethodsPipeline>(),
                    Substitute.For <IServiceProvider>());

                var fulfillmentMethod = new FulfillmentMethod()
                {
                    FulfillmentType = fulfillmentOptionNameValue
                };

                command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>()
                {
                    fulfillmentMethod
                }.AsEnumerable());
                commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase();
                commander.Command <GetFulfillmentMethodsCommand>().Returns(command);
                var action = new CartShippingOptionAmountOffAction(commander)
                {
                    FulfillmentOptionName = fulfillmentOptionName,
                    AmountOff             = amountOff
                };

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                command.Received().Process(commerceContext);
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #15
0
        public void YieldCartLines_04_NoLineItemComponent(
            IRuleValue <string> targetCategorySitecoreId,
            Cart cart,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            commerceContext.AddObject(cart);
            targetCategorySitecoreId.Yield(context).ReturnsForAnyArgs("c2bfaf91-7825-4846-0ad3-0479cdf7b607");;

            /**********************************************
            * Act
            **********************************************/
            IEnumerable <CartLineComponent> matchingLines = null;
            Action executeAction = () => matchingLines = targetCategorySitecoreId.YieldCartLinesWithCategory(context);

            /**********************************************
            * Assert
            **********************************************/
            executeAction.Should().NotThrow <Exception>();
            matchingLines.Should().BeEmpty();
        }
コード例 #16
0
        public void YieldCartLines_08_Multiple(
            IRuleValue <string> targetCategorySitecoreId,
            Cart cart,
            LineItemProductExtendedComponent component,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            component.ParentCategoryList = "8e456d84-4251-dba1-4b86-ce103dedcd02|c2bfaf91-7825-4846-0ad3-0479cdf7b607";
            cart.Lines.ForEach(l => l.SetComponent(component));
            commerceContext.AddObject(cart);
            targetCategorySitecoreId.Yield(context).ReturnsForAnyArgs("c2bfaf91-7825-4846-0ad3-0479cdf7b607");

            /**********************************************
            * Act
            **********************************************/
            var matchingLines = targetCategorySitecoreId.YieldCartLinesWithCategory(context);

            /**********************************************
            * Assert
            **********************************************/
            matchingLines.Should().HaveCount(3);
        }
コード例 #17
0
        public void YieldCartLines_04_NoLineItemComponent(
            IRuleValue <string> targetBrand,
            Cart cart,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            commerceContext.AddObject(cart);
            targetBrand.Yield(context).ReturnsForAnyArgs("Smartphone");

            /**********************************************
            * Act
            **********************************************/
            IEnumerable <CartLineComponent> matchingLines = null;
            Action executeAction = () => matchingLines = targetBrand.YieldCartLinesWithBrand(context);

            /**********************************************
            * Assert
            **********************************************/
            executeAction.Should().NotThrow <Exception>();
            matchingLines.Should().BeEmpty();
        }
コード例 #18
0
        public void YieldCartLines_06_EmptyTag(
            IRuleValue <string> targetTag,
            Cart cart,
            CartProductComponent component,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            component.Tags.Add(new Tag("Smartphone"));
            cart.Lines[1].SetComponent(component);
            commerceContext.AddObject(cart);
            targetTag.Yield(context).ReturnsForAnyArgs("");

            /**********************************************
            * Act
            **********************************************/
            IEnumerable <CartLineComponent> matchingLines = null;
            Action executeAction = () => matchingLines = targetTag.YieldCartLinesWithTag(context);

            /**********************************************
            * Assert
            **********************************************/
            executeAction.Should().NotThrow <Exception>();
            matchingLines.Should().BeEmpty();
        }
            public void Execute_03_NoCartLines(
                CartShippingOptionAmountOffAction action,
                IRuleValue <string> fulfillmentOptionName,
                IRuleValue <decimal> amountOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.Clear();

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                action.FulfillmentOptionName = fulfillmentOptionName;
                action.AmountOff             = amountOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }
コード例 #20
0
        public void YieldCartLines_08_Multiple(
            IRuleValue <string> targetBrand,
            Cart cart,
            LineItemProductExtendedComponent component,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            component.Brand = "Smartphone";
            cart.Lines.ForEach(l => l.SetComponent(component));
            commerceContext.AddObject(cart);
            targetBrand.Yield(context).ReturnsForAnyArgs("Smartphone");

            /**********************************************
            * Act
            **********************************************/
            var matchingLines = targetBrand.YieldCartLinesWithBrand(context);

            /**********************************************
            * Assert
            **********************************************/
            matchingLines.Should().HaveCount(3);
        }
コード例 #21
0
            public void Execute_02_NoCart(
                BaseCartItemSubtotalAmountOffAction action,
                IBinaryOperator <decimal, decimal> subtotalOperator,
                IRuleValue <decimal> subtotal,
                IRuleValue <decimal> amountOff,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                action.SubtotalOperator = subtotalOperator;
                action.Subtotal         = subtotal;
                action.AmountOff        = amountOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
            }
コード例 #22
0
        public void YieldCartLines_07_Single(
            IRuleValue <string> targetTag,
            Cart cart,
            CartProductComponent component,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            component.Tags.Add(new Tag("Smartphone"));
            cart.Lines[1].SetComponent(component);
            commerceContext.AddObject(cart);
            targetTag.Yield(context).ReturnsForAnyArgs("Smartphone");

            /**********************************************
            * Act
            **********************************************/
            var matchingLines = targetTag.YieldCartLinesWithTag(context);

            /**********************************************
            * Assert
            **********************************************/
            matchingLines.Should().HaveCount(1);
        }
コード例 #23
0
            public void Execute_02_NoCart(
                IRuleValue <string> targetTag,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                CartItemTargetTagFreeGiftAction action,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                commerceContext.AddObject(cartTotals);
                action.TargetTag = targetTag;

                context.Fact(Arg.Any <IFactIdentifier>()).Returns(commerceContext);

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
            }
コード例 #24
0
        public void YieldCartLines_06_EmptyCategory(
            IRuleValue <string> targetCategorySitecoreId,
            Cart cart,
            LineItemProductExtendedComponent component,
            CommerceContext commerceContext,
            IRuleExecutionContext context)
        {
            /**********************************************
            * Arrange
            **********************************************/
            context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
            component.ParentCategoryList = "8e456d84-4251-dba1-4b86-ce103dedcd02|c2bfaf91-7825-4846-0ad3-0479cdf7b607";
            cart.Lines[1].SetComponent(component);
            commerceContext.AddObject(cart);
            targetCategorySitecoreId.Yield(context).ReturnsForAnyArgs("");

            /**********************************************
            * Act
            **********************************************/
            IEnumerable <CartLineComponent> matchingLines = null;
            Action executeAction = () => matchingLines = targetCategorySitecoreId.YieldCartLinesWithCategory(context);

            /**********************************************
            * Assert
            **********************************************/
            executeAction.Should().NotThrow <Exception>();
            matchingLines.Should().BeEmpty();
        }
コード例 #25
0
            public void Execute_DecimalGreaterThanEqualToOperator_True(
                BaseCartItemSubtotalAmountOffAction action,
                IRuleValue <decimal> subtotal,
                IRuleValue <decimal> amountOff,
                Cart cart,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                var globalPricingPolicy = commerceContext.GetPolicy <GlobalPricingPolicy>();

                globalPricingPolicy.ShouldRoundPriceCalc         = false;
                cart.Lines.ForEach(l => l.Totals.SubTotal.Amount = 100);
                var cartline = cart.Lines[1];

                cartline.Totals.SubTotal.Amount = 175;
                var cartTotals = new CartTotals(cart);

                subtotal.Yield(context).ReturnsForAnyArgs(150);
                amountOff.Yield(context).ReturnsForAnyArgs(25);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.MatchingLines(context).ReturnsForAnyArgs(cart.Lines);
                action.SubtotalOperator = new DecimalGreaterThanEqualToOperator();
                action.Subtotal         = subtotal;
                action.AmountOff        = amountOff;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().HaveCount(1);
                cart.Adjustments.Should().BeEmpty();
                cartTotals.Lines[cartline.Id].SubTotal.Amount.Should().Be(150);
            }
コード例 #26
0
            public void Execute_HasMatchingLines_ShouldApplyCartLineAdjustment(
                string targetTag,
                bool autoRemove,
                Cart cart,
                CartProductComponent cartProductComponent,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                ApplyFreeGiftDiscountCommand    discountCommand;
                ApplyFreeGiftEligibilityCommand eligibilityCommand;
                ApplyFreeGiftAutoRemoveCommand  autoRemoveCommand;
                CartItemTargetTagFreeGiftAction action = BuildAction(out discountCommand, out eligibilityCommand, out autoRemoveCommand);


                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);

                cartProductComponent.Tags.Add(new Tag(targetTag));

                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());
                cart.Lines[0].SetComponent(cartProductComponent);

                action.TargetTag = Substitute.For <IRuleValue <string> >();
                action.TargetTag.Yield(context).ReturnsForAnyArgs(targetTag);

                action.AutoRemove = Substitute.For <IRuleValue <bool> >();
                action.AutoRemove.Yield(context).ReturnsForAnyArgs(autoRemove);

                context.Fact(Arg.Any <IFactIdentifier>()).Returns(commerceContext);

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                eligibilityCommand.Received().Process(Arg.Any <CommerceContext>(), cart, Arg.Any <string>());
                discountCommand.Received().Process(Arg.Any <CommerceContext>(), Arg.Any <CartLineComponent>(), Arg.Any <string>());
            }
            public void Evaluate_09_EmptyFulfillmentMethodName(
                IRuleValue <string> fulfillmentOptionName,
                CommerceContext commerceContext,
                Cart cart,
                IRuleExecutionContext context,
                SplitFulfillmentComponent splitFulfillmentComponent,
                FulfillmentComponent fulfillmentComponent,
                string fulfillmentOptionNameValue)
            {
                /**********************************************
                * Arrange
                **********************************************/
                fulfillmentComponent.FulfillmentMethod.Name = null;
                cart.SetComponent(splitFulfillmentComponent);
                while (cart.Lines.Count > 1)
                {
                    cart.Lines.RemoveAt(0);
                }
                cart.Lines[0].SetComponent(fulfillmentComponent);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue);
                var command = Substitute.For <GetFulfillmentMethodsCommand>(
                    Substitute.For <IFindEntityPipeline>(),
                    Substitute.For <IGetCartFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetFulfillmentMethodsPipeline>(),
                    Substitute.For <IServiceProvider>());

                var commander         = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>());
                var fulfillmentMethod = new FulfillmentMethod()
                {
                    FulfillmentType = fulfillmentOptionNameValue
                };

                command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>()
                {
                    fulfillmentMethod
                }.AsEnumerable());
                commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase();
                commander.Command <GetFulfillmentMethodsCommand>().Returns(command);
                var condition = new CartLineHasFulfillmentOptionCondition(commander)
                {
                    FulfillmentOptionName = fulfillmentOptionName
                };

                /**********************************************
                * Act
                **********************************************/
                var result = condition.Evaluate(context);

                /**********************************************
                * Assert
                **********************************************/
                result.Should().BeFalse();
            }
コード例 #28
0
            public void Evaluate_HasMatchingFulfillmentMethod_True(
                IRuleValue <string> fulfillmentOptionName,
                CommerceContext commerceContext,
                Cart cart,
                IRuleExecutionContext context,
                FulfillmentComponent fulfillmentComponent,
                string fulfillmentOptionNameValue)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.SetComponent(fulfillmentComponent);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cart);

                fulfillmentOptionName.Yield(context).ReturnsForAnyArgs(fulfillmentOptionNameValue);
                var command = Substitute.For <GetFulfillmentMethodsCommand>(
                    Substitute.For <IFindEntityPipeline>(),
                    Substitute.For <IGetCartFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetCartLineFulfillmentMethodsPipeline>(),
                    Substitute.For <IGetFulfillmentMethodsPipeline>(),
                    Substitute.For <IServiceProvider>());

                var commander         = Substitute.For <CommerceCommander>(Substitute.For <IServiceProvider>());
                var fulfillmentMethod = new FulfillmentMethod
                {
                    FulfillmentType = fulfillmentOptionNameValue,
                    Id   = fulfillmentComponent.FulfillmentMethod.EntityTarget,
                    Name = fulfillmentComponent.FulfillmentMethod.Name
                };

                command.Process(commerceContext).ReturnsForAnyArgs(new List <FulfillmentMethod>()
                {
                    fulfillmentMethod
                }.AsEnumerable());
                commander.When(x => x.Command <GetFulfillmentMethodsCommand>()).DoNotCallBase();
                commander.Command <GetFulfillmentMethodsCommand>().Returns(command);
                var condition = new CartHasFulfillmentOptionCondition(commander)
                {
                    FulfillmentOptionName = fulfillmentOptionName
                };

                /**********************************************
                * Act
                **********************************************/
                var result = condition.Evaluate(context);

                /**********************************************
                * Assert
                **********************************************/
                result.Should().BeTrue();
            }
コード例 #29
0
        private async Task TransformCatalog(CommerceContext commerceContext, List <Category> importItems)
        {
            var allCatalogs = commerceContext.GetObject <IEnumerable <Sitecore.Commerce.Plugin.Catalog.Catalog> >();

            if (allCatalogs == null)
            {
                allCatalogs = await Command <GetCatalogsCommand>().Process(commerceContext);

                commerceContext.AddObject(allCatalogs);
            }

            var allCatalogsDictionary = allCatalogs.ToDictionary(c => c.Name);

            foreach (var item in importItems)
            {
                var transientData = item.GetPolicy <TransientImportCategoryDataPolicy>();

                var catalogList = new List <string>();
                foreach (var association in transientData.CatalogAssociationList)
                {
                    allCatalogsDictionary.TryGetValue(association.Name, out Sitecore.Commerce.Plugin.Catalog.Catalog catalog);
                    if (catalog != null)
                    {
                        catalogList.Add(catalog.SitecoreId);
                    }
                    else
                    {
                        commerceContext.Logger.LogWarning($"Warning, Category with id '{item.Id}' attempting import into catalog '{association.Name}' which doesn't exist.");
                    }
                }

                var parentCatalogList = new List <string>();
                foreach (var association in transientData.ParentAssociationsToCreateList)
                {
                    if (association.ParentId.IsEntityId <Sitecore.Commerce.Plugin.Catalog.Catalog>())
                    {
                        allCatalogsDictionary.TryGetValue(association.ParentId.RemoveIdPrefix <Sitecore.Commerce.Plugin.Catalog.Catalog>(), out Sitecore.Commerce.Plugin.Catalog.Catalog catalog);
                        if (catalog != null)
                        {
                            parentCatalogList.Add(catalog.SitecoreId);
                        }
                        else
                        {
                            commerceContext.Logger.LogWarning($"Warning, Category with id {item.Id} attempting import into catalog {association.ParentId} which doesn't exist.");
                        }
                    }
                }

                // Primary responsibility of this method is to set these IDs
                item.CatalogToEntityList = catalogList.Any() ? string.Join("|", catalogList) : null;
                item.ParentCatalogList   = parentCatalogList.Any() ? string.Join("|", parentCatalogList) : null;
            }
        }
            public void Execute_11_EqualQuantityXAndQuantityY(
                CartItemQuantityXForQuantityYAction action,
                IRuleValue <string> targetItemId,
                IRuleValue <int> quantityX,
                IRuleValue <int> quantityY,
                IRuleValue <int> maximumApplications,
                Cart cart,
                CartTotals cartTotals,
                CommerceContext commerceContext,
                IRuleExecutionContext context)
            {
                /**********************************************
                * Arrange
                **********************************************/
                cart.Adjustments.Clear();
                cart.Lines.ForEach(l => l.Adjustments.Clear());

                quantityX.Yield(context).ReturnsForAnyArgs(4);
                quantityY.Yield(context).ReturnsForAnyArgs(4);

                context.Fact <CommerceContext>().ReturnsForAnyArgs(commerceContext);
                commerceContext.AddObject(cartTotals);
                commerceContext.AddObject(cart);
                action.TargetItemId        = targetItemId;
                action.QuantityX           = quantityX;
                action.QuantityY           = quantityY;
                action.MaximumApplications = maximumApplications;

                /**********************************************
                * Act
                **********************************************/
                Action executeAction = () => action.Execute(context);

                /**********************************************
                * Assert
                **********************************************/
                executeAction.Should().NotThrow <Exception>();
                cart.Lines.SelectMany(l => l.Adjustments).Should().BeEmpty();
                cart.Adjustments.Should().BeEmpty();
            }