Ejemplo n.º 1
0
        private ProductViewModel GetProductViewModel(Item productItem, Rendering rendering)
        {
            if (this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] != null)
            {
                return((ProductViewModel)this.CurrentSiteContext.Items[CurrentProductViewModelKeyName]);
            }

            var variants = new List <VariantViewModel>();

            if (productItem != null && productItem.HasChildren)
            {
                foreach (Item item in productItem.Children)
                {
                    var v = new VariantViewModel(item);
                    variants.Add(v);
                }
            }

            var productViewModel = new ProductViewModel(productItem);

            productViewModel.Initialize(rendering, variants);
            productViewModel.ProductName = productViewModel.DisplayName;

            if (this.CurrentSiteContext.UrlContainsCategory)
            {
                productViewModel.ParentCategoryId = CatalogUrlManager.ExtractCategoryNameFromCurrentUrl();
                var category = _catalogManager.GetCategory(productViewModel.ParentCategoryId);

                if (category != null)
                {
                    productViewModel.ParentCategoryName = category.DisplayName;
                }
            }

            _catalogManager.GetProductPrice(this.CurrentVisitorContext, productViewModel);
            productViewModel.CustomerAverageRating = _catalogManager.GetProductRating(productItem);
            this.CurrentSiteContext.Items[CurrentProductViewModelKeyName] = productViewModel;

            return(productViewModel);
        }
        [ItemWithChildrenDbData("22565422120")] // Gift Card ProductId
        public void GetGiftCardViewModel_should_apply_GiftCardSpecialHandling_For_GiftCardItem(Database db)
        {
            //arrange
            Item fakeGiftCardItem = GetFakeItemWithChildren(db);

            MockContexts(db);

            decimal adjustedPriceTest = new decimal(1.0);

            _catalogManagerMock.GetProductPrice(Arg.Any <IVisitorContext>(), Arg.Do <ProductViewModel>(p => p.Variants.ForEach(v => v.AdjustedPrice = adjustedPriceTest)));


            var rep = new GiftCardRepository(_accountManagerMock, _contactFactoryMock, _catalogManagerMock);

            // act
            var product = rep.GetGiftCardViewModel(fakeGiftCardItem, new Rendering());

            // assert
            product.GiftCardAmountOptions.Count.Should().Be(GiftCardTestsConstants.ItemWithChildrenNumberOfChildren);
            product.GiftCardAmountOptions[0].Key.Should().Be(GiftCardTestsConstants.ItemWithChildrenFirstChildItemName);
            product.GiftCardAmountOptions[0].Value.Should().Be(adjustedPriceTest);
            product.GiftCardAmountOptions[1].Key.Should().Be(GiftCardTestsConstants.ItemWithChildrenSecondChildItemName);
            product.GiftCardAmountOptions[1].Value.Should().Be(adjustedPriceTest);
        }