Example #1
0
        protected void Setup()
        {
            _cartService           = Resolve <ICartService>();
            _userService           = Resolve <IUserService>();
            _productService        = Resolve <IProductService>();
            _priceAccountant       = Resolve <IPriceAccountant>();
            _roleService           = Resolve <IRoleService>();
            _discountCouponService = Resolve <IDiscountCouponService>();
            _categoryService       = Resolve <ICategoryService>();
            _vendorService         = Resolve <IVendorService>();
            _manufacturerService   = Resolve <IManufacturerService>();
            _settingService        = Resolve <ISettingService>();
            _taxSettings           = Resolve <TaxSettings>();
            _registeredUser        = new User()
            {
                Email     = "*****@*****.**",
                CreatedOn = DateTime.UtcNow,
                UpdatedOn = DateTime.UtcNow,
            };
            _userService.Insert(_registeredUser);
            _registeredRole = _roleService.FirstOrDefault(x => x.SystemName == SystemRoleNames.Registered);
            _roleService.SetUserRoles(_registeredUser.Id, new [] { _registeredRole.Id }); //registered user
            _registeredUser = _userService.Get(_registeredUser.Id);

            _visitor = new User()
            {
                Email     = "*****@*****.**",
                CreatedOn = DateTime.UtcNow,
                UpdatedOn = DateTime.UtcNow,
            };

            _userService.Insert(_visitor);
            _visitorRole = _roleService.FirstOrDefault(x => x.SystemName == SystemRoleNames.Visitor);
            _roleService.SetUserRoles(_visitor.Id, new[] { _visitorRole.Id }); //visitor user
            _visitor = _userService.Get(_visitor.Id);

            _manufacturer = new Manufacturer()
            {
                Name = "Test Manufacturer"
            };
            _manufacturerService.Insert(_manufacturer);

            _vendor = new Vendor()
            {
                Name    = "Test Vendor",
                Address = "Test Address",
                City    = "Test City"
            };
            _vendorService.Insert(_vendor);

            var products = EvenCart.Tests.Data.Products.GetList();

            _product1 = products[0];
            _product1.ManufacturerId = _manufacturer.Id;
            _productService.Insert(_product1);

            _product2 = products[1];
            _productService.Insert(_product2);
            _vendorService.AddVendorProduct(_vendor.Id, _product2.Id);

            _category = new Category()
            {
                Name = "Test Category"
            };
            _categoryService.Insert(_category);

            _productService.LinkCategoryWithProduct(_category.Id, _product1.Id, 0);



            var discounts = new[]
            {
                new DiscountCoupon()
                {
                    Name                 = "Test Coupon One",
                    CalculationType      = CalculationType.FixedAmount,
                    HasCouponCode        = false,
                    DiscountValue        = 10,
                    Enabled              = true,
                    StartDate            = DateTime.UtcNow,
                    EndDate              = DateTime.UtcNow.AddDays(5),
                    NumberOfTimesPerUser = 1,
                    TotalNumberOfTimes   = 5,
                    RestrictionType      = RestrictionType.OrderTotal
                },
                new DiscountCoupon()
                {
                    Name                 = "Test Coupon Two",
                    CalculationType      = CalculationType.Percentage,
                    CouponCode           = "VALIDCOUPON",
                    HasCouponCode        = true,
                    DiscountValue        = 5,
                    Enabled              = true,
                    StartDate            = DateTime.UtcNow,
                    EndDate              = DateTime.UtcNow.AddDays(5),
                    NumberOfTimesPerUser = 1,
                    TotalNumberOfTimes   = 5
                },
                new DiscountCoupon()
                {
                    Name                  = "Expired Coupon",
                    CalculationType       = CalculationType.Percentage,
                    HasCouponCode         = true,
                    CouponCode            = "EXPIREDCOUPON",
                    DiscountValue         = 5,
                    Enabled               = true,
                    StartDate             = DateTime.UtcNow.AddDays(-5),
                    EndDate               = DateTime.UtcNow.AddDays(-1),
                    NumberOfTimesPerUser  = 1,
                    TotalNumberOfTimes    = 5,
                    MaximumDiscountAmount = 10
                },
            };

            //save some discounts
            _discountCouponService.Insert(discounts);
            _autoCoupon  = discounts[0];
            _validCoupon = discounts[1];
        }
        public void Get_Product_By_Id_Succeeds()
        {
            var manufacturer = new Manufacturer()
            {
                Name = "RoastedBytes"
            };

            _manufacturerService.Insert(manufacturer);

            var vendor1 = new Vendor()
            {
                Name = "Vendor One"
            };
            var vendor2 = new Vendor()
            {
                Name = "Vendor One"
            };

            _vendorService.Insert(new[] { vendor1, vendor2 });

            var availableAttributes = new[]
            {
                new AvailableAttribute()
                {
                    Name = "Size"
                },
                new AvailableAttribute()
                {
                    Name = "Color"
                }
            };

            _availableAttributeService.Insert(availableAttributes);

            var attributeValues = new[]
            {
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "S"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "M"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 1,
                    Value = "L"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Red"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Green"
                },
                new AvailableAttributeValue()
                {
                    AvailableAttributeId = 2,
                    Value = "Blue"
                },
            };

            _availableAttributeValueService.Insert(attributeValues);

            var product = new Product()
            {
                Name = "Microsoft Surface Pro",
                CanOrderWhenOutOfStock = true,
                ChargeTaxes            = true,
                ComparePrice           = 100,
                Price                 = 80,
                CreatedOn             = DateTime.UtcNow,
                UpdatedOn             = DateTime.UtcNow,
                IsShippable           = true,
                IsFeatured            = true,
                IsVisibleIndividually = true,
                DisplayOrder          = 1,
                Mpn            = "1",
                Gtin           = "2",
                Sku            = "SUR12",
                IsDownloadable = false,
                Description    =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Summary =
                    "Surface Pro delivers even more speed and performance thanks to a powerful Intel® Core™ processor — with up to 50% more battery life1 than Surface Pro 4 and 2.5x more performance than Surface Pro 3.",
                Published      = true,
                Deleted        = false,
                TrackInventory = true,
                ManufacturerId = manufacturer.Id
            };

            _productService.Insert(product);

            //link product attributes
            var productAttributeValuesSize = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[0].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[1].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[2].Id
                },
            };

            var productAttributeSize = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[0].Id,
                ProductId              = product.Id,
                ProductAttributeValues = productAttributeValuesSize
            };

            _productAttributeService.Insert(productAttributeSize);

            var productAttributeValuesColor = new List <ProductAttributeValue>()
            {
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[3].Id
                },
                new ProductAttributeValue()
                {
                    AvailableAttributeValueId = attributeValues[4].Id
                },
            };

            var productAttributeColor = new ProductAttribute()
            {
                AvailableAttributeId   = availableAttributes[1].Id,
                ProductId              = product.Id,
                ProductAttributeValues = productAttributeValuesColor
            };

            _productAttributeService.Insert(productAttributeColor);


            //generate variations
            // s + red
            var sRedVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id,
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = sRedVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            // s + green
            var sGreenVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[0].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[1].Id
                    }
                },
                TrackInventory = false
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = sGreenVariant.Id,
                TotalQuantity    = 5,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            // m + red
            var mRedVariant = _productVariantService.AddVariant(product, new ProductVariant()
            {
                ProductVariantAttributes = new List <ProductVariantAttribute>()
                {
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeSize.Id,
                        ProductAttributeValueId = productAttributeValuesSize[1].Id
                    },
                    new ProductVariantAttribute()
                    {
                        ProductAttributeId      = productAttributeColor.Id,
                        ProductAttributeValueId = productAttributeValuesColor[0].Id
                    }
                },
                TrackInventory = true
            });

            _warehouseInventoryService.Insert(new WarehouseInventory()
            {
                ProductId        = product.Id,
                ProductVariantId = mRedVariant.Id,
                TotalQuantity    = 0,
                ReservedQuantity = 0,
                WarehouseId      = _w1.Id
            });

            //link vendors
            _vendorService.AddVendorProduct(vendor1.Id, product.Id);
            _vendorService.AddVendorProduct(vendor2.Id, product.Id);

            //retrive the product
            var p = _productService.Get(product.Id);

            //same product?
            Assert.AreEqual(product.Id, p.Id);

            //attributes correct?
            Assert.AreEqual(2, p.ProductAttributes.Count);

            //attribute values correct
            Assert.AreEqual(3, p.ProductAttributes[0].ProductAttributeValues.Count);
            Assert.AreEqual(2, p.ProductAttributes[1].ProductAttributeValues.Count);

            //manufacturer right
            Assert.AreEqual(manufacturer.Id, p.Manufacturer.Id);

            //vendors right?
            Assert.AreEqual(2, p.Vendors.Count);

            //get variants
            var variants = _productVariantService.GetByProductId(product.Id);

            Assert.AreEqual(3, variants.Count);

            //single variants
            var singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[0].Id
            });

            Assert.AreEqual(sRedVariant.Id, singleVariant.Id);

            //single variants
            singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[1].Id
            });
            Assert.AreEqual(sGreenVariant.Id, singleVariant.Id);

            singleVariant = _productVariantService.GetByAttributeValueIds(new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[0].Id
            });
            Assert.AreEqual(mRedVariant.Id, singleVariant.Id);

            //verify stocks
            var stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[0].Id
            }, out ProductVariant _);

            Assert.AreEqual(StockStatus.InStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[0].Id,
                productAttributeValuesColor[1].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.InStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[0].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.OutOfStock, stockStatus);

            stockStatus = _productAccountant.GetStockStatus(product, new List <int>()
            {
                productAttributeValuesSize[1].Id,
                productAttributeValuesColor[1].Id
            }, out ProductVariant _);
            Assert.AreEqual(StockStatus.Unavailable, stockStatus);

            //remove a product attribute value to see how variants are affected
            _productVariantService.DeleteVariantsByProductAttributeValueId(productAttributeValuesSize[0].Id);

            //get the variants again
            variants = _productVariantService.GetByProductId(product.Id);
            Assert.AreEqual(1, variants.Count);
            Assert.AreEqual(mRedVariant.Id, variants[0].Id);
        }