/// <summary>
        /// The get shim http context base, used for tests. Must be encapsulated in
        /// using (ShimsContext.Create())
        /// {}
        /// statement.
        /// </summary>
        /// <param name="useCookie">
        /// Set to true if cookies are used.
        /// </param>
        /// <returns>
        /// Shimmed httpContextBase. <see cref="HttpContextBase"/>.
        /// </returns>
        private static HttpContextBase GetStubHttpContextBase(bool useCookie = true)
        {
            var cookie = new HttpCookieCollection();

            cookie.Add(new HttpCookie("CultureInfo", "en-GB"));
            var queryString = new NameValueCollection();

            queryString.Add("error", "false");
            queryString.Add("handler", "true");
            var cultureInfo = new HttpCookie("CultureInfo", "en-GB");

            if (useCookie)
            {
                cookie.Add(cultureInfo);
            }

            HttpRequestBase stubHttpRequestBase = new System.Web.Fakes.StubHttpRequestBase()
            {
                CookiesGet     = () => { return(cookie); },
                FormGet        = () => { return(new NameValueCollection()); },
                QueryStringGet = () => { return(queryString); }
            };
            // Might be needed? instead of stubHttpRequestBase: HttpRequestWrapper httpRequestWrapper = new System.Web.Fakes.ShimHttpRequestWrapper()
            // {
            //    CookiesGet = () => { return cookie; },
            //    FormGet = () => { return new NameValueCollection(); },
            //    QueryStringGet = () => { return queryString; }
            // };

            HttpResponseBase response = new System.Web.Fakes.StubHttpResponseBase()
            {
                CookiesGet = () => { return(cookie); }
            };
            HttpServerUtilityBase untilityBase = new System.Web.Fakes.StubHttpServerUtilityBase()
            {
                UrlEncodeString = (info) => { return(cultureInfo.ToString()); }
            };

            HttpContextBase stubHttpContext = new System.Web.Fakes.StubHttpContextBase()
            {
                RequestGet  = () => { return(stubHttpRequestBase); },
                ResponseGet = () => { return(response); },
                ServerGet   = () => { return(untilityBase); }
            };

            return(stubHttpContext);
        }
        /// <summary>
        /// The get shim http context base, used for tests. Must be encapsulated in
        /// using (ShimsContext.Create())
        /// {}
        /// statement.
        /// </summary>
        /// <param name="useCookie">
        /// Set to true if cookies are used.
        /// </param>
        /// <returns>
        /// Shimmed httpContextBase. <see cref="HttpContextBase"/>.
        /// </returns>
        private static HttpContext GetShimHttpContext(bool useCookie = true)
        {
            string[] allFactorEnumFieldValueKeys  = new string[] { "factorEnumFieldValue_1091_0_0_1097_2" };
            string[] allFactorEnumFieldValueKeys2 = new string[] { "factorEnumFieldValue2_1091_0_0_1097_2" };
            string[] allFactorEnumFieldValueKeys3 = new string[] { "factorEnumFieldValue3_1091_0_0_1097_2" };
            string   key1 = "1";
            string   key2 = "2";
            string   key3 = "3";

            NameValueCollection nameValueCollection = new NameValueCollection();

            nameValueCollection.Add("factorEnumFieldValue_1091_0_0_1097_2", key1);
            nameValueCollection.Add("factorEnumFieldValue2_1091_0_0_1097_2", key2);
            nameValueCollection.Add("factorEnumFieldValue3_1091_0_0_1097_2", key3);

            //var mockHttpContext = MockRepository.GenerateStub<HttpContextBase>();
            //mockHttpContext.Stub(c => c.Request.Form.AllKeys).Return(allFactorEnumFieldValueKeys).Repeat.Any();
            ////mockHttpContext.Stub(c => c.Request.Form.AllKeys.Where(key => key.StartsWith("factorEnumFieldValue_"))).Return(allFactorEnumFieldValueKeys).Repeat.Any();
            ////mockHttpContext.Stub(c => c.Request.Form.AllKeys.Where(key => key.StartsWith("factorEnumFieldValue2_"))).Return(allFactorEnumFieldValueKeys2).Repeat.Any();
            ////mockHttpContext.Stub(c => c.Request.Form.AllKeys.Where(key => key.StartsWith("factorEnumFieldValue3_"))).Return(allFactorEnumFieldValueKeys3).Repeat.Any();
            //
            //mockHttpContext.Stub(c => c.Request.Form).Return(value).Repeat.Any();



            var cookie = new HttpCookieCollection();

            cookie.Add(new HttpCookie("CultureInfo", "en-GB"));
            var queryString = new NameValueCollection();

            queryString.Add("error", "false");
            queryString.Add("handler", "true");
            var cultureInfo = new HttpCookie("CultureInfo", "en-GB");

            if (useCookie)
            {
                cookie.Add(cultureInfo);
            }

            HttpRequest stubHttpRequestBase = new System.Web.Fakes.ShimHttpRequest()
            {
                CookiesGet = () => { return(cookie); },
                FormGet    = () =>
                {
                    if (true)
                    {
                        return(nameValueCollection);
                    }
                    else
                    {
                        return(nameValueCollection);
                    }
                },
                QueryStringGet = () => { return(queryString); },
            };


            HttpResponse response = new System.Web.Fakes.ShimHttpResponse()
            {
                CookiesGet = () => { return(cookie); }
            };
            HttpServerUtilityBase untilityBase = new System.Web.Fakes.StubHttpServerUtilityBase()
            {
                UrlEncodeString = (info) => { return(cultureInfo.ToString()); },
                MapPathString   = (path) => { return(SelectedPath); }
            };
            HttpServerUtility untility = new System.Web.Fakes.ShimHttpServerUtility()
            {
                UrlEncodeString = (info) => { return(cultureInfo.ToString()); },
                MapPathString   = (path) => { return(SelectedPath); },
            };



            HttpApplicationState state = new System.Web.Fakes.ShimHttpApplicationState()
            {
                AddStringObject = (cacheKey, userContext) =>
                {
                    IUserContext tempContext = userContext as IUserContext;

                    if (tempContext.Locale.Id == DyntaxaTestSettings.Default.SwedishLocaleId)
                    {
                        ApplicationUserContextSV = tempContext;
                    }
                    else
                    {
                        ApplicationUserContext = tempContext;
                    }
                }
            };

            var context = new ShimHttpContext
            {
                ApplicationGet = () => { return(state); },
                RequestGet     = () => { return(stubHttpRequestBase); },
                ResponseGet    = () => { return(response); },
                ServerGet      = () => { return(untility); }
            };

            ShimHttpContext.CurrentGet = () =>
            {
                return(context);
            };


            // Create session varables
            var session = new System.Web.SessionState.Fakes.ShimHttpSessionState()
            {
                ItemGetString = (key) =>
                {
                    if (key == DyntaxaSettings.Default.ApplicationContextCacheKey)
                    {
                        return(ApplicationUserContext);
                    }
                    else if (key == DyntaxaSettings.Default.ApplicationContextCacheKey + DyntaxaTestSettings.Default.EnglishLocale)
                    {
                        return(ApplicationUserContext);
                    }
                    else if (key == DyntaxaSettings.Default.ApplicationContextCacheKey + DyntaxaTestSettings.Default.SwedishLocale)
                    {
                        return(ApplicationUserContextSV);
                    }
                    else if (key == "userContext")
                    {
                        if (UserContextData.IsNotNull())
                        {
                            return(UserContextData);
                        }
                        else
                        {
                            return(ApplicationUserContext);
                        }
                    }
                    else if (key == "RevisionId")
                    {
                        return(SessionRevisionId);
                    }
                    else if (key == "TaxonId")
                    {
                        return(SessionTaxonId);
                    }
                    else if (key == "Revision")
                    {
                        return(SessionRevision);
                    }
                    else if (key == "SpeciesFactHostTaxonIdList")
                    {
                        return(SessionSpeciesFactHostTaxonIdList);
                    }
                    return(null);
                },
                ItemSetStringObject = (key, sessionObject) =>
                {
                    if (key == "TaxonId")
                    {
                        SessionTaxonId = sessionObject as TaxonIdTuple;
                    }
                },
            };


            System.Web.Fakes.ShimHttpContext.AllInstances.SessionGet =
                (o) =>
            {
                return(session);
            };

            // Creat cash varables
            var cache = new System.Web.Caching.Fakes.ShimCache()
            {
                ItemGetString = (key) =>
                {
                    if (key == DyntaxaSettings.Default.ApplicationContextCacheKey)
                    {
                        return(ApplicationUserContext);
                    }
                    else if (key == DyntaxaSettings.Default.ApplicationContextCacheKey + DyntaxaTestSettings.Default.EnglishLocale)
                    {
                        return(ApplicationUserContext);
                    }
                    else if (key == DyntaxaSettings.Default.ApplicationContextCacheKey + DyntaxaTestSettings.Default.SwedishLocale)
                    {
                        return(ApplicationUserContextSV);
                    }
                    else if (key == "userContext")
                    {
                        if (UserContextData.IsNotNull())
                        {
                            return(UserContextData);
                        }
                        else
                        {
                            return(ApplicationUserContext);
                        }
                    }
                    else if (key == "RevisionId")
                    {
                        return(SessionRevisionId);
                    }
                    else if (key == "TaxonId")
                    {
                        return(SessionTaxonId);
                    }
                    else if (key == "Revision")
                    {
                        return(SessionRevision);
                    }
                    else if (key == "SpeciesFactHostTaxonIdList")
                    {
                        return(SessionSpeciesFactHostTaxonIdList);
                    }
                    return(null);
                },
            };


            System.Web.Fakes.ShimHttpContext.AllInstances.CacheGet =
                (o) =>
            {
                return(cache);
            };

            return(context);
        }