public void Can_Filter_BlogPosts_By_Tag() { // Arrange: Given a series of blogposts with tags associated, a repository and a blog controller var tag = new Tag {TagID = 1, Name = "tag 1"}; var tag2 = new Tag {TagID = 2, Name = "tag 2"}; var blogPost = new BlogPost {ContentID = 1}; var blogPost2 = new BlogPost() {ContentID = 2}; var blogPost3 = new BlogPost() {ContentID = 3}; blogPost.Tags.Add(tag); blogPost2.Tags.Add(tag2); blogPost3.Tags.Add(tag); blogPost3.Tags.Add(tag2); // mock blog post repository var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(blogPost, blogPost2, blogPost3); moqBlogPostRepository. Setup(x => x.GetQueryableBlogPostByTagName(tag.Name)). // when a user wants to filter by tag Returns((new List<BlogPost> {blogPost, blogPost3}).AsQueryable()); // They will obtain the filtered blogposts var controller = new BlogController(moqBlogPostRepository.Object); // Act: when the user clicks on a tag var result = controller.Tag("tag-1"); // Assert: the result will be a view with a list of blogposts filtered by tag var model = ((BlogPostViewModel) result.ViewData.Model).BlogPostPagedList; model.Count.ShouldEqual(2); model.Single(x => x.ContentID == 1).ContentID.ShouldEqual(1); model.Single(x => x.ContentID == 3).ContentID.ShouldEqual(3); Assert.IsNull(model.FirstOrDefault(x => x.ContentID == 2)); }
public void Can_Filter_Blogposts_By_Category() { // Arrange: Given a series of blogs associated with a categories, a repository that manages // the access to those blogposts, and a blog controller var category = new Category {CategoryID = 1, Name = "category 1"}; var category2 = new Category {CategoryID = 2, Name = "category 2"}; var blogPost = new BlogPost { ContentID = 1 }; var blogPost2 = new BlogPost() { ContentID = 2 }; var blogPost3 = new BlogPost() { ContentID = 3 }; blogPost.Categories.Add(category); blogPost2.Categories.Add(category2); blogPost3.Categories.Add(category); var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(blogPost, blogPost2, blogPost3); moqBlogPostRepository. Setup(x => x.GetQueryableBlogPostByCategory(category.Name)). // when a user wants to filter by category Returns((new List<BlogPost> {blogPost, blogPost3}).AsQueryable()); // They will obtain the filtered blogposts var controller = new BlogController(moqBlogPostRepository.Object); // Act: when the user clicks on category var result = controller.Category("category-1"); // Assert: the result will be a view with a list of blogposts filtered by category var model = ((BlogPostViewModel) result.ViewData.Model).BlogPostPagedList; model.Count.ShouldEqual(2); model.Single(x => x.ContentID == 1).ContentID.ShouldEqual(1); model.Single(x => x.ContentID == 3).ContentID.ShouldEqual(3); Assert.IsNull(model.FirstOrDefault(x => x.ContentID == 2)); }
/// <summary> /// Adds a blog post to the context with its categories /// </summary> /// <param name="blogPost"></param> /// <param name="categories"></param> /// <param name="listOfTags"></param> public void AddBlogPost(BlogPost blogPost, int[] categories, string listOfTags) { // add blogPost to Context blogPost.IsDraft = true; Add(blogPost); // add categories AddCategoriesToContentContainer(blogPost, categories); // add tags IEnumerable<string> formattedListOfTags = ExtractTagsFromString(listOfTags); AddListOfTagsToContentContainer(blogPost, formattedListOfTags); }
public void GivenASeriesOfTags() { Tag1 = new Tag {Name = "tag 1", TagID = 1}; Tag2 = new Tag {Name = "tag 2", TagID = 2}; Tag3 = new Tag {Name = "tag 3", TagID = 3}; Tag4 = new Tag {Name = "tag 4", TagID = 4}; Post1 = new BlogPost {ContentID = 1}; Post2 = new BlogPost {ContentID = 2}; Tag1.ContentContainers.Add(Post1); Tag1.ContentContainers.Add(Post2); Tag2.ContentContainers.Add(Post1); }
/// <summary> /// Gets a IList of Blogposts related to a given blogPost that is passed to the method /// Related in terms of categories /// </summary> /// <param name="blogPost"></param> /// <param name="numberOfRelatedPosts"></param> /// <returns></returns> public IList<BlogPost> GetListOfRelatedBlogPosts(BlogPost blogPost, int numberOfRelatedPosts) { // simplified solution, check first category Category category = blogPost.Categories.FirstOrDefault(); if (category == null) { return null; } else { var query = from post in GetQueryableEntitySet() from postCategory in post.Categories where postCategory.CategoryID == category.CategoryID orderby post.PublishingDate descending select post; return query.Take(numberOfRelatedPosts).ToList(); } }
/// <summary> /// Adds a new blogPost to the context /// </summary> /// <param name="entity"></param> public void Add(BlogPost entity) { _context.ContentContainers.AddObject(entity); }
private void FixupBlogPost(BlogPost previousValue) { if (previousValue != null && previousValue.Comments.Contains(this)) { previousValue.Comments.Remove(this); } if (BlogPost != null) { if (!BlogPost.Comments.Contains(this)) { BlogPost.Comments.Add(this); } if (BlogPostID != BlogPost.ContentID) { BlogPostID = BlogPost.ContentID; } } else if (!_settingFK) { BlogPostID = null; } }
/// <summary> /// Deletes a blogPost from the context /// </summary> /// <param name="entity"></param> public void Delete(BlogPost entity) { _context.ContentContainers.DeleteObject(entity); }
/// <summary> /// Method that manages the post of the CreateBlogPost action when the user clicks on the 'Publish' Button. /// It saves the blogpost and publishes it /// </summary> /// <param name="blogPost"></param> /// <param name="categoryID"></param> /// <param name="listOfTags"></param> /// <returns></returns> private ActionResult CreateBlogPost_OnPublish(BlogPost blogPost, int[] categoryID, string listOfTags) { // Add BlogPost to Context BlogPostRepo.AddBlogPost(blogPost, categoryID, listOfTags); BlogPostRepo.PublishBlogPost(blogPost); // Submits changes // BlogPostRepo.SubmitChanges(); // Redirects to Index with a message for the user TempData[vinCMS.Infraestructure.Constants.VIEW_MESSAGE] = SUCCESS_PUBLISH; return RedirectToAction("Index"); }
public void Can_Get_A_Related_List_Of_BLogPosts() { // Arrange: Given a series of blogposts that are related by tags and categories, a repository // that manages those blogposts and a blog controller // categories var category = new Category() { CategoryID = 1, Name = "category 1" }; var category2 = new Category() { CategoryID = 2, Name = "category 2" }; // blogposts var blogPost = new BlogPost() { ContentID = 1 , Title = "blogPost 1", Path="blogpost-1", PublishingDate = DateTime.Now}; var blogPost2 = new BlogPost() { ContentID = 2 }; var blogPost3 = new BlogPost() { ContentID = 3 }; var blogPost4 = new BlogPost() { ContentID = 4 }; // add categories to blogposts blogPost.Categories.Add(category); blogPost2.Categories.Add(category2); blogPost3.Categories.Add(category); // create mock repository var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(blogPost, blogPost2, blogPost3); const int numberOfRelatedPosts = 5; //moq get by id moqBlogPostRepository.Setup(x => x.GetByPath(blogPost.PublishingDate.Year, blogPost.PublishingDate.Month, blogPost.PublishingDate.Day, blogPost.Path)).Returns(blogPost); //moq getQueryableRelatedBlogPosts moqBlogPostRepository. Setup(x => x.GetListOfRelatedBlogPosts(blogPost, numberOfRelatedPosts)). // when a user wants to get related posts Returns((new List<BlogPost> { blogPost3 })); // They will obtain the related posts var controller = new BlogController(moqBlogPostRepository.Object); // Act: when the user goes into the detail screen var result = controller.Details(blogPost.PublishingDate.Year, blogPost.PublishingDate.Month, blogPost.PublishingDate.Day, blogPost.Path); // Assert: then he can see a series of related blogposts var viewResult = ((ViewResult) result); var model = ((BlogPostDetailModel) viewResult.ViewData.Model); model.HasRelatedBlogPosts.ShouldEqual(true); model.RelatedBlogPosts.Count.ShouldEqual(1); }
/// <summary> /// Updates an edited blog post. Updates it changes to the persistence layer. /// </summary> /// <param name="blogPost"></param> /// <param name="categoryId"></param> /// <param name="listOfTags"></param> /// <param name="submitButton"></param> /// <returns></returns> private ActionResult UpdateEditedBlogPost(BlogPost blogPost, int[] categoryId, string listOfTags, string submitButton) { // Update through model binding TryUpdateModel(blogPost); // Update tags and categories BlogPostRepo.UpdatePost(blogPost, categoryId, listOfTags); switch (submitButton) { case "Save": TempData[vinCMS.Infraestructure.Constants.VIEW_MESSAGE] = SUCCESS_EDIT; break; case "Publish": if (blogPost.IsDraft) { BlogPostRepo.PublishBlogPost(blogPost); TempData[vinCMS.Infraestructure.Constants.VIEW_MESSAGE] = SUCCESS_EDITPUBLISH; } else { TempData[vinCMS.Infraestructure.Constants.VIEW_MESSAGE] = SUCCESS_EDIT_ALREADY_PUBLISHED; } break; } // Submit changes to context // BlogPostRepo.SubmitChanges(); // Send back to index return RedirectToAction("previewblogpost", new{id = blogPost.ContentID}); }
/// <summary> /// Method that manages the post of the CreateBlogPost action when the user clicks on the /// 'Preview' Button. It saves the blogpost as a draft and shows a preview to the user /// </summary> /// <param name="blogPost"></param> /// <param name="categoryID"></param> /// <param name="listOfTags"></param> /// <returns></returns> private ActionResult CreateBlogPost_OnPreview(BlogPost blogPost, int[] categoryID, string listOfTags) { // Add BlogPost to Context BlogPostRepo.AddBlogPost(blogPost, categoryID, listOfTags); // Submits changes // BlogPostRepo.SubmitChanges(); // Redirects to PreviewBlogPostAction return RedirectToAction("PreviewBlogPost", new { id=blogPost.ContentID}); }
/// <summary> /// Method that publishes a specific post that receives as an argument /// </summary> /// <param name="blogPost"></param> public void PublishBlogPost(BlogPost blogPost) { blogPost.IsDraft = false; blogPost.PublishingDate = DateTime.Now; }
public void GivenASeriesOfBlogPosts() { // blogposts BlogPost1 = new BlogPost { ContentID = 1, PublishingDate = DateTime.Today}; BlogPost2 = new BlogPost { ContentID = 2, PublishingDate = DateTime.Today.AddDays(1)}; BlogPost3 = new BlogPost { ContentID = 3, PublishingDate = DateTime.Today.AddDays(2)}; BlogPost4 = new BlogPost { ContentID = 4, PublishingDate = DateTime.Today.AddDays(3)}; BlogPost5 = new BlogPost { ContentID = 5, PublishingDate = DateTime.Today.AddDays(4)}; BlogPost6 = new BlogPost { ContentID = 6, PublishingDate = DateTime.Today.AddDays(5)}; // drafts BlogPost7 = new BlogPost { ContentID = 7, IsDraft = true, PublishingDate = DateTime.Today.AddDays(6)}; // blogpost filled by user BlogPostFilledByUser = new BlogPost { ContentID = 11, AuthorID = 1, BodyContent = "Content", Title = "Title", CreationDate = DateTime.Now, PublishingDate = DateTime.Now, IsDraft = true }; // blogpost that is not a draft BlogPostNotDraft = new BlogPost() { ContentID = 20, IsDraft = false }; }
public void GivenANewBlogPostThatHasBeenCreatedAndFilledInByTheUser() { BlogPostFilledByUser = new BlogPost { ContentID = 11, AuthorID = 1, BodyContent = "Content", Title = "Title", CreationDate = DateTime.Now, PublishingDate = DateTime.Now, IsDraft = true }; }
public void GivenABlogPostThatIsNotADraft() { BlogPostNotDraft = new BlogPost() {ContentID = 20, IsDraft = false}; }
/// <summary> /// Checks whether or not the given url is canonical and has the proper format /// </summary> /// <param name="blogPost"></param> /// <param name="year"></param> /// <param name="month"></param> /// <param name="day"></param> /// <param name="title"></param> /// <param name="id"></param> /// <returns></returns> private bool IsCanonicalUrl(BlogPost blogPost, int year, int month, int day, string title, int id) { // To avoid accessing url helper and couple the controller with the routing system // I use simple yet not very good looking check return (blogPost.ContentID == id && blogPost.PublishingDate.Year == year && blogPost.PublishingDate.Month == month && blogPost.PublishingDate.Day == day && Helpers.Routing.RoutingHelpers.MakeSimpleUrlSegment(blogPost.Title) == title); /* RouteValueDictionary receivedRouteValues = new RouteValueDictionary { {"controller", "blog"}, {"action", "details"}, {"year", year}, {"month", month}, {"day", day}, {"title", title}, {"id", id} }; * */ // If the received route is equal to the expected canonical route then True! // return Url.RouteUrl(receivedRouteValues) == Url.RouteUrl(expectedRouteValues); }
/// <summary> /// Method that updates a blog that has been edited. Deleting the categories and tags with which /// it is not longer associated and adding new categories and tags if there are any /// </summary> /// <param name="blogPost"></param> /// <param name="categoryId"></param> /// <param name="listOfTags"></param> public void UpdatePost(BlogPost blogPost, int[] categoryId, string listOfTags) { UpdateCategoriesInContentContainer(blogPost, categoryId); UpdateTagsInContentContainer(blogPost, listOfTags); }
public void Can_View_A_Selected_BlogPost() { //Arrange: Given a series of blog posts, a repository and a controller const int year = 2010; const int month = 10; const int day = 10; BlogPost selectedBlogPost = new BlogPost { ContentID = 1, Title = "This is a post title", Path = "this-is-a-post-title", PublishingDate = new DateTime(year, month, day), }; var moqBlogPostRepository = UnitTestHelpers.MockBlogPostRepositoryReturnsMoqObject(selectedBlogPost, new BlogPost() {ContentID = 2}); moqBlogPostRepository.Setup(x => x.GetByPath(year, month, day, selectedBlogPost.Path)).Returns(selectedBlogPost); var controller = new BlogController(moqBlogPostRepository.Object); //Act: if the user clicks on one of the blogpost var result = controller.Details(year, month, day, // Date selectedBlogPost.Path); // path var view = (ViewResult) result; //Assert: then the user will be able to see the content of the blogpost ((BlogPostDetailModel) view.ViewData.Model).BlogPost.ContentID.ShouldEqual(1); }
/// <summary> /// Adds a comment to a blog posts /// </summary> /// <param name="blogPost"></param> /// <param name="comment"></param> public void AddCommentToBlogPost(BlogPost blogPost, Comment comment) { comment.CommentDate = DateTime.Now; comment.CommentContent = FormatCommentComment(comment.CommentContent); blogPost.Comments.Add(comment); }
public ActionResult CreateBlogPost(BlogPost blogPost, int[] categoryID, string listOfTags, string submitButton) { if (!ModelState.IsValid) { // get list of all categories again, and show form ViewData[vinCMS.Infraestructure.Constants.VIEW_CATEGORYLIST] = BlogPostRepo.GetListOfCategories(); return View(blogPost); } else { switch (submitButton) { case "Preview": return CreateBlogPost_OnPreview(blogPost, categoryID, listOfTags); case "Publish": return CreateBlogPost_OnPublish(blogPost, categoryID, listOfTags); default: return RedirectToAction("Index"); } } }