public void Can_Serialize_And_Deserialize_A_ProductAttribute()
            {
                var att = new ProductAttribute("test", "test") as IProductAttribute;
                var display = att.ToProductAttributeDisplay();

                var serialized = JsonConvert.SerializeObject(display);

                var deserialized = JsonConvert.DeserializeObject<ProductAttributeDisplay>(serialized);

                Assert.NotNull(deserialized);
            }
            public void Can_Serialize_And_Deserialize_A_ProductAttributeWithDetachedValues()
            {
                var att = new ProductAttribute("test", "test") as IProductAttribute;
                att.DetachedDataValues.AddOrUpdate("key", "value", (x, y) => "value");
                var display = att.ToProductAttributeDisplay();

                var serialized = JsonConvert.SerializeObject(display);

                var deserialized = JsonConvert.DeserializeObject<ProductAttributeDisplay>(serialized);

                Assert.NotNull(deserialized);
            }
Beispiel #3
0
        public void Can_Get_A_ProductVariant_From_GetVariantForPurchase_Method_Given_A_List_Of_Attributes()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() };
            var attCollection = new ProductAttributeCollection();
            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false,
                "Variant", "variant", 5M);
            _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var variant = _product.GetProductVariantForPurchase(attCollection);

            //// Assert
            Assert.NotNull(variant);
            Assert.AreEqual(expected.Key, variant.Key);
        }
        internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination)
        {
            if (productVariantDisplay.Key != Guid.Empty)
            {
                destination.Key = productVariantDisplay.Key;
            }
            if( !String.IsNullOrEmpty(productVariantDisplay.Name) )
            {
                destination.Name = productVariantDisplay.Name;
            }
            if( !String.IsNullOrEmpty(productVariantDisplay.Sku) )
            {
                destination.Sku = productVariantDisplay.Sku;
            }
            destination.Price = productVariantDisplay.Price;
            destination.CostOfGoods = productVariantDisplay.CostOfGoods;
            destination.SalePrice = productVariantDisplay.SalePrice;
            destination.OnSale = productVariantDisplay.OnSale;
            destination.Manufacturer = productVariantDisplay.Manufacturer;
            destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber;
            destination.Weight = productVariantDisplay.Weight;
            destination.Length = productVariantDisplay.Length;
            destination.Width = productVariantDisplay.Width;
            destination.Height = productVariantDisplay.Height;
            destination.Barcode = productVariantDisplay.Barcode;
            destination.Available = productVariantDisplay.Available;
            destination.TrackInventory = productVariantDisplay.TrackInventory;
            destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase;
            destination.Taxable = productVariantDisplay.Taxable;
            destination.Shippable = productVariantDisplay.Shippable;
            destination.Download = productVariantDisplay.Download;
            destination.DownloadMediaId = productVariantDisplay.DownloadMediaId;

            destination.ProductKey = productVariantDisplay.ProductKey;

            // We need to refactor the CatalogInventories to not be immutable if we are
            // going to need to do operations like this.  In the UI, the user "unchecks" a catalog that was
            // previously checked - so we need to remove it.
            var deletedCatalogKeys =
                destination.CatalogInventories.Where(
                    x => !productVariantDisplay.CatalogInventories.Select(ci => ci.CatalogKey).Contains(x.CatalogKey)).Select(x => x.CatalogKey).ToArray();
            foreach (var deletedCatalogKey in deletedCatalogKeys)
            {
                destination.RemoveFromCatalogInventory(deletedCatalogKey);
            }

            foreach (var catalogInventory in productVariantDisplay.CatalogInventories)
            {
                var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey);

                if (catInv != null)
                {
                    var destinationCatalogInventory = catInv;

                    destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory);
                }
                else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity)
                {
                    //// Add to a new catalog
                    destination.AddToCatalogInventory(catalogInventory.CatalogKey);
                }
            }

            foreach (var attribute in productVariantDisplay.Attributes)
            {
                IProductAttribute destinationProductAttribute;

                var attr = destination.Attributes.FirstOrDefault(x => x.Key == attribute.Key);
                if (attr != null)
                {
                    destinationProductAttribute = attr;

                    destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute);
                }
                else
                {
                    destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku);

                    destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute);

                    ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection;
                    variantAttributes.Add(destinationProductAttribute);
                }
            }

            destination.AddOrUpdateDetachedContent(productVariantDisplay);

            return destination;
        }
        /// <summary>
        /// The to product option.
        /// </summary>
        /// <param name="productOptionDisplay">
        /// The product option display.
        /// </param>
        /// <param name="destinationProductOption">
        /// The destination product option.
        /// </param>
        /// <returns>
        /// The <see cref="IProductOption"/>.
        /// </returns>
        internal static IProductOption ToProductOption(this ProductOptionDisplay productOptionDisplay, IProductOption destinationProductOption)
        {
            if (productOptionDisplay.Key != Guid.Empty)
            {
                destinationProductOption.Key = productOptionDisplay.Key;
            }
            destinationProductOption.Required = productOptionDisplay.Required;
            destinationProductOption.SortOrder = productOptionDisplay.SortOrder;

            // Fix with option deletion here #M-161 #M-150
            // remove any product choices that exist in destination and do not exist in productDisplay
            var removers = destinationProductOption.Choices.Where(x => !productOptionDisplay.Choices.Select(pd => pd.Key).Contains(x.Key)).Select(x => x.Key).ToArray();
            foreach (var remove in removers)
            {
                destinationProductOption.Choices.RemoveItem(remove);
            }

            foreach (var choice in productOptionDisplay.Choices)
            {
                // Sets the sku if it is empty - fixes M-170
                // http://issues.merchello.com/youtrack/issue/M-170
                if (string.IsNullOrEmpty(choice.Sku))
                {
                    choice.Sku = Regex.Replace(choice.Name, "[^0-9a-zA-Z]+", "");
                }

                IProductAttribute destinationProductAttribute;

                if (destinationProductOption.Choices.Contains(choice.Sku))
                {
                    destinationProductAttribute = destinationProductOption.Choices[choice.Key];

                    destinationProductAttribute = choice.ToProductAttribute(destinationProductAttribute);
                }
                else
                {
                    destinationProductAttribute = new ProductAttribute(choice.Name, choice.Sku);

                    destinationProductAttribute = choice.ToProductAttribute(destinationProductAttribute);
                }

                destinationProductOption.Choices.Add(destinationProductAttribute);
            }

            return destinationProductOption;
        }
        internal static IProductVariant ToProductVariant(this ProductVariantDisplay productVariantDisplay, IProductVariant destination)
        {
            if (productVariantDisplay.Key != Guid.Empty)
            {
                destination.Key = productVariantDisplay.Key;
            }
            if( !String.IsNullOrEmpty(productVariantDisplay.Name) )
            {
                destination.Name = productVariantDisplay.Name;
            }
            if( !String.IsNullOrEmpty(productVariantDisplay.Sku) )
            {
                destination.Sku = productVariantDisplay.Sku;
            }
            destination.Price = productVariantDisplay.Price;
            destination.CostOfGoods = productVariantDisplay.CostOfGoods;
            destination.SalePrice = productVariantDisplay.SalePrice;
            destination.OnSale = productVariantDisplay.OnSale;
            destination.Manufacturer = productVariantDisplay.Manufacturer;
            destination.ManufacturerModelNumber = productVariantDisplay.ManufacturerModelNumber;
            destination.Weight = productVariantDisplay.Weight;
            destination.Length = productVariantDisplay.Length;
            destination.Width = productVariantDisplay.Width;
            destination.Height = productVariantDisplay.Height;
            destination.Barcode = productVariantDisplay.Barcode;
            destination.Available = productVariantDisplay.Available;
            destination.TrackInventory = productVariantDisplay.TrackInventory;
            destination.OutOfStockPurchase = productVariantDisplay.OutOfStockPurchase;
            destination.Taxable = productVariantDisplay.Taxable;
            destination.Shippable = productVariantDisplay.Shippable;
            destination.Download = productVariantDisplay.Download;
            destination.DownloadMediaId = productVariantDisplay.DownloadMediaId;

            destination.ProductKey = productVariantDisplay.ProductKey;

            foreach (var catalogInventory in productVariantDisplay.CatalogInventories)
            {
				var catInv = destination.CatalogInventories.FirstOrDefault(x => x.CatalogKey == catalogInventory.CatalogKey);
				
				if (catInv != null)
				{
					var destinationCatalogInventory = catInv;
				
					destinationCatalogInventory = catalogInventory.ToCatalogInventory(destinationCatalogInventory);
				}
				else if (!Guid.Empty.Equals(catalogInventory.CatalogKey) && destination.HasIdentity)
				{
					//// Add to a new catalog
					destination.AddToCatalogInventory(catalogInventory.CatalogKey);
				}
            }

            foreach (var attribute in productVariantDisplay.Attributes)
            {
                IProductAttribute destinationProductAttribute;

                var attr = destination.Attributes.First(x => x.Key == attribute.Key);
                if (attr != null)
                {
                    destinationProductAttribute = attr;

                    destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute);
                }
                else
                {
                    destinationProductAttribute = new ProductAttribute(attribute.Name, attribute.Sku);

                    destinationProductAttribute = attribute.ToProductAttribute(destinationProductAttribute);

                    ProductAttributeCollection variantAttributes = destination.Attributes as ProductAttributeCollection;
                    variantAttributes.Add(destinationProductAttribute);
                }
            }

            return destination;
        }
