Example #1
0
        public void Passes_ArrayEqual_Success()
        {
            var array1 = new[] { 1, 2, 3, 4, 5 };
            var array2 = new[] { 1, 2, 3, 4, 5 };

            CommonHelper.ArraysEqual(array1, array2).TestBeTrue();
        }
Example #2
0
        public void CanEnsureArraysEqual()
        {
            // Ensure the checker can verify that two arrays are equal
            CommonHelper.ArraysEqual(new int[] {}, new int[] {}).ShouldBeTrue();
            CommonHelper.ArraysEqual(new[] { 1, 2, 3 }, new[] { 1, 2, 3 }).ShouldBeTrue();

            // Ensure the checker can verify that arrays can not be equal
            CommonHelper.ArraysEqual(new[] { 1 }, new[] { 1, 2 }).ShouldBeFalse();
            CommonHelper.ArraysEqual(new[] { 1, 2, 3 }, new[] { 3, 2, 1 }).ShouldBeFalse();
        }
        public void ArraysEqualTest()
        {
            //return true because: the same object reference/ address
            int[] array01 = { 1, 23, 2 };
            Assert.AreSame(array01, array01);                                   //the same address == the same object
            Assert.IsTrue(CommonHelper.ArraysEqual <int>(array01, array01));    //the same address == the same object

            //return true because: the same object values
            int[] array02 = { 1, 23, 2 };
            Assert.AreNotSame(array01, array02);                               //not the same object !
            Assert.IsTrue(CommonHelper.ArraysEqual <int>(array01, array02));   //still the same values

            //return false because: different number of elements
            int[] array03 = { 11, 23, 2, 4, 5, 5, 6 };
            int[] array04 = { 99, 23, 2, 4 };
            Assert.IsFalse(CommonHelper.ArraysEqual <int>(array03, array04));

            //return false because: different values
            int[] array05 = { 11, 23, 2, 4, 5, 5, 6 };
            int[] array06 = { 99, 23, 2, 4, 5, 5, 6 };
            Assert.IsFalse(CommonHelper.ArraysEqual <int>(array05, array06));
        }
        public virtual async Task <IList <string> > GetShoppingCartItemAttributeWarnings(Customer customer,
                                                                                         Product product, ShoppingCartItem shoppingCartItem, bool ignoreNonCombinableAttributes = false)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            var warnings = new List <string>();

            //ensure it's our attributes
            var attributes1 = _productAttributeParser.ParseProductAttributeMappings(product, shoppingCartItem.Attributes).ToList();

            if (product.ProductTypeId == ProductType.BundledProduct)
            {
                foreach (var bundle in product.BundleProducts)
                {
                    var p1 = await _productService.GetProductById(bundle.ProductId);

                    if (p1 != null)
                    {
                        var a1 = _productAttributeParser.ParseProductAttributeMappings(p1, shoppingCartItem.Attributes).ToList();
                        attributes1.AddRange(a1);
                    }
                }
            }
            if (ignoreNonCombinableAttributes)
            {
                attributes1 = attributes1.Where(x => !x.IsNonCombinable()).ToList();
            }

            //foreach (var attribute in attributes1)
            //{
            //    if (string.IsNullOrEmpty(attribute.ProductId))
            //    {
            //        warnings.Add("Attribute error");
            //        return warnings;
            //    }
            //}

            //validate required product attributes (whether they're chosen/selected/entered)
            var attributes2 = product.ProductAttributeMappings.ToList();

            if (product.ProductTypeId == ProductType.BundledProduct)
            {
                foreach (var bundle in product.BundleProducts)
                {
                    var p1 = await _productService.GetProductById(bundle.ProductId);

                    if (p1 != null && p1.ProductAttributeMappings.Any())
                    {
                        attributes2.AddRange(p1.ProductAttributeMappings);
                    }
                }
            }
            if (ignoreNonCombinableAttributes)
            {
                attributes2 = attributes2.Where(x => !x.IsNonCombinable()).ToList();
            }
            //validate conditional attributes only (if specified)
            attributes2 = attributes2.Where(x =>
            {
                var conditionMet = _productAttributeParser.IsConditionMet(product, x, shoppingCartItem.Attributes);
                return(!conditionMet.HasValue || conditionMet.Value);
            }).ToList();
            foreach (var a2 in attributes2)
            {
                if (a2.IsRequired)
                {
                    bool found = false;
                    //selected product attributes
                    foreach (var a1 in attributes1)
                    {
                        if (a1.Id == a2.Id)
                        {
                            var attributeValuesStr = _productAttributeParser.ParseValues(shoppingCartItem.Attributes, a1.Id);
                            foreach (string str1 in attributeValuesStr)
                            {
                                if (!String.IsNullOrEmpty(str1.Trim()))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                    }

                    //if not found
                    if (!found)
                    {
                        var paa = await _productAttributeService.GetProductAttributeById(a2.ProductAttributeId);

                        if (paa != null)
                        {
                            var notFoundWarning = !string.IsNullOrEmpty(a2.TextPrompt) ?
                                                  a2.TextPrompt :
                                                  string.Format(_translationService.GetResource("ShoppingCart.SelectAttribute"), paa.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id));

                            warnings.Add(notFoundWarning);
                        }
                    }
                }

                if (a2.AttributeControlTypeId == AttributeControlType.ReadonlyCheckboxes)
                {
                    //customers cannot edit read-only attributes
                    var allowedReadOnlyValueIds = a2.ProductAttributeValues
                                                  .Where(x => x.IsPreSelected)
                                                  .Select(x => x.Id)
                                                  .ToArray();

                    var selectedReadOnlyValueIds = _productAttributeParser.ParseProductAttributeValues(product, shoppingCartItem.Attributes)
                                                   //.Where(x => x.ProductAttributeMappingId == a2.Id)
                                                   .Select(x => x.Id)
                                                   .ToArray();

                    if (!CommonHelper.ArraysEqual(allowedReadOnlyValueIds, selectedReadOnlyValueIds))
                    {
                        warnings.Add("You cannot change read-only values");
                    }
                }
            }

            //validation rules
            foreach (var pam in attributes2)
            {
                if (!pam.ValidationRulesAllowed())
                {
                    continue;
                }

                //minimum length
                if (pam.ValidationMinLength.HasValue)
                {
                    if (pam.AttributeControlTypeId == AttributeControlType.TextBox ||
                        pam.AttributeControlTypeId == AttributeControlType.MultilineTextbox)
                    {
                        var valuesStr         = _productAttributeParser.ParseValues(shoppingCartItem.Attributes, pam.Id);
                        var enteredText       = valuesStr.FirstOrDefault();
                        int enteredTextLength = String.IsNullOrEmpty(enteredText) ? 0 : enteredText.Length;

                        if (pam.ValidationMinLength.Value > enteredTextLength)
                        {
                            var _pam = await _productAttributeService.GetProductAttributeById(pam.ProductAttributeId);

                            warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.TextboxMinimumLength"), _pam.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id), pam.ValidationMinLength.Value));
                        }
                    }
                }

                //maximum length
                if (pam.ValidationMaxLength.HasValue)
                {
                    if (pam.AttributeControlTypeId == AttributeControlType.TextBox ||
                        pam.AttributeControlTypeId == AttributeControlType.MultilineTextbox)
                    {
                        var valuesStr         = _productAttributeParser.ParseValues(shoppingCartItem.Attributes, pam.Id);
                        var enteredText       = valuesStr.FirstOrDefault();
                        int enteredTextLength = string.IsNullOrEmpty(enteredText) ? 0 : enteredText.Length;

                        if (pam.ValidationMaxLength.Value < enteredTextLength)
                        {
                            var _pam = await _productAttributeService.GetProductAttributeById(pam.ProductAttributeId);

                            warnings.Add(string.Format(_translationService.GetResource("ShoppingCart.TextboxMaximumLength"), _pam.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id), pam.ValidationMaxLength.Value));
                        }
                    }
                }
            }

            if (warnings.Any())
            {
                return(warnings);
            }

            //validate bundled products
            var attributeValues = _productAttributeParser.ParseProductAttributeValues(product, shoppingCartItem.Attributes);

            foreach (var attributeValue in attributeValues)
            {
                var _productAttributeMapping = product.ProductAttributeMappings.Where(x => x.ProductAttributeValues.Any(z => z.Id == attributeValue.Id)).FirstOrDefault();
                //TODO - check product.ProductAttributeMappings.Where(x => x.Id == attributeValue.ProductAttributeMappingId).FirstOrDefault();
                if (attributeValue.AttributeValueTypeId == AttributeValueType.AssociatedToProduct && _productAttributeMapping != null)
                {
                    if (ignoreNonCombinableAttributes && _productAttributeMapping.IsNonCombinable())
                    {
                        continue;
                    }

                    //associated product (bundle)
                    var associatedProduct = await _productService.GetProductById(attributeValue.AssociatedProductId);

                    if (associatedProduct != null)
                    {
                        var totalQty = shoppingCartItem.Quantity * attributeValue.Quantity;
                        var associatedProductWarnings = await GetShoppingCartItemWarnings(customer, new ShoppingCartItem()
                        {
                            ShoppingCartTypeId = shoppingCartItem.ShoppingCartTypeId,
                            StoreId            = _workContext.CurrentStore.Id,
                            Quantity           = totalQty,
                            WarehouseId        = shoppingCartItem.WarehouseId
                        }, associatedProduct, new ShoppingCartValidatorOptions());

                        foreach (var associatedProductWarning in associatedProductWarnings)
                        {
                            var productAttribute = await _productAttributeService.GetProductAttributeById(_productAttributeMapping.ProductAttributeId);

                            var attributeName      = productAttribute.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id);
                            var attributeValueName = attributeValue.GetTranslation(a => a.Name, _workContext.WorkingLanguage.Id);
                            warnings.Add(string.Format(
                                             _translationService.GetResource("ShoppingCart.AssociatedAttributeWarning"),
                                             attributeName, attributeValueName, associatedProductWarning));
                        }
                    }
                    else
                    {
                        warnings.Add(string.Format("Associated product cannot be loaded - {0}", attributeValue.AssociatedProductId));
                    }
                }
            }

            return(warnings);
        }
