public void BlogPostController_Edit_GIVEN_BlogPostRepository_Contains_BlogPost_With_ID_1_WHEN_blogPostId_Is_1_THEN_Returns_ViewModel_For_BlogPost()
        {
            const int BlogPostId = 1;

            var imageReference = new ImageReference(1, "/noimage.jpg");
            var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" };

            var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author);
            blogPost.SetProperty("Id", BlogPostId);
            blogPost.UpdateSeries(new Series { Name = "Series A" });
            blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } });

            var mockedBlogPostRepository = new Mock<IBlogPostRepository>();
            mockedBlogPostRepository.Setup(r => r.Load(BlogPostId)).Returns(blogPost);

            var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object);
            var result = controller.Edit(blogPost.Id.Value) as ViewResult;

            result.ShouldNotBeNull();
            result.Model.ShouldBeInstanceOfType(typeof(CreateEditModel));

            var model = (CreateEditModel)result.Model;

            model.BlogPostId.ShouldEqual(blogPost.Id);
            model.PublishedDate.ShouldEqual(blogPost.PublishedDate);
            model.Title.ShouldEqual(blogPost.Title);
            model.Description.ShouldEqual(blogPost.Description);
            model.FullArticle.ShouldEqual(blogPost.FullArticle);
            model.ImageId.ShouldEqual(blogPost.ImageReference.Id.Value);
            model.SeriesName.ShouldEqual(blogPost.Series.Name);
        }
        public void BlogPostController_List_GIVEN_BlogPost_Has_1_Item_THEN_Returns_1_ArticleListingViewModel_With_Data_From_Post()
        {
            const int BlogPostId = 1;

            var imageReference = new ImageReference(1, "/noimage.jpg");
            var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" };

            var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author);
            blogPost.SetProperty("Id", BlogPostId);
            blogPost.UpdateSeries(new Series { Name = "Series A" });
            blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } });

            var mockedBlogPostRepository = new Mock<IBlogPostRepository>();

            mockedBlogPostRepository.Setup(r => r.LoadAll()).Returns(new BlogPost[] { blogPost });

            var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object);
            var result = controller.List() as ViewResult;

            result.ShouldNotBeNull();
            result.Model.ShouldBeInstanceOfType(typeof(IEnumerable<BlogPostListingItemModel>));

            var viewModelForPost = ((IEnumerable<BlogPostListingItemModel>)result.Model).First();

            viewModelForPost.BlogPostId.ShouldEqual(blogPost.Id.Value);
            viewModelForPost.Title.ShouldEqual(blogPost.Title);
            viewModelForPost.PublishedDate.ShouldEqual(blogPost.PublishedDate);
            viewModelForPost.AuthorsName.ShouldEqual(blogPost.Author.FullName);
        }
        public void BlogPostController_Edit_Returns_ViewResult()
        {
            const int BlogPostId = 1;

            var imageReference = new ImageReference(1, "/noimage.jpg");
            var author = new BlogUser { Id = 1, Forenames = "Joe", Surname = "Blogs" };

            var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author);
            blogPost.SetProperty("Id", BlogPostId);
            blogPost.UpdateSeries(new Series { Name = "Series A" });
            blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } });

            var mockedBlogPostRepository = new Mock<IBlogPostRepository>();

            mockedBlogPostRepository.Setup(r => r.Load(BlogPostId)).Returns(blogPost);

            var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object);

            var result = controller.Edit(1);

            result.ShouldBeInstanceOfType(typeof(ViewResult));
        }
        public void BlogPostController_Save_WHEN_viewModel_Has_Id_THEN_Loads_Existing_Article_From_BlogPostRepository_Updates_And_Saves()
        {
            const int BlogPostId = 1;
            const int ImageId = 1;
            const int AuthorId = 1;

            const int OldSeriesId = 1;
            const int NewSeriesId = 2;
            const string NewSeriesName = "Series B";

            const int OldTagId = 1;
            const int NewTagId = 2;

            Series newSeries = new Series{Name = NewSeriesName};
            newSeries.SetProperty(s => s.Id, NewSeriesId);

            Tag oldTag = new Tag { Text = "Tag 1" };
            oldTag.SetProperty(t => t.Id, OldTagId);

            Tag newTag = new Tag { Text = "Tag 2" };
            newTag.SetProperty(t => t.Id, NewTagId);

            BlogPost savedBlogPost = null;

            var imageReference = new ImageReference(ImageId, "/noimage.jpg");
            var author = new BlogUser { Id = AuthorId, Forenames = "Joe", Surname = "Blogs" };

            var blogPost = new BlogPost("Test", "This is the description", "Not much in this article", imageReference, DateTime.Now, author);
            blogPost.SetProperty("Id", BlogPostId);

            var series = new Series { Name = "Series A" };
            series.SetProperty(s => s.Id, OldSeriesId);

            blogPost.UpdateSeries(series);
            blogPost.UpdateTags(new Tag[] { new Tag { Text = "Tag 1" } });

            var mockedBlogPostRepository = new Mock<IBlogPostRepository>();

            mockedBlogPostRepository.Setup(r => r.LoadFullArticle(BlogPostId)).Returns(blogPost).Verifiable();
            mockedBlogPostRepository.Setup(r => r.SaveOrUpdate(It.IsAny<BlogPost>())).Callback<BlogPost>(bp => savedBlogPost = bp);

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();

            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);
            mockedLookupRepository.Setup(r => r.LoadForDescription<Series>(It.IsAny<Expression<Func<Series, string>>>(), NewSeriesName)).Returns(newSeries);
            mockedLookupRepository.Setup(r => r.LoadForDescription<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), oldTag.Text)).Returns(oldTag);
            mockedLookupRepository.Setup(r => r.LoadForDescription<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), newTag.Text)).Returns(newTag);

            var controller = MockCreators.CreateBlogPostController(blogPostRepository: mockedBlogPostRepository.Object, lookupRepository: mockedLookupRepository.Object);

            var updatedBlogPost = new CreateEditModel
            {
                BlogPostId = BlogPostId,
                Description = "A different description",
                FullArticle = "A different Full Article",
                ImageId = ImageId,
                PublishedDate = blogPost.PublishedDate,
                SeriesName = NewSeriesName,
                TagTexts = "Tag 2",
                Title = blogPost.Title
            };

            controller.Save(updatedBlogPost);

            mockedBlogPostRepository.Verify(r => r.LoadFullArticle(BlogPostId), Times.Once());
            savedBlogPost.ShouldNotBeNull();

            savedBlogPost.Id.ShouldEqual(BlogPostId);
            savedBlogPost.Description.ShouldEqual(updatedBlogPost.Description);
            savedBlogPost.FullArticle.ShouldEqual(updatedBlogPost.FullArticle);
            savedBlogPost.ImageReference.ShouldEqual(imageReference);
            savedBlogPost.PublishedDate.ShouldEqual(updatedBlogPost.PublishedDate.Value);
            savedBlogPost.Series.ShouldEqual(newSeries);
            savedBlogPost.Tags.ShouldContain((t) => t.TextEquals(newTag));
        }
