public void DisplayCallsStoreWithTenantAndIdForSurveyToGet()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(new Survey());

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                controller.Display("tenant", "slug");
            }

            mockSurveyStore.Verify(
                r => r.GetSurveyByTenantAndSlugName(
                    It.Is<string>(t => "tenant" == t),
                    It.Is<string>(s => "slug" == s),
                    It.Is<bool>(b => b)),
                    Times.Once());
        }
        public void DisplayCopiesTheSurveyQuestionTextToTheSurveyAnswerReturnedInTheContentModel()
        {
            var survey = new Survey
            {
                Questions = new List<Question>(
                    new[]
                                     {
                                        new Question { Text = "question text to copy" },
                                     })
            };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                .Returns(survey);

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                var result = controller.Display("tenant", string.Empty) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<SurveyAnswer>;
                Assert.AreEqual("question text to copy", model.ContentModel.QuestionAnswers.First().QuestionText);
            }
        }
        public void DisplayWhenPostThrowsIfSaveSurveyAnswersReadFromSurveyStoreHaveDifferentCountToTheSurveyAnswerParameter()
        {
            var surveyWith1Question = new Survey
            {
                Questions = new List<Question>(new[] { new Question() })
            };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(surveyWith1Question);
            var surveyAnswerWithoutQuestions = new SurveyAnswer();

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                controller.Display(string.Empty, string.Empty, surveyAnswerWithoutQuestions);
            }
        }
        public void DisplayWhenPostReturnsSurveyTitleAsTitleInTheModelWhenModelIsNotValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            var survey = new Survey
            {
                Title = "title",
                Questions = new List<Question>(new[] { new Question() })
            };
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);
            var surveyAnswer = new SurveyAnswer
            {
                QuestionAnswers = new List<QuestionAnswer>(new[] { new QuestionAnswer() })
            };

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");

                var result = controller.Display(string.Empty, string.Empty, surveyAnswer) as ViewResult;

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("title", model.Title);
            }
        }
        public void DisplayWhenPostReturnsRedirectToThankYouAction()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(new Survey());
            var mockSurveyAnswerStore = new Mock<ISurveyAnswerStore>();

            using (var controller = new SurveysController(mockSurveyStore.Object, mockSurveyAnswerStore.Object))
            {
                var result = controller.Display("tenant", string.Empty, new SurveyAnswer()) as RedirectToRouteResult;

                Assert.AreEqual("ThankYou", result.RouteValues["action"]);
                Assert.AreEqual(null, result.RouteValues["controller"]);
            }
        }
        public void DisplayWhenPostReturnsModelWithTheAnswersReadFromTheParameterWhenModelIsNotValid()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            var survey = new Survey
            {
                Questions = new List<Question>(new[] { new Question() })
            };
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);
            var surveyAnswer = new SurveyAnswer
            {
                QuestionAnswers = new List<QuestionAnswer>(new[] { new QuestionAnswer { Answer = "answer" } })
            };

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                controller.ModelState.AddModelError("error for test", "invalid model state");

                var result = controller.Display(string.Empty, string.Empty, surveyAnswer) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<SurveyAnswer>;
                Assert.AreEqual("answer", model.ContentModel.QuestionAnswers.First().Answer);
            }
        }
        public void DisplayWhenPostCallsSaveSurveyAnswerFromSurveyAnswerStoreWithTheTenantReadFromTheControllerWhenModelIsValid()
        {
            var survey = new Survey();

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);
            var mockSurveyAnswerStore = new Mock<ISurveyAnswerStore>();

            using (var controller = new SurveysController(mockSurveyStore.Object, mockSurveyAnswerStore.Object))
            {
                controller.Display("tenant", string.Empty, new SurveyAnswer());
            }

            mockSurveyAnswerStore.Verify(
                r => r.SaveSurveyAnswer(
                    It.Is<SurveyAnswer>(sa => "tenant" == sa.Tenant)),
                Times.Once());
        }
        public void DisplayWhenPostCallsSaveSurveyAnswerFromSurveyAnswerStoreWithQuestionTypeReadFromTheSurveyWhenModelIsValid()
        {
            var survey = new Survey
            {
                Questions = new List<Question>(
                    new[]
                                     {
                                        new Question { Type = QuestionType.SimpleText },
                                     })
            };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);
            var mockSurveyAnswerStore = new Mock<ISurveyAnswerStore>();
            var surveyAnswer = new SurveyAnswer
            {
                QuestionAnswers = new List<QuestionAnswer>(new[] { new QuestionAnswer() })
            };

            using (var controller = new SurveysController(mockSurveyStore.Object, mockSurveyAnswerStore.Object))
            {
                controller.Display(string.Empty, string.Empty, surveyAnswer);
            }

            mockSurveyAnswerStore.Verify(
                r => r.SaveSurveyAnswer(
                    It.Is<SurveyAnswer>(sa => QuestionType.SimpleText == sa.QuestionAnswers[0].QuestionType)),
                Times.Once());
        }
        public void DisplayTransformsAllTheSurveyQuestionsToQuestionAnswerRetrnedInTheContentModel()
        {
            var survey = new Survey
            {
                Questions = new List<Question>(
                    new[]
                                     {
                                        new Question(),
                                        new Question()
                                     })
            };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                var result = controller.Display("tenant", string.Empty) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<SurveyAnswer>;
                Assert.AreEqual(2, model.ContentModel.QuestionAnswers.Count());
            }
        }
        public void DisplayReturnsSurveyTitleAsTitleInTheModel()
        {
            var survey = new Survey { Title = "title" };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                var result = controller.Display("tenant", string.Empty) as ViewResult;

                var model = result.ViewData.Model as TenantMasterPageViewData;
                Assert.AreSame("title", model.Title);
            }
        }
        public void DisplayReturnsEmptyViewName()
        {
            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(new Survey());

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                var result = controller.Display("tenant", string.Empty) as ViewResult;

                Assert.AreEqual(string.Empty, result.ViewName);
            }
        }
        public void DisplayCopiesTheSurveyTitleToTheSurveyAnswerReturnedInTheContentModel()
        {
            var survey = new Survey { Title = "title to be copied" };

            var mockSurveyStore = new Mock<ISurveyStore>();
            mockSurveyStore.Setup(r => r.GetSurveyByTenantAndSlugName(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<bool>()))
                                              .Returns(survey);

            using (var controller = new SurveysController(mockSurveyStore.Object, default(ISurveyAnswerStore)))
            {
                var result = controller.Display("tenant", string.Empty) as ViewResult;

                var model = result.ViewData.Model as TenantPageViewData<SurveyAnswer>;
                Assert.AreEqual("title to be copied", model.ContentModel.Title);
            }
        }