コード例 #1
0
        public void GetEntry_WithEntryNameAndEntryNotInCache_RetrievesFromRepositoryAndInsertsInCache()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, EntryName = "entry-slug", DateSyndicated = DateTime.Now.AddDays(-1)
            };
            var timeZone = new Mock <ITimeZone>();

            timeZone.Setup(tz => tz.Now).Returns(DateTime.Now);
            var blog = new Mock <Blog>();

            blog.Setup(b => b.TimeZone).Returns(timeZone.Object);
            blog.Object.Id = 1001;
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(blog.Object);
            context.Setup(c => c.Cache["EntryNameentry-slugBlogId1001"]).Returns(null);
            context.Setup(c => c.Repository.GetEntry("entry-slug", true /*activeOnly*/, true /*includeCategories*/)).Returns(entry);

            // act
            var cachedEntry = Cacher.GetEntry("entry-slug", context.Object);

            // assert
            Assert.AreEqual(entry, cachedEntry);
            context.Verify(c => c.Cache["EntryNameentry-slugBlogId1001"]);
        }
コード例 #2
0
ファイル: CacherTests.cs プロジェクト: timheuer/Subtext
        public void GetEntry_WithEntryNameAndEntryNotInCacheAndHasPublishDateInFuture_ReturnsNull()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, EntryName = "entry-slug", DatePublishedUtc = DateTime.UtcNow.AddDays(2)
            };
            var timeZone = new Mock <ITimeZone>();

            timeZone.Setup(tz => tz.Now).Returns(DateTime.UtcNow);
            var blog = new Mock <Blog>();

            blog.Setup(b => b.TimeZone).Returns(timeZone.Object);
            blog.Object.Id = 1001;
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(blog.Object);
            context.Setup(c => c.Cache["EntryNameentry-slugBlogId1001"]).Returns(null);
            context.Setup(c => c.Repository.GetEntry("entry-slug", true /*activeOnly*/, true /*includeCategories*/)).Returns(entry);

            // act
            var cachedEntry = Cacher.GetEntry("entry-slug", context.Object);

            // assert
            Assert.IsNull(cachedEntry);
            context.Verify(c => c.Cache["EntryNameentry-slugBlogId1001"]);
        }
コード例 #3
0
        public void GetEntry_WithEntryNameAndEntryInCacheWithPublishDateInFuture_ReturnsNull()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, EntryName = "entry-slug", DateSyndicated = DateTime.Now.AddDays(2)
            };
            var timeZone = new Mock <ITimeZone>();

            timeZone.Setup(tz => tz.Now).Returns(DateTime.Now);
            var blog = new Mock <Blog>();

            blog.Setup(b => b.TimeZone).Returns(timeZone.Object);
            blog.Object.Id = 1001;
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(blog.Object);
            context.Setup(c => c.Cache["EntryNameentry-slugBlogId1001"]).Returns(entry);
            context.Setup(c => c.Repository.GetEntry("entry-slug", true /*activeOnly*/, true /*includeCategories*/)).Throws(new Exception("Repository should not have been accessed"));

            // act
            var cachedEntry = Cacher.GetEntry("entry-slug", context.Object);

            // assert
            Assert.IsNull(cachedEntry);
        }
コード例 #4
0
        public int Create(FeedbackItem comment, bool runFilters)
        {
            Entry entry = Cacher.GetEntry(comment.EntryId, SubtextContext);

            if (entry != null && entry.CommentingClosed)
            {
                return(NullValue.NullInt32);
            }

            ISubtextContext context     = SubtextContext;
            HttpContextBase httpContext = context.HttpContext;

            if (httpContext != null && httpContext.Request != null)
            {
                comment.UserAgent = httpContext.Request.UserAgent;
                comment.IpAddress = HttpHelper.GetUserIpAddress(httpContext);
            }

            if (runFilters)
            {
                comment.FlaggedAsSpam = true; //We're going to start with this assumption.
            }
            comment.Author = HtmlHelper.SafeFormat(comment.Author, context.HttpContext.Server);
            comment.Body   = HtmlHelper.ConvertUrlsToHyperLinks(HtmlHelper.ConvertToAllowedHtml(comment.Body));
            comment.Title  = HtmlHelper.SafeFormat(comment.Title, context.HttpContext.Server);

            // If we are creating this feedback item as part of an import, we want to
            // be sure to use the item's datetime, and not set it to the current time.
            if (NullValue.NullDateTime.Equals(comment.DateCreated))
            {
                comment.DateCreated  = context.Blog.TimeZone.Now;
                comment.DateModified = comment.DateCreated;
            }
            else if (NullValue.NullDateTime.Equals(comment.DateModified))
            {
                comment.DateModified = comment.DateCreated;
            }

            comment.Entry = entry;

            if (runFilters)
            {
                OnBeforeCreate(comment);
            }

            comment.Id = Repository.Create(comment);

            if (runFilters)
            {
                OnAfterCreate(comment);
            }

            return(comment.Id);
        }