Beispiel #7
0
        public void Can_Create_A_Product_Then_Add_Options_And_Modify_Choices1()
        {
            //// Arrange
            var catalog = PreTestDataWorker.WarehouseCatalog;
            var product = PreTestDataWorker.MakeExistingProduct();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Black", "Black"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            var removeChoice = new ProductAttribute("Grey", "Grey");
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(removeChoice);
            product.ProductOptions.Add(new ProductOption("Size"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Large", "Lg"));
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Small", "Sm"));

            // this creates a record in the CatalogInventory as if the product was marked "shippable"
            // all variants generated with this option will also be marked shippable.
            product.AddToCatalogInventory(catalog);

            _productService.Save(product);

            Assert.AreEqual(2, product.ProductOptions.Count, "ProductOption count is not 2");
            Assert.AreEqual(6, product.ProductVariants.Count, "ProductVariant count is not 6");

            //// Remove a choice
            product.ProductOptions.First(x => x.Name == "Color").Choices.Remove(removeChoice);
            _productService.Save(product);

            // Assert the variants
            Assert.AreEqual(2, product.ProductOptions.Count, "ProductOption count is not 2");
            Assert.AreEqual(4, product.ProductVariants.Count, "ProductVariant count is not 4");

            //// Add a new size
            product.ProductOptions.First(x => x.Name == "Size").Choices.Add(new ProductAttribute("Medium", "Md"));

            _productService.Save(product);
            Assert.AreEqual(2, product.ProductOptions.Count, "ProductOption count is not 2");
            Assert.AreEqual(6, product.ProductVariants.Count, "ProductVariant count is not 6");

            //// Add a new product option
            product.ProductOptions.Add(new ProductOption("Material"));
            product.ProductOptions.First(x => x.Name == "Material").Choices.Add(new ProductAttribute("Wool", "Wool"));
            product.ProductOptions.First(x => x.Name == "Material").Choices.Add(new ProductAttribute("Cotton", "Cotton"));
            _productService.Save(product);

            Assert.AreEqual(3, product.ProductOptions.Count, "ProductOption count is not 3");
            Assert.AreEqual(12, product.ProductVariants.Count, "ProductVariant count is not 12");
        }
Beispiel #8
0
        public void Can_Remove_A_Choice_From_An_Option()
        {
            //// Arrange
            var catalog = PreTestDataWorker.WarehouseCatalog;
            var product = PreTestDataWorker.MakeExistingProduct();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Black", "Black"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            var removeChoice = new ProductAttribute("Grey", "Grey");
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(removeChoice);
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));

            product.AddToCatalogInventory(catalog);

            _productService.Save(product);

            Assert.AreEqual(1, product.ProductOptions.Count, "ProductOption count is not 1");
            Assert.AreEqual(4, product.ProductVariants.Count, "ProductVariant count is not 4");

            //// Act
            product.ProductOptions.First(x => x.Name == "Color").Choices.Remove(removeChoice);
            _productService.Save(product);

            // Assert
            Assert.IsTrue(product.ProductOptions.First(x => x.Name == "Color").Choices.Count == 3);
            Assert.AreEqual(3, product.ProductVariants.Count, "ProductVariant count is not 3");
        }