Example #5
0
        public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            var cinfo = new ClientInfo();

            //Collect any info that passed in the headers from Client, if any.

            cinfo.ServerTimeStamp = DateTime.Now; //Timestamp after the call is received.
            cinfo.Platform        = "WCF";
            OperationDescription operationDesc = GetOperationDescription(OperationContext.Current);

            if (operationDesc != null)
            {
                Type contractType = operationDesc.DeclaringContract.ContractType;
                cinfo.Action       = operationDesc.Name;
                cinfo.TypeName     = contractType.FullName;
                cinfo.AssemblyName = contractType.Assembly.GetName().Name;
                var syncMethod          = operationDesc.SyncMethod;
                AuditDataChanges[] attr = null;
                // Eger OperationContract te verilen Method Name ile methodun orjinal adi farkli ise bu kontrol gerekiyor. yoksa methodu bulamiyor.
                var methods = contractType.GetMethods();
                if (!operationDesc.Name.Equals(syncMethod.Name) || methods.Count() > 1)
                {
                    var mi = methods.First(method => method.Name == syncMethod.Name && !method.IsGenericMethod && CommonHelper.ArraysEqual(method.GetParameters(), syncMethod.GetParameters()));
                    if (mi != null)
                    {
                        attr = mi.GetCustomAttributes(typeof(AuditDataChanges), false) as AuditDataChanges[];
                    }
                }
                else
                {
                    attr = contractType.GetMethod(operationDesc.Name).GetCustomAttributes(typeof(AuditDataChanges), false) as AuditDataChanges[];
                }

                if (attr != null && attr.Length > 0)
                {
                    cinfo.IsLogEnable       = true;
                    ServiceBase.AuditEnable = true;
                }
            }

            cinfo.ServerName        = Dns.GetHostName();
            cinfo.ServerProcessName = System.AppDomain.CurrentDomain.FriendlyName;

            if (OperationContext.Current != null)
            {
                var index = OperationContext.Current.IncomingMessageHeaders.FindHeader(ServiceBase.ClientComputerInfoKey, ServiceBase.ClientComputerInfoNamespaceKey);

                if (index > 0)
                {
                    var clientComputerInfo = OperationContext.Current.IncomingMessageHeaders.GetHeader <ClientComputerInfo>(index);
                    cinfo.ClientComputerInfo = clientComputerInfo;
                }
            }

            var messageProperty = OperationContext.Current.IncomingMessageProperties[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;

            if (messageProperty != null)
            {
                cinfo.ClientIp = messageProperty.Address;
            }
            ServiceBase.ClientInfo = cinfo;

            return(cinfo);
        }