コード例 #5
0
ファイル: CommentService.cs プロジェクト: timheuer/Subtext
        public int Create(FeedbackItem comment, bool runFilters)
        {
            Entry entry = Cacher.GetEntry(comment.EntryId, SubtextContext);

            if (entry != null && entry.CommentingClosed)
            {
                return(NullValue.NullInt32);
            }

            ISubtextContext context     = SubtextContext;
            HttpContextBase httpContext = context.HttpContext;

            if (httpContext != null && httpContext.Request != null)
            {
                comment.UserAgent = httpContext.Request.UserAgent;
                comment.IpAddress = HttpHelper.GetUserIpAddress(httpContext);
            }

            if (runFilters)
            {
                comment.FlaggedAsSpam = true; //We're going to start with this assumption.
            }
            comment.Author          = HtmlHelper.SafeFormat(comment.Author, context.HttpContext.Server);
            comment.Body            = HtmlHelper.ConvertUrlsToHyperLinks(HtmlHelper.ConvertToAllowedHtml(comment.Body));
            comment.Title           = HtmlHelper.SafeFormat(comment.Title, context.HttpContext.Server);
            comment.Entry           = entry;
            comment.DateCreatedUtc  = comment.DateCreatedUtc.IsNull() ? DateTime.UtcNow : comment.DateCreatedUtc;
            comment.DateModifiedUtc = comment.DateModifiedUtc.IsNull() ? DateTime.UtcNow : comment.DateModifiedUtc;

            if (runFilters)
            {
                OnBeforeCreate(comment);
            }

            comment.Id = Repository.Create(comment);

            if (runFilters)
            {
                OnAfterCreate(comment);
            }

            return(comment.Id);
        }
コード例 #6
0
        public void GetEntry_WithEntryIdAndEntryInCache_RetrievesFromCache()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, Title = "Testing Cacher"
            };
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(new Blog {
                Id = 1001
            });
            context.Setup(c => c.Cache["Entry111BlogId1001"]).Returns(entry);
            context.Setup(c => c.Repository.GetEntry(111, true /*activeOnly*/, true /*includeCategories*/)).Throws(new Exception("Repository should not have been accessed"));

            // act
            var cachedEntry = Cacher.GetEntry(111, context.Object);

            // assert
            Assert.AreEqual(entry, cachedEntry);
        }
コード例 #7
0
        public void GetEntry_WithEntryIdAndEntryNotInCache_RetrievesFromRepositoryAndInsertsInCache()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, Title = "Testing Cacher"
            };
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(new Blog {
                Id = 1001
            });
            context.Setup(c => c.Cache["Entry111BlogId1001"]).Returns(null);
            context.Setup(c => c.Repository.GetEntry(111, true /*activeOnly*/, true /*includeCategories*/)).Returns(entry);

            // act
            var cachedEntry = Cacher.GetEntry(111, context.Object);

            // assert
            Assert.AreEqual(entry, cachedEntry);
            context.Verify(c => c.Cache["Entry111BlogId1001"]);
        }
コード例 #8
0
ファイル: CacherTests.cs プロジェクト: timheuer/Subtext
        public void GetEntry_WithEntryNameAndEntryInCache_RetrievesFromCache()
        {
            // arrange
            var entry = new Entry(PostType.BlogPost)
            {
                Id = 111, EntryName = "entry-slug", DatePublishedUtc = DateTime.UtcNow.AddDays(-1)
            };
            var blog = new Mock <Blog>();

            blog.Object.Id = 1001;
            var context = new Mock <ISubtextContext>();

            context.Setup(c => c.Blog).Returns(blog.Object);
            context.Setup(c => c.Cache["EntryNameentry-slugBlogId1001"]).Returns(entry);
            context.Setup(c => c.Repository.GetEntry("entry-slug", true /*activeOnly*/, true /*includeCategories*/)).Throws(new Exception("Repository should not have been accessed"));

            // act
            var cachedEntry = Cacher.GetEntry("entry-slug", context.Object);

            // assert
            Assert.AreEqual(entry, cachedEntry);
        }