public void CreateCheckoutViewModelWithPayment_ShouldReturnViewModelWithCorrectPayment()
        {
            var checkoutPage = new CheckoutPage();
            var viewModel = _subject.CreateCheckoutViewModel(_cart, checkoutPage, _creditPayment);

            Assert.Equal(viewModel.PaymentMethodViewModels.Count(), 2);
            Assert.Equal(viewModel.Payment.SystemName, creditPaymentName);
        }
        public void CreateCheckoutViewModel_WhenCartIsNull_ShouldReturnEmptyViewModel()
        {
            var checkoutPage = new CheckoutPage();
            var viewModel = _subject.CreateCheckoutViewModel(null, checkoutPage);

            Assert.Empty(viewModel.Shipments);
            Assert.Empty(viewModel.AppliedCouponCodes);
            Assert.Empty(viewModel.AvailableAddresses);
            Assert.Null(viewModel.BillingAddress);
            Assert.Equal(viewModel.CurrentPage, checkoutPage);
            Assert.Equal(viewModel.StartPage, _startPage);
            Assert.Empty(viewModel.PaymentMethodViewModels);
            Assert.Null(viewModel.Payment);
            Assert.Equal(viewModel.ViewName, CheckoutViewModel.SingleShipmentCheckoutViewName);
        }
        public virtual CheckoutViewModel CreateCheckoutViewModel(ICart cart, CheckoutPage currentPage, IPaymentMethodViewModel<PaymentMethodBase> paymentViewModel = null)
        {
            if (cart == null)
            {
                return CreateEmptyCheckoutViewModel(currentPage);
            }

            var shipments = _shipmentViewModelFactory.CreateShipmentsViewModel(cart).ToList();

            var viewModel = new CheckoutViewModel
            {
                CurrentPage = currentPage,
                Shipments = shipments,
                BillingAddress = CreateBillingAddressModel(),
                UseBillingAddressForShipment = shipments.Count == 1 && _addressBookService.UseBillingAddressForShipment(),
                StartPage = _contentLoader.Get<StartPage>(ContentReference.StartPage),
                AppliedCouponCodes = cart.GetFirstForm().CouponCodes.Distinct(),
                AvailableAddresses = new List<AddressModel>(),
                ReferrerUrl = GetReferrerUrl(),
                Payment = paymentViewModel,
            };

            UpdatePayment(viewModel);
                        
            var availableAddresses = GetAvailableAddresses();

            if (availableAddresses.Any())
            {
                viewModel.AvailableAddresses.Add(new AddressModel { Name = _localizationService.GetString("/Checkout/MultiShipment/SelectAddress") });
                
                foreach (var address in availableAddresses)
                {
                    viewModel.AvailableAddresses.Add(address);
                }
            }
            else
            {
                viewModel.AvailableAddresses.Add(new AddressModel { Name = _localizationService.GetString("/Checkout/MultiShipment/NoAddressFound") });
            }

            SetDefaultShipmentAddress(viewModel);

            _addressBookService.LoadAddress(viewModel.BillingAddress);
            PopulateCountryAndRegions(viewModel);

            return viewModel;
        }
        public void CreateCheckoutViewModel_ShouldReturnViewModel()
        {
            var checkoutPage = new CheckoutPage();
            var viewModel = _subject.CreateCheckoutViewModel(_cart, checkoutPage);

            Assert.Equal(1, viewModel.Shipments.Count);
            Assert.Equal("couponcode", viewModel.AppliedCouponCodes.Single());
            Assert.NotNull(viewModel.AvailableAddresses.SingleOrDefault(x => x.AddressId == "addressid"));
            Assert.Equal(viewModel.BillingAddress.AddressId, _preferredBillingAddress.Name);
            Assert.Equal(viewModel.CurrentPage, checkoutPage);
            Assert.Equal(viewModel.StartPage, _startPage);
            Assert.Equal(viewModel.PaymentMethodViewModels.Count(), 2);
            Assert.Equal(viewModel.Payment.SystemName, cashPaymentName);//Select first payment method as default
            Assert.Equal(viewModel.UseBillingAddressForShipment, true);
            Assert.True(viewModel.ReferrerUrl.Contains("http://site.com"));
            Assert.Equal(viewModel.ViewName, CheckoutViewModel.SingleShipmentCheckoutViewName);
        }
 private CheckoutViewModel CreateEmptyCheckoutViewModel(CheckoutPage currentPage)
 {
     return new CheckoutViewModel
     {
         CurrentPage = currentPage,
         Shipments = new List<ShipmentViewModel>(),
         AppliedCouponCodes = new List<string>(),
         StartPage = _contentLoader.Get<StartPage>(ContentReference.StartPage),
         AvailableAddresses = new List<AddressModel>(),
         PaymentMethodViewModels = Enumerable.Empty<PaymentMethodViewModel<Payment.PaymentMethods.PaymentMethodBase>>(),
         UseBillingAddressForShipment = true
     };
 }
 private CheckoutViewModel CreateCheckoutViewModelWithAddressIds(CheckoutPage currentPage, IPaymentMethodViewModel<PaymentMethodBase> paymentMethodViewModel, bool useBillingAddressForShipment)
 {
     return new CheckoutViewModel
     {
         CurrentPage = currentPage,
         Payment = paymentMethodViewModel,
         UseBillingAddressForShipment = useBillingAddressForShipment,
         Shipments = _cart.Forms.SelectMany(x => x.Shipments).Select(shipment => new ShipmentViewModel
         {
             ShipmentId = shipment.ShipmentId,
             ShippingMethodId = shipment.ShippingMethodId,
             Address = new AddressModel()
             {
                 AddressId = Guid.NewGuid().ToString()                        
             }
         }).ToList(),
         BillingAddress = new AddressModel
         {
             AddressId = Guid.NewGuid().ToString(),
             Email = "*****@*****.**"
         }
     };
 }
 private CheckoutViewModel CreateCheckoutViewModel(CheckoutPage currentPage, IPaymentMethodViewModel<PaymentMethodBase> paymentMethodViewModel)
 {
     return new CheckoutViewModel
     {
         CurrentPage = currentPage,
         Payment = paymentMethodViewModel,
         UseBillingAddressForShipment = true,
         Shipments = _cart.Forms.SelectMany(x => x.Shipments).Select(shipment => new ShipmentViewModel
         {
             ShipmentId = shipment.ShipmentId,
             ShippingMethodId = shipment.ShippingMethodId
         }).ToList(),
         BillingAddress = new AddressModel
         {
             Email = "*****@*****.**"
         }
     };
 }