public void Execute_AddsNewStep()
        {
            var jobContext = new TestJobContext();

            jobContext.JobDetail.JobDataMap[StepsAutomation.AttributeKey.DuplicatePreventionDayRange] = 7.ToString();

            var job = new StepsAutomation();

            job.Execute(jobContext);

            var rockContext        = new RockContext();
            var stepProgramService = new StepProgramService(rockContext);
            var stepProgram        = stepProgramService.Queryable("StepTypes.Steps.StepStatus").FirstOrDefault(sp => sp.ForeignKey == ForeignKey);

            Assert.AreEqual(4, stepProgram.StepTypes.Count);

            foreach (var stepType in stepProgram.StepTypes)
            {
                if (stepType.AutoCompleteDataViewId.HasValue)
                {
                    // The three people in the dataview should have completed steps
                    Assert.AreEqual(3, stepType.Steps.Count);

                    foreach (var step in stepType.Steps)
                    {
                        Assert.IsTrue(step.IsComplete);
                        Assert.IsTrue(step.StepStatus.IsCompleteStatus);
                        Assert.IsNotNull(step.CompletedDateTime);
                    }
                }
                else
                {
                    // No steps should exist for a step type with no auto-complete dataview
                    Assert.AreEqual(0, stepType.Steps.Count);
                }
            }
        }
        public void Execute_RespectsAllowMultipleTrue()
        {
            // Create a complete step for a step type that does allow multiple for a person in the dataview
            var stepGuid = Guid.NewGuid();
            var stepCompletedDateTime = new DateTime(2000, 1, 1);
            int stepTypeId;

            using (var rockContext = new RockContext())
            {
                var stepProgramService = new StepProgramService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);
                var stepService        = new StepService(rockContext);

                var stepProgram = stepProgramService.Queryable("StepTypes.Steps.StepStatus").FirstOrDefault(sp => sp.ForeignKey == ForeignKey);
                stepTypeId = stepProgram.StepTypes.FirstOrDefault(st => st.AutoCompleteDataViewId.HasValue && st.AllowMultiple).Id;

                stepService.Add(new Step
                {
                    ForeignKey        = ForeignKey,
                    StepTypeId        = stepTypeId,
                    StepStatus        = stepProgram.StepStatuses.FirstOrDefault(ss => ss.IsCompleteStatus),
                    CompletedDateTime = stepCompletedDateTime,
                    PersonAlias       = personAliasService.Queryable().FirstOrDefault(pa =>
                                                                                      pa.ForeignKey == ForeignKey &&
                                                                                      pa.Person.MiddleName == DataViewMiddleName),
                    Guid = stepGuid
                });

                rockContext.SaveChanges();
            }

            // Run the job
            var jobContext = new TestJobContext();

            jobContext.JobDetail.JobDataMap[StepsAutomation.AttributeKey.DuplicatePreventionDayRange] = 7.ToString();

            var job = new StepsAutomation();

            job.Execute(jobContext);

            // Refresh the data from the database
            using (var rockContext = new RockContext())
            {
                var stepProgramService = new StepProgramService(rockContext);
                var stepProgram        = stepProgramService.Queryable("StepTypes.Steps.StepStatus").FirstOrDefault(sp => sp.ForeignKey == ForeignKey);
                var foundOriginalStep  = false;

                Assert.AreEqual(4, stepProgram.StepTypes.Count);

                foreach (var stepType in stepProgram.StepTypes)
                {
                    if (stepType.Id == stepTypeId)
                    {
                        // This is the allow multiple type with an autocomplete dataview
                        // There should be the original step and now also 3 new ones.
                        Assert.AreEqual(4, stepType.Steps.Count);

                        foreach (var step in stepType.Steps)
                        {
                            // The original step should be found and the completed datetime should not have changed
                            if (step.Guid == stepGuid)
                            {
                                foundOriginalStep = true;
                                Assert.IsTrue(step.CompletedDateTime.HasValue);
                                Assert.AreEqual(stepCompletedDateTime, step.CompletedDateTime.Value);
                            }

                            Assert.IsTrue(step.IsComplete);
                            Assert.IsTrue(step.StepStatus.IsCompleteStatus);
                            Assert.IsNotNull(step.CompletedDateTime);
                        }
                    }
                    else if (stepType.AutoCompleteDataViewId.HasValue)
                    {
                        // There should be a completed step for each person in the dataview
                        Assert.AreEqual(3, stepType.Steps.Count);

                        foreach (var step in stepType.Steps)
                        {
                            Assert.IsTrue(step.IsComplete);
                            Assert.IsTrue(step.StepStatus.IsCompleteStatus);
                            Assert.IsNotNull(step.CompletedDateTime);
                        }
                    }
                    else
                    {
                        // No steps for types with no dataview
                        Assert.AreEqual(0, stepType.Steps.Count);
                    }
                }

                Assert.IsTrue(foundOriginalStep);
            }
        }
        public void Execute_CompletesExistingStep()
        {
            // Add an in-progress step that should be made complete by the job
            var stepGuid = Guid.NewGuid();

            using (var rockContext = new RockContext())
            {
                var stepProgramService = new StepProgramService(rockContext);
                var personAliasService = new PersonAliasService(rockContext);
                var stepService        = new StepService(rockContext);

                var stepProgram = stepProgramService.Queryable("StepTypes.Steps.StepStatus").FirstOrDefault(sp => sp.ForeignKey == ForeignKey);

                stepService.Add(new Step
                {
                    ForeignKey        = ForeignKey,
                    StepTypeId        = stepProgram.StepTypes.FirstOrDefault(st => st.AutoCompleteDataViewId.HasValue).Id,
                    StepStatus        = stepProgram.StepStatuses.FirstOrDefault(ss => !ss.IsCompleteStatus),
                    CompletedDateTime = null,
                    PersonAlias       = personAliasService.Queryable().FirstOrDefault(pa =>
                                                                                      pa.ForeignKey == ForeignKey &&
                                                                                      pa.Person.MiddleName == DataViewMiddleName),
                    Guid = stepGuid
                });

                rockContext.SaveChanges();
            }

            // Run the job
            var jobContext = new TestJobContext();

            jobContext.JobDetail.JobDataMap[StepsAutomation.AttributeKey.DuplicatePreventionDayRange] = 7.ToString();

            var job = new StepsAutomation();

            job.Execute(jobContext);

            // Refresh the data from the database
            using (var rockContext = new RockContext())
            {
                var stepProgramService = new StepProgramService(rockContext);
                var stepProgram        = stepProgramService.Queryable("StepTypes.Steps.StepStatus").FirstOrDefault(sp => sp.ForeignKey == ForeignKey);
                var foundOriginalStep  = false;

                Assert.AreEqual(4, stepProgram.StepTypes.Count);

                foreach (var stepType in stepProgram.StepTypes)
                {
                    if (stepType.AutoCompleteDataViewId.HasValue)
                    {
                        // The 3 people of the dataview should have a completed step
                        Assert.AreEqual(3, stepType.Steps.Count);

                        foreach (var step in stepType.Steps)
                        {
                            if (step.Guid == stepGuid)
                            {
                                // We need to ensure that the original step (was in-progress) still exists
                                foundOriginalStep = true;
                            }

                            Assert.IsTrue(step.IsComplete);
                            Assert.IsTrue(step.StepStatus.IsCompleteStatus);
                            Assert.IsNotNull(step.CompletedDateTime);
                        }
                    }
                    else
                    {
                        // No steps should exist for a type with no auto-complete dataview
                        Assert.AreEqual(0, stepType.Steps.Count);
                    }
                }

                Assert.IsTrue(foundOriginalStep);
            }
        }