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 Ctor_WHEN_url_Has_Value_THEN_Sets_Url_Property()
        {
            const string url = "http://www.jumbleblocks.co.uk/noimage.jpg";

            var imageReference = new ImageReference(url);

            imageReference.Url.ShouldEqual(url);
        }
        private Mock<IImageReferenceRepository> GetMockedImageReferenceRepositoryWith4Images()
        {
            var imageReferenceA = new ImageReference(1, "~/BlogImages/a.png");
            var imageReferenceB = new ImageReference(2, "~/BlogImages/b.png");
            var imageReferenceC = new ImageReference(3, "~/BlogImages/c.png");
            var imageReferenceD = new ImageReference(4, "~/BlogImages/d.png");

            var imageReferenceArray =  new ImageReference[]
            {
                imageReferenceA,
                imageReferenceB,
                imageReferenceC,
                imageReferenceD
            };

            var mockedImageReferenceRepository = new Mock<IImageReferenceRepository>();

            mockedImageReferenceRepository.Setup(r => r.LoadRange(It.IsAny<int>(), It.IsAny<int>()))
                .Returns<int, int>((skip, take) => imageReferenceArray.Skip(skip).Take(take));

            return mockedImageReferenceRepository;
        }
        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_Two_Tags_Seperated_By_SemiColon_THEN_Adds_Both_To_Saved_BlogPost()
        {
            const string Tag1Text = "tag1";
            const string Tag2Text = "tag2";

            const int ImageId = 1;
            BlogPost savedPost = null;

            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<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), It.IsAny<string>())).Returns(null as Tag);

            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,
                SeriesName = "HIJ",
                TagTexts = String.Format("{0};{1}", Tag1Text, Tag2Text)
            };

            controller.Save(model);

            savedPost.ShouldNotBeNull();
            savedPost.Tags.Count().ShouldEqual(2);
            savedPost.Tags.ShouldContain( t => t.Text == Tag1Text, String.Format("Does not contain {0}", Tag1Text));
            savedPost.Tags.ShouldContain( t => t.Text == Tag2Text, String.Format("Does not contain {0}", Tag2Text));
        }
        public void BlogPostController_Save_WHEN_TagTexts_Is_Null_THEN_Still_Saves_BlogPost()
        {
            const int ImageId = 1;
            BlogPost savedPost = null;

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

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);

            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,
                SeriesName = "HIJ",
                TagTexts = null
            };

            controller.Save(model);

            savedPost.ShouldNotBeNull();
        }
        public void BlogPostController_Save_WHEN_No_Tags_THEN_Saves_No_Tags()
        {
            const int ImageId = 1;
            BlogPost savedPost = null;

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

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);

            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 = "Title",
                Description = "Description",
                FullArticle = "Full Article",
                ImageId = ImageId,
                SeriesName = "Series",
                TagTexts = String.Empty
            };

            controller.Save(model);

            savedPost.ShouldNotBeNull();
            savedPost.Tags.Count().ShouldEqual(0);
        }
        public void BlogPostController_Save_WHEN_BlogPostRepository_Throws_Exception_THEN_Returns_ErrorMessage_On_ViewBag()
        {
            const int ImageId = 1;

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

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);

            var mockedBlogPostRepository = MockCreators.CreateMockedBlogPostRepository();
            mockedBlogPostRepository.Setup(r => r.SaveOrUpdate(It.IsAny<BlogPost>())).Throws<Exception>();

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

            var model = new CreateEditModel()
            {
                Title = "123",
                Description = "456",
                FullArticle = "ABC",
                ImageId = ImageId,
                SeriesName = "HIJ",
                TagTexts = "KLM"
            };

            var result = controller.Save(model) as ViewResult;

            string errorMessage = result.ViewBag.ErrorMessage;

            errorMessage.ShouldNotBeNullOrEmpty();
            errorMessage.ShouldEqual("Exception occured while saving blog post. Please try again.");
        }
        public void BlogPostController_Save_WHEN_BlogPostRepository_Throws_Exception_THEN_Returns_CreateEdit_View()
        {
            const int ImageId = 1;

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

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);

            var mockedBlogPostRepository = MockCreators.CreateMockedBlogPostRepository();
            mockedBlogPostRepository.Setup(r => r.SaveOrUpdate(It.IsAny<BlogPost>())).Throws<Exception>();

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

            var model = new CreateEditModel()
            {
                Title = "123",
                Description = "456",
                FullArticle = "ABC",
                ImageId = ImageId,
                SeriesName = "HIJ",
                TagTexts = "KLM"
            };

            var result = controller.Save(model) as ViewResult;

            result.ViewName.ShouldEqual("CreateEdit");
        }
        public void BlogPostController_Save_GIVEN_TagRepository_Has_Tag_ABC_WHEN_Tag_ABC_THEN_Loads_ABC_Tag_From_TagRepository_AND_Adds_To_BlogPost()
        {
            const int ImageId = 1;
            BlogPost savedPost = null;
            Tag tag = new Tag { Text = "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<Tag>(It.IsAny<Expression<Func<Tag, string>>>(), tag.Text)).Returns(tag);

            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,
                SeriesName = "HIJ",
                TagTexts = tag.Text
            };

            controller.Save(model);

            savedPost.ShouldNotBeNull();
            savedPost.Tags.First().ShouldEqual(tag);
        }
        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 BlogPostController_Save_GIVEN_ILookupRepository_LoadForId_ImageReference_Contains_Entry_For_Id_1_WHEN_ImageId_Is_1_THEN_Sets_The_ImageReference_On_The_BlogPost_To_That_Image()
        {
            const int ImageId = 1;

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

            var mockedLookupRepository = MockCreators.CreateMockedLookupRepository();
            mockedLookupRepository.Setup(r => r.LoadForId<ImageReference>(ImageId)).Returns(imageReference);

            BlogPost savedBlogPost = null;

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

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

            var model = new CreateEditModel()
            {
                Title = "123",
                Description = "456",
                FullArticle = "ABC",
                ImageId = ImageId,
                SeriesName = "HIJ",
                TagTexts = "KLM"
            };

            controller.Save(model);

            savedBlogPost.ShouldNotBeNull();
            savedBlogPost.ImageReference.ShouldEqual(imageReference);
        }
        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));
        }
예제 #15
0
 /// <summary>
 /// Checks to see if image reference id's match
 /// </summary>
 /// <param name="other">other Image reference to check</param>
 /// <returns>true if equal, otherwise false</returns>
 public virtual bool IdsEqual(ImageReference other)
 {
     return Id.Equals(other.Id);
 }