Exemplo n.º 5
0
        public void UpdateTags_WHEN_tags_Parameter_Is_Null_THEN_Throws_ArgumentNullException()
        {
            var imageReference = GetImageReference();
            var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser());

            post.UpdateTags(null, false);
        }
Exemplo n.º 6
0
        public void UpdateTags_GIVEN_No_Tags_WHEN_tags_Parameter_Contains_two_Tags_With_Differnt_Texts_THEN_Only_Adds_1_Tag()
        {
            var tag1 = new Tag { Text = "Tag1" };
            tag1.SetProperty(t => t.Id, 1);

            var tag2 = new Tag { Text = "Tag1" };
            tag2.SetProperty(t => t.Id, 2);

            var tags = new Tag[] { tag1, tag2 };

            var imageReference = GetImageReference();
            var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser());

            post.UpdateTags(tags);

            post.Tags.Count().ShouldEqual(1);
        }
Exemplo n.º 7
0
        public void UpdateTags_GIVEN_No_Tags_THEN_Adds_tags_Parameter_To_Tags_Property()
        {
            var tag = new Tag { Text = "Tag1" };
            tag.SetProperty(t => t.Id, 1);

            var tags = new Tag[]{ tag };

            var imageReference = GetImageReference();
            var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser());

            post.UpdateTags(tags);

            post.Tags.ShouldContainAll(tags);
        }
Exemplo n.º 8
0
        public void UpdateTags_GIVEN_1_Tag_Exists_With_Text_Tag1_WHEN_Adding_Tag_With_Text_Tag2_THEN_Tags_Property_Contains_Tag1_And_Tag2()
        {
            var tag1 = new Tag { Text = "Tag1" };
            tag1.SetProperty(t => t.Id, 1);

            var tag2 = new Tag { Text = "Tag2" };
            tag2.SetProperty(t => t.Id, 2);

            var imageReference = GetImageReference();
            var post = new BlogPost("ABC", "description", "fullText", imageReference, DateTime.Now, new BlogUser());

            post.UpdateTags(new Tag[] { tag1 });

            post.Tags.ShouldContain(tag1, "Pre-check");

            post.UpdateTags(new Tag[] { tag2 });

            post.Tags.ShouldContain(tag1);
            post.Tags.ShouldContain(tag2);
        }
        private void UpdateTagsOnBlogPost(BlogPost blogPost, string tagTexts)
        {
            var tagList = new List<Tag>();

            if (!String.IsNullOrWhiteSpace(tagTexts))
            {
                string[] seperatedTagTexts = tagTexts.Split(new char[] { TagSeperator }, StringSplitOptions.RemoveEmptyEntries);

                foreach (string tagText in seperatedTagTexts.Select(s => s.Trim()))
                {
                    Tag tag = LookupRepository.LoadForDescription<Tag>(t => t.Text, tagText);

                    if (tag == null && !String.IsNullOrWhiteSpace(tagTexts))
                        tag = new Tag { Text = tagText };

                    tagList.Add(tag);
                }
            }

            blogPost.UpdateTags(tagList, true);
        }