public void BlogPostController_Save_GIVEN_SeriesRepository_Has_Series_ABC_WHEN_Series_ABC_THEN_Loads_ABC_Series_From_SeriesRepository_AND_Adds_To_BlogPost()
        {
            const int ImageId = 1;
            BlogPost savedPost = null;
            Series series = new Series { Name = "ABC" };

            var imageReference = new ImageReference(ImageId, "~/1.jpg");

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);
            mockedLookupRepository.Setup(r => r.LoadForDescription(It.IsAny<Expression<Func<Series, string>>>(), series.Name)).Returns(series);

            var mockedBlogPostRepository = MockCreators.CreateMockedBlogPostRepository();
            mockedBlogPostRepository.Setup(r => r.SaveOrUpdate(It.IsAny<BlogPost>()))
                .Callback<BlogPost>(bp => savedPost = bp);

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

            var model = new CreateEditModel()
            {
                Title = "123",
                Description = "456",
                FullArticle = "ABC",
                ImageId = ImageId,
                TagTexts = String.Empty,
                SeriesName = series.Name
            };

            controller.Save(model);

            savedPost.ShouldNotBeNull();
            savedPost.Series.ShouldEqual(series);
        }
        public void UpdateSeries_Sets_Series_Property_To_series_parameter()
        {
            var series = new Series { Name = "Test" };

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

            post.UpdateSeries(series);
            post.Series.ShouldEqual(series);
        }
        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));
        }
        private void UpdateSeriesOnBlogPost(BlogPost blogPost, string seriesName)
        {
            if (!String.IsNullOrWhiteSpace(seriesName) && !blogPost.SeriesNameEqual(seriesName))
            {
                Series seriesFound = LookupRepository.LoadForDescription<Series>(s => s.Name, seriesName);

                if (seriesFound == null)
                    seriesFound = new Series { Name = seriesName };

                blogPost.UpdateSeries(seriesFound);
            }
        }