Esempio n. 1
0
        public void DeleteArticle_Valid_Id()
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                articleRepository = new ArticlesRepository();
                int count = articleRepository.GetAllArticles().Count(); //Count articles pre-editting of existing article

                SMCC.Models.Article        originalArticle = new SMCC.Models.Article();
                SMCC.Models.Article        deleteArticle   = new SMCC.Models.Article();
                List <SMCC.Models.Article> existArticles   = new List <SMCC.Models.Article>();

                try
                {
                    //Set your controller ControllerContext with fake context
                    articleController.ControllerContext = MockContext().Object;

                    originalArticle = articleRepository.GetArticle(1);

                    var result = articleController.Delete(1) as ViewResult;
                    deleteArticle = (SMCC.Models.Article)result.ViewData.Model;

                    articleController.DeleteConfirmed(deleteArticle.ArticleId);
                }
                finally
                {
                    articleRepository = new ArticlesRepository();
                    existArticles     = articleRepository.GetAllArticles().ToList();

                    Assert.AreEqual(count - 1, existArticles.Count());               //Record count should be deducted by 1 after deletion
                    CollectionAssert.DoesNotContain(existArticles, originalArticle); // Article should no longer be present in db.
                }
            }
        }
Esempio n. 2
0
        // GET: Admin/Articles/Edit/5
        public ActionResult Edit(int?id)
        {
            if (id == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            Article article = _repo.GetArticle(id.Value);

            if (article == null)
            {
                return(HttpNotFound());
            }
            ViewBag.Tags = _repo.GetArticleTagsStr(id.Value);
            ViewBag.ArticleCategoryId = new SelectList(_repo.GetArticleCategories(), "Id", "Title", article.ArticleCategoryId);
            return(View(article));
        }
Esempio n. 3
0
        public void CreateArticle_Null_Content_Invalid()
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                articleRepository = new ArticlesRepository();
                int count = articleRepository.GetAllArticles().Count(); //Count articles pre-creation of new article

                SMCC.Models.Article        newArticle     = new SMCC.Models.Article();
                SMCC.Models.Article        createdArticle = new SMCC.Models.Article();
                List <SMCC.Models.Article> existArticles  = new List <SMCC.Models.Article>();

                try
                {
                    //Set your controller ControllerContext with fake context
                    articleController.ControllerContext = MockContext().Object;
                    newArticle.Title = "A";
                    newArticle.Text  = null;

                    articleController.Create(newArticle);
                }
                finally
                {
                    articleRepository = new ArticlesRepository();
                    existArticles     = articleRepository.GetAllArticles().ToList();
                    createdArticle    = articleRepository.GetArticle(newArticle.ArticleId);

                    Assert.AreEqual(count, existArticles.Count());
                    CollectionAssert.DoesNotContain(existArticles, createdArticle);
                }
            }
        }
Esempio n. 4
0
        // GET: Articles/Details/5
        public IActionResult Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var article = _repository.GetArticle((int)id);

            if (article == null)
            {
                return(NotFound());
            }

            return(View(article));
        }
 public void GetArticle_ReturnsNull_WhenArticleDoesntExist()
 {
     using (var ctx = ContextUtil.GetContext())
     {
         var repo   = new ArticlesRepository(ctx);
         var result = repo.GetArticle(1).GetAwaiter().GetResult();
         Assert.IsNull(result);
     }
 }
Esempio n. 6
0
        public void DetailsArticle_Existing_Id_Valid()
        {
            articleRepository = new ArticlesRepository();
            SMCC.Models.Article expectedArticle = articleRepository.GetArticle(1);

            var result = articleController.Details(1) as ViewResult;

            SMCC.Models.Article actualArticle = (SMCC.Models.Article)result.ViewData.Model;

            Assert.IsTrue(AssertSameArticles(expectedArticle, actualArticle));
        }