Beispiel #9
0
        public void Can_Remove_A_Choice_From_An_Option()
        {
            //// Arrange
            var product = PreTestDataWorker.MakeExistingProduct();
            product.ProductOptions.Add(new ProductOption("Color"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Black", "Black"));
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Blue", "Blue"));
            var removeChoice = new ProductAttribute("Grey", "Grey");
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(removeChoice);
            product.ProductOptions.First(x => x.Name == "Color").Choices.Add(new ProductAttribute("Green", "Green"));
            _productService.Save(product);

            //// Act
            product.ProductOptions.First(x => x.Name == "Color").Choices.Remove(removeChoice);
            _productService.Save(product);

            //// Assert
            Assert.IsTrue(product.ProductOptions.First(x => x.Name == "Color").Choices.Count == 3);
        }
Beispiel #10
0
        public void Can_Serialize_A_Product_To_Xml()
        {
            //// Arrange
            var att = new ProductAttribute("Choice1", "choice") { Key = Guid.NewGuid() };
            var attCollection = new ProductAttributeCollection();
            attCollection.Add(att);
            var expected = new ProductVariant(Guid.NewGuid(), attCollection, new CatalogInventoryCollection(), false,
                "Variant", "variant", 5M);
            _product.ProductOptions.Add(new ProductOption("Option1") { Key = Guid.NewGuid() });
            _product.ProductOptions.First(x => x.Name == "Option1").Choices.Add(att);
            _product.ProductVariants.Add(expected);

            //// Act
            var xml = _product.SerializeToXml();
            Console.Write(xml.ToString());

            //// Assert
            Assert.NotNull(xml);
        }