Esempio n. 7
0
        public void EditArticle_Long_Valid()
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                articleRepository = new ArticlesRepository();
                int count = articleRepository.GetAllArticles().Count(); //Count articles pre-editting of existing article

                SMCC.Models.Article        originalArticle = new SMCC.Models.Article();
                SMCC.Models.Article        modArticle      = new SMCC.Models.Article();
                List <SMCC.Models.Article> existArticles   = new List <SMCC.Models.Article>();

                try
                {
                    //Set your controller ControllerContext with fake context
                    articleController.ControllerContext = MockContext().Object;

                    originalArticle = articleRepository.GetArticle(1);

                    var result = articleController.Edit(1) as ViewResult;
                    modArticle = (SMCC.Models.Article)result.ViewData.Model;

                    modArticle.Title = "";
                    for (int i = 0; i < 50; i++)
                    {
                        modArticle.Title += "A";
                    }
                    modArticle.Text = "A";

                    articleController.Edit(modArticle);
                }
                finally
                {
                    articleRepository = new ArticlesRepository();
                    existArticles     = articleRepository.GetAllArticles().ToList();
                    Assert.AreEqual(count, existArticles.Count());  //Record count should remain the same after editting

                    SMCC.Models.Article originalArticle_Modified = new SMCC.Models.Article();
                    originalArticle_Modified = articleRepository.GetArticle(1);
                    Assert.IsFalse(AssertSameArticles(originalArticle, originalArticle_Modified)); //Article content should change, hence return false
                    Assert.IsTrue(AssertModifiedArticles(originalArticle, originalArticle_Modified));
                }
            }
        }
        public ActionResult Details(int id)
        {
            _articlesRepo.UpdateArticleViewCount(id);
            var article           = _articlesRepo.GetArticle(id);
            var articleDetailsVm  = new ArticleDetailsViewModel(article);
            var articleComments   = _articlesRepo.GetArticleComments(id);
            var articleCommentsVm = new List <ArticleCommentViewModel>();

            foreach (var item in articleComments)
            {
                articleCommentsVm.Add(new ArticleCommentViewModel(item));
            }

            articleDetailsVm.ArticleComments = articleCommentsVm;
            var articleTags = _articlesRepo.GetArticleTags(id);

            articleDetailsVm.Tags = articleTags;
            return(View(articleDetailsVm));
        }
Esempio n. 9
0
        public void CreateArticle_Long_Valid()
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                articleRepository = new ArticlesRepository();
                int count = articleRepository.GetAllArticles().Count(); //Count articles pre-creation of new article

                SMCC.Models.Article        newArticle     = new SMCC.Models.Article();
                SMCC.Models.Article        createdArticle = new SMCC.Models.Article();
                List <SMCC.Models.Article> existArticles  = new List <SMCC.Models.Article>();

                try
                {
                    //Set your controller ControllerContext with fake context
                    articleController.ControllerContext = MockContext().Object;

                    for (int i = 0; i < 50; i++)
                    {
                        newArticle.Title += "A";
                    }

                    newArticle.Text = "A";

                    articleController.Create(newArticle);
                }
                finally
                {
                    articleRepository = new ArticlesRepository();
                    existArticles     = articleRepository.GetAllArticles().ToList();
                    createdArticle    = articleRepository.GetArticle(newArticle.ArticleId);

                    Assert.AreEqual(count + 1, existArticles.Count());
                    CollectionAssert.Contains(existArticles, createdArticle);
                }
            }
        }
        public void GetArticle_GetsArticle_WhenExists()
        {
            using (var ctx = ContextUtil.GetContext())
            {
                var tags = new List <string>()
                {
                    "Headlines",
                    "Morning"
                };
                var article = new Article
                {
                    Id    = 1,
                    Title = "Good Morning!",
                    Body  = "Morning headlines:",
                    Tags  = tags.ToArray()
                };
                ctx.Articles.Add(article);
                ctx.SaveChanges(true);

                var repo   = new ArticlesRepository(ctx);
                var result = repo.GetArticle(1).GetAwaiter().GetResult();
                Assert.AreEqual(article, result);
            }
        }